001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm;
020
021import java.util.Arrays;
022import java.util.Calendar;
023import java.util.Date;
024
025import org.junit.Before;
026import org.junit.Test;
027
028import static org.junit.Assert.assertEquals;
029import static org.junit.Assert.assertFalse;
030import static org.junit.Assert.assertNotEquals;
031import static org.junit.Assert.assertNotNull;
032import static org.junit.Assert.assertTrue;
033
034/**
035 * Tests for the {@link ChangeSet}class
036 *
037 * @author dion
038 *
039 */
040public class ChangeSetTest {
041    /**
042     * the {@link ChangeSet} used for testing
043     */
044    private ChangeSet instance;
045
046    /**
047     * Initialize per test data
048     */
049    @Before
050    public void setUp() {
051        instance = createInstance();
052    }
053
054    private static ChangeSet createInstance() {
055        ChangeSet instance = new ChangeSet();
056        instance.setAuthor("dion");
057        instance.setComment("comment");
058        instance.setDate("2002/04/01 00:00:00");
059        instance.setTags(Arrays.asList("v3.14", "v2<bla>.7]]1828", "<![CDATA[NastyTag"));
060        return instance;
061    }
062
063    /**
064     * Test of addFile methods: using ChangeFile
065     */
066    @Test
067    public void testAddFileWithFile() {
068        ChangeFile file = new ChangeFile("maven:dummy");
069        instance.addFile(file);
070        assertTrue("File name not found in list", instance.toString().indexOf("maven:dummy") != -1);
071
072        assertTrue(instance.containsFilename("maven:"));
073        assertTrue(instance.containsFilename(":dummy"));
074        assertTrue(instance.containsFilename(":"));
075        assertTrue(instance.containsFilename("maven:dummy"));
076        assertFalse(instance.containsFilename("dammy"));
077    }
078
079    /**
080     * Test of toString method
081     */
082    @Test
083    public void testToString() {
084        // dion, Mon Apr 01 00:00:00 EST 2002, comment
085        String value = instance.toString();
086        assertTrue("author not found in string", value.indexOf("dion") != -1);
087        assertTrue("comment not found in string", value.indexOf("comment") != -1);
088        assertTrue("date not found in string", value.indexOf("Mon Apr 01") != -1);
089    }
090
091    /**
092     * Test of getAuthor method
093     */
094    @Test
095    public void testGetAuthor() {
096        assertEquals("Author value not retrieved correctly", "dion", instance.getAuthor());
097    }
098
099    /**
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}