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: DummyRepositoryFileManager.java,v $ 
21   Created on $Date: 2004/02/17 18:37:15 $ 
22   */
23  package net.sf.statsvn.input;
24  
25  import java.io.BufferedReader;
26  import java.io.FileInputStream;
27  import java.io.FileReader;
28  import java.io.IOException;
29  import java.util.HashMap;
30  
31  import net.sf.statcvs.input.LogSyntaxException;
32  import net.sf.statcvs.input.NoLineCountException;
33  import net.sf.statsvn.util.ISvnProcessor;
34  import net.sf.statsvn.util.SvnCommandLineProcessor;
35  
36  /**
37   * Dummy <tt>RepositoryFileManager</tt> for unit tests
38   * 
39   * @author Manuel Schulze
40   * @author Jason Kealey <jkealey@shade.ca>
41   * @version $Id: DummyRepositoryFileManager.java,v 1.1 2004/02/17 18:37:15
42   *          cyganiak Exp $
43   */
44  public class DummyRepositoryFileManager extends RepositoryFileManager {
45  
46      protected HashMap hmFinalLineCounts;
47  
48      protected String sFinalLineCountsFile;
49  
50      protected String sSvnInfoUtilPath;
51  
52      protected String sSvnPropgetPath;
53  
54      /**
55       * Only call this constructor if you provide line counts using (@link
56       * #setLinesOfCode(String, int)) and only use (@link
57       * #getLinesOfCode(String)) to read them.
58       * 
59       */
60      public DummyRepositoryFileManager() {
61          super("foo");
62          hmFinalLineCounts = new HashMap();
63      }
64  
65      /**
66       * Creates a new instance with root at <code>pathName</code>.
67       * 
68       * @param checkedOutPath
69       *            the root of the checked out repository
70       * @param sSvnInfoUtilPath
71       *            the path of a saved svn info command.
72       * @param sSvnPropgetPath
73       *            the path of a saved svn propget command
74       * @param sFinalLineCountsFile
75       *            the path of a saved svn list augmented with linecounts
76       *            command.
77       * @throws IOException
78       */
79      public DummyRepositoryFileManager(final String checkedOutPath, final String sSvnInfoUtilPath, final String sSvnPropgetPath,
80              final String sFinalLineCountsFile) throws IOException {
81          super(checkedOutPath);
82          this.sSvnInfoUtilPath = sSvnInfoUtilPath;
83          this.sSvnPropgetPath = sSvnPropgetPath;
84          this.sFinalLineCountsFile = sFinalLineCountsFile;
85  
86          getProcessor().getPropgetProcessor().loadBinaryFiles(sSvnPropgetPath);
87  
88          FileReader freader = null;
89          BufferedReader reader = null;
90          try {
91              freader = new FileReader(sFinalLineCountsFile);
92              reader = new BufferedReader(freader);
93              String s;
94              hmFinalLineCounts = new HashMap();
95              while ((s = reader.readLine()) != null) {
96                  final String[] vals = s.split(" ");
97                  if (vals.length == 1) {
98                      continue;
99                  }
100 
101                 setLinesOfCode(vals[1], Integer.parseInt(vals[0]));
102             }
103         } finally {
104             if (freader != null) {
105                 freader.close();
106             }
107             if (reader != null) {
108                 reader.close();
109             }
110         }
111     }
112 
113     /**
114      * Returns line count differences between two revisions of a file.
115      * 
116      * @param oldRevNr
117      *            old revision number
118      * @param newRevNr
119      *            new revision number
120      * @param filename
121      *            the filename
122      * @return A int[2] array of [lines added, lines removed] is returned.
123      * @throws IOException
124      *             problem parsing the stream
125      */
126     public int[] getLineDiff(final String oldRevNr, final String newRevNr, final String filename) throws IOException {
127         // return SvnDiffUtils.getLineDiff(oldRevNr, newRevNr, filename);
128         final int[] lines = new int[2];
129         lines[0] = 0;
130         lines[1] = 0;
131         return lines;
132 
133     }
134 
135     /**
136      * @see net.sf.statsvn.input.RepositoryFileManager#getLinesOfCode(String)
137      */
138     public int getLinesOfCode(final String filename) throws NoLineCountException {
139         if (hmFinalLineCounts.containsKey(filename)) {
140             return ((Integer) hmFinalLineCounts.get(filename)).intValue();
141         }
142         throw new NoLineCountException();
143     }
144 
145     public String getRevision(final String filename) throws IOException {
146         if (sSvnInfoUtilPath != null) {
147             return super.getRevision(filename);
148         } else {
149             return "";
150         }
151     }
152 
153     /**
154      * Is the given path a binary file in the <b>working</b> directory?
155      * 
156      * @param relativePath
157      *            the directory
158      * @return true if it is marked as a binary file
159      */
160     public boolean isBinary(final String relativePath) {
161         return getProcessor().getPropgetProcessor().getBinaryFiles().contains(relativePath);
162     }
163 
164     /**
165      * Initializes our representation of the repository.
166      * 
167      * @throws LogSyntaxException
168      *             if the svn info --xml is malformed
169      * @throws IOException
170      *             if there is an error reading from the stream
171      */
172     public void loadInfo() throws LogSyntaxException, IOException {
173 
174         final FileInputStream stream = new FileInputStream(sSvnInfoUtilPath);
175         getProcessor().getInfoProcessor().loadInfo(stream);
176     }
177 
178     /**
179      * Sets the number of lines of code for specified file
180      * 
181      * @param filename
182      *            of file to change
183      * @param lines
184      *            lines of code for specified file
185      */
186     public void setLinesOfCode(final String filename, final int lines) {
187         hmFinalLineCounts.put(filename, new Integer(lines));
188     }
189 
190     private ISvnProcessor svnProcessor;
191 
192     public ISvnProcessor getProcessor() {
193         if (svnProcessor == null)
194             svnProcessor = new SvnCommandLineProcessor();
195         return svnProcessor;
196     }
197 
198 }