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