Author: tcurdt
Date: Mon Jan 31 14:53:31 2005
New Revision: 149313

URL: http://svn.apache.org/viewcvs?view=rev&rev=149313
Log:
improved the testcase as suggested by phil 
http://issues.apache.org/bugzilla/show_bug.cgi?id=33313
some nitpicking


Modified:
    
jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/Continuation.java
    
jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/bytecode/bcel/BcelClassTransformer.java
    
jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/ContinuationClassLoaderTestCase.java
    
jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/ContinuationCompilingClassLoaderTestCase.java
    
jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/testcode/Calculator.java

Modified: 
jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/Continuation.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/Continuation.java?view=diff&r1=149312&r2=149313
==============================================================================
--- 
jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/Continuation.java
 (original)
+++ 
jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/Continuation.java
 Mon Jan 31 14:53:31 2005
@@ -30,7 +30,7 @@
  *
  * @author <a href="mailto:[EMAIL PROTECTED]">Stephan Michels</a>
  * @author <a href="mailto:[EMAIL PROTECTED]">Torsten Curdt</a>
- * @version CVS $Id:$
+ * @version CVS $Id$
  */
 public class Continuation implements Serializable {
 
@@ -96,6 +96,7 @@
         final Method method = context.getMethod();
 
         try {
+
             method.invoke(instance, new Object[0]);
 
         } catch (final Exception e) {

Modified: 
jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/bytecode/bcel/BcelClassTransformer.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/bytecode/bcel/BcelClassTransformer.java?view=diff&r1=149312&r2=149313
==============================================================================
--- 
jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/bytecode/bcel/BcelClassTransformer.java
 (original)
+++ 
jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/bytecode/bcel/BcelClassTransformer.java
 Mon Jan 31 14:53:31 2005
@@ -16,6 +16,7 @@
 package org.apache.commons.javaflow.bytecode.bcel;
 
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Vector;
 
@@ -105,24 +106,46 @@
         final ClassGen clazzGen = new ClassGen(javaClazz);
         final ConstantPoolGen cp = clazzGen.getConstantPool();
 
-        final String path = clazzGen.getClassName(); //.replace('.', '/');
+        final String path = clazzGen.getClassName();
 
+        FileOutputStream out = null;
+        
         final byte[] orig = clazzGen.getJavaClass().getBytes();
         try {
-            final FileOutputStream out = new FileOutputStream(path + ".orig");
+            out = new FileOutputStream(path + ".orig");
             out.write(orig);
             out.flush();
             out.close();
-        } catch (java.io.IOException ioe) {
-            ioe.printStackTrace();
+        } catch (final IOException e) {
+            e.printStackTrace();
+
+            try {
+                if (out != null) {
+                    out.close();
+                }
+            } catch (final IOException e1) {
+                log.error(e1.getMessage(), e1);
+            } finally {
+                out = null;
+            }
         }
 
         try {
-            final FileOutputStream fos = new FileOutputStream(path + 
".orig.java");
-            final DecompilingVisitor v = new DecompilingVisitor(javaClazz, 
fos);
+            out = new FileOutputStream(path + ".orig.java");
+            final DecompilingVisitor v = new DecompilingVisitor(javaClazz, 
out);
             v.start();
-        } catch (Exception e) {
+        } catch (final Exception e) {
             e.printStackTrace();
+
+            try {
+                if (out != null) {
+                    out.close();
+                }
+            } catch (final IOException e1) {
+                log.error(e1.getMessage(), e1);
+            } finally {
+                out = null;
+            }
         }
 
         // vistor to build the frame information
@@ -157,22 +180,42 @@
 
         clazzGen.addInterface(CONTINUATIONCAPABLE_CLASS);
 
-        byte[] changed = clazzGen.getJavaClass().getBytes();
+        final byte[] changed = clazzGen.getJavaClass().getBytes();
         try {
-            java.io.FileOutputStream out = new 
java.io.FileOutputStream(clazzGen.getClassName() + ".rewritten");
+            out = new FileOutputStream(clazzGen.getClassName() + ".rewritten");
             out.write(changed);
             out.flush();
             out.close();
-        } catch (java.io.IOException ioe) {
-            ioe.printStackTrace();
+        } catch (final IOException e) {
+            e.printStackTrace();
+
+            try {
+                if (out != null) {
+                    out.close();
+                }
+            } catch (final IOException e1) {
+                log.error(e1.getMessage(), e1);
+            } finally {
+                out = null;
+            }
         }
 
         try {
-            final FileOutputStream fos = new FileOutputStream(path + 
".rewritten.java");
-            final DecompilingVisitor v = new 
DecompilingVisitor(clazzGen.getJavaClass(), fos);
+            out = new FileOutputStream(path + ".rewritten.java");
+            final DecompilingVisitor v = new 
DecompilingVisitor(clazzGen.getJavaClass(), out);
             v.start();
-        } catch (Exception e) {
+        } catch (final Exception e) {
             e.printStackTrace();
+
+            try {
+                if (out != null) {
+                    out.close();
+                }
+            } catch (final IOException e1) {
+                log.error(e1.getMessage(), e1);
+            } finally {
+                out = null;
+            }
         }
 
         return changed;
@@ -194,7 +237,7 @@
         log.debug("analyse " + method.getName());
 
         // build the initial frame situation for this method.
-        Frame vanillaFrame = new Frame(method.getMaxLocals(), 
method.getMaxStack());
+        final Frame vanillaFrame = new Frame(method.getMaxLocals(), 
method.getMaxStack());
         if (!method.isStatic()) {
             if (method.getName().equals(Constants.CONSTRUCTOR_NAME)) {
                 Frame._this = new UninitializedObjectType(new 
ObjectType(clazz.getClassName()));
@@ -205,7 +248,7 @@
             }
         }
         // fill local variables with parameter types
-        Type[] argtypes = method.getArgumentTypes();
+        final Type[] argtypes = method.getArgumentTypes();
         int twoslotoffset = 0;
         for (int j = 0; j < argtypes.length; j++) {
             if ((argtypes[j] == Type.SHORT) || (argtypes[j] == Type.BYTE) || 
(argtypes[j] == Type.CHAR) || (argtypes[j] == Type.BOOLEAN)) {

Modified: 
jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/ContinuationClassLoaderTestCase.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/ContinuationClassLoaderTestCase.java?view=diff&r1=149312&r2=149313
==============================================================================
--- 
jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/ContinuationClassLoaderTestCase.java
 (original)
+++ 
jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/ContinuationClassLoaderTestCase.java
 Mon Jan 31 14:53:31 2005
@@ -20,6 +20,7 @@
 
 import junit.framework.TestCase;
 
+import org.apache.commons.javaflow.testcode.Calculator;
 import org.apache.commons.javaflow.utils.ReflectionUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -31,14 +32,11 @@
  */
 public final class ContinuationClassLoaderTestCase extends TestCase {
 
-    /**
-     * Logger.
-     */
     private final static Log log = 
LogFactory.getLog(ContinuationClassLoaderTestCase.class);
 
     public void testCalculator() throws Exception {
-        log.debug("Doing testCalculator()");
-        
+        log.debug("Testing Calculator...");
+
         final String calculatorTestClass = 
"org.apache.commons.javaflow.testcode.Calculator";
 
         // creating object instance
@@ -62,9 +60,11 @@
         context.setMethod(method);
         context.setInstance(instance);
         
+        assertTrue(instance.toString(), "0.0".equals(instance.toString()));    
    
+
         log.debug("Continuation 1");
         continuation = Continuation.continueWith(continuation, context);
-        
+        assertTrue(instance.toString(), "1.1".equals(instance.toString()));    
            
         
         final Continuation parent = continuation;
 
@@ -74,14 +74,36 @@
 
         log.debug("Continuation 1.1");
         final Continuation continuation11 = Continuation.continueWith(parent, 
context);
-
+        assertTrue("" + instance, "2.2".equals(instance.toString()));
+        
         log.debug("Continuation 1.2");
         final Continuation continuation12 = Continuation.continueWith(parent, 
context);
+        assertTrue("" + instance, "3.2".equals(instance.toString()));
         
     }
     
     
     /**
+     * Verifies that an IllegalStateException gets thrown when a continuable 
class 
+     * is used incorrectly (i.e. without the ContinuationClassLoader).
+     * @throws Exception
+     */
+    public void testIncorrectUsageWithNormalClassLoader() throws Exception {
+        log.debug("Testing incorrect use of continuation...");
+        boolean exceptionThrown = false;
+        try {
+            final Calculator c = new Calculator();
+            c.main();
+        } catch (final Exception e) {
+            log.debug("Catching a " + e);
+            assertTrue(e instanceof IllegalStateException);
+            exceptionThrown = true;
+        }
+        assertTrue(exceptionThrown);
+    }
+    
+    
+    /**
      * The junit tests are preferred over running this main() method.
      * @param args
      * @throws Exception
@@ -89,5 +111,6 @@
     public static void main(final String[] args) throws Exception {
         final ContinuationClassLoaderTestCase t = new 
ContinuationClassLoaderTestCase();
         t.testCalculator();
+        t.testIncorrectUsageWithNormalClassLoader();
     }
 }

Modified: 
jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/ContinuationCompilingClassLoaderTestCase.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/ContinuationCompilingClassLoaderTestCase.java?view=diff&r1=149312&r2=149313
==============================================================================
--- 
jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/ContinuationCompilingClassLoaderTestCase.java
 (original)
+++ 
jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/ContinuationCompilingClassLoaderTestCase.java
 Mon Jan 31 14:53:31 2005
@@ -22,15 +22,19 @@
 import org.apache.commons.logging.LogFactory;
 import org.apache.jci.CompilingClassLoader;
 
+import junit.framework.TestCase;
+
 
 /**
  * @author tcurdt
  *
  */
-public final class ContinuationCompilingClassLoaderTestCase {
+public final class ContinuationCompilingClassLoaderTestCase extends TestCase {
+
     private final static Log log = 
LogFactory.getLog(ContinuationCompilingClassLoaderTestCase.class);
 
-    private void testReloading() throws Exception {
+    public void testReloading() throws Exception {
+/*
         final CompilingClassLoader cl = new ContinuationCompilingClassLoader(
                 this.getClass().getClassLoader(),
                 new File("/home/tcurdt/dev/jci/classes")
@@ -50,7 +54,7 @@
             
             Thread.sleep(2000);
         }
-        
+  */      
     }
     
     public static void main(String[] args) throws Exception {

Modified: 
jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/testcode/Calculator.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/testcode/Calculator.java?view=diff&r1=149312&r2=149313
==============================================================================
--- 
jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/testcode/Calculator.java
 (original)
+++ 
jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/testcode/Calculator.java
 Mon Jan 31 14:53:31 2005
@@ -28,18 +28,33 @@
  */
 public final class Calculator implements Continuable, Serializable {
     
-    /**
-     * Logger.
-     */
     private static final Log log = LogFactory.getLog(Calculator.class);
+
+    private int global = 0;
+    private int local = 0;
     
     public void main() {
 
-        log.debug("Calculator1");
+        int l = 0;
+        
+        log.debug("Start of Calculator: g=" + global + " l=" + local);
+
+        global++;
+        local = ++l;
 
-        Continuation.suspend();
+        log.debug("Calculator Step 1: g=" + global + " l=" + local);
         
-        log.debug("Calculator2");
+        Continuation.suspend();
         
+        log.debug("Calculator Step 2: g=" + global + " l=" + local);
+
+        global++;
+        local = ++l;
+
+        log.debug("End of Calculator: g=" + global + " l=" + local);        
+    }
+    
+    public String toString() {
+        return "" + global + "." + local;
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to