Tests

We use unit test suites to verify the correctness of our virtual machine. Here you can access the source code of the tests.

/*
 * Copyright 1997-2009 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

package at.ssw.hotswap.test.accesstests;

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import at.ssw.hotswap.HotSwapTool;
import at.ssw.hotswap.test.access.ClassAccess;
import at.ssw.hotswap.test.access.MethodAccess;
import at.ssw.hotswap.test.access.StackFrameAccess;
import at.ssw.hotswap.test.access.VMAccess;
import at.ssw.hotswap.test.access.jdi.JDIVMAccess;
import at.ssw.hotswap.test.access.jni.JNIVMAccess;
import at.ssw.hotswap.test.access.reflection.ReflectionVMAccess;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

/**
 * @author Kerstin Breiteneder
 * @author Christoph Wimberger
 */

@RunWith(Parameterized.class)
public class AccessTest {

    private static VMAccess vma;

    public AccessTest(VMAccess vma) {
        this.vma = vma;
    }

    // Version 0
    public static class A {

        public static String invokeMethod() {
            return method_A("", 10);
        }

        public static String method_A(String x, int y) {
            return getMethod();
        }

        public String hallo(String y, int x) {
            return "";
        }
    }

    // Version 1
    public static class A___1 {

        public static String invokeMethod() {
            return method_A("", 10);
        }

        private static String method_A(String x, int y) {
            return getMethod();
        }
    }

    private static int getIndexAfter(String methodName) throws ClassNotFoundException {
        int i = 0;
        for (StackFrameAccess stack : vma.getFrames("main")) {
            if (stack.getMethod().getName().equals(methodName)) {
                return i;
            }
            i++;
        }
        return -1;
    }

    private static String getMethod() {
        try {
            return vma.getFrames("main").get(getIndexAfter("access$000")).getMethod().getSignature();
        } catch (ClassNotFoundException ex) {
            return null;
        }
    }

    @Parameters
    public static Collection accessValues() {
        return Arrays.asList(new Object[][]{{new JDIVMAccess()}, {new JNIVMAccess()}, {new ReflectionVMAccess()}});
        //return Arrays.asList(new Object[][]{{new JNIVMAccess()}});
    }

    @Before
    public void setUp() throws Exception {
        HotSwapTool.toVersion(AccessTest.class, 0);
    }

    @Test
    public void testFindClass() {
        ClassAccess classAccess = vma.findClass("at.ssw.hotswap.test.accesstests.AccessTest$A");
        assertNotNull(classAccess);
    }

    @Test
    public void testFindNotClass() {
        ClassAccess classAccess = vma.findClass("at.ssw.hotswap.test.accesstests.AccessTest$XYZ");
        assertNull(classAccess);
    }

    //JDI: multiple methods if toVersion is used
    @Test
    public void testFindMethod() {
        HotSwapTool.toVersion(AccessTest.class, 0);
        ClassAccess classAccess = vma.findClass("at.ssw.hotswap.test.accesstests.AccessTest$A");
        assertNotNull(classAccess);
        MethodAccess methodAccess = classAccess.findMethod("method_A");
        assertNotNull(methodAccess);
    }

    @Test
    public void testFindNotMethod() {
        HotSwapTool.toVersion(AccessTest.class, 0);
        ClassAccess classAccess = vma.findClass("at.ssw.hotswap.test.accesstests.AccessTest$A");
        assertNotNull(classAccess);
        MethodAccess methodAccess = classAccess.findMethod("method_XYZ");
        assertNull(methodAccess);
    }

    @Test
    public void testStackTrace() {
        if (vma.canGetFrames()) {
            assertEquals("public static java.lang.String method_A(java.lang.String, int)", A.invokeMethod());

            HotSwapTool.toVersion(AccessTest.class, 1);

            assertEquals("private static java.lang.String method_A(java.lang.String, int)", A.invokeMethod());
        }
    }

    @Test
    public void testSignature() {
        HotSwapTool.toVersion(AccessTest.class, 0);
        MethodAccess mAccess = vma.findClass("at.ssw.hotswap.test.accesstests.AccessTest$A").findMethod("method_A");
        assertEquals("public static java.lang.String method_A(java.lang.String, int)", mAccess.getSignature());

        HotSwapTool.toVersion(AccessTest.class, 1);
        mAccess = vma.findClass("at.ssw.hotswap.test.accesstests.AccessTest$A").findMethod("method_A");

        assertEquals("private static java.lang.String method_A(java.lang.String, int)", mAccess.getSignature());
    }

    @Test
    public void testGetMethods() {
        HotSwapTool.toVersion(AccessTest.class, 0);
        ClassAccess cAccess = vma.findClass("at.ssw.hotswap.test.accesstests.AccessTest$A");
        assertNotNull(cAccess);
        List<MethodAccess> list = cAccess.getMethods();

        assertEquals(3, list.size());

        Object[] mAccesses = list.toArray();

        Arrays.sort(mAccesses, new Comparator() {

            @Override
            public int compare(Object o1, Object o2) {
                return ((MethodAccess) o1).getName().compareTo(((MethodAccess) o2).getName());
            }
        });

        assertEquals("hallo", ((MethodAccess) mAccesses[0]).getName());
        assertEquals("invokeMethod", ((MethodAccess) mAccesses[1]).getName());
        assertEquals("method_A", ((MethodAccess) mAccesses[2]).getName());
        HotSwapTool.toVersion(AccessTest.class, 1);

        cAccess = vma.findClass("at.ssw.hotswap.test.accesstests.AccessTest$A");
        assertNotNull(cAccess);
        list = cAccess.getMethods();

        assertEquals(2, list.size());

        mAccesses = list.toArray();

        Arrays.sort(mAccesses, new Comparator() {

            @Override
            public int compare(Object o1, Object o2) {
                return ((MethodAccess) o1).getName().compareTo(((MethodAccess) o2).getName());
            }
        });

        assertEquals("invokeMethod", ((MethodAccess) mAccesses[0]).getName());
        assertEquals("method_A", ((MethodAccess) mAccesses[1]).getName());
    }

    @Test
    public void classAccessTest() {

        HotSwapTool.toVersion(AccessTest.class, 0);

        ClassAccess cAccess = vma.findClass("at.ssw.hotswap.test.accesstests.AccessTest$A");

        assertEquals(3, cAccess.getMethods().size());

        HotSwapTool.toVersion(AccessTest.class, 1);

        assertEquals(2, cAccess.getMethods().size());
    }
}
 

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>