/**
 * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved.
 *
 * Email: subchen@gmail.com
 * URL: http://subchen.github.io/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jetbrick.reflect.asm;

import java.text.SimpleDateFormat;
import java.util.*;
import jetbrick.reflect.*;
import org.junit.*;

public class ASMFactoryTest {
    @Before
    public void setup() {
        System.setProperty("jetbrick.asm.threshold", "0");
        System.setProperty("jetbrick.asm.debug", "true");
    }

    @After
    public void cleanup() {
        System.clearProperty("jetbrick.asm.debug");
    }

    @Test
    public void generateConstructorAccessor() {
        ASMAccessor accessor = ASMFactory.generateAccessor(ArrayList.class);
        Assert.assertNotNull(accessor.newInstance());
        Assert.assertNotNull(accessor.newInstance(0, Arrays.asList()));
        Assert.assertNotNull(accessor.newInstance(1));
        Assert.assertNotNull(accessor.newInstance(2, 32));
    }

    @Test
    public void generateMethodAccessor() {
        List<Integer> list = Arrays.asList(11, 22, 33);

        KlassInfo klass = KlassInfo.create(List.class);
        ASMAccessor accessor = ASMFactory.generateAccessor(List.class);
        Assert.assertEquals(list.size(), accessor.invoke(list, klass.getDeclaredMethod("size").getOffset()));
        Assert.assertEquals(list.isEmpty(), accessor.invoke(list, klass.getDeclaredMethod("isEmpty").getOffset()));
        Assert.assertEquals(list.get(1), accessor.invoke(list, klass.getDeclaredMethod("get", int.class).getOffset(), 1));
    }

    @Test
    public void generateMethodAccessor2() {
        List<Integer> list = Arrays.asList(11, 22, 33);

        KlassInfo klass = KlassInfo.create(List.class);
        Assert.assertEquals(list.size(), klass.getMethod("size").invoke(list));
        Assert.assertEquals(list.isEmpty(), klass.getMethod("isEmpty").invoke(list));
        Assert.assertEquals(list.get(1), klass.getMethod("get", int.class).invoke(list, 1));
    }

    @Test
    public void testPrivateField() {
        String s = "abc";
        KlassInfo klass = KlassInfo.create(String.class);
        Assert.assertArrayEquals(s.toCharArray(), (char[]) klass.getField("value").get(s));
    }

    @Test
    public void testAsmGetFields() {
        SimpleDateFormat object = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        KlassInfo klass = KlassInfo.create(SimpleDateFormat.class);
        for (FieldInfo field : klass.getFields()) {
            field.get(object);
        }
    }

    @Test
    public void testReflectGetFields() throws Exception {
        SimpleDateFormat object = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        KlassInfo klass = KlassInfo.create(SimpleDateFormat.class);
        for (FieldInfo field : klass.getFields()) {
            field.getField().get(object);
        }
    }

    @Test
    public void testVarargs() {
        KlassInfo klass = KlassInfo.create(String.class);
        MethodInfo method = klass.getDeclaredMethod("format", String.class, Object[].class);
        Assert.assertEquals("aaa", method.invoke(null, "aaa", null));
        Assert.assertEquals("aaa123999", method.invoke(null, "aaa%s%s", new Object[] { 123, 999 }));
    }
}

@@ -912,50 +912,31 @@
     
-System.setProperty(%22jetbrick.asm.t
+ASMFactory.setT
 hreshold
 %22, %22
@@ -935,14 +935,10 @@
 hold
-%22, %220%22
+(0
 );%0A 
@@ -948,55 +948,34 @@
     
-System.setProperty(%22jetbrick.asm.debug%22, %22
+ASMFactory.setEnabled(
 true
-%22
 );%0A 
@@ -1031,49 +1031,35 @@
     
-System.clearProperty(%22jetbrick.asm.debug%22
+ASMFactory.setEnabled(false
 );%0A 

Update ASMFactory testcase