1   package org.apache.maven.jalopy;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import junit.framework.Test;
22  import junit.framework.TestCase;
23  import junit.framework.TestSuite;
24  
25  import org.codehaus.plexus.util.FileUtils;
26  
27  import java.io.File;
28  import java.io.FileInputStream;
29  import java.io.FileNotFoundException;
30  import java.io.IOException;
31  import java.util.StringTokenizer;
32  
33  /**
34   * Test case for JalopyBean.java
35   *
36   * @author <a href="mailto:jruiz@exist.com">Johnny R. Ruiz III</a>
37   * @author <a href="mailto:ltheussl@apache.org">Lukas Theussl</a>
38   * @version $Id: JalopyBeanTest.java 531612 2007-04-23 21:28:38Z ltheussl $
39   */
40  public class JalopyBeanTest extends TestCase
41  {
42      /** An instance of a JalopyBean. */
43      private JalopyBean jalopy;
44  
45      /** The basedir system property. */
46      private String baseDir = System.getProperty( "basedir" );
47  
48      /**
49       * Constructor.
50       *
51       * @param testName The name of the test.
52       */
53      public JalopyBeanTest( String testName )
54      {
55          super( testName );
56      }
57  
58      /** JUnit setUp.
59       * @throws Exception Exception
60       */
61      protected void setUp() throws Exception
62      {
63          jalopy = new JalopyBean();
64      }
65  
66      /** JUnit tearDown.
67       * @throws Exception Exception
68       */
69      protected void tearDown() throws Exception
70      {
71      }
72  
73      /**
74       * Rus the test suite.
75       *
76       * @return Test
77       */
78      public static Test suite()
79      {
80          TestSuite suite = new TestSuite( JalopyBeanTest.class );
81  
82          return suite;
83      }
84  
85      /** Tests equality of not yet formatted files.
86       * @throws IOException IOException
87       */
88      public void testIfInputFilesIsNotYetJalopyFormatted()
89          throws IOException
90      {
91          recopyInputFiles();
92  
93          assertTrue( compareTextFiles( new File( baseDir, "target/test-classes/input/test/Circle.java" ),
94                                        new File( baseDir, "target/test-classes/input-bak/Circle.java" ) ) );
95  
96          assertTrue( compareTextFiles( new File( baseDir, "target/test-classes/input/src/Rectangle.java" ),
97                                        new File( baseDir, "target/test-classes/input-bak/Rectangle.java" ) ) );
98  
99          assertTrue( compareTextFiles( new File( baseDir, "target/test-classes/input/test/Point.java" ),
100                                       new File( baseDir, "target/test-classes/input-bak/Point.java" ) ) );
101 
102         assertTrue( compareTextFiles( new File( baseDir, "target/test-classes/input/src/Point.java" ),
103                                       new File( baseDir, "target/test-classes/input-bak/Point.java" ) ) );
104     }
105 
106     /** Tests not-equality of formatted files. */
107     public void testExecute()
108     {
109         jalopy.setFailOnError( true );
110 
111         jalopy.setFileFormat( "auto" );
112 
113         jalopy.setConvention( new File( baseDir, "src/test/resources/jalopy.xml" ) );
114 
115         jalopy.setSourceDirectory( new File( baseDir, "target/test-classes/input/src" ) );
116 
117         jalopy.setSrcExcludesPattern( "Point.java" );
118 
119         jalopy.setSrcIncludesPattern( "Rectangle.java" );
120 
121         jalopy.setTestSourceDirectory( new File( baseDir, "target/test-classes/input/test" ) );
122 
123         jalopy.setTestExcludesPattern( "" );
124 
125         jalopy.setTestIncludesPattern( "*.java" );
126 
127         jalopy.setHistoryPolicy( "none" );
128 
129         jalopy.setInspect( false );
130 
131         jalopy.setForce( false );
132 
133         jalopy.setBackup( false );
134 
135         try
136         {
137             jalopy.execute();
138 
139             assertFalse( compareTextFiles( new File( baseDir, "target/test-classes/input/test/Circle.java" ),
140                                            new File( baseDir, "target/test-classes/input-bak/Circle.java" ) ) );
141 
142             assertFalse( compareTextFiles( new File( baseDir, "target/test-classes/input/src/Rectangle.java" ),
143                                            new File( baseDir, "target/test-classes/input-bak/Rectangle.java" ) ) );
144 
145             assertFalse( compareTextFiles( new File( baseDir, "target/test-classes/input/test/Point.java" ),
146                                            new File( baseDir, "target/test-classes/input-bak/Point.java" ) ) );
147 
148             assertTrue( compareTextFiles( new File( baseDir, "target/test-classes/input/src/Point.java" ),
149                                           new File( baseDir, "target/test-classes/input-bak/Point.java" ) ) );
150         }
151         catch ( Exception e )
152         {
153             e.printStackTrace();
154         }
155     }
156 
157     /** Test source input files.
158      * @throws IOException IOException
159      */
160     public void testIfSourceInputFilesWasFormatted()
161         throws IOException
162     {
163         assertTrue( compareTextFiles( new File( baseDir, "target/test-classes/input/src/Rectangle.java" ),
164                                       new File( baseDir, "target/test-classes/validator/Rectangle.java" ) ) );
165     }
166 
167     /** Test excludes pattern.
168      * @throws IOException IOException
169      */
170     public void testIfExcludedFilesNotFormatted()
171         throws IOException
172     {
173         assertTrue( compareTextFiles( new File( baseDir, "target/test-classes/input/src/Point.java" ),
174                                       new File( baseDir, "target/test-classes/input-bak/Point.java" ) ) );
175     }
176 
177     /** Test that all source input files are formatted.
178      * @throws IOException IOException
179      */
180     public void testIfTestInputFilesAllFormatted()
181         throws IOException
182     {
183         assertTrue( compareTextFiles( new File( baseDir, "target/test-classes/input/test/Circle.java" ),
184                                       new File( baseDir, "target/test-classes/validator/Circle.java" ) ) );
185 
186         assertTrue( compareTextFiles( new File( baseDir, "target/test-classes/input/test/Point.java" ),
187                                       new File( baseDir, "target/test-classes/validator/Point.java" ) ) );
188 
189         assertTrue( compareTextFiles( new File( baseDir, "target/test-classes/input/test/Rectangle.java" ),
190                                       new File( baseDir, "target/test-classes/validator/Rectangle.java" ) ) );
191 
192     }
193 
194     /** Test fileFormat. */
195     public void testSetGetFileFormat()
196     {
197         jalopy.setFileFormat( "fileFormat" );
198 
199         assertEquals( "fileFormat", jalopy.getFileFormat() );
200     }
201 
202     /** Test FailOnError. */
203     public void testSetIsFailOnError()
204     {
205         jalopy.setFailOnError( true );
206 
207         assertEquals( true, jalopy.isFailOnError() );
208     }
209 
210     /** Test SourceDirectory. */
211     public void testSetGetSourceDirectory()
212     {
213         jalopy.setSourceDirectory( new File( "src/test" ) );
214 
215         assertEquals( new File( "src/test" ), jalopy.getSourceDirectory() );
216     }
217 
218     /** Test SrcIncludesPattern. */
219     public void testSetGetSrcIncludesPattern()
220     {
221         jalopy.setSrcIncludesPattern( "**.*" );
222 
223         assertEquals( "**.*", jalopy.getSrcIncludesPattern() );
224     }
225 
226     /** Test SrcExcludesPattern. */
227     public void testSetGetSrcExcludesPattern()
228     {
229         jalopy.setSrcExcludesPattern( "**.pro" );
230 
231         assertEquals( "**.pro", jalopy.getSrcExcludesPattern() );
232     }
233 
234     /** Test HistoryPolicy. */
235     public void testSetGetHistory()
236     {
237         jalopy.setHistoryPolicy( "history" );
238 
239         assertEquals( "history", jalopy.getHistoryPolicy() );
240     }
241 
242     /**
243      * Compares two text files for equality.
244      *
245      * @param f1 The first file
246      * @param f2 The second file
247      * @return True if the files are equal
248      * @throws FileNotFoundException FileNotFoundException
249      * @throws IOException IOException
250      */
251     private boolean compareTextFiles( File f1, File f2 )
252         throws FileNotFoundException, IOException
253     {
254         String text1 = getTextContents( f1 );
255 
256         String text2 = getTextContents( f2 );
257 
258         StringTokenizer tokenizer1 = new StringTokenizer( text1, "\n" );
259 
260         StringTokenizer tokenizer2 = new StringTokenizer( text2, "\n" );
261 
262         if ( tokenizer1.countTokens() != tokenizer2.countTokens() )
263         {
264             return false;
265         }
266 
267         while ( tokenizer1.hasMoreTokens() )
268         {
269             if ( !tokenizer1.nextToken().equalsIgnoreCase( tokenizer2.nextToken() ) )
270             {
271                 return false;
272             }
273         }
274 
275         return true;
276     }
277 
278     /**
279      * Returns the text content of a file.
280      *
281      * @param f The file
282      * @return The file as a String
283      * @throws FileNotFoundException FileNotFoundException
284      * @throws IOException IOException
285      */
286     private String getTextContents( File f )
287         throws FileNotFoundException, IOException
288     {
289         FileInputStream fIn = new FileInputStream( f );
290 
291         byte[] fBytes = new byte[fIn.available()];
292 
293         fIn.read( fBytes );
294 
295         fIn.close();
296 
297         return new String( fBytes );
298     }
299 
300     /** Make a copy of input files so they can be compared several times.
301      * @throws IOException IOException
302      */
303     private void recopyInputFiles()
304         throws IOException
305     {
306         FileUtils.copyDirectory( new File( baseDir, "src/test/resources/input-bak/" ),
307                                  new File( baseDir, "target/test-classes/input/src" ) );
308 
309         FileUtils.copyDirectory( new File( baseDir, "src/test/resources/input-bak/" ),
310                                  new File( baseDir, "target/test-classes/input/test" ) );
311     }
312 
313 }