|
| 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 | +} |
0 commit comments