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.LinkedList;
24  import java.util.Map;
25  
26  import junit.framework.Assert;
27  
28  /**
29   * Mock Object implementing {@link CvsLogBuilder}
30   * 
31   * @author Richard Cyganiak <richard@cyganiak.de>
32   * @version $Id: MockLogBuilder.java,v 1.5 2008/04/02 11:22:14 benoitx Exp $
33   */
34  public class MockLogBuilder implements CvsLogBuilder {
35      private final LinkedList expectedMethods = new LinkedList();
36      private final LinkedList expectedData = new LinkedList();
37  
38      /* (non-Javadoc)
39       * @see net.sf.statcvs.input.CvsLogBuilder#buildModule(java.lang.String)
40       */
41      public void buildModule(final String moduleName) {
42          Assert.assertEquals(expectedMethods.removeFirst(), "buildModule");
43          Assert.assertEquals(expectedData.removeFirst(), moduleName);
44      }
45  
46      /* (non-Javadoc)
47       * @see net.sf.statcvs.input.CvsLogBuilder#buildFile(java.lang.String, boolean, boolean)
48       */
49      public void buildFile(final String filename, final boolean isBinary, final boolean isInAttic, final Map revBySymnames) {
50          Assert.assertEquals(expectedMethods.removeFirst(), "buildFile");
51          Assert.assertEquals(expectedData.removeFirst(), filename);
52          Assert.assertEquals(expectedData.removeFirst(), new Boolean(isBinary));
53          Assert.assertEquals(expectedData.removeFirst(), new Boolean(isInAttic));
54      }
55  
56      /* (non-Javadoc)
57       * @see net.sf.statcvs.input.CvsLogBuilder#buildRevision(net.sf.statcvs.input.RevisionData)
58       */
59      public void buildRevision(final RevisionData data) {
60          if (expectedMethods.isEmpty()) {
61              Assert.fail("expected no more revisions");
62          }
63          if (!"buildRevision".equals(expectedMethods.getFirst()) && !"nextRevision".equals(expectedMethods.getFirst())
64                  && !((String) expectedMethods.getFirst()).startsWith("current")) {
65              Assert.assertEquals(expectedMethods.getFirst(), "buildRevision");
66          }
67          if ("buildRevision".equals(expectedMethods.getFirst())) {
68              Assert.assertEquals(expectedMethods.removeFirst(), "buildRevision");
69              final RevisionData expected = (RevisionData) expectedData.removeFirst();
70              Assert.assertEquals(expected.getRevisionNumber(), data.getRevisionNumber());
71              Assert.assertEquals(expected.getDate().getTime() / 1000, data.getDate().getTime() / 1000);
72              Assert.assertEquals(expected.getLoginName(), data.getLoginName());
73              Assert.assertEquals(expected.isAddOnSubbranch(), data.isAddOnSubbranch());
74              Assert.assertEquals(expected.isDeletion(), data.isDeletion());
75              Assert.assertEquals(expected.isCreation(), data.isCreation());
76              Assert.assertEquals(expected.isChangeOrRestore(), data.isChangeOrRestore());
77              Assert.assertEquals(expected.getComment(), data.getComment());
78              Assert.assertEquals(expected.getLinesAdded(), data.getLinesAdded());
79              Assert.assertEquals(expected.getLinesRemoved(), data.getLinesRemoved());
80              return;
81          }
82          while (!expectedMethods.isEmpty() && ((String) expectedMethods.getFirst()).startsWith("current")) {
83              final String expected = (String) expectedMethods.removeFirst();
84              if ("currentRevisionNumber".equals(expected)) {
85                  Assert.assertEquals(expectedData.removeFirst(), data.getRevisionNumber());
86              } else if ("currentDate".equals(expected)) {
87                  Assert.assertEquals(((Date) expectedData.removeFirst()).getTime() / 1000, data.getDate().getTime() / 1000);
88              } else if ("currentAuthor".equals(expected)) {
89                  Assert.assertEquals(expectedData.removeFirst(), data.getLoginName());
90              } else if ("currentComment".equals(expected)) {
91                  Assert.assertEquals(expectedData.removeFirst(), data.getComment());
92              } else if ("currentStateExp".equals(expected)) {
93                  Assert.assertTrue(data.isStateExp());
94              } else if ("currentStateDead".equals(expected)) {
95                  Assert.assertTrue(data.isStateDead());
96              } else if ("currentLines".equals(expected)) {
97                  Assert.assertEquals(expectedData.removeFirst(), new Integer(data.getLinesAdded()));
98                  Assert.assertEquals(expectedData.removeFirst(), new Integer(data.getLinesRemoved()));
99              } else if ("currentNoLines".equals(expected)) {
100                 Assert.assertTrue(data.hasNoLines());
101             } else { // can't happen
102                 Assert.fail("bad state: " + expected);
103             }
104         }
105         if (!expectedMethods.isEmpty() && "nextRevision".equals(expectedMethods.getFirst())) {
106             expectedMethods.removeFirst();
107         }
108     }
109 
110     public void expectBuildModule(final String moduleName) {
111         expectedMethods.add("buildModule");
112         expectedData.add(moduleName);
113     }
114 
115     public void expectBuildFile(final String filename, final boolean isBinary, final boolean isInAttic) {
116         expectedMethods.add("buildFile");
117         expectedData.add(filename);
118         expectedData.add(new Boolean(isBinary));
119         expectedData.add(new Boolean(isInAttic));
120     }
121 
122     public void expectBuildRevision(final RevisionData data) {
123         expectedMethods.add("buildRevision");
124         expectedData.add(data);
125     }
126 
127     public void expectNextRevision() {
128         expectedMethods.add("nextRevision");
129     }
130 
131     public void expectCurrentRevisionNumber(final String revision) {
132         expectedMethods.add("currentRevisionNumber");
133         expectedData.add(revision);
134     }
135 
136     public void expectCurrentDate(final Date date) {
137         expectedMethods.add("currentDate");
138         expectedData.add(date);
139     }
140 
141     public void expectCurrentAuthor(final String name) {
142         expectedMethods.add("currentAuthor");
143         expectedData.add(name);
144     }
145 
146     public void expectCurrentComment(final String comment) {
147         expectedMethods.add("currentComment");
148         expectedData.add(comment);
149     }
150 
151     public void expectCurrentStateExp() {
152         expectedMethods.add("currentStateExp");
153     }
154 
155     public void expectCurrentStateDead() {
156         expectedMethods.add("currentStateDead");
157     }
158 
159     public void expectCurrentNoLines() {
160         expectedMethods.add("currentNoLines");
161     }
162 
163     public void expectCurrentLines(final int added, final int removed) {
164         expectedMethods.add("currentLines");
165         expectedData.add(new Integer(added));
166         expectedData.add(new Integer(removed));
167     }
168 
169     public void verify() {
170         if (!expectedMethods.isEmpty()) {
171             Assert.fail("expected " + expectedMethods.getFirst());
172         }
173     }
174 }