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  package net.sf.statcvs.input;
21  
22  import java.util.Date;
23  import java.util.HashMap;
24  
25  import junit.framework.TestCase;
26  import net.sf.statcvs.model.Revision;
27  import net.sf.statcvs.model.VersionedFile;
28  
29  /**
30   * Tests for {@link FileBuilder}
31   * 
32   * TODO: Add tests for all revision types
33   * 
34   * @author Richard Cyganiak <richard@cyganiak.de>
35   * @version $Id: FileBuilderTest.java,v 1.8 2008/04/02 11:22:14 benoitx Exp $
36   */
37  public class FileBuilderTest extends TestCase {
38      private FileBuilder fb;
39      private final DummyBuilder builder;
40      private final Date date1 = new Date(100000000);
41      private final Date date2 = new Date(200000000);
42      private final RevisionData rev1;
43      private final RevisionData rev1dead;
44      private final RevisionData rev1branch;
45  
46      public FileBuilderTest(final String arg0) {
47          super(arg0);
48          builder = new DummyBuilder();
49          rev1 = new RevisionData();
50          rev1.setDate(date1);
51          rev1.setRevisionNumber("1.1");
52          rev1.setLoginName("author1");
53          rev1.setComment("comment");
54          rev1.setStateExp();
55          rev1dead = new RevisionData();
56          rev1dead.setDate(date1);
57          rev1dead.setRevisionNumber("1.1");
58          rev1dead.setLoginName("author1");
59          rev1dead.setComment("comment");
60          rev1dead.setStateDead();
61          rev1branch = new RevisionData();
62          rev1branch.setDate(date2);
63          rev1branch.setRevisionNumber("1.1.2.1");
64          rev1branch.setLoginName("author1");
65          rev1branch.setComment("comment");
66          rev1branch.setStateExp();
67          rev1branch.setLines(100, 0);
68      }
69  
70      /*
71       * @see TestCase#setUp()
72       */
73      protected void setUp() throws Exception {
74          super.setUp();
75      }
76  
77      public void testCreation() throws Exception {
78          initBuilder("file", false);
79          fb.createFile(date1);
80      }
81  
82      public void testNotInLogTimespan() throws Exception {
83          initBuilder("nolinecount", false);
84          assertNull(fb.createFile(null));
85      }
86  
87      public void testSimple() throws Exception {
88          initBuilder("file", false);
89          fb.addRevisionData(rev1);
90          final VersionedFile file = fb.createFile(date1);
91          assertNotNull(file);
92          assertEquals("file", file.getFilenameWithPath());
93          assertEquals(builder.getDirectory("file"), file.getDirectory());
94          assertEquals(1, file.getRevisions().size());
95      }
96  
97      public void testFileWithoutRevs() throws Exception {
98          initBuilder("file", false);
99          final VersionedFile file = fb.createFile(date1);
100         assertNotNull(file);
101         assertEquals(1, file.getRevisions().size());
102         assertTrue(file.getInitialRevision().isBeginOfLog());
103         assertEquals(100, file.getInitialRevision().getLines());
104         assertEquals(new Date(date1.getTime() - 60000), file.getInitialRevision().getDate());
105     }
106 
107     public void testOneRev() throws Exception {
108         initBuilder("file", false);
109         fb.addRevisionData(rev1);
110         final VersionedFile file = fb.createFile(date1);
111         assertEquals(1, file.getRevisions().size());
112         final Revision rev = file.getInitialRevision();
113         assertEquals(date1, rev.getDate());
114         assertEquals("1.1", rev.getRevisionNumber());
115         assertEquals(builder.getAuthor("author1"), rev.getAuthor());
116         assertEquals("comment", rev.getComment());
117         assertEquals(100, rev.getLines());
118         assertTrue(rev.isInitialRevision());
119     }
120 
121     /**
122      * A file added only on a subbranch and not merged
123      * into the trunk must be ignored.
124      * @throws Exception
125      */
126     public void testAddOnSubbranch() throws Exception {
127         initBuilder("nolinecount", false);
128         fb.addRevisionData(rev1dead);
129         fb.addRevisionData(rev1branch);
130         final VersionedFile file = fb.createFile(date1);
131         assertNull(file);
132     }
133 
134     public void testIgnoreRevisionsOnBranches() throws Exception {
135         initBuilder("file", false);
136         fb.addRevisionData(rev1);
137         fb.addRevisionData(rev1branch);
138         final VersionedFile file = fb.createFile(date1);
139         assertEquals(1, file.getRevisions().size());
140         assertEquals("1.1", file.getInitialRevision().getRevisionNumber());
141     }
142 
143     private void initBuilder(final String filename, final boolean isBinary) {
144         fb = new FileBuilder(builder, filename, isBinary, new HashMap());
145     }
146 }