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: 2002/08/23 02:04:06 $ 
22  */
23  package net.sf.statcvs.model;
24  
25  import java.util.Date;
26  import java.util.Set;
27  
28  import junit.framework.TestCase;
29  
30  /**
31   * @author Richard Cyganiak
32   * @version $Id: CommitTest.java,v 1.6 2002/08/23 02:04:06 cyganiak Exp $
33   */
34  public class CommitTest extends TestCase {
35  
36      private static final int DATE = 10000000;
37  
38      private VersionedFile file1;
39      private VersionedFile file2;
40      private VersionedFile file3;
41      private VersionedFile file4;
42      private VersionedFile file5;
43      private Revision rev1;
44      private Revision rev2;
45      private Revision rev4;
46      private Revision rev5;
47      private Revision rev6;
48      private Revision rev7;
49      private Revision rev8;
50      private Commit commit;
51      private Author author1;
52      private Author author2;
53  
54      /**
55       * Constructor for CommitTest.
56       * @param arg0 input
57       */
58      public CommitTest(final String arg0) {
59          super(arg0);
60      }
61  
62      /**
63       * @see TestCase#setUp()
64       */
65      protected void setUp() throws Exception {
66          super.setUp();
67          author1 = new Author("author1");
68          author2 = new Author("author2");
69          final Directory root = Directory.createRoot();
70          file1 = new VersionedFile("file1", root);
71          file2 = new VersionedFile("file2", root);
72          file3 = new VersionedFile("file3", root);
73          file4 = new VersionedFile("file4", root);
74          file5 = new VersionedFile("file5", root);
75          rev1 = createRevision(file1, "rev1", DATE, author1, "message1");
76          rev2 = createRevision(file5, "rev2", DATE + 100, author2, "message1");
77          rev4 = createRevision(file2, "rev4", DATE + 100000, author1, "message1");
78          rev5 = createRevision(file3, "rev5", DATE + 200000, author1, "message1");
79          rev6 = createRevision(file1, "rev6", DATE + 400000, author1, "message1");
80          rev7 = createRevision(file2, "rev7", DATE + 650000, author1, "message1");
81          rev8 = createRevision(file4, "rev8", DATE + 900000, author1, "message1");
82      }
83  
84      /**
85       * Method testCreation.
86       */
87      public void testCreation() {
88          commit = new Commit(rev1);
89          assertEquals(author1, commit.getAuthor());
90          assertEquals("message1", commit.getComment());
91          assertEquals(rev1.getDate(), commit.getDate());
92          assertEquals(1, commit.getRevisions().size());
93          assertTrue(commit.getRevisions().contains(rev1));
94      }
95  
96      /**
97       * Method testAddAfter.
98       */
99      public void testAddAfter() {
100         commit = new Commit(rev6);
101         commit.addRevision(rev7);
102         assertEquals(author1, commit.getAuthor());
103         assertEquals("message1", commit.getComment());
104         assertEquals(rev6.getDate(), commit.getDate());
105         assertEquals(2, commit.getRevisions().size());
106         assertTrue("should contain rev6", commit.getRevisions().contains(rev6));
107         assertTrue("should contain rev7", commit.getRevisions().contains(rev7));
108     }
109 
110     /**
111      * Method testAddBefore.
112      */
113     public void testAddBefore() {
114         commit = new Commit(rev6);
115         commit.addRevision(rev5);
116         assertEquals(author1, commit.getAuthor());
117         assertEquals("message1", commit.getComment());
118         assertEquals(rev6.getDate(), commit.getDate());
119         assertEquals(2, commit.getRevisions().size());
120         assertTrue("should contain rev6", commit.getRevisions().contains(rev6));
121         assertTrue("should contain rev5", commit.getRevisions().contains(rev5));
122     }
123 
124     /**
125      * Method testAffectedFiles.
126      */
127     public void testAffectedFiles() {
128         commit = new Commit(rev1);
129         commit.addRevision(rev4);
130         commit.addRevision(rev5);
131         commit.addRevision(rev6);
132         commit.addRevision(rev7);
133         commit.addRevision(rev8);
134         final Set affectedFiles = commit.getAffectedFiles();
135         assertEquals(4, affectedFiles.size());
136         assertTrue("should contain file1", affectedFiles.contains("file1"));
137         assertTrue("should contain file2", affectedFiles.contains("file2"));
138         assertTrue("should contain file3", affectedFiles.contains("file3"));
139         assertTrue("should contain file4", affectedFiles.contains("file4"));
140     }
141 
142     public void testCompareTo() {
143         final Commit commit1 = new Commit(rev1);
144         final Commit commit2 = new Commit(rev1);
145         final Commit commit3 = new Commit(rev2);
146         assertEquals(0, commit1.compareTo(commit2));
147         assertEquals(0, commit2.compareTo(commit1));
148         assertTrue(commit1.compareTo(commit3) < 0);
149         assertTrue(commit3.compareTo(commit1) > 0);
150     }
151 
152     private Revision createRevision(final VersionedFile file, final String revision, final long time, final Author author, final String message) {
153         return new Revision(file, revision, Revision.TYPE_CHANGE, author, new Date(time), message, 0, 0, 0, null);
154     }
155 }