View Javadoc
1   package org.apache.maven.scm;
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 junit.framework.TestCase;
23  
24  import java.util.Calendar;
25  import java.util.Date;
26  
27  /**
28   * Tests for the {@link ChangeSet}class
29   *
30   * @author dion
31   *
32   */
33  public class ChangeSetTest
34      extends TestCase
35  {
36      /**
37       * the {@link ChangeSet} used for testing
38       */
39      private ChangeSet instance;
40  
41      /**
42       * Initialize per test data
43       */
44      public void setUp()
45      {
46          instance = createInstance();
47      }
48  
49      private static ChangeSet createInstance()
50      {
51          ChangeSet instance = new ChangeSet();
52          instance.setAuthor( "dion" );
53          instance.setComment( "comment" );
54          instance.setDate( "2002/04/01 00:00:00" );
55          return instance;
56      }
57  
58      /**
59       * Test of addFile methods: using ChangeFile
60       */
61      public void testAddFileWithFile()
62      {
63          ChangeFile file = new ChangeFile( "maven:dummy" );
64          instance.addFile( file );
65          assertTrue( "File name not found in list", instance.toString().indexOf( "maven:dummy" ) != -1 );
66  
67          assertTrue( instance.containsFilename("maven:") );
68          assertTrue( instance.containsFilename(":dummy") );
69          assertTrue( instance.containsFilename(":") );
70          assertTrue( instance.containsFilename("maven:dummy") );
71          assertFalse( instance.containsFilename("dammy") );
72      }
73  
74      /**
75       * Test of toString method
76       */
77      public void testToString()
78      {
79          //dion, Mon Apr 01 00:00:00 EST 2002, comment
80          String value = instance.toString();
81          assertTrue( "author not found in string", value.indexOf( "dion" ) != -1 );
82          assertTrue( "comment not found in string", value.indexOf( "comment" ) != -1 );
83          assertTrue( "date not found in string", value.indexOf( "Mon Apr 01" ) != -1 );
84      }
85  
86      /**
87       * Test of getAuthor method
88       */
89      public void testGetAuthor()
90      {
91          assertEquals( "Author value not retrieved correctly", "dion", instance.getAuthor() );
92      }
93  
94      /**
95       * Test of setAuthor method
96       */
97      public void testSetAuthor()
98      {
99          instance.setAuthor( "maven:dion" );
100         assertEquals( "Author not set correctly", "maven:dion", instance.getAuthor() );
101     }
102 
103     /**
104      * Test of getComment method
105      */
106     public void testGetComment()
107     {
108         assertEquals( "Comment value not retrieved correctly", "comment", instance.getComment() );
109     }
110 
111     /**
112      * Test of setComment method
113      */
114     public void testSetComment()
115     {
116         instance.setComment( "maven:comment" );
117         assertEquals( "Comment not set correctly", "maven:comment", instance.getComment() );
118     }
119 
120     /**
121      * Test of getDate method
122      */
123     public void testGetDate()
124     {
125         assertEquals( "Date value not retrieved correctly", getDate( 2002, 3, 1 ), instance.getDate() );
126     }
127 
128     /**
129      * Test of setDate method with Date object
130      */
131     public void testSetDate()
132     {
133         Calendar cal = Calendar.getInstance();
134         Date date = cal.getTime();
135         instance.setDate( date );
136         assertEquals( "Date value not set correctly", date, instance.getDate() );
137     }
138 
139 
140     /**
141      * Test of setDate method with String
142      */
143     public void testSetDateFromString()
144     {
145         instance.setDate( "2002/03/04 00:00:00" );
146         assertEquals( "Date value not set correctly from a string", getDate( 2002, 2, 4 ), instance.getDate() );
147     }
148 
149     /**
150      * Test of getDateFormatted method
151      */
152     public void testGetDateFormatted()
153     {
154         assertEquals( "Date not formatted correctly", "2002-04-01", instance.getDateFormatted() );
155     }
156 
157     /**
158      * Test of getDateFormatted method
159      */
160     public void testGetTimeFormatted()
161     {
162         assertEquals( "Time not formatted correctly", "00:00:00", instance.getTimeFormatted() );
163     }
164 
165     public static Date getDate( int year, int month, int day )
166     {
167         Calendar cal = Calendar.getInstance();
168 
169         cal.set( year, month, day, 0, 0, 0 );
170         cal.set( Calendar.MILLISECOND, 0 );
171 
172         return cal.getTime();
173     }
174 
175     public void testEscapeValue()
176     {
177         assertEquals( "", ChangeSet.escapeValue("") );
178         assertEquals( "'", ChangeSet.escapeValue("'") );
179         assertEquals( "a", ChangeSet.escapeValue("a") );
180         assertEquals( "a'", ChangeSet.escapeValue("a'") );
181         assertEquals( "'a'", ChangeSet.escapeValue("'a'") );
182 
183         assertEquals( "a&lt;b&gt;c", ChangeSet.escapeValue("a<b>c") );
184         assertEquals( "&apos;&amp;;&lt;&gt;&quot;", ChangeSet.escapeValue("'&;<>\"") );
185     }
186 
187     public void testEquals()
188     {
189         ChangeSet instance2 = createInstance();
190         assertEquals(instance, instance2);
191 
192         instance2.setComment("another comment");
193         assertFalse(instance2.equals(instance));
194 
195         instance2.setComment("comment");
196         assertEquals(instance, instance2);
197     }
198 
199     public void testHashCode()
200     {
201         int hashCode1 = instance.hashCode();
202         instance.setAuthor("anotherAuthor");
203 
204         assertFalse( hashCode1 == instance.hashCode() );
205         instance.setAuthor( "dion" );
206         assertEquals( hashCode1, instance.hashCode() );
207     }
208 
209     public void testToXml()
210     {
211         String sXml = instance.toXML();
212         assertNotNull(sXml);
213 
214         assertTrue(sXml.indexOf("<changelog-entry>") > -1);
215         assertTrue(sXml.indexOf("</changelog-entry>") > -1);
216     }
217 
218     public void testToXmlWithFiles()
219     {
220         instance.addFile( new ChangeFile( "maven1:dummy" ) );
221         instance.addFile( new ChangeFile( "maven2:dummy2" ) );
222 
223         String sXml = instance.toXML();
224         assertNotNull(sXml);
225 
226         assertTrue(sXml.indexOf("<changelog-entry>") > -1);
227         assertTrue(sXml.indexOf("</changelog-entry>") > -1);
228 
229         assertTrue(sXml.indexOf("<file>") > -1);
230         assertTrue(sXml.indexOf("<name>maven1:dummy</name>") > -1);
231         assertTrue(sXml.indexOf("<name>maven2:dummy2</name>") > -1);
232 
233 
234     }
235 
236 }