OSDN Git Service

cflib は plugins プロジェクトから,Stigmata直下のプロジェクトに移行したため,このリポジトリからは削除した.
[stigmata/stigmata-plugins.git] / osb / src / test / java / jp / sourceforge / stigmata / birthmarks / osb / OperandStackBehaviorsBirthmarkExtractorTest.java
1 package jp.sourceforge.stigmata.birthmarks.osb;\r
2 \r
3 import java.io.IOException;\r
4 import java.net.URL;\r
5 \r
6 import jp.sourceforge.stigmata.Birthmark;\r
7 import jp.sourceforge.stigmata.BirthmarkElement;\r
8 import jp.sourceforge.stigmata.BirthmarkExtractionFailedException;\r
9 import jp.sourceforge.stigmata.ExtractionUnit;\r
10 import jp.sourceforge.stigmata.cflib.Opcode;\r
11 import jp.sourceforge.stigmata.cflib.OpcodeManager;\r
12 \r
13 import org.junit.Assert;\r
14 import org.junit.Before;\r
15 import org.junit.Test;\r
16 \r
17 public class OperandStackBehaviorsBirthmarkExtractorTest{\r
18     private OperandStackBehaviorsBirthmarkExtractor extractor;\r
19 \r
20     @Before\r
21     public void setUp(){\r
22         OperandStackBehaviorsBirthmarkService service = new OperandStackBehaviorsBirthmarkService();\r
23         extractor = (OperandStackBehaviorsBirthmarkExtractor)service.getExtractor();\r
24     }\r
25 \r
26     @Test\r
27     public void testBasic(){\r
28         OperandStackBehaviorsBirthmarkElement element = \r
29             (OperandStackBehaviorsBirthmarkElement)extractor.buildElement("27, 28, 96, 62");\r
30 \r
31         Assert.assertArrayEquals(\r
32             new ExtractionUnit[] { ExtractionUnit.CLASS, },\r
33             extractor.getAcceptableUnits()\r
34         );\r
35 \r
36         Assert.assertEquals(Opcode.Category.LOAD,  element.getCategory(0));\r
37         Assert.assertEquals(Opcode.Category.LOAD,  element.getCategory(1));\r
38         Assert.assertEquals(Opcode.Category.ADD,   element.getCategory(2));\r
39         Assert.assertEquals(Opcode.Category.STORE, element.getCategory(3));\r
40 \r
41         OpcodeManager manager = OpcodeManager.getInstance();\r
42         Assert.assertEquals(manager.getOpcode(27), element.getOpcode(0));\r
43         Assert.assertEquals(manager.getOpcode(28), element.getOpcode(1));\r
44         Assert.assertEquals(manager.getOpcode(96), element.getOpcode(2));\r
45         Assert.assertEquals(manager.getOpcode(62), element.getOpcode(3));\r
46     }\r
47 \r
48     @Test\r
49     public void testExtract1() throws BirthmarkExtractionFailedException, IOException{\r
50         URL url = getClass().getResource("/resources/Sample1.class");\r
51         Birthmark birthmark = extractor.extract(url.openStream());\r
52 \r
53         Assert.assertEquals("osb", birthmark.getType());\r
54         Assert.assertEquals(7, birthmark.getElementCount());\r
55 \r
56         BirthmarkElement[] elements = birthmark.getElements();\r
57         OperandStackBehaviorsBirthmarkElement element = (OperandStackBehaviorsBirthmarkElement)elements[0];\r
58         Assert.assertEquals(2, element.getLength());\r
59         Assert.assertEquals(Opcode.Category.LOAD,  element.getCategory(0));  // aload_0\r
60         Assert.assertEquals(Opcode.Category.INVOKE, element.getCategory(1)); // invokespecial\r
61 \r
62         element = (OperandStackBehaviorsBirthmarkElement)elements[1];\r
63         Assert.assertEquals(1, element.getLength());\r
64         Assert.assertEquals(Opcode.Category.RETURN, element.getCategory(0)); // return\r
65 \r
66         element = (OperandStackBehaviorsBirthmarkElement)elements[2];\r
67         Assert.assertEquals(2, element.getLength());\r
68         Assert.assertEquals(Opcode.Category.CONSTANT, element.getCategory(0)); // iconst_2\r
69         Assert.assertEquals(Opcode.Category.STORE, element.getCategory(1));    // istore_1\r
70 \r
71         element = (OperandStackBehaviorsBirthmarkElement)elements[3];\r
72         Assert.assertEquals(2, element.getLength());\r
73         Assert.assertEquals(Opcode.Category.CONSTANT, element.getCategory(0)); // iconst_4\r
74         Assert.assertEquals(Opcode.Category.STORE,    element.getCategory(1)); // istore_2\r
75 \r
76         element = (OperandStackBehaviorsBirthmarkElement)elements[4];\r
77         Assert.assertEquals(4, element.getLength());\r
78         Assert.assertEquals(Opcode.Category.LOAD,  element.getCategory(0)); // iconst_4\r
79         Assert.assertEquals(Opcode.Category.LOAD,  element.getCategory(1)); // iconst_4\r
80         Assert.assertEquals(Opcode.Category.ADD,   element.getCategory(2)); // iadd\r
81         Assert.assertEquals(Opcode.Category.STORE, element.getCategory(3)); // istore_3\r
82 \r
83         element = (OperandStackBehaviorsBirthmarkElement)elements[5];\r
84         Assert.assertEquals(3, element.getLength());\r
85         Assert.assertEquals(Opcode.Category.FIELD,  element.getCategory(0)); // getstatic\r
86         Assert.assertEquals(Opcode.Category.LOAD,   element.getCategory(1)); // iload_3\r
87         Assert.assertEquals(Opcode.Category.INVOKE, element.getCategory(2)); // invokevirtual\r
88 \r
89         element = (OperandStackBehaviorsBirthmarkElement)elements[6];\r
90         Assert.assertEquals(1, element.getLength());\r
91         Assert.assertEquals(Opcode.Category.RETURN, element.getCategory(0)); // return\r
92     }\r
93 \r
94     @Test\r
95     public void testExtract2() throws BirthmarkExtractionFailedException, IOException{\r
96         URL url = getClass().getResource("/resources/Sample2.class");\r
97         Birthmark birthmark = extractor.extract(url.openStream());\r
98 \r
99         Assert.assertEquals("osb", birthmark.getType());\r
100         Assert.assertEquals(8, birthmark.getElementCount());\r
101 \r
102         BirthmarkElement[] elements = birthmark.getElements();\r
103         OperandStackBehaviorsBirthmarkElement element = (OperandStackBehaviorsBirthmarkElement)elements[0];\r
104         Assert.assertEquals(2, element.getLength());\r
105         Assert.assertEquals(Opcode.Category.LOAD,  element.getCategory(0));  // aload_0\r
106         Assert.assertEquals(Opcode.Category.INVOKE, element.getCategory(1)); // invokespecial\r
107 \r
108         element = (OperandStackBehaviorsBirthmarkElement)elements[1];\r
109         Assert.assertEquals(1, element.getLength());\r
110         Assert.assertEquals(Opcode.Category.RETURN, element.getCategory(0)); // return\r
111 \r
112         element = (OperandStackBehaviorsBirthmarkElement)elements[2];\r
113         Assert.assertEquals(2, element.getLength());\r
114         Assert.assertEquals(Opcode.Category.CONSTANT, element.getCategory(0)); // iconst_2\r
115         Assert.assertEquals(Opcode.Category.STORE, element.getCategory(1));    // istore_1\r
116 \r
117         element = (OperandStackBehaviorsBirthmarkElement)elements[3];\r
118         Assert.assertEquals(2, element.getLength());\r
119         Assert.assertEquals(Opcode.Category.CONSTANT, element.getCategory(0)); // iconst_4\r
120         Assert.assertEquals(Opcode.Category.STORE,    element.getCategory(1)); // istore_2\r
121 \r
122         element = (OperandStackBehaviorsBirthmarkElement)elements[4];\r
123         Assert.assertEquals(4, element.getLength());\r
124         Assert.assertEquals(Opcode.Category.LOAD,  element.getCategory(0)); // iconst_4\r
125         Assert.assertEquals(Opcode.Category.LOAD,  element.getCategory(1)); // iconst_4\r
126         Assert.assertEquals(Opcode.Category.ADD,   element.getCategory(2)); // iadd\r
127         Assert.assertEquals(Opcode.Category.STORE, element.getCategory(3)); // istore_3\r
128 \r
129         element = (OperandStackBehaviorsBirthmarkElement)elements[5];\r
130         Assert.assertEquals(6, element.getLength());\r
131         Assert.assertEquals(Opcode.Category.LOAD,     element.getCategory(0));\r
132         Assert.assertEquals(Opcode.Category.LOAD,     element.getCategory(1));\r
133         Assert.assertEquals(Opcode.Category.MULTIPLY, element.getCategory(2));\r
134         Assert.assertEquals(Opcode.Category.LOAD,     element.getCategory(3));\r
135         Assert.assertEquals(Opcode.Category.SUBTRACT, element.getCategory(4));\r
136         Assert.assertEquals(Opcode.Category.STORE,    element.getCategory(5));\r
137 \r
138         element = (OperandStackBehaviorsBirthmarkElement)elements[6];\r
139         Assert.assertEquals(3, element.getLength());\r
140         Assert.assertEquals(Opcode.Category.FIELD,  element.getCategory(0)); // getstatic\r
141         Assert.assertEquals(Opcode.Category.LOAD,   element.getCategory(1)); // iload_3\r
142         Assert.assertEquals(Opcode.Category.INVOKE, element.getCategory(2)); // invokevirtual\r
143 \r
144         element = (OperandStackBehaviorsBirthmarkElement)elements[7];\r
145         Assert.assertEquals(1, element.getLength());\r
146         Assert.assertEquals(Opcode.Category.RETURN, element.getCategory(0)); // return\r
147     }\r
148 }\r