1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.bcel;
21
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23
24 import java.io.File;
25 import java.lang.management.ManagementFactory;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import org.apache.bcel.classfile.AnnotationEntry;
30 import org.apache.bcel.classfile.Attribute;
31 import org.apache.bcel.classfile.ConstantUtf8;
32 import org.apache.bcel.classfile.JavaClass;
33 import org.apache.bcel.classfile.Method;
34 import org.apache.bcel.classfile.Utility;
35 import org.apache.bcel.generic.AnnotationEntryGen;
36 import org.apache.bcel.generic.ConstantPoolGen;
37 import org.apache.bcel.generic.ElementValueGen;
38 import org.apache.bcel.generic.ElementValuePairGen;
39 import org.apache.bcel.generic.ObjectType;
40 import org.apache.bcel.generic.SimpleElementValueGen;
41 import org.apache.bcel.util.ClassPath;
42 import org.apache.bcel.util.SyntheticRepository;
43 import org.apache.bcel.verifier.VerifierFactory;
44 import org.apache.commons.io.function.Uncheck;
45
46 public abstract class AbstractTest {
47
48 private static final boolean VERBOSE = false;
49
50 protected static final String PACKAGE_BASE_NAME = AbstractTest.class.getPackage().getName();
51
52
53 protected static final File TESTDATA = new File("target", "testdata");
54
55
56 protected static final String PACKAGE_BASE_SIG = Utility.packageToPath(PACKAGE_BASE_NAME);
57
58 public static void clear() {
59 VerifierFactory.clear();
60 Repository.clearCache();
61 ConstantUtf8.clearCache();
62 }
63
64 public AnnotationEntryGen createFruitAnnotationEntry(final ConstantPoolGen cp, final String aFruit, final boolean visibility) {
65 final SimpleElementValueGen evg = new SimpleElementValueGen(ElementValueGen.STRING, cp, aFruit);
66 final ElementValuePairGen nvGen = new ElementValuePairGen("fruit", evg, cp);
67 final ObjectType t = new ObjectType("SimpleStringAnnotation");
68 final List<ElementValuePairGen> elements = new ArrayList<>();
69 elements.add(nvGen);
70 return new AnnotationEntryGen(t, elements, visibility, cp);
71 }
72
73 public SyntheticRepository createRepos(final String cpentry) {
74 return Uncheck.get(() -> {
75 try (ClassPath cp = new ClassPath("target" + File.separator + "testdata" + File.separator + cpentry + File.separator)) {
76 return SyntheticRepository.getInstance(cp);
77 }
78 });
79 }
80
81
82
83
84
85 protected File createTestdataFile(final String name) {
86 return new File(TESTDATA, name);
87 }
88
89
90
91
92
93
94
95 protected boolean delete(final String name) {
96 return new File(TESTDATA, name).delete();
97 }
98
99
100
101
102
103
104
105
106 protected boolean delete(final String dir, final String name) {
107
108 final boolean b = delete(dir + File.separator + name);
109 final File testDir = new File(TESTDATA, dir);
110 final String[] files = testDir.list();
111 if (files == null || files.length == 0) {
112 if (!testDir.delete()) {
113 System.err.println("Failed to remove: " + testDir);
114 }
115 } else {
116 System.err.println("Non-empty directory: " + testDir);
117 }
118 return b;
119 }
120
121 protected String dumpAnnotationEntries(final AnnotationEntry[] as) {
122 final StringBuilder result = new StringBuilder();
123 result.append("[");
124 for (int i = 0; i < as.length; i++) {
125 final AnnotationEntry annotation = as[i];
126 result.append(annotation.toShortString());
127 if (i + 1 < as.length) {
128 result.append(",");
129 }
130 }
131 result.append("]");
132 return result.toString();
133 }
134
135 protected String dumpAnnotationEntries(final AnnotationEntryGen[] as) {
136 final StringBuilder result = new StringBuilder();
137 result.append("[");
138 for (int i = 0; i < as.length; i++) {
139 final AnnotationEntryGen annotation = as[i];
140 result.append(annotation.toShortString());
141 if (i + 1 < as.length) {
142 result.append(",");
143 }
144 }
145 result.append("]");
146 return result.toString();
147 }
148
149 protected Attribute findAttribute(final String name, final Attribute[] all) {
150 final List<Attribute> chosenAttrsList = new ArrayList<>();
151 for (final Attribute element : all) {
152 if (VERBOSE) {
153 System.err.println("Attribute: " + element.getName());
154 }
155 if (element.getName().equals(name)) {
156 chosenAttrsList.add(element);
157 }
158 }
159 assertEquals(1, chosenAttrsList.size(), "Wrong number of matches");
160 return chosenAttrsList.get(0);
161 }
162
163 protected Attribute[] findAttribute(final String name, final JavaClass clazz) {
164 final List<Attribute> chosenAttrsList = new ArrayList<>();
165 for (final Attribute element : clazz.getAttributes()) {
166 if (VERBOSE) {
167 System.err.println("Attribute: " + element.getName());
168 }
169 if (element.getName().equals(name)) {
170 chosenAttrsList.add(element);
171 }
172 }
173 return chosenAttrsList.toArray(Attribute.EMPTY_ARRAY);
174 }
175
176
177
178
179
180
181 protected String getJavaAgent() {
182 final List<String> jvmArgs = ManagementFactory.getRuntimeMXBean().getInputArguments();
183 return jvmArgs.stream().filter(arg -> arg.startsWith("-javaagent")).findFirst().orElse(null);
184 }
185
186 protected Method getMethod(final JavaClass cl, final String methodname) {
187 for (final Method m : cl.getMethods()) {
188 if (m.getName().equals(methodname)) {
189 return m;
190 }
191 }
192 return null;
193 }
194
195 protected JavaClass getTestJavaClass(final String name) throws ClassNotFoundException {
196 return SyntheticRepository.getInstance().loadClass(name);
197 }
198
199 }