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