OSDN Git Service

[maven-release-plugin] copy for tag opcodes-1.0.0
[stigmata/stigmata-plugins.git] / opcodes / opcodes-1.0.0 / src / test / java / jp / sourceforge / stigmata / birthmarks / OpcodeTest.java
1 package jp.sourceforge.stigmata.birthmarks;
2
3 /*
4  * $Id$
5  */
6
7 import java.util.Iterator;
8
9 import org.junit.Assert;
10 import org.junit.Before;
11 import org.junit.Test;
12 import org.objectweb.asm.Label;
13
14 /**
15  * 
16  * @author Haruaki Tamada
17  * @version $Revision$
18  */
19 public class OpcodeTest{
20     private Opcode opcode;
21
22     @Before
23     public void setup(){
24         opcode = new Opcode(26, "iload_0", 0, 1, Opcode.Category.NORMAL);
25     }
26
27     @Test
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());
34     }
35
36     @Test
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());
44     }
45
46     @Test(expected=IllegalStateException.class)
47     public void testSetActThrowException(){
48         opcode.setAct(1);
49     }
50
51     @Test(expected=IllegalStateException.class)
52     public void testAddLabelThrowException(){
53         opcode.addLabel(new Label());
54     }
55
56     @Test(expected=NullPointerException.class)
57     public void testAddLabelNullPointer(){
58         opcode.addLabel(null);
59     }
60
61     @Test(expected=IllegalStateException.class)
62     public void testSetLabelsThrowException(){
63         opcode.setLabels(new Label[] { new Label() });
64     }
65
66     @Test(expected=NullPointerException.class)
67     public void testSetLabelsNullPointer1(){
68         opcode.setLabels(null);
69     }
70
71     @Test
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();
77
78         opcode.addLabel(label1);
79         opcode.addLabel(label2);
80         opcode.addLabel(label3);
81
82         Assert.assertEquals(label1, opcode.getLabel(0));
83         Assert.assertEquals(label2, opcode.getLabel(1));
84         Assert.assertEquals(label3, opcode.getLabel(2));
85
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());
94     }
95
96     @Test
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();
102
103         opcode.addLabel(label1);
104         opcode.setLabels(new Label[] { label1, label2, label3, });
105
106         Assert.assertEquals(label1, opcode.getLabel(0));
107         Assert.assertEquals(label2, opcode.getLabel(1));
108         Assert.assertEquals(label3, opcode.getLabel(2));
109
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());
118     }
119
120     @Test(expected=NullPointerException.class)
121     public void testSetLabelsThrownNullPointerException(){
122         opcode = new Opcode(154, "ifne", 2, -1, "BRANCH");
123
124         opcode.setLabels(new Label[] { null, });
125     }
126
127     /**
128      * this test will be thrown IllegalStateException.
129      * Because, IllegalStateException is checked before null check of array elements.
130      */
131     @Test(expected=IllegalStateException.class)
132     public void testSetLabelsNullPointer2(){
133         opcode.setLabels(new Label[] { null, });
134     }
135
136     @Test
137     public void testSetAct() throws Exception{
138         opcode = new Opcode(182, "invokevirtual", 2, 0, "INVOKE");
139         opcode.setAct(4);
140     }
141 }