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  	$Name:  $ 
21  	Created on $Date: 2004/12/14 13:38:13 $ 
22  */
23  package net.sf.statsvn.input;
24  
25  import java.util.Date;
26  import java.util.List;
27  import java.util.TreeSet;
28  
29  import junit.framework.TestCase;
30  import net.sf.statcvs.input.CommitListBuilder;
31  import net.sf.statcvs.model.Author;
32  import net.sf.statcvs.model.Commit;
33  import net.sf.statcvs.model.Directory;
34  import net.sf.statcvs.model.Revision;
35  import net.sf.statcvs.model.VersionedFile;
36  
37  /**
38   * @author Anja Jentzsch
39   * @author Richard Cyganiak
40   * @version $Id: CommitListBuilderTest.java,v 1.8 2004/12/14 13:38:13 squig Exp $
41   */
42  public class CommitListBuilderTest extends TestCase {
43  
44  	private static final int DATE = 10000000;
45  
46  	private Revision rev1;
47  
48  	private Revision rev2;
49  
50  	private Revision rev3;
51  
52  	private Revision rev4;
53  
54  	private Revision rev4b;
55  
56  	private Revision rev5;
57  
58  	private Revision rev6;
59  
60  	private Revision rev6b;
61  
62  	private Revision rev7;
63  
64  	private Revision rev8;
65  
66  	private List commits;
67  
68  	private Author author1;
69  
70  	private Author author2;
71  
72  	private Author author3;
73  
74  	/**
75  	 * Constructor for CommitListBuilderTest.
76  	 * @param arg0 input argument
77  	 */
78  	public CommitListBuilderTest(final String arg0) {
79  		super(arg0);
80  	}
81  
82  	/**
83  	 * @see TestCase#setUp()
84  	 */
85  	protected void setUp() throws Exception {
86  		super.setUp();
87  		author1 = new Author("author1");
88  		author2 = new Author("author2");
89  		author3 = new Author("author3");
90  		final Directory root = Directory.createRoot();
91  		final VersionedFile file1 = new VersionedFile("file1", root);
92  		final VersionedFile file2 = new VersionedFile("file2", root);
93  		final VersionedFile file3 = new VersionedFile("file3", root);
94  		final VersionedFile file4 = new VersionedFile("file4", root);
95  		final VersionedFile file4b = new VersionedFile("file4b", root);
96  		final VersionedFile file5 = new VersionedFile("file5", root);
97  		final VersionedFile file6 = new VersionedFile("file6", root);
98  		rev1 = createRevision(file1, "rev1", DATE, author1, "message1");
99  		rev2 = createRevision(file2, "rev2", DATE + 100, author2, "message1");
100 		rev3 = createRevision(file3, "rev3", DATE + 200, author1, "message2");
101 		rev4 = createRevision(file4, "rev4", DATE + 100000, author3, "message1");
102 		rev4b = createRevision(file4b, "rev4b", DATE + 100000, author1, "message1");
103 		rev5 = createRevision(file5, "rev5", DATE + 200000, author1, "message1");
104 		rev6 = createRevision(file6, "rev6", DATE + 250000, author2, "message1");
105 		rev6b = createRevision(file6, "rev6b", DATE + 400000, author2, "message1");
106 		rev7 = createRevision(file2, "rev7", DATE + 650000, author1, "message1");
107 		rev8 = createRevision(file4, "rev8", DATE + 900000, author1, "message1");
108 	}
109 
110 	/**
111 	 * Method testCreation.
112 	 */
113 	public void testCreation() {
114 		runBuilder(null);
115 		assertEquals(0, commits.size());
116 	}
117 
118 	/**
119 	 * Method testOneRevision.
120 	 */
121 	public void testOneRevision() {
122 		final Revision[] revs = { rev1 };
123 		runBuilder(revs);
124 		assertEquals(1, commits.size());
125 		assertEquals(1, getCommit(0).getRevisions().size());
126 		assertTrue(getCommit(0).getRevisions().contains(rev1));
127 	}
128 
129 	/**
130 	 * Method testOneCommitMultipleRevisions.
131 	 */
132 	public void testOneCommitMultipleRevisions() {
133 		final Revision[] revs = { rev1, rev4b, rev5 };
134 		runBuilder(revs);
135 		assertEquals(1, commits.size());
136 		assertEquals(3, getCommit(0).getRevisions().size());
137 		assertTrue(getCommit(0).getRevisions().contains(rev1));
138 		assertTrue(getCommit(0).getRevisions().contains(rev4b));
139 		assertTrue(getCommit(0).getRevisions().contains(rev5));
140 	}
141 
142 	/**
143 	 * Method testMultipleCommits.
144 	 */
145 	public void testMultipleCommits() {
146 		final Revision[] revs = { rev1, rev2, rev3 };
147 		runBuilder(revs);
148 		assertEquals(3, commits.size());
149 		assertEquals(1, getCommit(0).getRevisions().size());
150 		assertEquals(1, getCommit(1).getRevisions().size());
151 		assertEquals(1, getCommit(2).getRevisions().size());
152 		assertTrue(getCommit(0).getRevisions().contains(rev1));
153 		assertTrue(getCommit(1).getRevisions().contains(rev2));
154 		assertTrue(getCommit(2).getRevisions().contains(rev3));
155 	}
156 
157 	/**
158 	 * Method testSimultaneousCommits.
159 	 */
160 	public void testSimultaneousCommits() {
161 		final Revision[] revs = { rev1, rev2, rev4, rev5, rev6 };
162 		runBuilder(revs);
163 		assertEquals(3, commits.size());
164 		assertEquals(2, getCommit(0).getRevisions().size());
165 		assertEquals(2, getCommit(1).getRevisions().size());
166 		assertEquals(1, getCommit(2).getRevisions().size());
167 		assertTrue(getCommit(0).getRevisions().contains(rev1));
168 		assertTrue(getCommit(0).getRevisions().contains(rev5));
169 		assertTrue(getCommit(1).getRevisions().contains(rev2));
170 		assertTrue(getCommit(1).getRevisions().contains(rev6));
171 		assertTrue(getCommit(2).getRevisions().contains(rev4));
172 	}
173 
174 	/**
175 	 * Method testIsSameCommit.
176 	 */
177 	public void testIsSameCommit() {
178 		final Commit commit = new Commit(rev1);
179 		assertTrue("has different author", !CommitListBuilder.isSameCommit(commit, rev2));
180 		assertTrue("has different comment", !CommitListBuilder.isSameCommit(commit, rev3));
181 		assertTrue("is same commit", CommitListBuilder.isSameCommit(commit, rev4b));
182 	}
183 
184 	/**
185 	 * Method testIsInTimeFrame1.
186 	 */
187 	public void testIsInTimeFrame1() {
188 		final Commit commit = new Commit(rev6b);
189 		assertTrue("rev5 should be in time frame", CommitListBuilder.isInTimeFrame(commit, rev5.getDate()));
190 		assertTrue("rev7 should be in time frame", CommitListBuilder.isInTimeFrame(commit, rev7.getDate()));
191 		assertTrue("rev1 should not be in time frame", !CommitListBuilder.isInTimeFrame(commit, rev1.getDate()));
192 		assertTrue("rev8 should not be in time frame", !CommitListBuilder.isInTimeFrame(commit, rev8.getDate()));
193 	}
194 
195 	private Commit getCommit(final int index) {
196 		return (Commit) commits.get(index);
197 	}
198 
199 	private Revision createRevision(final VersionedFile file, final String revision, final long time, final Author author, final String message) {
200 		return file.addChangeRevision(revision, author, new Date(time), message, 0, 0, 0, null);
201 	}
202 
203 	private void runBuilder(final Revision[] revisions) {
204 		final TreeSet revList = new TreeSet();
205 		if (revisions != null) {
206 			for (int i = 0; i < revisions.length; i++) {
207 				revList.add(revisions[i]);
208 			}
209 		}
210 		commits = new CommitListBuilder(revList).createCommitList();
211 	}
212 }