package net.folab.fo.jast;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import net.folab.fo.bytecode.ByteArrayClassLoader;
import net.folab.fo.jast.AstVisitor;
import net.folab.fo.jast.AstWriter;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class AstWriterTest {

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testSetName() throws InstantiationException, IllegalAccessException {

        AstVisitor av;
        Class<?> generatedClass;
        byte[] bytecode;
        String name;

        Object obj;

        // - - -

        name = "MainClass";
        av = new AstWriter();

        new ClassDeclaration(name).accept(av);
        bytecode = av.toByteArray();

        generatedClass = defineClass(name, bytecode);
        assertThat(generatedClass.getName(), is(name));

        obj = generatedClass.newInstance();
        assertThat(obj, is(not(nullValue())));

        // - - -

        name = "foo.MainClass";
        av = new AstWriter();

        new ClassDeclaration(name).accept(av);
        bytecode = av.toByteArray();

        generatedClass = defineClass(name, bytecode);

        assertThat(generatedClass.getName(), is(name));

        obj = generatedClass.newInstance();
        assertThat(obj, is(not(nullValue())));

    }

    public static Class<?> defineClass(String name, byte[] bytecode) {
        return new ByteArrayClassLoader().defineClass(name, bytecode);
    }

}

@@ -153,45 +153,8 @@
 er;%0A
-import net.folab.fo.jast.AstVisitor;%0A
 impo
@@ -285,24 +285,55 @@
 iterTest %7B%0A%0A
+    private AstWriter writer;%0A%0A
     @Before%0A
@@ -367,32 +367,66 @@
 ows Exception %7B%0A
+        writer = new AstWriter();%0A
     %7D%0A%0A    @Afte
@@ -581,31 +581,8 @@
  %7B%0A%0A
-        AstVisitor av;%0A
     
@@ -727,38 +727,8 @@
 ss%22;
-%0A        av = new AstWriter();
 %0A%0A  
@@ -759,34 +759,38 @@
 on(name).accept(
-av
+writer
 );%0A        bytec
@@ -787,34 +787,38 @@
      bytecode = 
-av
+writer
 .toByteArray();%0A
@@ -1079,18 +1079,22 @@
         
-av
+writer
  = new A
@@ -1148,18 +1148,22 @@
 .accept(
-av
+writer
 );%0A     
@@ -1180,10 +1180,14 @@
 e = 
-av
+writer
 .toB

Update test codes to fit test target.