001: /*
002: * @(#)ModifiedMethodUTest.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.TestSuite;
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.junit.v1.SubTestTestCase;
035:
036: import org.apache.bcel.classfile.JavaClass;
037: import org.apache.bcel.classfile.Method;
038: import org.apache.bcel.generic.MethodGen;
039:
040: /**
041: * Tests the ModifiedMethod class.
042: *
043: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
044: * @version $Date: 2004/04/15 05:48:28 $
045: * @since January 13, 2003
046: */
047: public class ModifiedMethodUTest extends SubTestTestCase {
048: //-------------------------------------------------------------------------
049: // Standard JUnit Class-specific declarations
050:
051: private static final Class THIS_CLASS = ModifiedMethodUTest.class;
052: private static final AutoDoc DOC = new AutoDoc(THIS_CLASS);
053:
054: public ModifiedMethodUTest(String name) {
055: super (name);
056: }
057:
058: //-------------------------------------------------------------------------
059: // Tests
060:
061: public void testConstructor1() throws Exception {
062: try {
063: new ModifiedMethod((short) 0, 0, 0, null, null, null);
064: fail("Did not throw IllegalArgumentException.");
065: } catch (IllegalArgumentException e) {
066: // test exception
067: }
068: }
069:
070: public void testConstructor2() throws Exception {
071: try {
072: new ModifiedMethod((short) 0, 0, 0,
073: createJavaClass(THIS_CLASS), null, null);
074: fail("Did not throw IllegalArgumentException.");
075: } catch (IllegalArgumentException e) {
076: // test exception
077: }
078: }
079:
080: public void testConstructor3() throws Exception {
081: try {
082: new ModifiedMethod((short) 0, 0, 0, null, getMethod(
083: createJavaClass(THIS_CLASS), 0), null);
084: fail("Did not throw IllegalArgumentException.");
085: } catch (IllegalArgumentException e) {
086: // test exception
087: }
088: }
089:
090: public void testConstructor4() throws Exception {
091: try {
092: new ModifiedMethod((short) 0, 0, 0, null, null,
093: createMethodGen(createJavaClass(THIS_CLASS), 0));
094: fail("Did not throw IllegalArgumentException.");
095: } catch (IllegalArgumentException e) {
096: // test exception
097: }
098: }
099:
100: public void testConstructor5() throws Exception {
101: JavaClass jc = createJavaClass(THIS_CLASS);
102: try {
103: new ModifiedMethod((short) 0, 0, 0, null, getMethod(jc, 0),
104: createMethodGen(jc, 0));
105: fail("Did not throw IllegalArgumentException.");
106: } catch (IllegalArgumentException e) {
107: // test exception
108: }
109: }
110:
111: public void testConstructor6() throws Exception {
112: JavaClass jc = createJavaClass(THIS_CLASS);
113: try {
114: new ModifiedMethod((short) 0, 0, 0, jc, null,
115: createMethodGen(jc, 0));
116: fail("Did not throw IllegalArgumentException.");
117: } catch (IllegalArgumentException e) {
118: // test exception
119: }
120: }
121:
122: public void testConstructor7() throws Exception {
123: JavaClass jc = createJavaClass(THIS_CLASS);
124: try {
125: new ModifiedMethod((short) 0, 0, 0, jc, getMethod(jc, 0),
126: null);
127: fail("Did not throw IllegalArgumentException.");
128: } catch (IllegalArgumentException e) {
129: // test exception
130: }
131: }
132:
133: public void testGetMethodName1() throws Exception {
134: JavaClass jc = createJavaClass(THIS_CLASS);
135: Method m = getMethod(jc, 2);
136: MethodGen mg = createMethodGen(jc, m);
137: ModifiedMethod mm = createModifiedMethod(jc, 2, m, mg);
138: assertEquals("Did not return correct method name.", m.getName()
139: + m.getSignature(), mm.getMethodName());
140: }
141:
142: public void testGetInstructionList1() throws Exception {
143: JavaClass jc = createJavaClass(THIS_CLASS);
144: Method m = getMethod(jc, 4);
145: MethodGen mg = createMethodGen(jc, m);
146: ModifiedMethod mm = createModifiedMethod(jc, 4, m, mg);
147: ModifiedInstructionList mil = mm.getInstructionList();
148: assertNotNull("Returned null instruction list.", mil);
149: assertEquals("Instruction list size not correct.", mg
150: .getInstructionList().size(), mil.getInstructionCount());
151: }
152:
153: public void testGetOriginalClass1() throws Exception {
154: JavaClass jc = createJavaClass(THIS_CLASS);
155: Method m = getMethod(jc, 4);
156: MethodGen mg = createMethodGen(jc, m);
157: ModifiedMethod mm = createModifiedMethod(jc, 4, m, mg);
158: assertSame("Did not return the original java class.", jc, mm
159: .getOriginalClass());
160: }
161:
162: public void testGetOriginalMethod1() throws Exception {
163: JavaClass jc = createJavaClass(THIS_CLASS);
164: Method m = getMethod(jc, 4);
165: MethodGen mg = createMethodGen(jc, m);
166: ModifiedMethod mm = createModifiedMethod(jc, 4, m, mg);
167: assertSame("Did not return the original method.", m, mm
168: .getOriginalMethod());
169: }
170:
171: public void testCanAddMarks1() throws Exception {
172: ModifiedMethod mm = createModifiedMethod(THIS_CLASS, 14);
173: // every method in the test class should be able to be marked.
174: assertTrue(mm.canAddMarks());
175: }
176:
177: public void testGetModifiedMethodGen1() throws Exception {
178: JavaClass jc = createJavaClass(THIS_CLASS);
179: Method m = getMethod(jc, 4);
180: MethodGen mg = createMethodGen(jc, m);
181: ModifiedMethod mm = createModifiedMethod(jc, 4, m, mg);
182: assertSame("Did not return the original MethodGen.", mg, mm
183: .getModifiedMethodGen());
184: }
185:
186: public void testGetNewMethod1() throws Exception {
187: ModifiedMethod mm = createModifiedMethod(THIS_CLASS, 7);
188: try {
189: mm.getNewMethod();
190: fail("Did not throw IllegalStateException.");
191: } catch (IllegalStateException e) {
192: // test exception
193: }
194: }
195:
196: public void testGetNewMethod2() throws Exception {
197: JavaClass jc = createJavaClass(THIS_CLASS);
198: Method m = getMethod(jc, 7);
199: MethodGen mg = createMethodGen(jc, m);
200: ModifiedMethod mm = createModifiedMethod(jc, 7, m, mg);
201: mm.close();
202: Method m2 = mm.getNewMethod();
203: assertNotSame("Returned the original object.", m, m2);
204: assertEquals("Method name was changed.", m.getName(), m2
205: .getName());
206: assertEquals("Method signature was changed.", m.getSignature(),
207: m2.getSignature());
208: }
209:
210: public void testClose1() throws Exception {
211: ModifiedMethod mm = createModifiedMethod(THIS_CLASS, 1);
212: mm.close();
213: try {
214: mm.close();
215: fail("Did not throw IllegalStateException.");
216: } catch (IllegalStateException e) {
217: // test exception
218: }
219: }
220:
221: public void testClose2() throws Exception {
222: ModifiedMethod mm = createModifiedMethod(THIS_CLASS, 1);
223: mm.close();
224: try {
225: mm.getInstructionList();
226: fail("Did not throw IllegalStateException.");
227: } catch (IllegalStateException e) {
228: // test exception
229: }
230: }
231:
232: public void testClose3() throws Exception {
233: ModifiedMethod mm = createModifiedMethod(THIS_CLASS, 1);
234: mm.close();
235: try {
236: mm.getModifiedMethodGen();
237: fail("Did not throw IllegalStateException.");
238: } catch (IllegalStateException e) {
239: // test exception
240: }
241: }
242:
243: //-------------------------------------------------------------------------
244: // Helpers
245:
246: protected ModifiedMethod createModifiedMethod(Class c,
247: int methodIndex) throws Exception {
248: return CCCreatorUtil.createModifiedMethod(c, methodIndex);
249: }
250:
251: protected ModifiedMethod createModifiedMethod(JavaClass jc,
252: int methodIndex, Method m, MethodGen mg) {
253: return CCCreatorUtil.createModifiedMethod(jc, methodIndex, m,
254: mg);
255: }
256:
257: protected JavaClass createJavaClass(Class c) throws Exception {
258: return BCELCreatorUtil.createJavaClass(c);
259: }
260:
261: protected Method getMethod(JavaClass jc, int methodIndex) {
262: return BCELCreatorUtil.getMethod(jc, methodIndex);
263: }
264:
265: protected MethodGen createMethodGen(JavaClass jc, Method m) {
266: return BCELCreatorUtil.createMethodGen(jc, m);
267: }
268:
269: protected MethodGen createMethodGen(JavaClass jc, int methodIndex) {
270: return BCELCreatorUtil.createMethodGen(jc, methodIndex);
271: }
272:
273: //-------------------------------------------------------------------------
274: // Standard JUnit declarations
275:
276: public static Test suite() {
277: TestSuite suite = new TestSuite(THIS_CLASS);
278:
279: return suite;
280: }
281:
282: public static void main(String[] args) {
283: String[] name = { THIS_CLASS.getName() };
284:
285: // junit.textui.TestRunner.main( name );
286: // junit.swingui.TestRunner.main( name );
287:
288: junit.textui.TestRunner.main(name);
289: }
290:
291: /**
292: *
293: * @exception Exception thrown under any exceptional condition.
294: */
295: protected void setUp() throws Exception {
296: super .setUp();
297:
298: // set ourself up
299: }
300:
301: /**
302: *
303: * @exception Exception thrown under any exceptional condition.
304: */
305: protected void tearDown() throws Exception {
306: // tear ourself down
307:
308: super.tearDown();
309: }
310: }
|