KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > AbstractTest


1 /***
2  * ASM tests
3  * Copyright (c) 2002-2005 France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package org.objectweb.asm;
31
32 import java.io.File JavaDoc;
33 import java.io.FileInputStream JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.io.PrintWriter JavaDoc;
36 import java.io.StringWriter JavaDoc;
37 import java.util.Arrays JavaDoc;
38 import java.util.Enumeration JavaDoc;
39 import java.util.zip.ZipEntry JavaDoc;
40 import java.util.zip.ZipFile JavaDoc;
41
42 import org.objectweb.asm.util.TraceClassVisitor;
43
44 import junit.framework.TestCase;
45 import junit.framework.TestSuite;
46
47 /**
48  * Super class for test suites based on a jar file.
49  *
50  * @author Eugene Kuleshov
51  * @author Eric Bruneton
52  */

53 public abstract class AbstractTest extends TestCase {
54
55     protected String JavaDoc n;
56
57     protected InputStream JavaDoc is;
58
59     public AbstractTest() {
60         super("test");
61     }
62
63     protected void init(final String JavaDoc n, final InputStream JavaDoc is) {
64         this.n = n;
65         this.is = is;
66     }
67
68     protected TestSuite getSuite() throws Exception JavaDoc {
69         TestSuite suite = new TestSuite();
70         String JavaDoc files = System.getProperty("asm.test") + ",";
71         String JavaDoc clazz = System.getProperty("asm.test.class");
72         while (files.indexOf(',') != -1) {
73             String JavaDoc file = files.substring(0, files.indexOf(','));
74             files = files.substring(files.indexOf(',') + 1);
75             File JavaDoc f = new File JavaDoc(file);
76             if (f.isDirectory()) {
77                 File JavaDoc[] fs = f.listFiles();
78                 for (int i = 0; i < fs.length; ++i) {
79                     String JavaDoc n = fs[i].getName();
80                     if (n.endsWith(".class")) {
81                         n = n.substring(0, n.length() - 6).replace('/', '.');
82                         if (clazz == null || n.indexOf(clazz) != -1) {
83                             InputStream JavaDoc is = new FileInputStream JavaDoc(fs[i]);
84                             AbstractTest t = (AbstractTest) getClass().newInstance();
85                             t.init(n, is);
86                             suite.addTest(t);
87                         }
88                     }
89                 }
90             } else {
91                 ZipFile JavaDoc zip = new ZipFile JavaDoc(file);
92                 Enumeration JavaDoc entries = zip.entries();
93                 while (entries.hasMoreElements()) {
94                     ZipEntry JavaDoc e = (ZipEntry JavaDoc) entries.nextElement();
95                     String JavaDoc n = e.getName();
96                     if (n.endsWith(".class")) {
97                         n = n.substring(0, n.length() - 6).replace('/', '.');
98                         if (clazz == null || n.indexOf(clazz) != -1) {
99                             InputStream JavaDoc is = zip.getInputStream(e);
100                             AbstractTest t = (AbstractTest) getClass().newInstance();
101                             t.init(n, is);
102                             suite.addTest(t);
103                         }
104                     }
105                 }
106             }
107         }
108         return suite;
109     }
110
111     public abstract void test() throws Exception JavaDoc;
112
113     public void assertEquals(final ClassReader cr1, final ClassReader cr2)
114             throws Exception JavaDoc
115     {
116         if (!Arrays.equals(cr1.b, cr2.b)) {
117             StringWriter JavaDoc sw1 = new StringWriter JavaDoc();
118             StringWriter JavaDoc sw2 = new StringWriter JavaDoc();
119             ClassVisitor cv1 = new TraceClassVisitor(new PrintWriter JavaDoc(sw1));
120             ClassVisitor cv2 = new TraceClassVisitor(new PrintWriter JavaDoc(sw2));
121             cr1.accept(new ClassFilter(cv1), false);
122             cr2.accept(new ClassFilter(cv2), false);
123             String JavaDoc s1 = sw1.toString();
124             String JavaDoc s2 = sw2.toString();
125             assertEquals("different data", s1, s2);
126         }
127     }
128
129     public String JavaDoc getName() {
130         return super.getName() + ": " + n;
131     }
132
133     // -------------------------------------------------------------------------
134

135     static class ClassFilter extends ClassAdapter {
136
137         public ClassFilter(final ClassVisitor cv) {
138             super(cv);
139         }
140
141         public void visitAttribute(final Attribute attr) {
142             // remove unknown attributes
143
}
144
145         public FieldVisitor visitField(
146             final int access,
147             final String JavaDoc name,
148             final String JavaDoc desc,
149             final String JavaDoc signature,
150             final Object JavaDoc value)
151         {
152             return new FieldFilter(cv.visitField(access,
153                     name,
154                     desc,
155                     signature,
156                     value));
157         }
158
159         public MethodVisitor visitMethod(
160             final int access,
161             final String JavaDoc name,
162             final String JavaDoc desc,
163             final String JavaDoc signature,
164             final String JavaDoc[] exceptions)
165         {
166             return new MethodFilter(cv.visitMethod(access,
167                     name,
168                     desc,
169                     signature,
170                     exceptions));
171         }
172     }
173
174     static class MethodFilter extends MethodAdapter {
175
176         public MethodFilter(final MethodVisitor mv) {
177             super(mv);
178         }
179
180         public void visitAttribute(final Attribute attr) {
181             // remove unknown attributes
182
}
183     }
184
185     static class FieldFilter implements FieldVisitor {
186
187         FieldVisitor fv;
188
189         public FieldFilter(final FieldVisitor fv) {
190             this.fv = fv;
191         }
192
193         public AnnotationVisitor visitAnnotation(
194             final String JavaDoc desc,
195             final boolean visible)
196         {
197             return fv.visitAnnotation(desc, visible);
198         }
199
200         public void visitAttribute(final Attribute attr) {
201             // remove unknown attributes
202
}
203
204         public void visitEnd() {
205             fv.visitEnd();
206         }
207     }
208 }
209
Popular Tags