KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > IAnalysisModuleUTestI


1 /*
2  * @(#)IAnalysisModuleUTestI.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 package net.sourceforge.groboutils.codecoverage.v2;
24
25 import java.io.IOException JavaDoc;
26
27 import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
28 import net.sourceforge.groboutils.junit.v1.iftc.ImplFactory;
29 import net.sourceforge.groboutils.junit.v1.iftc.InterfaceTestCase;
30 import net.sourceforge.groboutils.junit.v1.iftc.InterfaceTestSuite;
31
32 import org.apache.bcel.classfile.JavaClass;
33 import org.apache.bcel.classfile.LineNumberTable;
34 import org.apache.bcel.classfile.Method;
35 import org.apache.bcel.generic.ConstantPoolGen;
36 import org.apache.bcel.generic.Instruction;
37 import org.apache.bcel.generic.MethodGen;
38
39
40 /**
41  * Tests the IAnalysisModule interface.
42  *
43  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
44  * @version $Date: 2004/04/15 05:48:27 $
45  * @since December 28, 2002
46  */

47 public class IAnalysisModuleUTestI extends InterfaceTestCase
48 {
49     //-------------------------------------------------------------------------
50
// Standard JUnit Class-specific declarations
51

52     private static final Class JavaDoc THIS_CLASS = IAnalysisModuleUTestI.class;
53     private static final AutoDoc DOC = new AutoDoc( THIS_CLASS );
54     
55     public IAnalysisModuleUTestI( String JavaDoc name, ImplFactory f )
56     {
57         super( name, IAnalysisModule.class, f );
58     }
59
60     
61     public IAnalysisModule createIAnalysisModule()
62     {
63         return (IAnalysisModule)createImplObject();
64     }
65
66
67     //-------------------------------------------------------------------------
68
// Tests
69

70     public void testGetMeasureName1()
71     {
72         IAnalysisModule am = createIAnalysisModule();
73         String JavaDoc s = am.getMeasureName();
74         assertNotNull(
75             "Null measure name.",
76             s );
77         assertTrue(
78             "Empty measure name.",
79             s.length() > 0 );
80     }
81     
82     
83     public void testGetMeasureUnit1()
84     {
85         IAnalysisModule am = createIAnalysisModule();
86         String JavaDoc s = am.getMeasureUnit();
87         assertNotNull(
88             "Null measure Unit.",
89             s );
90         assertTrue(
91             "Empty measure Unit.",
92             s.length() > 0 );
93     }
94     
95     
96     public void testGetMimeEncoding1()
97     {
98         IAnalysisModule am = createIAnalysisModule();
99         String JavaDoc s = am.getMimeEncoding();
100         assertNotNull(
101             "Null mime encoding.",
102             s );
103         assertTrue(
104             "Empty mime encoding.",
105             s.length() > 0 );
106         int pos = s.indexOf( '/' );
107         assertTrue(
108             "No '/' in mime encoding: invalid encoding type.",
109             pos > 0 );
110         assertTrue(
111             "Multiple mime encoding types are not allowed.",
112             s.indexOf( ';' ) < 0 );
113     }
114     
115     
116     private static class MockMethodCode implements IMethodCode
117     {
118         private Method meth;
119         private Instruction[] instrL;
120         private String JavaDoc className;
121         public MockMethodCode( Class JavaDoc c, int methodIndex )
122                 throws IOException JavaDoc
123         {
124             if (c == null)
125             {
126                 c = this.getClass();
127             }
128             JavaClass jc = loadBytecode( c.getName() );
129             this.className = jc.getClassName();
130             this.meth = jc.getMethods()[ methodIndex ];
131             MethodGen mg = new MethodGen( this.meth, this.className,
132                 new ConstantPoolGen( jc.getConstantPool() ) );
133             this.instrL = mg.getInstructionList().getInstructions();
134         }
135         
136         public Method getOriginalMethod() { return this.meth; }
137         public String JavaDoc getMethodName() { return this.meth.getName(); }
138         public String JavaDoc getClassName() { return this.className; }
139         public int getInstructionCount() { return this.instrL.length; }
140         public Instruction getInstructionAt( int index )
141         {
142             if (index < 0 || index >= getInstructionCount())
143             {
144                 throw new IndexOutOfBoundsException JavaDoc( ""+index );
145             }
146             return this.instrL[ index ];
147         }
148         public void markInstruction( int index, IAnalysisMetaData meta )
149         {
150             if (index < 0 || index >= getInstructionCount())
151             {
152                 throw new IndexOutOfBoundsException JavaDoc( ""+index );
153             }
154             // ignore mark.
155
}
156         public LineNumberTable getLineNumberTable()
157         { return this.meth.getLineNumberTable(); }
158     }
159     
160     
161     
162     public void testAnalyze1() throws Exception JavaDoc
163     {
164         IAnalysisModule am = createIAnalysisModule();
165         am.analyze( new MockMethodCode( null, 0 ) );
166     }
167     
168     
169     //-------------------------------------------------------------------------
170

171     protected static JavaClass loadBytecode( String JavaDoc className )
172             throws IOException JavaDoc
173     {
174         return BytecodeLoaderUtil.loadJavaClass( className );
175     }
176     
177     
178     
179     //-------------------------------------------------------------------------
180
// Standard JUnit declarations
181

182     
183     public static InterfaceTestSuite suite()
184     {
185         InterfaceTestSuite suite = new InterfaceTestSuite( THIS_CLASS );
186         
187         return suite;
188     }
189     
190     public static void main( String JavaDoc[] args )
191     {
192         String JavaDoc[] 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      *
203      * @exception Exception thrown under any exceptional condition.
204      */

205     protected void setUp() throws Exception JavaDoc
206     {
207         super.setUp();
208         
209         // set ourself up
210
}
211     
212     
213     /**
214      *
215      * @exception Exception thrown under any exceptional condition.
216      */

217     protected void tearDown() throws Exception JavaDoc
218     {
219         // tear ourself down
220

221         
222         super.tearDown();
223     }
224 }
225
226
Popular Tags