001: /*
002: * User: Michael Rettig
003: * Date: Aug 14, 2002
004: * Time: 2:54:26 PM
005: */
006: package net.sourceforge.jaxor.tests;
007:
008: import junit.framework.*;
009: import net.sourceforge.jaxor.example.db.*;
010: import net.sourceforge.jaxor.example.tests.MultiTableTestCase;
011: import net.sourceforge.jaxor.util.FileUtils;
012: import net.sourceforge.jaxor.util.SystemException;
013: import org.apache.tools.ant.BuildException;
014: import org.apache.tools.ant.DirectoryScanner;
015: import org.apache.tools.ant.taskdefs.MatchingTask;
016: import org.apache.tools.ant.types.FileSet;
017:
018: import java.util.*;
019:
020: public class TestCoverage extends MatchingTask {
021:
022: private String _packages;
023: private int _maxTimes;
024:
025: private final List _classFiles = new ArrayList();
026: private final List _testFiles = new ArrayList();
027: private boolean _integration = false;
028: private boolean _shuffle = true;
029: private boolean _verbose = false;
030:
031: public void addConfiguredFileset(FileSet fs) {
032: _classFiles.add(fs);
033: }
034:
035: public void addTests(TestFileSet testFileSet) {
036: _testFiles.add(testFileSet);
037: }
038:
039: public void setPackages(String packages) {
040: _packages = packages;
041: }
042:
043: public void setShuffle(boolean shuffle) {
044: _shuffle = shuffle;
045: }
046:
047: public void setMaxTimes(int maxTimes) {
048: _maxTimes = maxTimes;
049: }
050:
051: public void setIntegration(boolean integration) {
052: _integration = integration;
053: }
054:
055: public void setVerbose(boolean verbose) {
056: _verbose = verbose;
057: }
058:
059: public void execute() throws BuildException {
060: List tests = getAllTests();
061: List untested = getAllClasses();
062:
063: TestSuite suite = new TestSuite();
064: for (Iterator iterator = tests.iterator(); iterator.hasNext();) {
065: String s = (String) iterator.next();
066: suite.addTestSuite(toClass(s));
067: }
068: List testContext = new ArrayList();
069:
070: testContext.add(new TestContext("Hypersonic") {
071: protected JaxorContextTestingFactory getFactory() {
072: return new HypersonicContextTestingFactory();
073: }
074: });
075: if (_integration) {
076: testContext.add(new TestContext("Mckoi") {
077: protected JaxorContextTestingFactory getFactory() {
078: return new MckoiContextTestingFactory();
079: }
080: });
081:
082: testContext.add(new TestContext("Axion") {
083: protected JaxorContextTestingFactory getFactory() {
084: return new AxionContextTestingFactory();
085: }
086: });
087: log("Including integration tests!!!");
088: testContext.add(new TestContext("SqlServer") {
089: protected JaxorContextTestingFactory getFactory() {
090: return new SqlServerContextTestingFactory();
091: }
092: });
093: testContext.add(new TestContext("Postgres") {
094: protected JaxorContextTestingFactory getFactory() {
095: return new PostgresContextTestingFactory();
096: }
097: });
098: testContext.add(new TestContext("MySql") {
099: protected JaxorContextTestingFactory getFactory() {
100: return new MySqlContextTestingFactory();
101: }
102: });
103: testContext.add(new TestContext("Oracle") {
104: protected JaxorContextTestingFactory getFactory() {
105: return new OracleContextTestingFactory();
106: }
107: });
108: }
109:
110: TestResult result = new TestResult();
111: MyTestListener myTestListener = new MyTestListener();
112: result.addListener(myTestListener);
113:
114: runAllContexts(testContext, suite, myTestListener, result);
115:
116: if (!result.wasSuccessful())
117: throw new BuildException("Tests Failed");
118:
119: log("Sorted Times");
120: List sortedTimes = myTestListener.getSortedTimes();
121: int count = 0;
122: for (Iterator iterator = sortedTimes.iterator(); iterator
123: .hasNext()
124: && count < _maxTimes; count++) {
125: log(iterator.next().toString());
126: }
127:
128: List testedClasses = ClassLoaderHack
129: .findLoadedClasses(getPackages());
130: untested.removeAll(testedClasses);
131: log("TestCases: " + tests.size());
132: log("Tests: " + result.runCount());
133: log("Tested Classes : "
134: + (testedClasses.size() - tests.size()));
135: log("Untested Classes: " + untested.size());
136: if (untested.size() > 0)
137: throw new BuildException(createFailure(untested));
138: }
139:
140: private List getAllClasses() {
141: List untested = new ArrayList();
142: for (int i = 0; i < _classFiles.size(); i++) {
143: FileSet fs = (FileSet) _classFiles.get(i);
144: DirectoryScanner ds = fs.getDirectoryScanner(project);
145: String[] srcFiles = ds.getIncludedFiles();
146: for (int j = 0; j < srcFiles.length; j++) {
147: String file = FileUtils.toClassFileName(srcFiles[j]);
148: untested.add(file);
149: }
150: }
151: return untested;
152: }
153:
154: private List getAllTests() {
155: List untested = new ArrayList();
156: for (int i = 0; i < _testFiles.size(); i++) {
157: TestFileSet fs = (TestFileSet) _testFiles.get(i);
158: untested.addAll(fs.getTests(getProject()));
159: }
160: if (_shuffle)
161: Collections.shuffle(untested);
162: return untested;
163: }
164:
165: private void runAllContexts(List testContext, TestSuite suite,
166: MyTestListener testListener, TestResult result) {
167: if (_shuffle)
168: Collections.shuffle(testContext);
169: for (Iterator iterator = testContext.iterator(); iterator
170: .hasNext();) {
171: TestContext test = (TestContext) iterator.next();
172: JaxorContextTestingFactory factory = test.getFactory();
173: MultiTableTestCase.JAXOR_Factory = factory;
174: testListener.setDb(test.getName());
175: run(suite, result, test.getName() + " Testing.....");
176: factory.cleanupSuite();
177: }
178: }
179:
180: private void run(TestSuite suite, TestResult result, String msg) {
181: log(msg);
182: long start = System.currentTimeMillis();
183: suite.run(result);
184: long time = (System.currentTimeMillis() - start) / 1000;
185: log("Time: " + time);
186: }
187:
188: private Class toClass(String s) {
189: try {
190: return Class.forName(s);
191: } catch (ClassNotFoundException e) {
192: throw new SystemException(e);
193: }
194: }
195:
196: private abstract class TestContext {
197: private String _name;
198:
199: public TestContext(String name) {
200: _name = name;
201: }
202:
203: /**
204: * Make this a method so we only resolve the context when we start the test.
205: * @return
206: */
207: protected abstract JaxorContextTestingFactory getFactory();
208:
209: public String getName() {
210: return _name;
211: }
212: }
213:
214: private class MyTestListener implements TestListener {
215: private final List times = new ArrayList();
216: private long startTime;
217: private String _db;
218:
219: public void addError(Test test, Throwable throwable) {
220: log(test.toString());
221: throwable.printStackTrace();
222: }
223:
224: public void setDb(String db) {
225: _db = db;
226: }
227:
228: public void addFailure(Test test,
229: AssertionFailedError assertionFailedError) {
230: log(test.toString());
231: assertionFailedError.printStackTrace();
232: }
233:
234: public void endTest(Test test) {
235: long time = System.currentTimeMillis() - startTime;
236: times.add(new TestRun(time, test, _db));
237: }
238:
239: public void startTest(Test test) {
240: if (_verbose)
241: log("start: " + test);
242: startTime = System.currentTimeMillis();
243: }
244:
245: public List getSortedTimes() {
246: Collections.sort(times);
247: return times;
248: }
249:
250: }
251:
252: private String[] getPackages() {
253: StringTokenizer toker = new StringTokenizer(_packages, ":;");
254: List all = new ArrayList();
255: while (toker.hasMoreTokens())
256: all.add(toker.nextElement());
257: return (String[]) all.toArray(new String[all.size()]);
258: }
259:
260: private String createFailure(List all) {
261: String failure = "\nFound untested classes:\n";
262: for (Iterator iterator = all.iterator(); iterator.hasNext();) {
263: String s = (String) iterator.next();
264: failure += "\n" + s;
265: }
266: return failure;
267: }
268:
269: public static class TestRun implements Comparable {
270: private final long _time;
271: private final Test _test;
272: private final String db;
273:
274: public TestRun(long time, Test test, String db) {
275: _time = time;
276: _test = test;
277: this .db = db;
278: }
279:
280: public long getTime() {
281: return _time;
282: }
283:
284: public int compareTo(Object o) {
285: return (int) (((TestRun) o).getTime() - _time);
286: }
287:
288: public String toString() {
289: return _time + "\t" + db + "\t" + _test.toString();
290: }
291: }
292: }
|