1 package jp.sourceforge.stigmata.birthmarks;
7 import java.util.Iterator;
9 import org.junit.Assert;
10 import org.junit.Before;
11 import org.junit.Test;
12 import org.objectweb.asm.Label;
16 * @author Haruaki Tamada
19 public class OpcodeTest{
20 private Opcode opcode;
24 opcode = new Opcode(26, "iload_0", 0, 1, Opcode.Category.NORMAL);
28 public void testBasic(){
29 Assert.assertEquals(26, opcode.getOpcode());
30 Assert.assertEquals("iload_0", opcode.getName());
31 Assert.assertEquals(0, opcode.getArgumentCount());
32 Assert.assertEquals(1, opcode.getAct());
33 Assert.assertEquals(Opcode.Category.NORMAL, opcode.getCategory());
37 public void testSelfConstructor(){
38 Opcode o = new Opcode(opcode);
39 Assert.assertEquals(26, o.getOpcode());
40 Assert.assertEquals("iload_0", o.getName());
41 Assert.assertEquals(0, o.getArgumentCount());
42 Assert.assertEquals(1, o.getAct());
43 Assert.assertEquals(Opcode.Category.NORMAL, o.getCategory());
46 @Test(expected=IllegalStateException.class)
47 public void testSetActThrowException(){
51 @Test(expected=IllegalStateException.class)
52 public void testAddLabelThrowException(){
53 opcode.addLabel(new Label());
56 @Test(expected=NullPointerException.class)
57 public void testAddLabelNullPointer(){
58 opcode.addLabel(null);
61 @Test(expected=IllegalStateException.class)
62 public void testSetLabelsThrowException(){
63 opcode.setLabels(new Label[] { new Label() });
66 @Test(expected=NullPointerException.class)
67 public void testSetLabelsNullPointer1(){
68 opcode.setLabels(null);
72 public void testAddLabel(){
73 opcode = new Opcode(154, "ifne", 2, -1, "BRANCH");
74 Label label1 = new Label();
75 Label label2 = new Label();
76 Label label3 = new Label();
78 opcode.addLabel(label1);
79 opcode.addLabel(label2);
80 opcode.addLabel(label3);
82 Assert.assertEquals(label1, opcode.getLabel(0));
83 Assert.assertEquals(label2, opcode.getLabel(1));
84 Assert.assertEquals(label3, opcode.getLabel(2));
86 Iterator<Label> iterator = opcode.labels();
87 Assert.assertTrue(iterator.hasNext());
88 Assert.assertEquals(label1, iterator.next());
89 Assert.assertTrue(iterator.hasNext());
90 Assert.assertEquals(label2, iterator.next());
91 Assert.assertTrue(iterator.hasNext());
92 Assert.assertEquals(label3, iterator.next());
93 Assert.assertFalse(iterator.hasNext());
97 public void testSetLabels(){
98 opcode = new Opcode(154, "ifne", 2, -1, "BRANCH");
99 Label label1 = new Label();
100 Label label2 = new Label();
101 Label label3 = new Label();
103 opcode.addLabel(label1);
104 opcode.setLabels(new Label[] { label1, label2, label3, });
106 Assert.assertEquals(label1, opcode.getLabel(0));
107 Assert.assertEquals(label2, opcode.getLabel(1));
108 Assert.assertEquals(label3, opcode.getLabel(2));
110 Iterator<Label> iterator = opcode.labels();
111 Assert.assertTrue(iterator.hasNext());
112 Assert.assertEquals(label1, iterator.next());
113 Assert.assertTrue(iterator.hasNext());
114 Assert.assertEquals(label2, iterator.next());
115 Assert.assertTrue(iterator.hasNext());
116 Assert.assertEquals(label3, iterator.next());
117 Assert.assertFalse(iterator.hasNext());
120 @Test(expected=NullPointerException.class)
121 public void testSetLabelsThrownNullPointerException(){
122 opcode = new Opcode(154, "ifne", 2, -1, "BRANCH");
124 opcode.setLabels(new Label[] { null, });
128 * this test will be thrown IllegalStateException.
129 * Because, IllegalStateException is checked before null check of array elements.
131 @Test(expected=IllegalStateException.class)
132 public void testSetLabelsNullPointer2(){
133 opcode.setLabels(new Label[] { null, });
137 public void testSetAct() throws Exception{
138 opcode = new Opcode(182, "invokevirtual", 2, 0, "INVOKE");