001: /*
002: * ============================================================================
003: * The Apache Software License, Version 1.1
004: * ============================================================================
005: *
006: * Copyright (C) 2000-2003 Lucas Bruand. All
007: * rights reserved.
008: *
009: * Redistribution and use in source and binary forms, with or without modifica-
010: * tion, are permitted provided that the following conditions are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright notice,
013: * this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright notice,
016: * this list of conditions and the following disclaimer in the documentation
017: * and/or other materials provided with the distribution.
018: *
019: * 3. The end-user documentation included with the redistribution, if any, must
020: * include the following acknowledgment: "This product includes software
021: * developed by the Apache Software Foundation (http://www.apache.org/)."
022: * Alternately, this acknowledgment may appear in the software itself, if
023: * and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. The names "Just4Log" and "Apache Software Foundation" must not be used to
026: * endorse or promote products derived from this software without prior
027: * written permission. For written permission, please contact
028: * apache@apache.org.
029: *
030: * 5. Products derived from this software may not be called "Apache", nor may
031: * "Apache" appear in their name, without prior written permission of the
032: * Apache Software Foundation.
033: *
034: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
035: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
036: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
037: * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
038: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
039: * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
040: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
041: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
042: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
043: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
044: *
045: * This software consists of voluntary contributions made by many individuals
046: * on behalf of the Apache Software Foundation. For more information on the
047: * Apache Software Foundation, please see <http://www.apache.org/>.
048: *
049: */
050:
051: package net.sf.just4log;
052:
053: import java.io.ByteArrayInputStream;
054: import java.io.ByteArrayOutputStream;
055: import java.io.File;
056: import java.io.FileInputStream;
057: import java.io.FileNotFoundException;
058: import java.io.FileOutputStream;
059: import java.io.IOException;
060: import java.io.PrintWriter;
061:
062: import net.sf.just4log.transform.Transform;
063:
064: import org.apache.bcel.classfile.ClassParser;
065: import org.apache.bcel.classfile.JavaClass;
066: import org.apache.bcel.classfile.Method;
067: import org.apache.bcel.util.Class2HTML; //import org.apache.bcel.util.Class2HTML;
068:
069: import com.sun.tools.javac.Main;
070:
071: //import junit.framework.AssertionFailedError;
072: import junit.framework.TestCase;
073:
074: /**
075: * @author Lucas Bruand
076: * @version $Id: JustLog1Test.java,v 1.5 2003/09/23 20:41:41 lbruand Exp $
077: */
078:
079: public class JustLog1Test extends TestCase {
080: private File tempdir = new File("/home/luke/test.tmp/");
081: //private Main javac = new Main();
082: public String filename = "SimpleClass.java";
083: public String classFilename = "net" + File.separator + "sf"
084: + File.separator + "just4log" + File.separator
085: + filename.substring(0, filename.length() - 5) + ".class";
086: public String before;
087: public String after;
088:
089: /**
090: * Constructor for JustLog1Test.
091: * @param arg0
092: */
093:
094: public JustLog1Test(String arg0) {
095: super (arg0);
096: }
097:
098: public static void main(String[] args) {
099: junit.textui.TestRunner.run(JustLog1Test.class);
100: }
101:
102: private static final int bufferSize = 4096;
103:
104: public JavaClass getJavaClassForSource(String filename,
105: String sourceCode) throws IOException {
106: File sourceFile = null;
107: File classFile = null;
108: FileInputStream in = null;
109: PrintWriter beforeWriter = null;
110: try {
111: if (!tempdir.isDirectory()) {
112: tempdir.mkdirs();
113: }
114: sourceFile = new File(tempdir, filename);
115: beforeWriter = new PrintWriter(new FileOutputStream(
116: sourceFile));
117: beforeWriter.print(sourceCode);
118: beforeWriter.close();
119:
120: String[] args = new String[] { "-g:none", "-d",
121: tempdir.getPath(), sourceFile.getPath() };
122: Main.compile(args);
123: classFile = new File(tempdir, classFilename);
124: if (!classFile.exists()) {
125: throw new FileNotFoundException("ClassFile "
126: + classFile + " seems not to have been created");
127: }
128: in = new FileInputStream(classFile);
129: ByteArrayOutputStream out = new ByteArrayOutputStream();
130: byte[] buffer = new byte[bufferSize];
131: int count;
132: while ((count = in.read(buffer)) != -1) {
133: out.write(buffer, 0, count);
134: }
135:
136: return (new ClassParser(new ByteArrayInputStream(out
137: .toByteArray()), filename)).parse();
138: } finally {
139: try {
140: in.close();
141: beforeWriter.close();
142: sourceFile.delete();
143: classFile.delete();
144: } catch (Throwable t) {
145: // This is OK !!
146: }
147:
148: }
149: }
150:
151: /*
152: * Test for JavaClass speedup(JavaClass)
153: */
154: public void testSpeedupJavaClass() throws IOException {
155: //System.out.println(before);
156: JavaClass beforeClass = getJavaClassForSource(getFilename(),
157: getBefore());
158: new Class2HTML(beforeClass, tempdir.getPath() + File.separator
159: + "before" + File.separator);
160: JavaClass afterClass = getJavaClassForSource(getFilename(),
161: getAfter());
162: new Class2HTML(afterClass, tempdir.getPath() + File.separator
163: + "after" + File.separator);
164: JavaClass justLogClass = Transform.speedup(beforeClass);
165: new Class2HTML(justLogClass, tempdir.getPath() + File.separator
166: + "modified" + File.separator);
167: //assertNotSame(beforeClass.getBytes(),justLog.getBytes());
168:
169: //justLog.dump(new File(tempdir, "justlog.class"));
170:
171: assertEquals(justLogClass, afterClass);
172:
173: }
174:
175: public void assertEquals(JavaClass obtained, JavaClass expected) {
176: assertEquals(" File length in bytes",
177: expected.getBytes().length, obtained.getBytes().length);
178: assertEquals(" File Head ", expected.toString(), obtained
179: .toString());
180: Method[] expected_methods = expected.getMethods();
181: Method[] obtained_methods = obtained.getMethods();
182: int i = 0;
183: int k = 0;
184: for (i = 0; i < expected_methods.length; i++) {
185: Method expectedm = expected_methods[i];
186: Method obtainedm;
187: for (k = i; k < obtained_methods.length; k++) {
188: obtainedm = obtained_methods[k];
189: if (obtainedm.getSignature().equals(
190: expectedm.getSignature())) {
191: obtained_methods[k] = obtained_methods[i];
192: obtained_methods[i] = null;
193: //System.out.println(expectedm.getCode().toString(true));
194: assertEquals("MethodCode is different", expectedm
195: .getCode().toString(false), obtainedm
196: .getCode().toString(false));
197: break;
198: }
199: }
200: if (k == obtained_methods.length) {
201: fail("Method " + expectedm.getSignature()
202: + " not found in obtained");
203: }
204: }
205: assertEquals(i, k + 1);
206: }
207:
208: /**
209: * @return
210: */
211: public String getAfter() {
212: return after;
213: }
214:
215: /**
216: * @return
217: */
218: public String getBefore() {
219: return before;
220: }
221:
222: /**
223: * @return
224: */
225: public String getClassFilename() {
226: return classFilename;
227: }
228:
229: /**
230: * @return
231: */
232: public String getFilename() {
233: return filename;
234: }
235:
236: /**
237: * @return
238: */
239: public File getTempdir() {
240: return tempdir;
241: }
242:
243: /**
244: * @param string
245: */
246: public void setAfter(String string) {
247: after = string;
248: }
249:
250: /**
251: * @param string
252: */
253: public void setBefore(String string) {
254: before = string;
255: }
256:
257: /**
258: * @param string
259: */
260: public void setClassFilename(String string) {
261: classFilename = string;
262: }
263:
264: /**
265: * @param string
266: */
267: public void setFilename(String string) {
268: filename = string;
269: }
270:
271: /**
272: * @param file
273: */
274: public void setTempdir(File file) {
275: tempdir = file;
276: }
277:
278: /* (non-Javadoc)
279: * @see junit.framework.TestCase#setUp()
280: */
281: protected void setUp() throws Exception {
282: super .setUp();
283: setBefore("/*\n"
284: + " * Created on 23 juin 2003\n"
285: + " *\n"
286: + " */\n"
287: + "package net.sf.just4log;\n"
288: + "\n"
289: + "import org.apache.commons.logging.Log;\n"
290: + "import org.apache.commons.logging.LogFactory;\n"
291: + "\n"
292: + "/**\n"
293: + " * @author Lucas Bruand\n"
294: + " */\n"
295: + "\n"
296: + "public class SimpleClass {\n"
297: + " private static Log logger = LogFactory.getLog(SimpleClass.class);\n"
298: + " public int sum(int a, int b) {\n"
299: + " int result = a+b;\n"
300: + " logger.warn(\"Sum(\"+a+\",\"+b+\")=\"+result);\n"
301: + " return result;\n" + " }\n" + "}");
302:
303: setAfter("/*\n"
304: + " * Created on 23 juin 2003\n"
305: + " *\n"
306: + " */\n"
307: + "package net.sf.just4log;\n"
308: + "\n"
309: + "import org.apache.commons.logging.Log;\n"
310: + "import org.apache.commons.logging.LogFactory;\n"
311: + "\n"
312: + "/**\n"
313: + " * @author Lucas Bruand\n"
314: + " */\n"
315: + "\n"
316: + "public class SimpleClass {\n"
317: + " private static Log logger = LogFactory.getLog(SimpleClass.class);\n"
318: + " public int sum(int a, int b) {\n"
319: + " int result = a+b;\n"
320: + " if (logger.isWarnEnabled()) {"
321: + " logger.warn(\"Sum(\"+a+\",\"+b+\")=\"+result);\n"
322: + " }" + " return result;\n" + " }\n" + "}");
323: }
324:
325: /* (non-Javadoc)
326: * @see junit.framework.TestCase#tearDown()
327: */
328: protected void tearDown() throws Exception {
329: super.tearDown();
330: }
331:
332: }
|