KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > just4log > JustLog1Test


1 /*
2  * ============================================================================
3  * The Apache Software License, Version 1.1
4  * ============================================================================
5  *
6  * Copyright (C) 2000-2003 Lucas Bruand. All
7  * rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "Just4Log" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  *
45  * This software consists of voluntary contributions made by many individuals
46  * on behalf of the Apache Software Foundation. For more information on the
47  * Apache Software Foundation, please see <http://www.apache.org/>.
48  *
49  */

50
51 package net.sf.just4log;
52
53 import java.io.ByteArrayInputStream JavaDoc;
54 import java.io.ByteArrayOutputStream JavaDoc;
55 import java.io.File JavaDoc;
56 import java.io.FileInputStream JavaDoc;
57 import java.io.FileNotFoundException JavaDoc;
58 import java.io.FileOutputStream JavaDoc;
59 import java.io.IOException JavaDoc;
60 import java.io.PrintWriter JavaDoc;
61
62 import net.sf.just4log.transform.Transform;
63
64 import org.apache.bcel.classfile.ClassParser;
65 import org.apache.bcel.classfile.JavaClass;
66 import org.apache.bcel.classfile.Method;
67 import org.apache.bcel.util.Class2HTML;
68 //import org.apache.bcel.util.Class2HTML;
69

70 import com.sun.tools.javac.Main;
71
72
73 //import junit.framework.AssertionFailedError;
74
import junit.framework.TestCase;
75
76 /**
77  * @author Lucas Bruand
78  * @version $Id: JustLog1Test.java,v 1.5 2003/09/23 20:41:41 lbruand Exp $
79  */

