Skip to content

Commit 7a719cd

Browse files
committed
Unit test for the annotation scanning classes
1 parent 68923ba commit 7a719cd

6 files changed

Lines changed: 564 additions & 0 deletions

File tree

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.annotation.scan;
17+
18+
import static org.junit.Assert.assertFalse;
19+
import static org.junit.Assert.assertNotNull;
20+
import static org.junit.Assert.assertTrue;
21+
22+
import java.io.ByteArrayInputStream;
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import java.lang.annotation.Annotation;
26+
import java.util.HashSet;
27+
import java.util.Set;
28+
29+
import org.junit.After;
30+
import org.junit.Before;
31+
import org.junit.Test;
32+
33+
public class ByteCodeFilterTest
34+
{
35+
36+
private ByteCodeFilter filter;
37+
38+
@Before
39+
public void setup()
40+
{
41+
Set<Class<? extends Annotation>> types = new HashSet<Class<? extends Annotation>>();
42+
types.add(TestAnnotation.class);
43+
filter = new ByteCodeFilter(types);
44+
}
45+
46+
@After
47+
public void teardown()
48+
{
49+
filter = null;
50+
}
51+
52+
@Test
53+
public void testInvalidClass() throws IOException
54+
{
55+
assertFalse(filter.accept(new ByteArrayInputStream(new byte[] {
56+
// some random bytes
57+
(byte) 0x12, (byte) 0x34, (byte) 0xd3, (byte) 0x45,
58+
(byte) 0x13, (byte) 0x35, (byte) 0xd4, (byte) 0x46,
59+
(byte) 0x14, (byte) 0x36, (byte) 0xd5, (byte) 0x47,
60+
(byte) 0x15, (byte) 0x37, (byte) 0xd6, (byte) 0x48
61+
})));
62+
}
63+
64+
@Test
65+
public void testJdk14Class() throws IOException
66+
{
67+
assertFalse(filter.accept(new ByteArrayInputStream(new byte[] {
68+
(byte) 0xca, (byte) 0xfe, (byte) 0xba, (byte) 0xbe, // magic
69+
(byte) 0x00, (byte) 0x00, // minor
70+
(byte) 0x00, (byte) 48 // major 48 = JDK14
71+
})));
72+
}
73+
74+
@Test
75+
public void testNoAnnotationsOnClass() throws IOException
76+
{
77+
assertFalse(filter.accept(getByteCodeInputStream(ClassWithoutAnnotations.class)));
78+
}
79+
80+
@Test
81+
public void testUrlMappingAnnotationOnClass() throws IOException
82+
{
83+
assertTrue(filter.accept(getByteCodeInputStream(ClassWithUrlMapping.class)));
84+
}
85+
86+
@Test
87+
public void testClassWithLongValueInConstantPool() throws IOException
88+
{
89+
assertFalse(filter.accept(getByteCodeInputStream(ClassWithLongValueInConstantPool.class)));
90+
}
91+
92+
@Test
93+
public void testUrlActionOnMethod() throws IOException
94+
{
95+
assertTrue(filter.accept(getByteCodeInputStream(ClassWithUrlAction.class)));
96+
}
97+
98+
private InputStream getByteCodeInputStream(Class<?> clazz)
99+
{
100+
String name = clazz.getName().replace(".", "/") + ".class";
101+
InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
102+
assertNotNull("Cannot find class file: " + name, stream);
103+
return stream;
104+
}
105+
106+
/**
107+
* Class without any PrettyFaces annotations
108+
*/
109+
public static class ClassWithoutAnnotations
110+
{
111+
112+
private String someProperty;
113+
114+
public String getSomeProperty()
115+
{
116+
return someProperty;
117+
}
118+
119+
}
120+
121+
/**
122+
* Class with some {@link Long} constant in the constant pool
123+
*/
124+
public class ClassWithLongValueInConstantPool
125+
{
126+
public final static long value = 9223342036854775807l;
127+
}
128+
129+
/**
130+
* Class with a relevant annotation on the type
131+
*/
132+
@TestAnnotation
133+
public static class ClassWithUrlMapping
134+
{
135+
136+
private String someProperty;
137+
138+
public String getSomeProperty()
139+
{
140+
return someProperty;
141+
}
142+
143+
}
144+
145+
/**
146+
* Class with a relevant annotation on a method
147+
*/
148+
public static class ClassWithUrlAction
149+
{
150+
151+
private String someProperty;
152+
153+
@TestAnnotation
154+
public void urlAction()
155+
{
156+
// nothing
157+
}
158+
159+
public String getSomeProperty()
160+
{
161+
return someProperty;
162+
}
163+
164+
}
165+
166+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.annotation.scan;
17+
18+
/**
19+
* Simple test bean used by {@link WebClassesFinderTest} and {@link WebLibFinderTest}.
20+
*
21+
* @author Christian Kaltepoth
22+
*/
23+
@TestAnnotation
24+
public class ClassFinderTestBean
25+
{
26+
// Nothing
27+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.annotation.scan;
17+
18+
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertTrue;
20+
21+
import org.junit.Test;
22+
23+
public class PackageFilterTest
24+
{
25+
26+
@Test
27+
public void testEmptyFilter()
28+
{
29+
assertTrue(new PackageFilter(null).isAllowedPackage("org.ocpsoft"));
30+
assertTrue(new PackageFilter("").isAllowedPackage("org.ocpsoft"));
31+
assertTrue(new PackageFilter(" ").isAllowedPackage("org.ocpsoft"));
32+
assertTrue(new PackageFilter(" ,, ").isAllowedPackage("org.ocpsoft"));
33+
}
34+
35+
@Test
36+
public void testSinglePackage()
37+
{
38+
39+
// test simple filter
40+
PackageFilter filter = new PackageFilter("org.ocpsoft");
41+
assertEquals(true, filter.isAllowedPackage("org.ocpsoft.pretty.faces"));
42+
assertEquals(true, filter.isAllowedPackage("org.ocpsoft"));
43+
assertEquals(false, filter.isAllowedPackage("com"));
44+
assertEquals(false, filter.isAllowedPackage("de"));
45+
46+
// test some danger configuration parameter inputs
47+
assertEquals(true, new PackageFilter(" org.ocpsoft ").isAllowedPackage("org.ocpsoft.pretty.faces"));
48+
assertEquals(true, new PackageFilter(" , org.ocpsoft, ").isAllowedPackage("org.ocpsoft.pretty.faces"));
49+
50+
}
51+
52+
@Test
53+
public void testMultiplePackages()
54+
{
55+
56+
// test simple filter
57+
PackageFilter filter = new PackageFilter("org.ocpsoft,de.chkal");
58+
assertEquals(true, filter.isAllowedPackage("org.ocpsoft.pretty.faces"));
59+
assertEquals(true, filter.isAllowedPackage("org.ocpsoft"));
60+
assertEquals(true, filter.isAllowedPackage("de.chkal.bla"));
61+
assertEquals(true, filter.isAllowedPackage("de.chkal"));
62+
assertEquals(false, filter.isAllowedPackage("com"));
63+
assertEquals(false, filter.isAllowedPackage("com.google"));
64+
assertEquals(false, filter.isAllowedPackage("de"));
65+
66+
// test some danger configuration parameter inputs
67+
assertEquals(true, new PackageFilter(" org.ocpsoft , de.chkal ").isAllowedPackage("org.ocpsoft.pretty.faces"));
68+
assertEquals(true, new PackageFilter(" , org.ocpsoft, de.chkal ,,").isAllowedPackage("de.chkal.bla"));
69+
70+
}
71+
72+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.annotation.scan;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Inherited;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
24+
/**
25+
* Simple annotation for the test
26+
*/
27+
@Retention(RetentionPolicy.RUNTIME)
28+
@Target({ ElementType.METHOD, ElementType.TYPE })
29+
@Inherited
30+
public @interface TestAnnotation
31+
{
32+
33+
}

0 commit comments

Comments
 (0)