View Javadoc

1   package org.codehaus.mojo.javascript.compress;
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  
21  import junit.framework.TestCase;
22  
23  import org.codehaus.plexus.util.FileUtils;
24  
25  /**
26   * @author <a href="mailto:nicolas@apache.org">nicolas De Loof</a>
27   */
28  public class ShrinksafeCompressorTest
29      extends TestCase
30  {
31      private JSCompressor compressor = new ShrinksafeCompressor();
32  
33      /**
34       * Check the compressor is invoked as expected.
35       * 
36       * @throws Exception
37       */
38      public void testCompress()
39          throws Exception
40      {
41          File input = new File( "src/test/resources/test1.js" );
42          File output = new File( "target/test-out.js" );
43  
44          compressor.compress( input, output, JSCompressor.MAX, JSCompressor.JAVASCRIPT_1_3 );
45  
46          assertTrue( "exepected file not found", output.exists() );
47          assertTrue( "no compression occured", output.length() < input.length() );
48      }
49  
50      /**
51       * @throws Exception
52       */
53      public void testMultipleCompress()
54          throws Exception
55      {
56          // Fix issue with Shrinksafe : the rhino context is not cleared when
57          // used multiple times.
58  
59          File output = new File( "target/test-out.js" );
60          File input = null;
61          for ( int i = 1; i <= 3; i++ )
62          {
63              input = new File( "src/test/resources/test" + i + ".js" );
64              compressor.compress( input, output, JSCompressor.MAX, JSCompressor.JAVASCRIPT_1_3 );
65          }
66  
67          assertTrue( "exepected file not found", output.exists() );
68          assertTrue( "no compression occured", output.length() < input.length() );
69          String out = FileUtils.fileRead( output );
70          assertTrue( "test1.js code present in test3 compressed", out.indexOf( "test1" ) < 0 );
71      }
72  }