001: /*
002: * @(#)ModifiedClassUTest.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 java.io.IOException;
030:
031: import junit.framework.Test;
032: import junit.framework.TestCase;
033: import junit.framework.TestSuite;
034: import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
035: import net.sourceforge.groboutils.codecoverage.v2.BytecodeLoaderUtil;
036: import net.sourceforge.groboutils.codecoverage.v2.CCCreatorUtil;
037: import net.sourceforge.groboutils.codecoverage.v2.datastore.AnalysisModuleSet;
038: import net.sourceforge.groboutils.codecoverage.v2.datastore.ClassRecord;
039:
040: /**
041: * Tests the ModifiedClass 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 ModifiedClassUTest extends TestCase {
048: //-------------------------------------------------------------------------
049: // Standard JUnit Class-specific declarations
050:
051: private static final Class THIS_CLASS = ModifiedClassUTest.class;
052: private static final AutoDoc DOC = new AutoDoc(THIS_CLASS);
053:
054: public ModifiedClassUTest(String name) {
055: super (name);
056: }
057:
058: //-------------------------------------------------------------------------
059: // Tests
060:
061: public void testConstructor1() throws Exception {
062: try {
063: new ModifiedClass(null, null);
064: fail("Did not throw IllegalArgumentException.");
065: } catch (IllegalArgumentException iae) {
066: // test exception
067: }
068: }
069:
070: public void testConstructor2() throws Exception {
071: try {
072: new ModifiedClass("", null);
073: fail("Did not throw IllegalArgumentException.");
074: } catch (IllegalArgumentException iae) {
075: // test exception
076: }
077: }
078:
079: public void testConstructor3() throws Exception {
080: try {
081: new ModifiedClass(null, new byte[0]);
082: fail("Did not throw IllegalArgumentException.");
083: } catch (IllegalArgumentException iae) {
084: // test exception
085: }
086: }
087:
088: public void testConstructor4() throws Exception {
089: try {
090: new ModifiedClass(null, null, null);
091: fail("Did not throw IllegalArgumentException.");
092: } catch (IllegalArgumentException iae) {
093: // test exception
094: }
095: }
096:
097: public void testConstructor5() throws Exception {
098: try {
099: new ModifiedClass(null, "", null);
100: fail("Did not throw IllegalArgumentException.");
101: } catch (IllegalArgumentException iae) {
102: // test exception
103: }
104: }
105:
106: public void testConstructor6() throws Exception {
107: try {
108: new ModifiedClass(null, null, new byte[0]);
109: fail("Did not throw IllegalArgumentException.");
110: } catch (IllegalArgumentException iae) {
111: // test exception
112: }
113: }
114:
115: public void testConstructor7() throws Exception {
116: try {
117: new ModifiedClass(new ParseCoverageLogger(), null, null);
118: fail("Did not throw IllegalArgumentException.");
119: } catch (IllegalArgumentException iae) {
120: // test exception
121: }
122: }
123:
124: public void testConstructor8() throws Exception {
125: try {
126: new ModifiedClass(new ParseCoverageLogger(), "", null);
127: fail("Did not throw IllegalArgumentException.");
128: } catch (IllegalArgumentException iae) {
129: // test exception
130: }
131: }
132:
133: public void testConstructor9() throws Exception {
134: try {
135: new ModifiedClass(new ParseCoverageLogger(), null,
136: new byte[0]);
137: fail("Did not throw IllegalArgumentException.");
138: } catch (IllegalArgumentException iae) {
139: // test exception
140: }
141: }
142:
143: public void testConstructor10() throws Exception {
144: try {
145: new ModifiedClass(new ParseCoverageLogger(), "",
146: new byte[0]);
147: fail("Did not throw an exception.");
148: } catch (IOException io) {
149: // test exception
150: }
151: }
152:
153: public void testConstructor11() throws Exception {
154: String filename = BytecodeLoaderUtil
155: .getClassFilename(THIS_CLASS);
156: new ModifiedClass(filename, BytecodeLoaderUtil
157: .loadBytecode(filename));
158: }
159:
160: public void testConstructor12() throws Exception {
161: String filename = BytecodeLoaderUtil
162: .getClassFilename(THIS_CLASS);
163: new ModifiedClass(new ParseCoverageLogger(), filename,
164: BytecodeLoaderUtil.loadBytecode(filename));
165: }
166:
167: public void testGetClassName1() throws Exception {
168: ModifiedClass mc = createModifiedClass(THIS_CLASS);
169: assertEquals(
170: "Created class and returned classname don't match.",
171: THIS_CLASS.getName(), mc.getClassName());
172: }
173:
174: public void testGetClassCRC1() throws Exception {
175: ModifiedClass mc = createModifiedClass(THIS_CLASS);
176: assertTrue("Checksum is zero or -1.", mc.getClassCRC() != 0L
177: && mc.getClassCRC() != -1L);
178: }
179:
180: public void testGetClassSignature1() throws Exception {
181: ModifiedClass mc = createModifiedClass(THIS_CLASS);
182: long crc = mc.getClassCRC();
183: String name = THIS_CLASS.getName();
184: assertTrue("Does not contain the class name.", mc
185: .getClassSignature().indexOf(name) >= 0);
186: assertTrue("Does not contain the class CRC.", mc
187: .getClassSignature().indexOf(Long.toString(crc)) >= 0);
188: }
189:
190: public void testCreateClassRecord1() throws Exception {
191: ModifiedClass mc = createModifiedClass(THIS_CLASS);
192: ClassRecord cr = mc
193: .createClassRecord(createAnalysisModuleSet());
194: assertNotNull("Returned null class record.", cr);
195: assertEquals("Mismatch class name.", THIS_CLASS.getName(), cr
196: .getClassName());
197: assertEquals("Mismatch class CRC.", mc.getClassCRC(), cr
198: .getClassCRC());
199: assertEquals("Mismatch signature.", mc.getClassSignature(), cr
200: .getClassSignature());
201: assertEquals("Mismatch method count.", mc.getMethods().length,
202: cr.getMethodCount());
203: }
204:
205: public void testGetMethods1() throws Exception {
206: ModifiedClass mc = createModifiedClass(THIS_CLASS);
207: ModifiedMethod[] mmL = mc.getMethods();
208: assertNotNull("Returned null method list.", mmL);
209: /* never could get this to work
210: java.lang.reflect.Method mL[] = THIS_CLASS.getDeclaredMethods();
211: assertTrue(
212: "Returned incorrect list size (found "+mmL.length+
213: ", expected close to "+mL.length+").",
214: mmL.length == mL.length || mmL.length == mL.length + 1 );
215: */
216: }
217:
218: public void testGetModifiedClass1() throws Exception {
219: ModifiedClass mc = createModifiedClass(THIS_CLASS);
220: byte[] orig = BytecodeLoaderUtil
221: .loadBytecode(BytecodeLoaderUtil
222: .getClassFilename(THIS_CLASS));
223: byte[] c = mc.getModifiedClass();
224: assertNotNull("Returned null class bytecode.", c);
225: assertTrue(
226: "Returned smaller length bytecode than the original.",
227: c.length >= orig.length);
228: }
229:
230: //-------------------------------------------------------------------------
231: // Helpers
232:
233: protected ModifiedClass createModifiedClass(Class c)
234: throws IOException {
235: return CCCreatorUtil.createModifiedClass(c);
236: }
237:
238: protected AnalysisModuleSet createAnalysisModuleSet() {
239: return CCCreatorUtil.createAnalysisModuleSet(2);
240: }
241:
242: //-------------------------------------------------------------------------
243: // Standard JUnit declarations
244:
245: public static Test suite() {
246: TestSuite suite = new TestSuite(THIS_CLASS);
247:
248: return suite;
249: }
250:
251: public static void main(String[] args) {
252: String[] name = { THIS_CLASS.getName() };
253:
254: // junit.textui.TestRunner.main( name );
255: // junit.swingui.TestRunner.main( name );
256:
257: junit.textui.TestRunner.main(name);
258: }
259:
260: /**
261: *
262: * @exception Exception thrown under any exceptional condition.
263: */
264: protected void setUp() throws Exception {
265: super .setUp();
266:
267: // set ourself up
268: }
269:
270: /**
271: *
272: * @exception Exception thrown under any exceptional condition.
273: */
274: protected void tearDown() throws Exception {
275: // tear ourself down
276:
277: super.tearDown();
278: }
279: }
|