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