View Javadoc

1   package org.codehaus.mojo.javascript;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  import java.io.FilenameFilter;
21  
22  import org.apache.maven.plugin.Mojo;
23  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
24  import org.codehaus.plexus.util.FileUtils;
25  
26  public class CompressMojoTest
27      extends AbstractMojoTestCase
28  {
29      public void testAttachCompressedScriptaculous()
30          throws Exception
31      {
32          File testPom1 = new File( getBasedir(), "src/test/resources/compress.pom" );
33          File testPom2 = new File( getBasedir(), "src/test/resources/attach-compressed.pom" );
34          Mojo mojo1 = (Mojo) lookupMojo( "compress", testPom1 );
35          Mojo mojo2 = (Mojo) lookupMojo( "attach-compressed", testPom2 );
36          assertNotNull( "Failed to configure the plugin (compress)", mojo1 );
37          assertNotNull( "Failed to configure the plugin (attach)", mojo2 );
38   
39          mojo1.execute();
40          mojo2.execute();
41  
42          File[] js = getScripts( "./src/test/resources/scripts" );
43          long size = 0;
44          for ( int i = 0; i < js.length; i++ )
45          {
46              size += js[i].length();
47          }
48  
49          File expected =
50              new File( "./target/test-target/attach-compressed/scriptaculous-1.7-compressed.jar" );
51          assertTrue( "expected file not found " + expected, expected.exists() );
52          assertTrue( "no compression occured", expected.length() < size );
53      }
54  
55      public void testCompressScriptaculous()
56          throws Exception
57      {
58          File target = new File( "target/test-target/war-compress" );
59          target.mkdirs();
60          FileUtils.cleanDirectory( target );
61          FileUtils.copyDirectory( new File( "src/test/resources/scripts" ), target );
62  
63          File testPom = new File( getBasedir(), "/target/test-classes/war-compress.pom" );
64          Mojo mojo = (Mojo) lookupMojo( "war-compress", testPom );
65          assertNotNull( "Failed to configure the plugin", mojo );
66  
67          mojo.execute();
68  
69          File[] js = getScripts( "./src/test/resources/scripts" );
70          for ( int i = 0; i < js.length; i++ )
71          {
72              File expected = new File( target, js[i].getName().replace( ".js", "-compressed.js" ) );
73              assertTrue( "expected file not found " + expected.getName(), expected.exists() );
74              assertTrue( "no compression occured on " + expected.getName(),
75                  expected.length() < js[i].length() );
76          }
77      }
78  
79      public void testZeroLengthInput()
80          throws Exception
81      {
82          File target = new File( "target/test-target/compress-zero-length" );
83          target.mkdirs();
84          FileUtils.cleanDirectory( target );
85          FileUtils.copyDirectory( new File( "src/test/resources/zero-length" ), target );
86  
87          File testPom = new File( getBasedir(), "/target/test-classes/compress-zero-length.pom" );
88          Mojo mojo = (Mojo) lookupMojo( "war-compress", testPom );
89          assertNotNull( "Failed to configure the plugin", mojo );
90  
91          mojo.execute();
92  
93          File[] js = getScripts( "./src/test/resources/zero-length" );
94          for ( int i = 0; i < js.length; i++ )
95          {
96              File expected = new File( target, js[i].getName().replace( ".js", "-min.js" ) );
97              assertTrue( "expected file not found " + expected.getName(), expected.exists() );
98          }
99      }
100 
101     private File[] getScripts( String path )
102     {
103         File[] js = new File( path ).listFiles( new FilenameFilter()
104         {
105             public boolean accept( File dir, String name )
106             {
107                 return name.endsWith( ".js" );
108             }
109         } );
110         return js;
111     }
112 }