001: /*
002: * @(#)DefaultMethodCodeUTest.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.compiler;
028:
029: import junit.framework.Test;
030: import junit.framework.TestCase;
031: import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
032: import net.sourceforge.groboutils.codecoverage.v2.BCELCreatorUtil;
033: import net.sourceforge.groboutils.codecoverage.v2.CCCreatorUtil;
034: import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule;
035: import net.sourceforge.groboutils.codecoverage.v2.IMethodCodeUTestI;
036: import net.sourceforge.groboutils.codecoverage.v2.datastore.AnalysisModuleSet;
037: import net.sourceforge.groboutils.codecoverage.v2.datastore.ClassRecord;
038: import net.sourceforge.groboutils.junit.v1.iftc.CxFactory;
039: import net.sourceforge.groboutils.junit.v1.iftc.InterfaceTestSuite;
040:
041: import org.apache.bcel.classfile.JavaClass;
042: import org.apache.bcel.classfile.Method;
043: import org.apache.bcel.generic.MethodGen;
044:
045: /**
046: * Tests the DefaultMethodCode 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 11, 2003
051: */
052: public class DefaultMethodCodeUTest extends TestCase {
053: //-------------------------------------------------------------------------
054: // Standard JUnit Class-specific declarations
055:
056: private static final Class THIS_CLASS = DefaultMethodCodeUTest.class;
057: private static final AutoDoc DOC = new AutoDoc(THIS_CLASS);
058:
059: public DefaultMethodCodeUTest(String name) {
060: super (name);
061: }
062:
063: //-------------------------------------------------------------------------
064: // Tests
065:
066: public void testConstructor1() {
067: try {
068: new DefaultMethodCode((short) 0, null, null);
069: fail("Did not throw IllegalArgumentException");
070: } catch (IllegalArgumentException e) {
071: // test exception
072: }
073: }
074:
075: public void testConstructor2() throws Exception {
076: try {
077: new DefaultMethodCode((short) 0, createModifiedMethod(),
078: null);
079: fail("Did not throw IllegalArgumentException");
080: } catch (IllegalArgumentException e) {
081: // test exception
082: }
083: }
084:
085: public void testConstructor3() throws Exception {
086: try {
087: new DefaultMethodCode((short) 0, null,
088: createClassRecord(createModifiedMethod()));
089: fail("Did not throw IllegalArgumentException");
090: } catch (IllegalArgumentException e) {
091: // test exception
092: }
093: }
094:
095: public void testConstructor4() throws Exception {
096: ModifiedMethod mm = createModifiedMethod();
097: new DefaultMethodCode((short) 0, mm, createClassRecord(mm));
098: }
099:
100: public void testConstructor5() throws Exception {
101: ModifiedMethod mm = createModifiedMethod(GCI1.class, "m");
102: try {
103: new DefaultMethodCode((short) 0, mm, createClassRecord(mm));
104: fail("Did not throw an IllegalStateException.");
105: } catch (IllegalStateException ise) {
106: assertTrue("Did not mention the abstract method.", ise
107: .getMessage().indexOf("abstract method") >= 0);
108: }
109: }
110:
111: public void testGetOriginalMethod1() throws Exception {
112: ModifiedMethod mm = createModifiedMethod();
113: DefaultMethodCode dmc = new DefaultMethodCode((short) 0, mm,
114: createClassRecord(mm));
115: assertNotNull("Returned null original method.", dmc
116: .getOriginalMethod());
117: assertSame("Not the same original method.", mm
118: .getOriginalMethod(), dmc.getOriginalMethod());
119: }
120:
121: public void testGetMethodName1() throws Exception {
122: ModifiedMethod mm = createModifiedMethod();
123: Method m = mm.getOriginalMethod();
124: DefaultMethodCode dmc = new DefaultMethodCode((short) 0, mm,
125: createClassRecord(mm));
126: assertNotNull("Returned null method name.", dmc.getMethodName());
127: assertEquals("Not the same original method.", m.getName()
128: + m.getSignature(), dmc.getMethodName());
129: }
130:
131: public void testGetClassName1() throws Exception {
132: ModifiedMethod mm = createModifiedMethod();
133: DefaultMethodCode dmc = new DefaultMethodCode((short) 0, mm,
134: createClassRecord(mm));
135: assertNotNull("Returned null class name.", dmc.getClassName());
136: assertSame("Not the same original method.", mm
137: .getOriginalClass().getClassName(), dmc.getClassName());
138: }
139:
140: // the other methods are sufficiently tested by IMethodCodeUTestI
141:
142: //-------------------------------------------------------------------------
143: // Helpers
144:
145: protected static ModifiedMethod createModifiedMethod()
146: throws Exception {
147: return CCCreatorUtil.createModifiedMethod(THIS_CLASS, 0);
148: }
149:
150: protected static ModifiedMethod getModifiedMethod(Class c,
151: String methodName) throws Exception {
152: // return the first method with the given name (not sig)
153:
154: ModifiedClass mc = CCCreatorUtil.createModifiedClass(c);
155: ModifiedMethod mmL[] = mc.getMethods();
156:
157: for (int i = 0; i < mmL.length; ++i) {
158: if (methodName.equals(mmL[i].getOriginalMethod().getName())) {
159: return mmL[i];
160: }
161: }
162: fail("Could not find method named '" + methodName
163: + "' in class " + c + ".");
164: // for the fans...
165: throw new Exception();
166: }
167:
168: protected static ModifiedMethod createModifiedMethod(Class c,
169: String methodName) throws Exception {
170: // This is really difficult now, because the non-modifiable
171: // methods are not returned by ModifierClass.
172: JavaClass jc = BCELCreatorUtil.createJavaClass(c);
173: Method mL[] = jc.getMethods();
174: for (int i = 0; i < mL.length; ++i) {
175: if (methodName.equals(mL[i].getName())) {
176: Method m = mL[i];
177: MethodGen mg = BCELCreatorUtil.createMethodGen(jc, m);
178: ModifiedMethod mm = CCCreatorUtil.createModifiedMethod(
179: jc, i, m, mg);
180: return mm;
181: }
182: }
183: fail("Could not find method named '" + methodName
184: + "' in class " + c + ".");
185: // for the fans...
186: throw new Exception();
187: }
188:
189: protected static AnalysisModuleSet createAnalysisModuleSet() {
190: IAnalysisModule amL[] = new IAnalysisModule[] {
191: CCCreatorUtil.createIAnalysisModule(0),
192: CCCreatorUtil.createIAnalysisModule(1),
193: CCCreatorUtil.createIAnalysisModule(2), };
194: return CCCreatorUtil.createAnalysisModuleSet(amL);
195: }
196:
197: protected static ClassRecord createClassRecord(ModifiedMethod mm) {
198: return CCCreatorUtil.createClassRecord(THIS_CLASS, mm,
199: createAnalysisModuleSet());
200: }
201:
202: private static abstract class GCI1 {
203: public abstract void m();
204: }
205:
206: //-------------------------------------------------------------------------
207: // Standard JUnit declarations
208:
209: public static Test suite() {
210: InterfaceTestSuite suite = IMethodCodeUTestI.suite();
211: suite.addTestSuite(THIS_CLASS);
212: suite.addFactory(new CxFactory("A") {
213: public Object createImplObject() throws Exception {
214: ModifiedMethod mm = createModifiedMethod();
215: return new DefaultMethodCode((short) 0, mm,
216: createClassRecord(mm));
217: }
218: });
219:
220: return suite;
221: }
222:
223: public static void main(String[] args) {
224: String[] name = { THIS_CLASS.getName() };
225:
226: // junit.textui.TestRunner.main( name );
227: // junit.swingui.TestRunner.main( name );
228:
229: junit.textui.TestRunner.main(name);
230: }
231:
232: /**
233: *
234: * @exception Exception thrown under any exceptional condition.
235: */
236: protected void setUp() throws Exception {
237: super .setUp();
238:
239: // set ourself up
240: }
241:
242: /**
243: *
244: * @exception Exception thrown under any exceptional condition.
245: */
246: protected void tearDown() throws Exception {
247: // tear ourself down
248:
249: super.tearDown();
250: }
251: }
|