1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
package com.jeantessier.classreader.impl; |
34 | |
|
35 | |
import java.io.*; |
36 | |
|
37 | |
import static org.hamcrest.Matchers.*; |
38 | |
import org.jmock.*; |
39 | |
import org.jmock.integration.junit4.*; |
40 | |
import org.jmock.lib.legacy.*; |
41 | |
import static org.junit.Assert.*; |
42 | |
import org.junit.*; |
43 | |
import org.junit.runner.*; |
44 | |
|
45 | |
@RunWith(JMock.class) |
46 | 12 | public class TestMethod_info { |
47 | |
private static final int TEST_ACCESS_FLAG = 0x0008; |
48 | |
private static final int TEST_NAME_INDEX = 123; |
49 | |
private static final int TEST_SIGNATURE_INDEX = 456; |
50 | |
private static final int TEST_NB_ATTRIBUTES = 0; |
51 | |
|
52 | |
private Mockery context; |
53 | |
|
54 | |
private DataInput mockIn; |
55 | |
private ConstantPool mockConstantPool; |
56 | |
|
57 | |
private Sequence dataReads; |
58 | |
|
59 | |
@Before |
60 | |
public void setUp() { |
61 | 1 | context = new Mockery(); |
62 | 1 | context.setImposteriser(ClassImposteriser.INSTANCE); |
63 | |
|
64 | 1 | mockIn = context.mock(DataInput.class); |
65 | 1 | mockConstantPool = context.mock(ConstantPool.class); |
66 | |
|
67 | 1 | dataReads = context.sequence("dataReads"); |
68 | 1 | } |
69 | |
|
70 | |
@Test |
71 | |
public void testDeclarationForStaticInitializer() throws IOException { |
72 | 1 | final Classfile mockClassfile = context.mock(Classfile.class); |
73 | |
|
74 | 1 | context.checking(new Expectations() {{ |
75 | 1 | allowing (mockClassfile).getConstantPool(); |
76 | 1 | will(returnValue(mockConstantPool)); |
77 | 1 | }}); |
78 | |
|
79 | 1 | expectReadU2(TEST_ACCESS_FLAG); |
80 | 1 | expectReadU2(TEST_NAME_INDEX); |
81 | 1 | expectLookupUtf8(TEST_NAME_INDEX, "<clinit>", "name"); |
82 | 1 | expectReadU2(TEST_SIGNATURE_INDEX); |
83 | 1 | expectLookupUtf8(TEST_SIGNATURE_INDEX, "()V", "signature"); |
84 | 1 | expectReadU2(TEST_NB_ATTRIBUTES); |
85 | |
|
86 | 1 | Method_info sut = new Method_info(mockClassfile, mockIn); |
87 | 1 | assertThat("declaration", sut.getDeclaration(), is("static {}")); |
88 | 1 | } |
89 | |
|
90 | |
protected void expectReadU2(final int i) throws IOException { |
91 | 4 | context.checking(new Expectations() {{ |
92 | 4 | one (mockIn).readUnsignedShort(); |
93 | 4 | inSequence(dataReads); |
94 | 4 | will(returnValue(i)); |
95 | 4 | }}); |
96 | 4 | } |
97 | |
|
98 | |
protected void expectLookupUtf8(int index, String value, String mockName) { |
99 | 2 | expectLookupUtf8(index, value, context.mock(UTF8_info.class, mockName)); |
100 | 2 | } |
101 | |
|
102 | |
private void expectLookupUtf8(final int index, final String value, final UTF8_info mockUtf8_info) { |
103 | 2 | context.checking(new Expectations() {{ |
104 | 2 | atLeast(1).of (mockConstantPool).get(index); |
105 | 2 | will(returnValue(mockUtf8_info)); |
106 | 2 | atLeast(1).of (mockUtf8_info).getValue(); |
107 | 2 | will(returnValue(value)); |
108 | 2 | }}); |
109 | 2 | } |
110 | |
} |