001: /*
002: * @(#)IAnalysisModuleUTestI.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Lesser General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: package net.sourceforge.groboutils.codecoverage.v2;
024:
025: import java.io.IOException;
026:
027: import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
028: import net.sourceforge.groboutils.junit.v1.iftc.ImplFactory;
029: import net.sourceforge.groboutils.junit.v1.iftc.InterfaceTestCase;
030: import net.sourceforge.groboutils.junit.v1.iftc.InterfaceTestSuite;
031:
032: import org.apache.bcel.classfile.JavaClass;
033: import org.apache.bcel.classfile.LineNumberTable;
034: import org.apache.bcel.classfile.Method;
035: import org.apache.bcel.generic.ConstantPoolGen;
036: import org.apache.bcel.generic.Instruction;
037: import org.apache.bcel.generic.MethodGen;
038:
039: /**
040: * Tests the IAnalysisModule interface.
041: *
042: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
043: * @version $Date: 2004/04/15 05:48:27 $
044: * @since December 28, 2002
045: */
046: public class IAnalysisModuleUTestI extends InterfaceTestCase {
047: //-------------------------------------------------------------------------
048: // Standard JUnit Class-specific declarations
049:
050: private static final Class THIS_CLASS = IAnalysisModuleUTestI.class;
051: private static final AutoDoc DOC = new AutoDoc(THIS_CLASS);
052:
053: public IAnalysisModuleUTestI(String name, ImplFactory f) {
054: super (name, IAnalysisModule.class, f);
055: }
056:
057: public IAnalysisModule createIAnalysisModule() {
058: return (IAnalysisModule) createImplObject();
059: }
060:
061: //-------------------------------------------------------------------------
062: // Tests
063:
064: public void testGetMeasureName1() {
065: IAnalysisModule am = createIAnalysisModule();
066: String s = am.getMeasureName();
067: assertNotNull("Null measure name.", s);
068: assertTrue("Empty measure name.", s.length() > 0);
069: }
070:
071: public void testGetMeasureUnit1() {
072: IAnalysisModule am = createIAnalysisModule();
073: String s = am.getMeasureUnit();
074: assertNotNull("Null measure Unit.", s);
075: assertTrue("Empty measure Unit.", s.length() > 0);
076: }
077:
078: public void testGetMimeEncoding1() {
079: IAnalysisModule am = createIAnalysisModule();
080: String s = am.getMimeEncoding();
081: assertNotNull("Null mime encoding.", s);
082: assertTrue("Empty mime encoding.", s.length() > 0);
083: int pos = s.indexOf('/');
084: assertTrue("No '/' in mime encoding: invalid encoding type.",
085: pos > 0);
086: assertTrue("Multiple mime encoding types are not allowed.", s
087: .indexOf(';') < 0);
088: }
089:
090: private static class MockMethodCode implements IMethodCode {
091: private Method meth;
092: private Instruction[] instrL;
093: private String className;
094:
095: public MockMethodCode(Class c, int methodIndex)
096: throws IOException {
097: if (c == null) {
098: c = this .getClass();
099: }
100: JavaClass jc = loadBytecode(c.getName());
101: this .className = jc.getClassName();
102: this .meth = jc.getMethods()[methodIndex];
103: MethodGen mg = new MethodGen(this .meth, this .className,
104: new ConstantPoolGen(jc.getConstantPool()));
105: this .instrL = mg.getInstructionList().getInstructions();
106: }
107:
108: public Method getOriginalMethod() {
109: return this .meth;
110: }
111:
112: public String getMethodName() {
113: return this .meth.getName();
114: }
115:
116: public String getClassName() {
117: return this .className;
118: }
119:
120: public int getInstructionCount() {
121: return this .instrL.length;
122: }
123:
124: public Instruction getInstructionAt(int index) {
125: if (index < 0 || index >= getInstructionCount()) {
126: throw new IndexOutOfBoundsException("" + index);
127: }
128: return this .instrL[index];
129: }
130:
131: public void markInstruction(int index, IAnalysisMetaData meta) {
132: if (index < 0 || index >= getInstructionCount()) {
133: throw new IndexOutOfBoundsException("" + index);
134: }
135: // ignore mark.
136: }
137:
138: public LineNumberTable getLineNumberTable() {
139: return this .meth.getLineNumberTable();
140: }
141: }
142:
143: public void testAnalyze1() throws Exception {
144: IAnalysisModule am = createIAnalysisModule();
145: am.analyze(new MockMethodCode(null, 0));
146: }
147:
148: //-------------------------------------------------------------------------
149:
150: protected static JavaClass loadBytecode(String className)
151: throws IOException {
152: return BytecodeLoaderUtil.loadJavaClass(className);
153: }
154:
155: //-------------------------------------------------------------------------
156: // Standard JUnit declarations
157:
158: public static InterfaceTestSuite suite() {
159: InterfaceTestSuite suite = new InterfaceTestSuite(THIS_CLASS);
160:
161: return suite;
162: }
163:
164: public static void main(String[] args) {
165: String[] name = { THIS_CLASS.getName() };
166:
167: // junit.textui.TestRunner.main( name );
168: // junit.swingui.TestRunner.main( name );
169:
170: junit.textui.TestRunner.main(name);
171: }
172:
173: /**
174: *
175: * @exception Exception thrown under any exceptional condition.
176: */
177: protected void setUp() throws Exception {
178: super .setUp();
179:
180: // set ourself up
181: }
182:
183: /**
184: *
185: * @exception Exception thrown under any exceptional condition.
186: */
187: protected void tearDown() throws Exception {
188: // tear ourself down
189:
190: super.tearDown();
191: }
192: }
|