View Javadoc
1   package org.apache.maven.scm.provider.svn.svnexe.command.changelog;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.ChangeFile;
23  import org.apache.maven.scm.ChangeSet;
24  import org.apache.maven.scm.ScmFileStatus;
25  import org.apache.maven.scm.log.DefaultLog;
26  import org.codehaus.plexus.PlexusTestCase;
27  import org.codehaus.plexus.logging.Logger;
28  import org.junit.Assert;
29  
30  import java.io.BufferedReader;
31  import java.io.File;
32  import java.io.FileReader;
33  import java.io.IOException;
34  import java.text.DateFormat;
35  import java.text.SimpleDateFormat;
36  import java.util.Date;
37  import java.util.LinkedHashMap;
38  import java.util.List;
39  import java.util.Map;
40  import java.util.TimeZone;
41  import java.util.concurrent.atomic.AtomicInteger;
42  
43  /**
44   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
45   *
46   */
47  public class SvnChangeLogConsumerTest
48      extends PlexusTestCase
49  {
50      Logger logger;
51  
52      SvnChangeLogConsumer consumer;
53  
54  
55      protected void setUp()
56          throws Exception
57      {
58          super.setUp();
59  
60          logger = getContainer().getLogger();
61          consumer = new SvnChangeLogConsumer( new DefaultLog(), null );
62      }
63  
64      /**
65       * Initial modifications should be empty.
66       */
67      public void testGetModifications_Initial()
68      {
69          assertTrue( "Initial modifications should be empty", consumer.getModifications().isEmpty() );
70      }
71  
72      /**
73       * Valid svn log output should have expected values.
74       *
75       * @throws Exception if any problem occurs.
76       */
77      public void testConsumeLine_ValidOutput()
78          throws Exception
79      {
80          final File svnLog = getTestFile( "/src/test/resources/svn/changelog/svnLogValidOutput.txt" );
81  
82          consumeLog( svnLog );
83  
84          final ChangeSet entry = consumer.getModifications().get( 0 );
85  
86          final List changedFiles = entry.getFiles();
87          final String revision = ( (ChangeFile) changedFiles.get( 0 ) ).getRevision();
88  
89          assertEquals( "Valid revision expected", "15", revision );
90          assertEquals( "Valid num changed files expected", 2, changedFiles.size() );
91          assertEquals( "Valid name expected", "unconventional author output (somedata)", entry.getAuthor() );
92          String expectedDate = getLocalizedDate( "2002-08-26 14:33:26", TimeZone.getTimeZone( "GMT-4" ) );
93          assertEquals( "Valid date expected", expectedDate, entry.getDateFormatted() );
94          assertEquals( "Valid comment expected", "Minor formatting changes.\n", entry.getComment() );
95      }
96  
97      private static String getLocalizedDate( String date, TimeZone timeZone )
98          throws Exception
99      {
100         DateFormat fmt = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
101         fmt.setTimeZone( timeZone );
102         Date parsed = fmt.parse( date );
103         fmt = new SimpleDateFormat( "yyyy-MM-dd" );
104         return fmt.format( parsed );
105     }
106 
107     /**
108      * Svn log output with an invalid reason should throw an IllegalOutputException.
109      *
110      * @throws Exception
111      */
112     public void testConsumeLine_InvalidReason()
113         throws Exception
114     {
115         final File svnLog = getTestFile( "/src/test/resources/svn/changelog/svnLogInvalidReason.txt" );
116 
117         try
118         {
119             consumeLog( svnLog );
120             fail( "Svn log output with an invalid reason should throw IllegalOutputException" );
121         }
122         catch ( final IllegalOutputException e )
123         {
124             assertTrue( true );
125         }
126     }
127 
128     /**
129      * Svn log output with an invalid date should throw an IllegalOutputException.
130      *
131      * @throws Exception
132      */
133     public void testConsumeLine_InvalidDate()
134         throws Exception
135     {
136         final File svnLog = getTestFile( "/src/test/resources/svn/changelog/svnLogInvalidDate.txt" );
137         try
138         {
139             consumeLog( svnLog );
140             fail( "Svn log output with an invalid date should throw IllegalOutputException" );
141         }
142         catch ( final IllegalOutputException e )
143         {
144             assertTrue( true );
145         }
146     }
147 
148     /**
149      * Consumes change log information stored in a file.
150      *
151      * @param logFile the file.
152      * @throws IOException if a problem occurs.
153      */
154     private void consumeLog( final File logFile )
155         throws IOException
156     {
157         final BufferedReader reader = new BufferedReader( new FileReader( logFile ) );
158         String line = reader.readLine();
159 
160         while ( line != null )
161         {
162             consumer.consumeLine( line );
163             line = reader.readLine();
164         }
165     }
166 
167     public void testConsumerWithPattern1()
168         throws Exception
169     {
170         StringBuilder out = new StringBuilder();
171 
172         File f = getTestFile( "/src/test/resources/svn/changelog/svnlog.txt" );
173 
174         BufferedReader r = new BufferedReader( new FileReader( f ) );
175 
176         String line;
177 
178         while ( ( line = r.readLine() ) != null )
179         {
180             consumer.consumeLine( line );
181         }
182 
183         List<ChangeSet> modifications = consumer.getModifications();
184 
185         out.append( "Text format:" );
186 
187         out.append( "nb modifications : " + modifications.size() );
188 
189         for ( ChangeSet entry : modifications )
190         {
191 
192             out.append( "Author:" + entry.getAuthor() );
193 
194             out.append( "Date:" + entry.getDate() );
195 
196             out.append( "Comment:" + entry.getComment() );
197 
198             for ( ChangeFile file : entry.getFiles() )
199             {
200 
201                 out.append( "File:" + file.getName() );
202             }
203 
204             out.append( "==============================" );
205         }
206 
207         out.append( "XML format:" );
208 
209         out.append( "nb modifications : " + modifications.size() );
210 
211         for ( ChangeSet entry : modifications )
212         {
213             out.append( entry.toXML() );
214 
215             out.append( "==============================" );
216         }
217 
218         if ( logger.isDebugEnabled() )
219         {
220             logger.debug( out.toString() );
221         }
222     }
223 
224     public void testConsumerWithPattern2()
225         throws Exception
226     {
227         StringBuilder out = new StringBuilder();
228 
229         File f = getTestFile( "/src/test/resources/svn/changelog/svnlog2.txt" );
230 
231         BufferedReader r = new BufferedReader( new FileReader( f ) );
232 
233         String line;
234 
235         while ( ( line = r.readLine() ) != null )
236         {
237             consumer.consumeLine( line );
238         }
239 
240         List modifications = consumer.getModifications();
241 
242         out.append( "nb modifications : " + modifications.size() );
243 
244         int origFileCounter = 0;
245 
246         // must use *Linked* HashMap to have predictable toString
247         final Map<ScmFileStatus, AtomicInteger> summary = new LinkedHashMap<ScmFileStatus, AtomicInteger>();
248 
249         for ( ChangeSet entry : consumer.getModifications() )
250         {
251 
252             out.append( "Author:" + entry.getAuthor() );
253 
254             out.append( "Date:" + entry.getDate() );
255 
256             out.append( "Comment:" + entry.getComment() );
257 
258             for ( ChangeFile file : entry.getFiles() )
259             {
260                 final ScmFileStatus action = file.getAction();
261                 if ( !summary.containsKey( action ) )
262                 {
263                     summary.put( action, new AtomicInteger() );
264                 }
265                 summary.get( action ).incrementAndGet();
266 
267                 final String fileName = file.getName();
268                 out.append( "File:" + fileName );
269 
270                 // files in this log are known to be from one subtree
271                 Assert.assertTrue( "Unexpected file name: " + fileName, fileName.startsWith( "/maven/scm/trunk" ) );
272 
273                 // files in this log are known not to contain space
274                 Assert.assertEquals( "Unexpected space found in filename: " + fileName, -1, fileName.indexOf( " " ) );
275 
276                 if ( file.getOriginalName() != null )
277                 {
278                     origFileCounter++;
279                 }
280             }
281 
282             out.append( "==============================" );
283         }
284 
285         Assert.assertEquals( "Unexpected number of file copy records", 1, origFileCounter );
286 
287         Assert.assertEquals( "Action summary differs from expectations",
288                              "{modified=626, deleted=56, added=310, copied=1}", summary.toString() );
289 
290         if ( logger.isDebugEnabled() )
291         {
292             logger.debug( out.toString() );
293         }
294     }
295 }