1 50 51 package net.sf.just4log; 52 53 import java.io.ByteArrayInputStream ; 54 import java.io.ByteArrayOutputStream ; 55 import java.io.File ; 56 import java.io.FileInputStream ; 57 import java.io.FileNotFoundException ; 58 import java.io.FileOutputStream ; 59 import java.io.IOException ; 60 import java.io.PrintWriter ; 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 70 import com.sun.tools.javac.Main; 71 72 73 import junit.framework.TestCase; 75 76 80 81 public class JustLog1Test extends TestCase { 82 private File tempdir= new File ("/home/luke/test.tmp/"); 83 public String filename="SimpleClass.java"; 85 public String classFilename="net"+File.separator+"sf"+File.separator+"just4log"+File.separator+ filename.substring(0,filename.length()-5)+ ".class"; 86 public String before; 87 public String after; 88 92 93 public JustLog1Test(String arg0) { 94 super(arg0); 95 } 96 97 public static void main(String [] args) { 98 junit.textui.TestRunner.run(JustLog1Test.class); 99 } 100 private static final int bufferSize = 4096; 101 public JavaClass getJavaClassForSource(String filename,String sourceCode) throws IOException { 102 File sourceFile = null; 103 File classFile = null; 104 FileInputStream in = null; 105 PrintWriter beforeWriter = null; 106 try { 107 if (!tempdir.isDirectory()) { 108 tempdir.mkdirs(); 109 } 110 sourceFile = new File (tempdir, filename); 111 beforeWriter = new PrintWriter (new FileOutputStream (sourceFile)); 112 beforeWriter.print(sourceCode); 113 beforeWriter.close(); 114 115 String [] args = new String [] { 116 "-g:none", "-d", tempdir.getPath(), 117 sourceFile.getPath() 118 }; 119 Main.compile(args); 120 classFile = new File (tempdir, classFilename); 121 if (!classFile.exists()) { 122 throw new FileNotFoundException ("ClassFile "+ classFile + " seems not to have been created"); 123 } 124 in = new FileInputStream (classFile); 125 ByteArrayOutputStream out = new ByteArrayOutputStream (); 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 (out.toByteArray()), filename)).parse(); 133 } finally { 134 try { 135 in.close(); 136 beforeWriter.close(); 137 sourceFile.delete(); 138 classFile.delete(); 139 } catch (Throwable t) { 140 } 142 143 } 144 } 145 148 public void testSpeedupJavaClass() throws IOException { 149 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 158 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 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 195 public String getAfter() { 196 return after; 197 } 198 199 202 public String getBefore() { 203 return before; 204 } 205 206 209 public String getClassFilename() { 210 return classFilename; 211 } 212 213 216 public String getFilename() { 217 return filename; 218 } 219 220 223 public File getTempdir() { 224 return tempdir; 225 } 226 227 230 public void setAfter(String string) { 231 after = string; 232 } 233 234 237 public void setBefore(String string) { 238 before = string; 239 } 240 241 244 public void setClassFilename(String string) { 245 classFilename = string; 246 } 247 248 251 public void setFilename(String string) { 252 filename = string; 253 } 254 255 258 public void setTempdir(File file) { 259 tempdir = file; 260 } 261 262 265 protected void setUp() throws Exception { 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 317 protected void tearDown() throws Exception { 318 super.tearDown(); 319 } 320 321 } 322 | Popular Tags |