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

import static org.junit.Assert.*;

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

import at.ssw.hotswap.HotSwapTool;

/**
 * @author Thomas Wuerthinger
 */

public class FieldModificationTest {

    // Version 0
    public static class A {

        public int val0;
        public int val1;
        public int val2;
        public int val3;
        public int val4;
        public int val5;
        public int val6;
        public int val7;

        public void increaseAllByOne() {
            val0++;
            val1++;
            val2++;
            val3++;
            val4++;
            val5++;
            val6++;
            val7++;
        }

        public int sum() {
            return val0 + val1 + val2 + val3 + val4 + val5 + val6 + val7;
        }
    }

    // Version 1
    public static class A___1 {

        public int val0;

        public void increaseAllByOne() {
            val0++;
        }

        public int sum() {
            return val0;
        }
    }

    // Version 2
    public static class A___2 {

        public int val0;
        public int val1;
        public int val2;
        public int val3;
        public int val4;
        public int val5;
        public int val6;
        public int val7;
        public int val8;
        public int val9;
        public int val10;
        public int val11;
        public int val12;
        public int val13;
        public int val14;
        public int val15;

        public int sum() {
            return val0 + val1 + val2 + val3 + val4 + val5 + val6 + val7 + val8 + val9 + val10 + val11 + val12 + val13 + val14 + val15;
        }

        public void increaseAllByOne() {
            val0++;
            val1++;
            val2++;
            val3++;
            val4++;
            val5++;
            val6++;
            val7++;
            val8++;
            val9++;
            val10++;
            val11++;
            val12++;
            val13++;
            val14++;
            val15++;
        }
    }

    // Version 3
    public static class A___3 {

        public int val6;
        public int val0;
        public int val7;
        public int val1;
        public int val2;
        public int val5;
        public int val3;
        public int val4;
    }

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

    @Test
    public void testReorder() {

        A a = new A();

        a.val0 = 0;
        a.val1 = 1;
        a.val2 = 2;
        a.val3 = 3;
        a.val4 = 4;
        a.val5 = 5;
        a.val6 = 6;
        a.val7 = 7;
    }

    @Test
    public void testIncreaseFirst() {

        A a = new A();

        a.val0 = 0;
        a.val1 = 1;
        a.val2 = 2;
        a.val3 = 3;
        a.val4 = 4;
        a.val5 = 5;
        a.val6 = 6;
        a.val7 = 7;

        assertEquals(0, a.val0);
        assertEquals(1, a.val1);
        assertEquals(2, a.val2);
        assertEquals(3, a.val3);
        assertEquals(4, a.val4);
        assertEquals(5, a.val5);
        assertEquals(6, a.val6);
        assertEquals(7, a.val7);
        assertEquals(0 + 1 + 2 + 3 + 4 + 5 + 6 + 7, a.sum());

        HotSwapTool.toVersion(FieldModificationTest.class, 2);

        assertEquals(0, a.val0);
        assertEquals(1, a.val1);
        assertEquals(2, a.val2);
        assertEquals(3, a.val3);
        assertEquals(4, a.val4);
        assertEquals(5, a.val5);
        assertEquals(6, a.val6);
        assertEquals(7, a.val7);
        assertEquals(0 + 1 + 2 + 3 + 4 + 5 + 6 + 7, a.sum());

        a.increaseAllByOne();
        assertEquals(0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 16, a.sum());

        HotSwapTool.toVersion(FieldModificationTest.class, 0);

        assertEquals(0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8, a.sum());
        assertEquals(1, a.val0);
        assertEquals(2, a.val1);
        assertEquals(3, a.val2);
        assertEquals(4, a.val3);
        assertEquals(5, a.val4);
        assertEquals(6, a.val5);
        assertEquals(7, a.val6);
        assertEquals(8, a.val7);

        HotSwapTool.toVersion(FieldModificationTest.class, 2);

        assertEquals(0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8, a.sum());
        assertEquals(1, a.val0);
        assertEquals(2, a.val1);
        assertEquals(3, a.val2);
        assertEquals(4, a.val3);
        assertEquals(5, a.val4);
        assertEquals(6, a.val5);
        assertEquals(7, a.val6);
        assertEquals(8, a.val7);

        a.increaseAllByOne();

        assertEquals(0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 16, a.sum());
        assertEquals(2, a.val0);
        assertEquals(3, a.val1);
        assertEquals(4, a.val2);
        assertEquals(5, a.val3);
        assertEquals(6, a.val4);
        assertEquals(7, a.val5);
        assertEquals(8, a.val6);
        assertEquals(9, a.val7);
        HotSwapTool.toVersion(FieldModificationTest.class, 0);

        assertEquals(0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 16, a.sum());
        assertEquals(2, a.val0);
        assertEquals(3, a.val1);
        assertEquals(4, a.val2);
        assertEquals(5, a.val3);
        assertEquals(6, a.val4);
        assertEquals(7, a.val5);
        assertEquals(8, a.val6);
        assertEquals(9, a.val7);
    }

    @Test
    public void testAddRemoveField() {

        assert HotSwapTool.getCurrentVersion(FieldModificationTest.class) == 0;

        A a = new A();

        assertEquals(0, a.val0);
        assertEquals(0, a.val1);

        HotSwapTool.toVersion(FieldModificationTest.class, 1);

        a.val0 = 1234;

        HotSwapTool.toVersion(FieldModificationTest.class, 0);

        assertEquals(1234, a.val0);
        assertEquals(0, a.val1);

        a.val1 = 1234;

        assertEquals(1234, a.val0);
        assertEquals(1234, a.val1);

        HotSwapTool.toVersion(FieldModificationTest.class, 1);

        assertEquals(1234, a.val0);

        HotSwapTool.toVersion(FieldModificationTest.class, 0);

        assertEquals(1234, a.val0);
        assertEquals(0, a.val1);
    }
}
 

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>