001: /*
002: * @(#)MultipleAnalyzersIUTest.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.codecoverage.v2.compiler;
028:
029: import java.io.ByteArrayOutputStream;
030: import java.io.File;
031: import java.io.IOException;
032:
033: import junit.framework.Test;
034: import junit.framework.TestCase;
035: import junit.framework.TestSuite;
036: import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
037: import net.sourceforge.groboutils.codecoverage.v2.BytecodeLoaderUtil;
038: import net.sourceforge.groboutils.codecoverage.v2.CCCreatorUtil;
039: import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule;
040: import net.sourceforge.groboutils.codecoverage.v2.datastore.DirMetaDataWriter;
041: import net.sourceforge.groboutils.codecoverage.v2.module.BranchCountMeasure;
042: import net.sourceforge.groboutils.codecoverage.v2.module.BytecodeCountMeasure;
043: import net.sourceforge.groboutils.codecoverage.v2.module.CallPairMeasure;
044: import net.sourceforge.groboutils.codecoverage.v2.module.FunctionMeasure;
045: import net.sourceforge.groboutils.codecoverage.v2.module.LineCountMeasure;
046: import net.sourceforge.groboutils.junit.v1.SubTestTestCase;
047:
048: /**
049: * Attempts to load a collection of simple class files, add in all known
050: * analyzer module markings on them, and run the recompiled classes.
051: *
052: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
053: * @version $Date: 2004/04/15 05:48:27 $
054: * @since May 2, 2003
055: */
056: public class MultipleAnalyzersIUTest extends SubTestTestCase {
057: //-------------------------------------------------------------------------
058: // Standard JUnit Class-specific declarations
059:
060: private static final Class THIS_CLASS = MultipleAnalyzersIUTest.class;
061: private static final AutoDoc DOC = new AutoDoc(THIS_CLASS);
062:
063: public MultipleAnalyzersIUTest(String name) {
064: super (name);
065: }
066:
067: //-------------------------------------------------------------------------
068: // Tests
069:
070: public static class MyLogger2 {
071: public static void cover(String classSig, short methodIndex,
072: short channel, short markIndex) {
073: System.out.println("MyLogger2.cover");
074: DOC.getLog().info(getMeasureName(channel));
075: }
076: }
077:
078: private static final String COVER_METHOD_NAME = "cover";
079: private static final String CLASSFILE_PATH = "net/sourceforge/groboutils/codecoverage/v2/compiler/testcode/";
080: private static final String CLASSNAME_PACKAGE = "net.sourceforge.groboutils.codecoverage.v2.compiler.testcode.";
081: private static final String MAIN_SIG = "main([Ljava/lang/String;)V";
082:
083: public static final String[] TESTCLASSNAMES = { "Main1", "If1",
084: "If2", "If3", "Case1", "Case2", "Case3", "Case4", "Case5",
085: "Except1", "Except2", "Finally1", "Finally2", "Finally3",
086: "Finally4", "Finally5", "Synchronized1", "Synchronized2",
087: "Synchronized3", };
088:
089: public void testRebuildMain() throws Exception {
090: File metadir = CCCreatorUtil.createNewDirectory();
091: for (int i = 0; i < TESTCLASSNAMES.length; ++i) {
092: addSubTest(new Recompile(TESTCLASSNAMES[i], metadir));
093: }
094: }
095:
096: public static class Recompile extends TestCase {
097: public String TESTNAME;
098: public File dir;
099:
100: public Recompile(String testname, File dir) {
101: super ("testRecompile");
102: this .TESTNAME = testname;
103: this .dir = dir;
104: DOC.getLog().info("===== Adding test for " + testname);
105: }
106:
107: public String getName() {
108: return "testRecompile:" + TESTNAME;
109: }
110:
111: public void testRecompile() throws Exception {
112: File f = new File(TESTNAME + ".passed");
113: if (f.exists())
114: f.delete();
115:
116: String filename = CLASSFILE_PATH + TESTNAME + ".class";
117: String classname = CLASSNAME_PACKAGE + TESTNAME;
118: byte[] bytecode = compileClass(dir, filename);
119: //DOC.getLog().info( "Original Classfile:" );
120: //RebuildClassIUTest.debugClass( origClassBytes, filename );
121: DOC.getLog().info("Recompiled Classfile:");
122: RebuildClassIUTest.debugClass(bytecode, filename);
123:
124: BytecodeLoaderUtil.verifyClass(classname, bytecode);
125: Class clazz = BytecodeLoaderUtil.loadClassFromBytecode(
126: classname, bytecode);
127: BytecodeLoaderUtil.runMain(clazz);
128:
129: // check for errors
130: assertTrue(
131: TESTNAME + " failed: recompilation didn't work.", f
132: .exists());
133: }
134: }
135:
136: //-------------------------------------------------------------------------
137: // helpers
138:
139: protected static byte[] compileClass(File metadir, String filename)
140: throws IOException {
141: byte[] bytecode = null;
142: DirMetaDataWriter dmdw = new DirMetaDataWriter(metadir);
143: try {
144: PostCompileClass pcc = new PostCompileClass(
145: new ParseCoverageLogger(MyLogger2.class,
146: COVER_METHOD_NAME), dmdw,
147: getAnalysisModules());
148: ByteArrayOutputStream baos = new ByteArrayOutputStream();
149: byte[] origBytecode = BytecodeLoaderUtil
150: .loadBytecode(filename);
151: pcc.postCompile(filename, origBytecode, baos);
152: bytecode = baos.toByteArray();
153: } finally {
154: dmdw.close();
155: }
156: return bytecode;
157: }
158:
159: /**
160: *
161: */
162: private static IAnalysisModule[] getAnalysisModules() {
163: final IAnalysisModule[] amL = new IAnalysisModule[] {
164: new BranchCountMeasure(), new BytecodeCountMeasure(),
165: new CallPairMeasure(), new FunctionMeasure(),
166: new LineCountMeasure() };
167: return amL;
168: }
169:
170: private static String getMeasureName(short i) {
171: if (i < 0)
172: return null;
173: String s[] = getMeasureNames();
174: if (s.length <= i)
175: return null;
176: return s[i];
177: }
178:
179: private static String[] getMeasureNames() {
180: IAnalysisModule[] am = getAnalysisModules();
181: String s[] = new String[am.length];
182: for (int i = 0; i < am.length; ++i) {
183: s[i] = am[i].getMeasureName();
184: }
185: return s;
186: }
187:
188: //-------------------------------------------------------------------------
189: // Standard JUnit declarations
190:
191: public static Test suite() {
192: TestSuite suite = new TestSuite(THIS_CLASS);
193:
194: return suite;
195: }
196:
197: public static void main(String[] args) {
198: String[] name = { THIS_CLASS.getName() };
199:
200: // junit.textui.TestRunner.main( name );
201: // junit.swingui.TestRunner.main( name );
202:
203: junit.textui.TestRunner.main(name);
204: }
205:
206: /**
207: *
208: * @exception Exception thrown under any exceptional condition.
209: */
210: protected void setUp() throws Exception {
211: super .setUp();
212:
213: // set ourself up
214: }
215:
216: /**
217: *
218: * @exception Exception thrown under any exceptional condition.
219: */
220: protected void tearDown() throws Exception {
221: // tear ourself down
222:
223: super.tearDown();
224: }
225: }
|