1   package org.apache.maven.changelog;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import java.util.Calendar;
22  import java.util.Date;
23  import junit.framework.Test;
24  import junit.framework.TestCase;
25  import junit.framework.TestSuite;
26  import junit.textui.TestRunner;
27  
28  /**
29   * Tests for the {@link ChangeLogEntry} class
30   *
31   * @author dion
32   * @version $Id: ChangeLogEntryTest.java 532339 2007-04-25 12:28:56Z ltheussl $
33   */
34  public class ChangeLogEntryTest extends TestCase
35  {
36      
37      /** the {@link ChangeLogEntry} used for testing */
38      private ChangeLogEntry instance;
39      
40      /** the {@link java.util.Date} used for testing */
41      private Date date;
42      
43      /**
44       * Create a test with the given name
45       * @param testName the name of the test
46       */
47      public ChangeLogEntryTest(String testName)
48      {
49          super(testName);
50      }
51      
52      /**
53       * Run the test using the {@link TestRunner}
54       * @param args command line provided arguments
55       */
56      public static void main(String[] args) 
57      {
58          TestRunner.run(suite());
59      }
60      
61      /**
62       * Create a test suite for this class
63       * @return a {@link TestSuite} for all tests in this class
64       */
65      public static Test suite()
66      {
67          return new TestSuite(ChangeLogEntryTest.class);
68      }
69      
70      /**
71       * Initialize per test data
72       */
73      public void setUp()
74      {
75          Calendar cal = Calendar.getInstance();
76          cal.set(2002, 3, 1, 0, 0, 0);
77          cal.set(Calendar.MILLISECOND, 0);
78          this.date = cal.getTime();
79          
80          instance = new ChangeLogEntry();
81          instance.setAuthor("dion");
82          instance.setComment("comment");
83          instance.setDate(date);
84      }
85      
86      /** 
87       * Test of addFile methods: using ChangeLogFile
88       */
89      public void testAddFileWithFile()
90      {
91          ChangeLogFile file = new ChangeLogFile("maven:dummy");
92          instance.addFile(file);
93          assertTrue("File name not found in list",
94              instance.toString().indexOf("maven:dummy") != -1 );
95      }
96  
97      /** 
98       * Test of addFile methods: using file & revision
99       */
100     public void testAddFileWithFileAndRevision()
101     {
102         instance.addFile("maven:dummy", "x.y");
103         assertTrue("File name not found in list", 
104             instance.toString().indexOf("maven:dummy") != -1);
105         assertTrue("Revision not found in list", 
106             instance.toString().indexOf("x.y") != -1);
107     }
108 
109     /** 
110      * Test of toString method
111      */
112     public void testToString()
113     {
114         //dion, Mon Apr 01 00:00:00 EST 2002, [], comment
115         String value = instance.toString();
116         assertTrue("author not found in string", value.indexOf("dion") != -1);
117         assertTrue("comment not found in string", 
118             value.indexOf("comment") != -1);
119         assertTrue("date not found in string", 
120             value.indexOf("Mon Apr 01") != -1);
121         assertTrue("empty file list not found in string", 
122             value.indexOf("[]") != -1);
123     }
124     
125     /**
126      * Test of toXML method
127      */
128     public void testToXML()
129     {
130         String value = instance.toXML();
131         String trimmedValue = value.trim();
132         assertTrue("XML doesn't start with changelog-entry",
133             trimmedValue.startsWith("<changelog-entry>"));
134         assertTrue("XML doesn't contain date", 
135             value.indexOf("<date>2002-04-01</date>") != -1);
136         assertTrue("XML doesn't contain author CDATA", 
137             value.indexOf("<author><![CDATA[dion]]></author>") != -1);
138         assertTrue("XML doesn't contain comment CDATA",
139             value.indexOf("<msg><![CDATA[comment]]></msg>") != -1);
140     }
141     
142     /**
143      * Test of getAuthor method
144      */
145     public void testGetAuthor()
146     {
147         assertEquals("Author value not retrieved correctly", "dion",  
148             instance.getAuthor());
149     }
150     
151     /** 
152      * Test of setAuthor method
153      */
154     public void testSetAuthor()
155     {
156         instance.setAuthor("maven:dion");
157         assertEquals("Author not set correctly", "maven:dion", 
158             instance.getAuthor());
159     }
160     
161     /** 
162      * Test of getComment method
163      */
164     public void testGetComment()
165     {
166         assertEquals("Comment value not retrieved correctly", "comment", 
167             instance.getComment());
168     }
169     
170     /**
171      * Test of setComment method
172      */
173     public void testSetComment()
174     {
175         instance.setComment("maven:comment");
176         assertEquals("Comment not set correctly", "maven:comment", 
177             instance.getComment());
178     }
179     
180     /**
181      * Test of getDate method
182      */
183     public void testGetDate()
184     {
185         Calendar cal = Calendar.getInstance();
186         cal.set(2002, 3, 1, 0, 0, 0);
187         cal.set(Calendar.MILLISECOND, 0);
188         assertEquals("Date value not retrieved correctly",  cal.getTime(), 
189             instance.getDate());
190     }
191     
192     /**
193      * Test of setDate method with Date object
194      */
195     public void testSetDate()
196     {
197         Calendar cal = Calendar.getInstance();
198         Date date = cal.getTime();
199         instance.setDate(date);
200         assertEquals("Date value not set correctly", date, instance.getDate());
201     }
202     
203     /** 
204      * Test of getDateFormatted method
205      */
206     public void testGetDateFormatted()
207     {
208         assertEquals("Date not formatted correctly", "2002-04-01",
209             instance.getDateFormatted());
210     }
211 
212     /** 
213      * Test of getDateFormatted method
214      */
215     public void testGetTimeFormatted()
216     {
217         assertEquals("Time not formatted correctly", "00:00:00",
218             instance.getTimeFormatted());
219     }
220 
221     // Add test methods here, they have to start with 'test' name.
222     // for example:
223     // public void testHello() {}
224 }