001: /*
002: * @(#)ModifiedInstructionListUTest.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 junit.framework.TestSuite;
032: import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
033: import net.sourceforge.groboutils.codecoverage.v2.BCELCreatorUtil;
034:
035: import org.apache.bcel.generic.InstructionList;
036:
037: /**
038: * Tests the ModifiedInstructionList class.
039: *
040: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
041: * @version $Date: 2004/04/15 05:48:28 $
042: * @since January 13, 2003
043: */
044: public class ModifiedInstructionListUTest extends TestCase {
045: //-------------------------------------------------------------------------
046: // Standard JUnit Class-specific declarations
047:
048: private static final Class THIS_CLASS = ModifiedInstructionListUTest.class;
049: private static final AutoDoc DOC = new AutoDoc(THIS_CLASS);
050:
051: public ModifiedInstructionListUTest(String name) {
052: super (name);
053: }
054:
055: //-------------------------------------------------------------------------
056: // Tests
057:
058: public void testConstructor1() {
059: try {
060: new ModifiedInstructionList((short) 0, 0, 0, null, null);
061: fail("Did not throw IllegalArgumentException.");
062: } catch (IllegalArgumentException e) {
063: // test exception
064: }
065: }
066:
067: public void testConstructor2() {
068: new ModifiedInstructionList((short) 0, 0, 0,
069: createInstructionList(), createModifiedTargeters());
070: }
071:
072: public void testClose1() {
073: ModifiedInstructionList mil = new ModifiedInstructionList(
074: (short) 0, 0, 0, createInstructionList(),
075: createModifiedTargeters());
076: mil.close();
077: }
078:
079: public void testClose2() {
080: ModifiedInstructionList mil = new ModifiedInstructionList(
081: (short) 0, 0, 0, createInstructionList(),
082: createModifiedTargeters());
083: mil.close();
084: try {
085: mil.close();
086: fail("Did not throw IllegalStateException.");
087: } catch (IllegalStateException e) {
088: // test exception
089: }
090: }
091:
092: public void testClose3() {
093: ModifiedInstructionList mil = new ModifiedInstructionList(
094: (short) 0, 0, 0, createInstructionList(),
095: createModifiedTargeters());
096: mil.close();
097: try {
098: mil.getInstructionAt(0);
099: fail("Did not throw IllegalStateException.");
100: } catch (IllegalStateException e) {
101: // test exception
102: }
103: }
104:
105: public void testClose4() {
106: ModifiedInstructionList mil = new ModifiedInstructionList(
107: (short) 0, 0, 0, createInstructionList(),
108: createModifiedTargeters());
109: mil.close();
110: try {
111: mil.getInstructionCount();
112: fail("Did not throw IllegalStateException.");
113: } catch (IllegalStateException e) {
114: // test exception
115: }
116: }
117:
118: public void testClose5() {
119: ModifiedInstructionList mil = new ModifiedInstructionList(
120: (short) 0, 0, 0, createInstructionList(),
121: createModifiedTargeters());
122: mil.close();
123: try {
124: mil.updateInstructionList();
125: fail("Did not throw IllegalStateException.");
126: } catch (IllegalStateException e) {
127: // test exception
128: }
129: }
130:
131: public void testUpdateInstructionList1() {
132: ModifiedInstructionList mil = new ModifiedInstructionList(
133: (short) 0, 0, 0, createInstructionList(),
134: createModifiedTargeters());
135: int origCount = mil.getInstructionCount();
136: mil.updateInstructionList();
137: assertEquals("Changed instruction count!", origCount, mil
138: .getInstructionCount());
139: mil.updateInstructionList();
140: assertEquals("Changed instruction count!", origCount, mil
141: .getInstructionCount());
142: mil.updateInstructionList();
143: assertEquals("Changed instruction count!", origCount, mil
144: .getInstructionCount());
145: }
146:
147: public void testUpdateInstructionList2() {
148: ModifiedInstructionList mil = new ModifiedInstructionList(
149: (short) 0, 0, 0, createInstructionList(),
150: createModifiedTargeters());
151: int origCount = mil.getInstructionCount();
152: mil.getInstructionAt(origCount - 1).addMark((short) 1,
153: Short.MAX_VALUE);
154: mil.updateInstructionList();
155: mil.getInstructionAt(origCount - 1).addMark((short) 1,
156: Short.MAX_VALUE);
157: mil.updateInstructionList();
158: int count = mil.getInstructionCount();
159: mil.updateInstructionList();
160: assertEquals("Changed instruction count!", count, mil
161: .getInstructionCount());
162: }
163:
164: public void testGetInstructionAt1() {
165: // requirement: we need to be able to put a mark at the end of the
166: // list.
167: ModifiedInstructionList mil = new ModifiedInstructionList(
168: (short) 0, 0, 0, new InstructionList(),
169: createModifiedTargeters());
170: MarkedInstruction mi = mil.getInstructionAt(0);
171: assertNotNull("Returned null end instruction.", mi);
172: assertTrue("End instruction is not the NullInstruction.", mi
173: .getInstruction() instanceof NullInstruction);
174: }
175:
176: //-------------------------------------------------------------------------
177: // Helpers
178:
179: protected InstructionList createInstructionList() {
180: InstructionList il = BCELCreatorUtil.createInstructionList();
181: BCELCreatorUtil.addInstructionHandle(il);
182: return il;
183: }
184:
185: protected ModifiedTargeters createModifiedTargeters() {
186: org.apache.bcel.classfile.JavaClass jc = null;
187: try {
188: jc = BCELCreatorUtil.createJavaClass(THIS_CLASS);
189: } catch (java.io.IOException ex) {
190: ex.printStackTrace();
191: fail(ex.getMessage());
192: }
193: return new ModifiedTargeters(BCELCreatorUtil.createMethodGen(
194: jc, 0));
195: }
196:
197: //-------------------------------------------------------------------------
198: // Standard JUnit declarations
199:
200: public static Test suite() {
201: TestSuite suite = new TestSuite(THIS_CLASS);
202:
203: return suite;
204: }
205:
206: public static void main(String[] args) {
207: String[] name = { THIS_CLASS.getName() };
208:
209: // junit.textui.TestRunner.main( name );
210: // junit.swingui.TestRunner.main( name );
211:
212: junit.textui.TestRunner.main(name);
213: }
214:
215: /**
216: *
217: * @exception Exception thrown under any exceptional condition.
218: */
219: protected void setUp() throws Exception {
220: super .setUp();
221:
222: // set ourself up
223: }
224:
225: /**
226: *
227: * @exception Exception thrown under any exceptional condition.
228: */
229: protected void tearDown() throws Exception {
230: // tear ourself down
231:
232: super.tearDown();
233: }
234: }
|