1   /*
2    StatCvs - CVS statistics generation 
3    Copyright (C) 2002  Lukasz Pekacki <lukasz@pekacki.de>
4    http://statcvs.sf.net/
5    
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10  
11   This library is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   Lesser General Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with this library; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   
20   $RCSfile: BuilderTest.java,v $ 
21   Created on $Date: 2004/12/14 13:38:13 $ 
22   */
23  
24  package net.sf.statsvn.input;
25  
26  import java.util.Date;
27  import java.util.HashMap;
28  import java.util.Iterator;
29  import java.util.Set;
30  
31  import junit.framework.TestCase;
32  import net.sf.statcvs.model.Author;
33  import net.sf.statcvs.model.Directory;
34  import net.sf.statcvs.model.Repository;
35  import net.sf.statcvs.model.Revision;
36  import net.sf.statcvs.model.VersionedFile;
37  import net.sf.statcvs.util.FilePatternMatcher;
38  
39  /**
40   * Test cases for {@link Builder}.
41   * 
42   * @author Anja Jentzsch
43   * @author Richard Cyganiak
44   * @see LinesOfCodeTest
45   * @version $Id: BuilderTest.java,v 1.22 2004/12/14 13:38:13 squig Exp $
46   */
47  public class BuilderTest extends TestCase {
48  	private Builder builder;
49  
50  	private RevisionData rev1;
51  
52  	private RevisionData rev2;
53  
54  	private RevisionData rev3;
55  
56  	/**
57  	 * Constructor
58  	 * 
59  	 * @param arg0
60  	 *            input
61  	 */
62  	public BuilderTest(final String arg0) {
63  		super(arg0);
64  	}
65  
66  	/**
67  	 * @see TestCase#setUp()
68  	 */
69  	protected void setUp() throws Exception {
70  		super.setUp();
71  		builder = new Builder(null, null, null, null);
72  		builder.buildModule("testmodule");
73  		rev1 = new RevisionData();
74  		rev1.setRevisionNumber("1");
75  		rev1.setLoginName("author1");
76  		rev1.setStateExp(true);
77  		rev1.setStateAdded(true);
78  		rev1.setDate(new Date(100000));
79  		rev1.setComment("comment1");
80  		rev2 = new RevisionData();
81  		rev2.setRevisionNumber("2");
82  		rev2.setLoginName("author2");
83  		rev2.setStateExp(true);
84  		rev2.setLines(10, 2);
85  		rev2.setDate(new Date(200000));
86  		rev2.setComment("comment2");
87  		rev3 = new RevisionData();
88  		rev3.setRevisionNumber("3");
89  		rev3.setLoginName("author1");
90  		rev3.setStateDead(true);
91  		rev3.setDate(new Date(300000));
92  		rev3.setComment("comment3");
93  	}
94  
95  	/**
96  	 * Test if the module name is correctly passed on
97  	 */
98  	public void testBuildModule() {
99  		final Builder b = new Builder(null, null, null, null);
100 		b.buildModule("test");
101 		assertEquals("test", b.getProjectName());
102 		assertTrue(b.getAtticFileNames().isEmpty());
103 	}
104 
105 	/**
106 	 * test {@link Builder.getAuthor(String)}
107 	 */
108 	public void testGetAuthor() {
109 		final Author author1 = builder.getAuthor("author1");
110 		final Author author2 = builder.getAuthor("author2");
111 		final Author author1b = builder.getAuthor("author1");
112 		assertEquals(author1, author1b);
113 		assertTrue(!author1.equals(author2));
114 		assertTrue(!author1b.equals(author2));
115 	}
116 
117 	/**
118 	 * On some Windows systems, people can use both upper and lower case for the
119 	 * same login
120 	 */
121 	public void testGetAuthorCase() {
122 		final Author upper = this.builder.getAuthor("richard");
123 		final Author lower = this.builder.getAuthor("Richard");
124 		assertSame(upper, lower);
125 	}
126 
127 	/**
128 	 * test {@link Builder.getDirectory(String)}
129 	 */
130 	public void testGetDirectoryRoot() {
131 		final Directory dir1 = builder.getDirectory("fileInRoot");
132 		final Directory dir2 = builder.getDirectory("anotherFileInRoot");
133 		assertEquals(dir1, dir2);
134 		assertTrue(dir1.isRoot());
135 		assertEquals("", dir1.getName());
136 		assertEquals("", dir1.getPath());
137 	}
138 
139 	/**
140 	 * test {@link Builder.getDirectory(String)}
141 	 */
142 	public void testGetDirectoryDeepPath() {
143 		final Directory dir1 = builder.getDirectory("src/file");
144 		final Directory dir2 = builder.getDirectory("src/net/sf/statcvs/Main.java");
145 		assertEquals(dir1, dir2.getParent().getParent().getParent());
146 		assertTrue(dir1.getParent().isRoot());
147 	}
148 
149 	/**
150 	 * test {@link Builder.getDirectory(String)}
151 	 */
152 	public void testGetDirectorySeveralPaths() {
153 		final Directory dir1 = builder.getDirectory("src/net/sf/statcvs/Main.java");
154 		final Directory dir2 = builder.getDirectory("src/com/microsoft/Windows95.java");
155 		final Directory dir3 = builder.getDirectory("src/com/microsoft/Windows98.java");
156 		assertEquals(dir2, dir3);
157 		assertEquals(dir1.getParent().getParent().getParent(), dir2.getParent().getParent());
158 	}
159 
160 	/**
161 	 * test {@link Builder.addFile(VersionedFile)} and
162 	 * {@link Builder.getFiles()}
163 	 */
164 	public void testFilesEmpty() throws Exception {
165 		final Builder builder1 = new Builder(null, null, null, null);
166 		final Repository repository = builder1.createRepository();
167 		assertTrue(repository.isEmpty());
168 	}
169 
170 	/**
171 	 * test {@link Builder.addFile(VersionedFile)} and
172 	 * {@link Builder.getFiles()}
173 	 */
174 	public void testFilesOneFile() throws Exception {
175 		builder.buildFile("file1", false, false, new HashMap(), new HashMap());
176 		builder.buildRevision(rev1);
177 		final Repository content = builder.createRepository();
178 
179 		assertNotNull(content.getFiles());
180 		assertEquals(1, content.getFiles().size());
181 		final VersionedFile file1 = (VersionedFile) content.getFiles().first();
182 		assertEquals("file1", file1.getFilenameWithPath());
183 		assertEquals(builder.getDirectory(""), file1.getDirectory());
184 		assertEquals(1, file1.getRevisions().size());
185 	}
186 
187 	/**
188 	 * test {@link Builder.addFile(VersionedFile)} and
189 	 * {@link Builder.getFiles()}
190 	 */
191 	public void testFileTwoFiles() throws Exception {
192 		builder.buildFile("file2", false, false, new HashMap(), new HashMap());
193 		builder.buildRevision(rev1);
194 		builder.buildFile("file3", false, false, new HashMap(), new HashMap());
195 		builder.buildRevision(rev2);
196 		final Repository content = builder.createRepository();
197 
198 		assertNotNull(content.getFiles());
199 		assertEquals(2, content.getFiles().size());
200 		final VersionedFile file2 = (VersionedFile) content.getFiles().first();
201 		final VersionedFile file3 = (VersionedFile) content.getFiles().last();
202 		assertEquals("file2", file2.getFilenameWithPath());
203 		assertEquals("file3", file3.getFilenameWithPath());
204 	}
205 
206 	/**
207 	 * Tests {@link Builder.buildRevisionBegin}
208 	 */
209 	public void testBuildRevision() throws Exception {
210 		builder.buildFile("file", false, false, new HashMap(), new HashMap());
211 		builder.buildRevision(rev3);
212 		builder.buildRevision(rev2);
213 		builder.buildRevision(rev1);
214 		final Repository content = builder.createRepository();
215 
216 		final VersionedFile file = (VersionedFile) content.getFiles().first();
217 		final Iterator it = file.getRevisions().iterator();
218 		assertTrue(it.hasNext());
219 		final Revision r1 = (Revision) it.next();
220 		assertTrue(it.hasNext());
221 		final Revision r2 = (Revision) it.next();
222 		assertTrue(it.hasNext());
223 		final Revision r3 = (Revision) it.next();
224 		assertTrue(!it.hasNext());
225 
226 		assertEquals("1", r1.getRevisionNumber());
227 		assertTrue(r1.isInitialRevision());
228 		assertEquals("author1", r1.getAuthor().getName());
229 		assertEquals("comment1", r1.getComment());
230 
231 		assertEquals("2", r2.getRevisionNumber());
232 		assertTrue(!r2.isInitialRevision());
233 		assertEquals(2, r2.getReplacedLines());
234 		assertEquals(8, r2.getLinesDelta());
235 		assertEquals("author2", r2.getAuthor().getName());
236 
237 		assertEquals("3", r3.getRevisionNumber());
238 		assertTrue(r3.isDead());
239 		assertEquals("author1", r3.getAuthor().getName());
240 
241 		assertSame(r1.getAuthor(), r3.getAuthor());
242 	}
243 
244 	public void testStartDateForPartialLog() throws Exception {
245 		final RevisionData rev4 = new RevisionData();
246 		rev4.setRevisionNumber("5");
247 		rev4.setLines(10, 5);
248 		rev4.setStateExp(true);
249 		rev4.setLoginName("somebody");
250 		rev4.setDate(new Date(100000000));
251 		builder.buildFile("dir/normal_file", true, false, new HashMap(), new HashMap());
252 		builder.buildRevision(rev3);
253 		builder.buildRevision(rev2);
254 		builder.buildRevision(rev1);
255 		builder.buildFile("partial_logged_file", true, false, new HashMap(), new HashMap());
256 		builder.buildRevision(rev4);
257 		final Repository content = builder.createRepository();
258 
259 		final VersionedFile file = (VersionedFile) content.getRoot().getFiles().iterator().next();
260 		assertTrue(file.getInitialRevision().isBeginOfLog());
261 		final Date beforeRev1 = new Date(rev1.getDate().getTime() - 60000);
262 		assertEquals(beforeRev1, file.getInitialRevision().getDate());
263 	}
264 
265 	public void testIncludePattern() throws Exception {
266 		final Builder b = new Builder(null, new FilePatternMatcher("a*"), new FilePatternMatcher("*z"), null);
267 		assertTrue(b.matchesPatterns("abc"));
268 		assertTrue(!b.matchesPatterns("xyz"));
269 		assertTrue(!b.matchesPatterns("az"));
270 	}
271 
272 	public void testNoAtticFiles() throws Exception {
273 		builder.buildFile("file1", false, false, new HashMap(), new HashMap());
274 		builder.buildFile("file2", false, false, new HashMap(), new HashMap());
275 		builder.buildFile("file3", false, false, new HashMap(), new HashMap());
276 		assertTrue(builder.getAtticFileNames().isEmpty());
277 	}
278 
279 	public void testAtticFiles() throws Exception {
280 		builder.buildFile("file1", false, false, new HashMap(), new HashMap());
281 		builder.buildFile("file2", false, true, new HashMap(), new HashMap());
282 		builder.buildFile("file3", false, true, new HashMap(), new HashMap());
283 		builder.buildFile("file4", false, false, new HashMap(), new HashMap());
284 		final Set attic = builder.getAtticFileNames();
285 		assertTrue(attic.contains("file2"));
286 		assertTrue(attic.contains("file3"));
287 		assertEquals(2, attic.size());
288 	}
289 }