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.AstWriter;

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

public class AstWriterTest {

    private AstWriter writer;

    @Before
    public void setUp() throws Exception {
        writer = new AstWriter();
    }

    @After
    public void tearDown() throws Exception {
    }

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

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

        Object obj;

        // - - -

        name = "MainClass";

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

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

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

        // - - -

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

        new ClassDeclaration(name).accept(writer);
        bytecode = writer.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);
    }

}

@@ -514,15 +514,20 @@
 test
-SetName
+VisitClass_0
 () t
@@ -555,16 +555,28 @@
 ception,
+%0A           
  Illegal
@@ -593,25 +593,24 @@
 ption %7B%0A
-%0A
         
 Class%3C?%3E
@@ -605,537 +605,345 @@
     
-Class%3C?%3E generated
+testVisitClass(%22Main
 Class
+%22)
 ;%0A    
+%7D%0A%0A
     
-byte%5B%5D bytecode;%0A        String name;%0A%0A        Object obj;%0A%0A        // - - -%0A%0A        name = %22MainClass%22;%0A%0A        new ClassDeclaration(name).accept(writer);%0A        bytecode = writer.toByteArray();%0A%0A        generatedClass = def
+@Test%0A    public void testVisitClass_1() throws InstantiationException,%0A            IllegalAccessException %7B%0A        testVisitClass(%22foo.Ma
 in
-e
 Class
-(name, bytecode
+%22
 );%0A    
+%7D%0A%0A
     
-assertThat(generatedClass.getName(), is(name));%0A%0A        obj = generatedClass.newInstance();%0A        assertThat(obj, is(not(nullValue())));%0A%0A        // - - -%0A%0A        name = %22foo.MainClass%22;%0A        writer = new AstWriter();%0A%0A        
+private void testVisitClass(String name) throws InstantiationException,%0A            IllegalAccessException %7B%0A%0A        writer.visitClass(
 new 
@@ -968,22 +968,8 @@
 ame)
-.accept(writer
 );%0A 
@@ -974,16 +974,23 @@
 %0A       
+ byte%5B%5D
  bytecod
@@ -1023,16 +1023,25 @@
 %0A       
+ Class%3C?%3E
  generat
@@ -1079,17 +1079,16 @@
 ecode);%0A
-%0A
         
@@ -1144,16 +1144,23 @@
         
+Object 
 obj = ge

Refactor test methods