1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 package org.apache.bcel;
20 
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.List;
24 
25 import org.apache.bcel.classfile.AnnotationEntry;
26 import org.apache.bcel.classfile.Attribute;
27 import org.apache.bcel.classfile.JavaClass;
28 import org.apache.bcel.classfile.Method;
29 import org.apache.bcel.generic.AnnotationEntryGen;
30 import org.apache.bcel.generic.ConstantPoolGen;
31 import org.apache.bcel.generic.ElementValueGen;
32 import org.apache.bcel.generic.ElementValuePairGen;
33 import org.apache.bcel.generic.ObjectType;
34 import org.apache.bcel.generic.SimpleElementValueGen;
35 import org.apache.bcel.util.ClassPath;
36 import org.apache.bcel.util.SyntheticRepository;
37 
38 import junit.framework.TestCase;
39 
40 public abstract class AbstractTestCase extends TestCase
41 {
42     private static final boolean verbose = false;
43 
44     protected static final String PACKAGE_BASE_NAME = AbstractTestCase.class.getPackage().getName();
45 
46     // Location of test data
47     protected static final File TESTDATA = new File("target", "testdata");
48 
49     // package base name in signature format, i.e. with '/' separators instead of '.'
50     protected static final String PACKAGE_BASE_SIG = PACKAGE_BASE_NAME.replace('.', '/');
51 
52     /**
53      * @param name
54      * @return Path to file under the TESTDATA directory
55      */
createTestdataFile(final String name)56     protected File createTestdataFile(final String name)
57     {
58         return new File(TESTDATA, name);
59     }
60 
getTestClass(final String name)61     protected JavaClass getTestClass(final String name) throws ClassNotFoundException
62     {
63         return SyntheticRepository.getInstance().loadClass(name);
64     }
65 
getMethod(final JavaClass cl, final String methodname)66     protected Method getMethod(final JavaClass cl, final String methodname)
67     {
68         final Method[] methods = cl.getMethods();
69         for (final Method m : methods) {
70             if (m.getName().equals(methodname))
71             {
72                 return m;
73             }
74         }
75         return null;
76     }
77 
78     /**
79      * Delete a file under the TESTDATA directory
80      * @param name
81      * @return
82      */
wipe(final String name)83     protected boolean wipe(final String name)
84     {
85         return new File(TESTDATA, name).delete();
86     }
87 
88     /**
89      * Delete a directory and file under the TESTDATA directory
90      * @param dir
91      * @param name
92      * @return true if the file was deleted
93      */
wipe(final String dir, final String name)94     protected boolean wipe(final String dir, final String name)
95     {
96         // The parameter is relative to the TESTDATA dir
97         final boolean b = wipe(dir + File.separator + name);
98         final File testDir = new File(TESTDATA, dir);
99         final String[] files = testDir.list();
100         if (files == null || files.length == 0)
101         {
102             if (!testDir.delete()) {
103                 System.err.println("Failed to remove: " + testDir);
104             }
105         } else {
106             System.err.println("Non-empty directory: " + testDir);
107         }
108         return b;
109     }
110 
createRepos(final String cpentry)111     public SyntheticRepository createRepos(final String cpentry)
112     {
113         final ClassPath cp = new ClassPath("target" + File.separator + "testdata"
114                 + File.separator + cpentry + File.separator);
115         return SyntheticRepository.getInstance(cp);
116     }
117 
findAttribute(final String name, final JavaClass clazz)118     protected Attribute[] findAttribute(final String name, final JavaClass clazz)
119     {
120         final Attribute[] all = clazz.getAttributes();
121         final List<Attribute> chosenAttrsList = new ArrayList<>();
122         for (final Attribute element : all) {
123             if (verbose) {
124                 System.err.println("Attribute: " + element.getName());
125             }
126             if (element.getName().equals(name)) {
127                 chosenAttrsList.add(element);
128             }
129         }
130         return chosenAttrsList.toArray(new Attribute[] {});
131     }
132 
findAttribute(final String name, final Attribute[] all)133     protected Attribute findAttribute(final String name, final Attribute[] all)
134     {
135         final List<Attribute> chosenAttrsList = new ArrayList<>();
136         for (final Attribute element : all) {
137             if (verbose) {
138                 System.err.println("Attribute: " + element.getName());
139             }
140             if (element.getName().equals(name)) {
141                 chosenAttrsList.add(element);
142             }
143         }
144         assertTrue("Should be one match: " + chosenAttrsList.size(),
145                 chosenAttrsList.size() == 1);
146         return chosenAttrsList.get(0);
147     }
148 
dumpAttributes(final Attribute[] as)149     protected String dumpAttributes(final Attribute[] as)
150     {
151         final StringBuilder result = new StringBuilder();
152         result.append("AttributeArray:[");
153         for (int i = 0; i < as.length; i++)
154         {
155             final Attribute attr = as[i];
156             result.append(attr.toString());
157             if (i + 1 < as.length) {
158                 result.append(",");
159             }
160         }
161         result.append("]");
162         return result.toString();
163     }
164 
dumpAnnotationEntries(final AnnotationEntry[] as)165     protected String dumpAnnotationEntries(final AnnotationEntry[] as)
166     {
167         final StringBuilder result = new StringBuilder();
168         result.append("[");
169         for (int i = 0; i < as.length; i++)
170         {
171             final AnnotationEntry annotation = as[i];
172             result.append(annotation.toShortString());
173             if (i + 1 < as.length) {
174                 result.append(",");
175             }
176         }
177         result.append("]");
178         return result.toString();
179     }
180 
dumpAnnotationEntries(final AnnotationEntryGen[] as)181     protected String dumpAnnotationEntries(final AnnotationEntryGen[] as)
182     {
183         final StringBuilder result = new StringBuilder();
184         result.append("[");
185         for (int i = 0; i < as.length; i++)
186         {
187             final AnnotationEntryGen annotation = as[i];
188             result.append(annotation.toShortString());
189             if (i + 1 < as.length) {
190                 result.append(",");
191             }
192         }
193         result.append("]");
194         return result.toString();
195     }
196 
createFruitAnnotationEntry(final ConstantPoolGen cp, final String aFruit, final boolean visibility)197     public AnnotationEntryGen createFruitAnnotationEntry(final ConstantPoolGen cp,
198             final String aFruit, final boolean visibility)
199     {
200         final SimpleElementValueGen evg = new SimpleElementValueGen(
201                 ElementValueGen.STRING, cp, aFruit);
202         final ElementValuePairGen nvGen = new ElementValuePairGen("fruit", evg, cp);
203         final ObjectType t = new ObjectType("SimpleStringAnnotation");
204         final List<ElementValuePairGen> elements = new ArrayList<>();
205         elements.add(nvGen);
206         return new AnnotationEntryGen(t, elements, visibility, cp);
207     }
208 }
209