001: /*
002: * @(#)LineCountMeasureUTest.java
003: *
004: * Copyright (C) 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.module;
028:
029: import java.io.IOException;
030:
031: import junit.framework.Test;
032: import junit.framework.TestCase;
033: import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
034: import net.sourceforge.groboutils.codecoverage.v2.BytecodeLoaderUtil;
035: import net.sourceforge.groboutils.codecoverage.v2.CCCreatorUtil;
036: import net.sourceforge.groboutils.codecoverage.v2.CreateMainClassHelper;
037: import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule;
038: import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModuleUTestI;
039: import net.sourceforge.groboutils.codecoverage.v2.IMethodCode;
040: import net.sourceforge.groboutils.codecoverage.v2.compiler.ModifiedClass;
041: import net.sourceforge.groboutils.codecoverage.v2.compiler.ModifiedMethod;
042: import net.sourceforge.groboutils.codecoverage.v2.logger.TestLogger;
043: import net.sourceforge.groboutils.junit.v1.iftc.CxFactory;
044: import net.sourceforge.groboutils.junit.v1.iftc.InterfaceTestSuite;
045:
046: import org.apache.bcel.Constants;
047: import org.apache.bcel.generic.InstructionConstants;
048: import org.apache.bcel.generic.ObjectType;
049: import org.apache.bcel.generic.PUSH;
050: import org.apache.bcel.generic.Type;
051:
052: /**
053: * Tests the LineCountMeasure class.
054: *
055: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
056: * @version $Date: 2004/04/15 05:48:28 $
057: * @since January 22, 2003
058: */
059: public class LineCountMeasureUTest extends TestCase {
060: //-------------------------------------------------------------------------
061: // Standard JUnit Class-specific declarations
062:
063: private static final Class THIS_CLASS = LineCountMeasureUTest.class;
064: private static final AutoDoc DOC = new AutoDoc(THIS_CLASS);
065:
066: public LineCountMeasureUTest(String name) {
067: super (name);
068: }
069:
070: //-------------------------------------------------------------------------
071: // Tests
072:
073: public static class MyTestClass {
074: public static void main(String[] args) {
075: int i = 0;
076: i += 10;
077: System.out.println("" + i);
078: }
079: }
080:
081: public void testAnalyze1() throws Exception {
082: LineCountMeasure bcm = new LineCountMeasure();
083: ModifiedClass mc = CCCreatorUtil.createModifiedClass(TestLogger
084: .createPCL(), MyTestClass.class);
085: ModifiedMethod mm = CCCreatorUtil.getMainModifiedMethod(mc);
086: IMethodCode imc = createMethodCode(MyTestClass.class, mm, bcm);
087:
088: // populate the method with marks
089: bcm.analyze(imc);
090:
091: // Generate the class from the modified bytecode, and run it.
092: Class c = BytecodeLoaderUtil.loadClassFromBytecode(mc
093: .getClassName(), mc.getModifiedClass());
094: TestLogger.reset();
095: BytecodeLoaderUtil.runMain(c);
096: assertEquals("Did not mark every bytecode in method.", 2,
097: TestLogger.size());
098: }
099:
100: public void testAnalyze2() throws Exception {
101: LineCountMeasure bcm = new LineCountMeasure();
102: CreateMainClassHelper cmch = createHelloWorld();
103: byte bytecode[] = cmch.getBytecode();
104: Class cc = cmch.getGenClass();
105: ModifiedClass mc = CCCreatorUtil.createModifiedClass(TestLogger
106: .createPCL(), "test/HelloWorld.class", bytecode);
107: ModifiedMethod mm = CCCreatorUtil.getMainModifiedMethod(mc);
108: IMethodCode imc = createMethodCode(cc, mm, bcm);
109:
110: // populate the method with marks
111: bcm.analyze(imc);
112:
113: // Generate the class from the modified bytecode, and run it.
114: Class c = BytecodeLoaderUtil.loadClassFromBytecode(mc
115: .getClassName(), mc.getModifiedClass());
116: TestLogger.reset();
117: BytecodeLoaderUtil.runMain(c);
118: assertEquals("Somehow we found a line number.", 0, TestLogger
119: .size());
120: }
121:
122: //-------------------------------------------------------------------------
123: // Helpers
124:
125: protected IMethodCode createMethodCode(Class c, ModifiedMethod mm,
126: IAnalysisModule am) {
127: return CCCreatorUtil.createIMethodCode(c, mm, CCCreatorUtil
128: .createAnalysisModuleSet(new IAnalysisModule[] { am }),
129: 0);
130: }
131:
132: protected CreateMainClassHelper createHelloWorld()
133: throws IOException {
134: CreateMainClassHelper cmch = new CreateMainClassHelper(
135: "test.HelloWorld");
136: ObjectType p_stream = new ObjectType("java.io.PrintStream");
137: cmch.il.append(cmch.factory.createFieldAccess(
138: "java.lang.System", "out", p_stream,
139: Constants.GETSTATIC));
140: cmch.il.append(new PUSH(cmch.cp, "Hello, world."));
141: cmch.il.append(cmch.factory.createInvoke("java.io.PrintStream",
142: "println", Type.VOID, new Type[] { Type.STRING },
143: Constants.INVOKEVIRTUAL));
144: cmch.il.append(InstructionConstants.RETURN);
145:
146: cmch.close();
147: return cmch;
148: }
149:
150: //-------------------------------------------------------------------------
151: // Standard JUnit declarations
152:
153: public static Test suite() {
154: InterfaceTestSuite suite = IAnalysisModuleUTestI.suite();
155: suite.addTestSuite(THIS_CLASS);
156: suite.addFactory(new CxFactory("A") {
157: public Object createImplObject() throws IOException {
158: return new LineCountMeasure();
159: }
160: });
161:
162: return suite;
163: }
164:
165: public static void main(String[] args) {
166: String[] name = { THIS_CLASS.getName() };
167:
168: // junit.textui.TestRunner.main( name );
169: // junit.swingui.TestRunner.main( name );
170:
171: junit.textui.TestRunner.main(name);
172: }
173:
174: /**
175: *
176: * @exception Exception thrown under any exceptional condition.
177: */
178: protected void setUp() throws Exception {
179: super .setUp();
180:
181: // set ourself up
182: }
183:
184: /**
185: *
186: * @exception Exception thrown under any exceptional condition.
187: */
188: protected void tearDown() throws Exception {
189: // tear ourself down
190:
191: super.tearDown();
192: }
193: }
|