001: /*
002: * @(#)IMethodCodeUTestI.java
003: *
004: * Copyright (C) 2002,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;
028:
029: import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
030: import net.sourceforge.groboutils.codecoverage.v2.module.DefaultAnalysisMetaData;
031: import net.sourceforge.groboutils.junit.v1.iftc.ImplFactory;
032: import net.sourceforge.groboutils.junit.v1.iftc.InterfaceTestCase;
033: import net.sourceforge.groboutils.junit.v1.iftc.InterfaceTestSuite;
034:
035: import org.apache.bcel.classfile.Method;
036:
037: /**
038: * Tests the IMethodCode interface.
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:27 $
042: * @since December 28, 2002
043: */
044: public class IMethodCodeUTestI extends InterfaceTestCase {
045: //-------------------------------------------------------------------------
046: // Standard JUnit Class-specific declarations
047:
048: private static final Class THIS_CLASS = IMethodCodeUTestI.class;
049: private static final AutoDoc DOC = new AutoDoc(THIS_CLASS);
050:
051: public IMethodCodeUTestI(String name, ImplFactory f) {
052: super (name, IMethodCode.class, f);
053: }
054:
055: public IMethodCode createIMethodCode() {
056: return (IMethodCode) createImplObject();
057: }
058:
059: //-------------------------------------------------------------------------
060: // Tests
061:
062: public void testGetOriginalMethod1() {
063: IMethodCode mc = createIMethodCode();
064: Method m = mc.getOriginalMethod();
065: assertNotNull("Must not return null original method.", m);
066: }
067:
068: public void testGetMethod1() {
069: IMethodCode mc = createIMethodCode();
070: assertNotNull("Method name is null.", mc.getMethodName());
071: }
072:
073: public void testGetClassName1() {
074: IMethodCode mc = createIMethodCode();
075: assertNotNull("Class name is null.", mc.getClassName());
076: }
077:
078: public void testGetInstructionCount1() {
079: IMethodCode mc = createIMethodCode();
080: int count = mc.getInstructionCount();
081: assertTrue("Count is not valid", count >= 0);
082: }
083:
084: public void testMethodName1() {
085: IMethodCode mc = createIMethodCode();
086: Method m = mc.getOriginalMethod();
087: String name = mc.getMethodName();
088: assertEquals(
089: "Method name doesn't match returned method's name", m
090: .getName()
091: + m.getSignature(), name);
092: }
093:
094: public void testInstructionConsistency1() {
095: IMethodCode mc = createIMethodCode();
096: int count = mc.getInstructionCount();
097: for (int i = 0; i < count; ++i) {
098: assertNotNull("Null instruction at position " + i + ".", mc
099: .getInstructionAt(i));
100: }
101: }
102:
103: public void testInstructionConsistency2() {
104: IMethodCode mc = createIMethodCode();
105: int count = mc.getInstructionCount();
106: try {
107: mc.getInstructionAt(-1);
108: fail("Did not throw excption w/ -1 index.");
109: } catch (IndexOutOfBoundsException e) {
110: // test exception
111: }
112:
113: try {
114: mc.getInstructionAt(count);
115: fail("Did not throw exception w/ " + count + " index.");
116: } catch (IndexOutOfBoundsException e) {
117: // test exception
118: }
119: }
120:
121: public void testMarkInstruction1() {
122: IMethodCode mc = createIMethodCode();
123: int count = mc.getInstructionCount();
124: if (count <= 0) {
125: DOC.getLog().warn(
126: "No instructions to mark for '" + mc + "'.");
127: return;
128: }
129:
130: // something to test
131: // note: we can mark the last index + one over it!
132: for (int i = 0; i <= count; ++i) {
133: IAnalysisMetaData meta = new DefaultAnalysisMetaData("",
134: "", (byte) 0);
135: mc.markInstruction(i, meta);
136: }
137: }
138:
139: public void testMarkInstruction2() {
140: IMethodCode mc = createIMethodCode();
141:
142: try {
143: IAnalysisMetaData meta = new DefaultAnalysisMetaData("",
144: "", (byte) 0);
145: mc.markInstruction(-1, meta);
146: fail("Did not throw IndexOutOfBoundsException.");
147: } catch (IndexOutOfBoundsException e) {
148: // test exception
149: }
150: }
151:
152: public void testMarkInstruction4() {
153: IMethodCode mc = createIMethodCode();
154: int count = mc.getInstructionCount();
155:
156: try {
157: IAnalysisMetaData meta = new DefaultAnalysisMetaData("",
158: "", (byte) 0);
159: mc.markInstruction(count + 1, meta);
160: fail("Did not throw IndexOutOfBoundsException.");
161: } catch (IndexOutOfBoundsException e) {
162: // test exception
163: }
164: }
165:
166: public void testMarkInstruction5() {
167: IMethodCode mc = createIMethodCode();
168: int count = mc.getInstructionCount();
169:
170: // even if the instruction count is == 0, we can still mark it,
171: // hence this will be a valid test for 'null' metadata.
172:
173: // something to test
174: try {
175: mc.markInstruction(0, null);
176: fail("Did not throw IllegalArgumentException.");
177: } catch (IllegalArgumentException e) {
178: // test exception
179: }
180: }
181:
182: //-------------------------------------------------------------------------
183: // Standard JUnit declarations
184:
185: public static InterfaceTestSuite suite() {
186: InterfaceTestSuite suite = new InterfaceTestSuite(THIS_CLASS);
187:
188: return suite;
189: }
190:
191: public static void main(String[] args) {
192: String[] name = { THIS_CLASS.getName() };
193:
194: // junit.textui.TestRunner.main( name );
195: // junit.swingui.TestRunner.main( name );
196:
197: junit.textui.TestRunner.main(name);
198: }
199:
200: /**
201: *
202: * @exception Exception thrown under any exceptional condition.
203: */
204: protected void setUp() throws Exception {
205: super .setUp();
206:
207: // set ourself up
208: }
209:
210: /**
211: *
212: * @exception Exception thrown under any exceptional condition.
213: */
214: protected void tearDown() throws Exception {
215: // tear ourself down
216:
217: super.tearDown();
218: }
219: }
|