1 | /* |
2 | * Copyright (c) 2001-2009, Jean Tessier |
3 | * All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * |
9 | * * Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * |
12 | * * Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
15 | * |
16 | * * Neither the name of Jean Tessier nor the names of his contributors |
17 | * may be used to endorse or promote products derived from this software |
18 | * without specific prior written permission. |
19 | * |
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR |
24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 | */ |
32 | |
33 | package com.jeantessier.classreader.impl; |
34 | |
35 | import static org.hamcrest.Matchers.*; |
36 | import org.jmock.*; |
37 | import org.jmock.integration.junit4.*; |
38 | import org.jmock.lib.legacy.*; |
39 | import static org.junit.Assert.*; |
40 | import org.junit.*; |
41 | import org.junit.runner.*; |
42 | |
43 | @RunWith(JMock.class) |
44 | public class TestClass_info { |
45 | private static final int CLASS_NAME_INDEX = 1; |
46 | private static final String PACKAGE_NAME = "foo"; |
47 | private static final String SIMPLE_CLASS_NAME = "Foo"; |
48 | private static final String ENCODED_SIMPLE_CLASS_NAME = "L" + SIMPLE_CLASS_NAME + ";"; |
49 | private static final String ENCODED_FULL_CLASS_NAME = "L" + PACKAGE_NAME + "/" + SIMPLE_CLASS_NAME + ";"; |
50 | private static final String FULL_CLASS_NAME = PACKAGE_NAME + "." + SIMPLE_CLASS_NAME; |
51 | |
52 | private Mockery context; |
53 | |
54 | private ConstantPool constantPool; |
55 | |
56 | private Class_info sut; |
57 | |
58 | @Before |
59 | public void setUp() throws Exception { |
60 | context = new Mockery(); |
61 | context.setImposteriser(ClassImposteriser.INSTANCE); |
62 | |
63 | constantPool = context.mock(ConstantPool.class); |
64 | |
65 | sut = new Class_info(constantPool, CLASS_NAME_INDEX); |
66 | } |
67 | |
68 | @Test |
69 | public void testGetName_defaultPackage() { |
70 | expectClassNameLookup(CLASS_NAME_INDEX, ENCODED_SIMPLE_CLASS_NAME); |
71 | |
72 | String actualValue = sut.getName(); |
73 | assertThat("full name", actualValue, is(SIMPLE_CLASS_NAME)); |
74 | } |
75 | |
76 | @Test |
77 | public void testGetName_withPackageName() { |
78 | expectClassNameLookup(CLASS_NAME_INDEX, ENCODED_FULL_CLASS_NAME); |
79 | |
80 | String actualValue = sut.getName(); |
81 | assertThat("full name", actualValue, is(FULL_CLASS_NAME)); |
82 | } |
83 | |
84 | @Test |
85 | public void testGetPackageName_defaultPackage() { |
86 | expectClassNameLookup(CLASS_NAME_INDEX, ENCODED_SIMPLE_CLASS_NAME); |
87 | |
88 | String actualValue = sut.getPackageName(); |
89 | assertThat("package", actualValue, is("")); |
90 | } |
91 | |
92 | @Test |
93 | public void testGetPackageName_withPackageName() { |
94 | expectClassNameLookup(CLASS_NAME_INDEX, ENCODED_FULL_CLASS_NAME); |
95 | |
96 | String actualValue = sut.getPackageName(); |
97 | assertThat("package", actualValue, is(PACKAGE_NAME)); |
98 | } |
99 | |
100 | @Test |
101 | public void testGetSimpleName_defaultPackage() { |
102 | expectClassNameLookup(CLASS_NAME_INDEX, ENCODED_SIMPLE_CLASS_NAME); |
103 | |
104 | String actualValue = sut.getSimpleName(); |
105 | assertThat("simple name", actualValue, is(SIMPLE_CLASS_NAME)); |
106 | } |
107 | |
108 | @Test |
109 | public void testGetSimpleName_withPackageName() { |
110 | expectClassNameLookup(CLASS_NAME_INDEX, ENCODED_FULL_CLASS_NAME); |
111 | |
112 | String actualValue = sut.getSimpleName(); |
113 | assertThat("simple name", actualValue, is(SIMPLE_CLASS_NAME)); |
114 | } |
115 | |
116 | private void expectClassNameLookup(final int index, final String value) { |
117 | final UTF8_info utf8_info = context.mock(UTF8_info.class); |
118 | |
119 | context.checking(new Expectations() {{ |
120 | one (constantPool).get(index); |
121 | will(returnValue(utf8_info)); |
122 | one (utf8_info).getValue(); |
123 | will(returnValue(value)); |
124 | }}); |
125 | } |
126 | } |