Package net.sf.just4log

Source Code of net.sf.just4log.JustLog1Test

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

package net.sf.just4log;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import net.sf.just4log.transform.Transform;

import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.classfile.Method;
import org.apache.bcel.util.Class2HTML;
//import org.apache.bcel.util.Class2HTML;

import com.sun.tools.javac.Main;


//import junit.framework.AssertionFailedError;
import junit.framework.TestCase;

/**
* @author Lucas Bruand
* @version $Id: JustLog1Test.java,v 1.5 2003/09/23 20:41:41 lbruand Exp $
*/

public class JustLog1Test extends TestCase {
  private File tempdir= new File("/home/luke/test.tmp/");
  //private Main javac = new Main();
  public String filename="SimpleClass.java";
  public String classFilename="net"+File.separator+"sf"+File.separator+"just4log"+File.separator+ filename.substring(0,filename.length()-5)+ ".class";
  public String before;
  public String after;
  /**
   * Constructor for JustLog1Test.
   * @param arg0
   */
 
  public JustLog1Test(String arg0) {
    super(arg0);
  }

  public static void main(String[] args) {
    junit.textui.TestRunner.run(JustLog1Test.class);
  }
  private static final int bufferSize = 4096;
  public JavaClass getJavaClassForSource(String filename,String sourceCode) throws IOException {
    File sourceFile = null;
    File classFile = null;
    FileInputStream in = null;
    PrintWriter beforeWriter = null;
    try {
      if (!tempdir.isDirectory()) {
        tempdir.mkdirs();
      }
      sourceFile = new File(tempdir, filename);
      beforeWriter = new PrintWriter(new FileOutputStream(sourceFile));
      beforeWriter.print(sourceCode);
      beforeWriter.close();
     
      String[] args = new String[] {
          "-g:none", "-d", tempdir.getPath(),
          sourceFile.getPath()
        };
      Main.compile(args);
      classFile = new File(tempdir, classFilename);
      if (!classFile.exists()) {
        throw new FileNotFoundException("ClassFile "+ classFile + " seems not to have been created");
      }
      in = new FileInputStream(classFile);
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      byte[] buffer= new byte[bufferSize];
      int count;
      while ((count = in.read(buffer)) != -1 ) {
        out.write(buffer,0,count);
      }
     
      return (new ClassParser( new ByteArrayInputStream(out.toByteArray()), filename)).parse();
    } finally {
      try {
        in.close();
        beforeWriter.close();
        sourceFile.delete();
        classFile.delete();
      } catch (Throwable t) {
        // This is OK !!
      }
     
    }
  }
  /*
   * Test for JavaClass speedup(JavaClass)
   */
  public void testSpeedupJavaClass() throws IOException {
    //System.out.println(before);
    JavaClass beforeClass = getJavaClassForSource(getFilename(), getBefore());
    new Class2HTML(beforeClass, tempdir.getPath()+File.separator+"before"+File.separator);
    JavaClass afterClass = getJavaClassForSource(getFilename(), getAfter());
    new Class2HTML(afterClass, tempdir.getPath()+File.separator+"after"+File.separator);
    JavaClass justLogClass = Transform.speedup(beforeClass);
    new Class2HTML(justLogClass, tempdir.getPath()+File.separator+"modified"+File.separator);
    //assertNotSame(beforeClass.getBytes(),justLog.getBytes());
   
    //justLog.dump(new File(tempdir, "justlog.class"));
   
   
    assertEquals(justLogClass, afterClass);   

  }
 
  public void assertEquals(JavaClass obtained, JavaClass expected) {
    assertEquals(" File length in bytes", expected.getBytes().length, obtained.getBytes().length);
    assertEquals(" File Head ", expected.toString(), obtained.toString());
    Method[] expected_methods = expected.getMethods();
    Method[] obtained_methods = obtained.getMethods();
    int i=0;
    int k=0;
    for(i=0;i< expected_methods.length;i++) {
      Method expectedm = expected_methods[i];
      Method obtainedm;
      for(k=i;k < obtained_methods.length; k++) {
        obtainedm = obtained_methods[k];
        if (obtainedm.getSignature().equals(expectedm.getSignature())) {
          obtained_methods[k]=obtained_methods[i];
          obtained_methods[i]=null;
          //System.out.println(expectedm.getCode().toString(true));
          assertEquals("MethodCode is different", expectedm.getCode().toString(false), obtainedm.getCode().toString(false));
          break;
        }
      }
      if (k==obtained_methods.length) {
        fail("Method "+expectedm.getSignature()+ " not found in obtained");       
      }
    }
    assertEquals(i,k+1);
  }

  /**
   * @return
   */
  public String getAfter() {
    return after;
  }

  /**
   * @return
   */
  public String getBefore() {
    return before;
  }

  /**
   * @return
   */
  public String getClassFilename() {
    return classFilename;
  }

  /**
   * @return
   */
  public String getFilename() {
    return filename;
  }

  /**
   * @return
   */
  public File getTempdir() {
    return tempdir;
  }

  /**
   * @param string
   */
  public void setAfter(String string) {
    after = string;
  }

  /**
   * @param string
   */
  public void setBefore(String string) {
    before = string;
  }

  /**
   * @param string
   */
  public void setClassFilename(String string) {
    classFilename = string;
  }

  /**
   * @param string
   */
  public void setFilename(String string) {
    filename = string;
  }

  /**
   * @param file
   */
  public void setTempdir(File file) {
    tempdir = file;
  }

  /* (non-Javadoc)
   * @see junit.framework.TestCase#setUp()
   */
  protected void setUp() throws Exception {
    super.setUp();
    setBefore("/*\n"+
    " * Created on 23 juin 2003\n"+
    " *\n"+
    " */\n"+
    "package net.sf.just4log;\n"+
    "\n"+
    "import org.apache.commons.logging.Log;\n"+
    "import org.apache.commons.logging.LogFactory;\n"+
    "\n"+
    "/**\n"+
    " * @author Lucas Bruand\n"+
    " */\n"+
    "\n"+
    "public class SimpleClass {\n"+
    "  private static Log logger = LogFactory.getLog(SimpleClass.class);\n"+
    "  public int sum(int a, int b) {\n"+
    "    int result = a+b;\n"+
    "    logger.warn(\"Sum(\"+a+\",\"+b+\")=\"+result);\n"+
    "    return result;\n"+
    "  }\n"+
    "}");
   
    setAfter("/*\n"+
    " * Created on 23 juin 2003\n"+
    " *\n"+
    " */\n"+
    "package net.sf.just4log;\n"+
    "\n"+
    "import org.apache.commons.logging.Log;\n"+
    "import org.apache.commons.logging.LogFactory;\n"+
    "\n"+
    "/**\n"+
    " * @author Lucas Bruand\n"+
    " */\n"+
    "\n"+
    "public class SimpleClass {\n"+
    "  private static Log logger = LogFactory.getLog(SimpleClass.class);\n"+
    "  public int sum(int a, int b) {\n"+
    "    int result = a+b;\n"+
    "    if (logger.isWarnEnabled()) {"+
    "      logger.warn(\"Sum(\"+a+\",\"+b+\")=\"+result);\n"+
    "    }"+
    "    return result;\n"+
    "  }\n"+
    "}");
  }

  /* (non-Javadoc)
   * @see junit.framework.TestCase#tearDown()
   */
  protected void tearDown() throws Exception {
    super.tearDown();
  }

}
TOP

Related Classes of net.sf.just4log.JustLog1Test

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.