80
81 public class JustLog1Test extends TestCase {
82     private File JavaDoc tempdir= new File JavaDoc("/home/luke/test.tmp/");
83     //private Main javac = new Main();
84
public String JavaDoc filename="SimpleClass.java";
85     public String JavaDoc classFilename="net"+File.separator+"sf"+File.separator+"just4log"+File.separator+ filename.substring(0,filename.length()-5)+ ".class";
86     public String JavaDoc before;
87     public String JavaDoc after;
88     /**
89      * Constructor for JustLog1Test.
90      * @param arg0
91      */

92     
93     public JustLog1Test(String JavaDoc arg0) {
94         super(arg0);
95     }
96
97     public static void main(String JavaDoc[] args) {
98         junit.textui.TestRunner.run(JustLog1Test.class);
99     }
100     private static final int bufferSize = 4096;
101     public JavaClass getJavaClassForSource(String JavaDoc filename,String JavaDoc sourceCode) throws IOException JavaDoc {
102         File JavaDoc sourceFile = null;
103         File JavaDoc classFile = null;
104         FileInputStream JavaDoc in = null;
105         PrintWriter JavaDoc beforeWriter = null;
106         try {
107             if (!tempdir.isDirectory()) {
108                 tempdir.mkdirs();
109             }
110             sourceFile = new File JavaDoc(tempdir, filename);
111             beforeWriter = new PrintWriter JavaDoc(new FileOutputStream JavaDoc(sourceFile));
112             beforeWriter.print(sourceCode);
113             beforeWriter.close();
114             
115             String JavaDoc[] args = new String JavaDoc[] {
116                     "-g:none", "-d", tempdir.getPath(),
117                     sourceFile.getPath()
118                 };
119             Main.compile(args);
120             classFile = new File JavaDoc(tempdir, classFilename);
121             if (!classFile.exists()) {
122                 throw new FileNotFoundException JavaDoc("ClassFile "+ classFile + " seems not to have been created");
123             }
124             in = new FileInputStream JavaDoc(classFile);
125             ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
126             byte[] buffer= new byte[bufferSize];
127             int count;
128             while ((count = in.read(buffer)) != -1 ) {
129                 out.write(buffer,0,count);
130             }
131             
132             return (new ClassParser( new ByteArrayInputStream JavaDoc(out.toByteArray()), filename)).parse();
133         } finally {
134             try {
135                 in.close();
136                 beforeWriter.close();
137                 sourceFile.delete();
138                 classFile.delete();
139             } catch (Throwable JavaDoc t) {
140                 // This is OK !!
141
}
142             
143         }
144     }
145     /*
146      * Test for JavaClass speedup(JavaClass)
147      */

148     public void testSpeedupJavaClass() throws IOException JavaDoc {
149         //System.out.println(before);
150
JavaClass beforeClass = getJavaClassForSource(getFilename(), getBefore());
151         new Class2HTML(beforeClass, tempdir.getPath()+File.separator+"before"+File.separator);
152         JavaClass afterClass = getJavaClassForSource(getFilename(), getAfter());
153         new Class2HTML(afterClass, tempdir.getPath()+File.separator+"after"+File.separator);
154         JavaClass justLogClass = Transform.speedup(beforeClass);
155         new Class2HTML(justLogClass, tempdir.getPath()+File.separator+"modified"+File.separator);
156         //assertNotSame(beforeClass.getBytes(),justLog.getBytes());
157

158         //justLog.dump(new File(tempdir, "justlog.class"));
159

160         
161         assertEquals(justLogClass, afterClass);
162
163     }
164     
165     public void assertEquals(JavaClass obtained, JavaClass expected) {
166         assertEquals(" File length in bytes", expected.getBytes().length, obtained.getBytes().length);
167         assertEquals(" File Head ", expected.toString(), obtained.toString());
168         Method[] expected_methods = expected.getMethods();
169         Method[] obtained_methods = obtained.getMethods();
170         int i=0;
171         int k=0;
172         for(i=0;i< expected_methods.length;i++) {
173             Method expectedm = expected_methods[i];
174             Method obtainedm;
175             for(k=i;k < obtained_methods.length; k++) {
176                 obtainedm = obtained_methods[k];
177                 if (obtainedm.getSignature().equals(expectedm.getSignature())) {
178                     obtained_methods[k]=obtained_methods[i];
179                     obtained_methods[i]=null;
180                     //System.out.println(expectedm.getCode().toString(true));
181
assertEquals("MethodCode is different", expectedm.getCode().toString(false), obtainedm.getCode().toString(false));
182                     break;
183                 }
184             }
185             if (k==obtained_methods.length) {
186                 fail("Method "+expectedm.getSignature()+ " not found in obtained");
187             }
188         }
189         assertEquals(i,k+1);
190     }
191
192     /**
193      * @return
194      */

195     public String JavaDoc getAfter() {
196         return after;
197     }
198
199     /**
200      * @return
201      */

202     public String JavaDoc getBefore() {
203         return before;
204     }
205
206     /**
207      * @return
208      */

209     public String JavaDoc getClassFilename() {
210         return classFilename;
211     }
212
213     /**
214      * @return
215      */

216     public String JavaDoc getFilename() {
217         return filename;
218     }
219
220     /**
221      * @return
222      */

223     public File JavaDoc getTempdir() {
224         return tempdir;
225     }
226
227     /**
228      * @param string
229      */

230     public void setAfter(String JavaDoc string) {
231         after = string;
232     }
233
234     /**
235      * @param string
236      */

237     public void setBefore(String JavaDoc string) {
238         before = string;
239     }
240
241     /**
242      * @param string
243      */

244     public void setClassFilename(String JavaDoc string) {
245         classFilename = string;
246     }
247
248     /**
249      * @param string
250      */

251     public void setFilename(String JavaDoc string) {
252         filename = string;
253     }
254
255     /**
256      * @param file
257      */

258     public void setTempdir(File JavaDoc file) {
259         tempdir = file;
260     }
261
262     /* (non-Javadoc)
263      * @see junit.framework.TestCase#setUp()
264      */

265     protected void setUp() throws Exception JavaDoc {
266         super.setUp();
267         setBefore("/*\n"+
268         " * Created on 23 juin 2003\n"+
269         " *\n"+
270         " */\n"+
271         "package net.sf.just4log;\n"+
272         "\n"+
273         "import org.apache.commons.logging.Log;\n"+
274         "import org.apache.commons.logging.LogFactory;\n"+
275         "\n"+
276         "/**\n"+
277         " * @author Lucas Bruand\n"+
278         " */\n"+
279         "\n"+
280         "public class SimpleClass {\n"+
281         " private static Log logger = LogFactory.getLog(SimpleClass.class);\n"+
282         " public int sum(int a, int b) {\n"+
283         " int result = a+b;\n"+
284         " logger.warn(\"Sum(\"+a+\",\"+b+\")=\"+result);\n"+
285         " return result;\n"+
286         " }\n"+
287         "}");
288         
289         setAfter("/*\n"+
290         " * Created on 23 juin 2003\n"+
291         " *\n"+
292         " */\n"+
293         "package net.sf.just4log;\n"+
294         "\n"+
295         "import org.apache.commons.logging.Log;\n"+
296         "import org.apache.commons.logging.LogFactory;\n"+
297         "\n"+
298         "/**\n"+
299         " * @author Lucas Bruand\n"+
300         " */\n"+
301         "\n"+
302         "public class SimpleClass {\n"+
303         " private static Log logger = LogFactory.getLog(SimpleClass.class);\n"+
304         " public int sum(int a, int b) {\n"+
305         " int result = a+b;\n"+
306         " if (logger.isWarnEnabled()) {"+
307         " logger.warn(\"Sum(\"+a+\",\"+b+\")=\"+result);\n"+
308         " }"+
309         " return result;\n"+
310         " }\n"+
311         "}");
312     }
313
314     /* (non-Javadoc)
315      * @see junit.framework.TestCase#tearDown()
316      */

317     protected void tearDown() throws Exception JavaDoc {
318         super.tearDown();
319     }
320
321 }
322
Popular Tags