001: /*
002: * @(#)BranchCountMeasureUTest.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.IAnalysisModule;
037: import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModuleUTestI;
038: import net.sourceforge.groboutils.codecoverage.v2.IMethodCode;
039: import net.sourceforge.groboutils.codecoverage.v2.compiler.ModifiedClass;
040: import net.sourceforge.groboutils.codecoverage.v2.compiler.ModifiedMethod;
041: import net.sourceforge.groboutils.codecoverage.v2.logger.TestLogger;
042: import net.sourceforge.groboutils.junit.v1.iftc.CxFactory;
043: import net.sourceforge.groboutils.junit.v1.iftc.InterfaceTestSuite;
044:
045: /**
046: * Tests the BranchCountMeasure class.
047: *
048: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
049: * @version $Date: 2004/04/15 05:48:28 $
050: * @since January 26, 2003
051: */
052: public class BranchCountMeasureUTest extends TestCase {
053: //-------------------------------------------------------------------------
054: // Standard JUnit Class-specific declarations
055:
056: private static final Class THIS_CLASS = BranchCountMeasureUTest.class;
057: private static final AutoDoc DOC = new AutoDoc(THIS_CLASS);
058:
059: public BranchCountMeasureUTest(String name) {
060: super (name);
061: }
062:
063: //-------------------------------------------------------------------------
064: // Tests
065:
066: public static class MyTestClass {
067: public static void main(String[] args) {
068: int i = 0;
069: i += 10;
070: if (i > 1) {
071: System.out.println("" + i);
072: }
073: }
074: }
075:
076: public void testAnalyze1() throws Exception {
077: BranchCountMeasure bcm = new BranchCountMeasure();
078: ModifiedClass mc = CCCreatorUtil.createModifiedClass(TestLogger
079: .createPCL(), MyTestClass.class);
080: ModifiedMethod mm = CCCreatorUtil.getMainModifiedMethod(mc);
081: IMethodCode imc = createMethodCode(MyTestClass.class, mm, bcm);
082:
083: // populate the method with marks
084: bcm.analyze(imc);
085:
086: // Generate the class from the modified bytecode, and run it.
087: Class c = BytecodeLoaderUtil.loadClassFromBytecode(mc
088: .getClassName(), mc.getModifiedClass());
089: TestLogger.reset();
090: BytecodeLoaderUtil.runMain(c);
091: assertEquals("Did not mark every bytecode in method.", 3,
092: TestLogger.size());
093: }
094:
095: //-------------------------------------------------------------------------
096: // Helpers
097:
098: protected IMethodCode createMethodCode(Class c, ModifiedMethod mm,
099: IAnalysisModule am) {
100: return CCCreatorUtil.createIMethodCode(c, mm, CCCreatorUtil
101: .createAnalysisModuleSet(new IAnalysisModule[] { am }),
102: 0);
103: }
104:
105: //-------------------------------------------------------------------------
106: // Standard JUnit declarations
107:
108: public static Test suite() {
109: InterfaceTestSuite suite = IAnalysisModuleUTestI.suite();
110: suite.addTestSuite(THIS_CLASS);
111: suite.addFactory(new CxFactory("A") {
112: public Object createImplObject() throws IOException {
113: return new BranchCountMeasure();
114: }
115: });
116:
117: return suite;
118: }
119:
120: public static void main(String[] args) {
121: String[] name = { THIS_CLASS.getName() };
122:
123: // junit.textui.TestRunner.main( name );
124: // junit.swingui.TestRunner.main( name );
125:
126: junit.textui.TestRunner.main(name);
127: }
128:
129: /**
130: *
131: * @exception Exception thrown under any exceptional condition.
132: */
133: protected void setUp() throws Exception {
134: super .setUp();
135:
136: // set ourself up
137: }
138:
139: /**
140: *
141: * @exception Exception thrown under any exceptional condition.
142: */
143: protected void tearDown() throws Exception {
144: // tear ourself down
145:
146: super.tearDown();
147: }
148: }
|