aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/apache/bcel/AbstractTestCase.java
blob: 4cf22b3ccebe27d816c0f6a410383da20c5e0175 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.apache.bcel;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.bcel.classfile.AnnotationEntry;
import org.apache.bcel.classfile.Attribute;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.classfile.Method;
import org.apache.bcel.generic.AnnotationEntryGen;
import org.apache.bcel.generic.ConstantPoolGen;
import org.apache.bcel.generic.ElementValueGen;
import org.apache.bcel.generic.ElementValuePairGen;
import org.apache.bcel.generic.ObjectType;
import org.apache.bcel.generic.SimpleElementValueGen;
import org.apache.bcel.util.ClassPath;
import org.apache.bcel.util.SyntheticRepository;

import junit.framework.TestCase;

public abstract class AbstractTestCase extends TestCase
{
    private static final boolean verbose = false;

    protected static final String PACKAGE_BASE_NAME = AbstractTestCase.class.getPackage().getName();

    // Location of test data
    protected static final File TESTDATA = new File("target", "testdata");

    // package base name in signature format, i.e. with '/' separators instead of '.'
    protected static final String PACKAGE_BASE_SIG = PACKAGE_BASE_NAME.replace('.', '/');

    /**
     * @param name
     * @return Path to file under the TESTDATA directory
     */
    protected File createTestdataFile(final String name)
    {
        return new File(TESTDATA, name);
    }

    protected JavaClass getTestClass(final String name) throws ClassNotFoundException
    {
        return SyntheticRepository.getInstance().loadClass(name);
    }

    protected Method getMethod(final JavaClass cl, final String methodname)
    {
        final Method[] methods = cl.getMethods();
        for (final Method m : methods) {
            if (m.getName().equals(methodname))
            {
                return m;
            }
        }
        return null;
    }

    /**
     * Delete a file under the TESTDATA directory
     * @param name
     * @return
     */
    protected boolean wipe(final String name)
    {
        return new File(TESTDATA, name).delete();
    }

    /**
     * Delete a directory and file under the TESTDATA directory
     * @param dir
     * @param name
     * @return true if the file was deleted
     */
    protected boolean wipe(final String dir, final String name)
    {
        // The parameter is relative to the TESTDATA dir
        final boolean b = wipe(dir + File.separator + name);
        final File testDir = new File(TESTDATA, dir);
        final String[] files = testDir.list();
        if (files == null || files.length == 0)
        {
            if (!testDir.delete()) {
                System.err.println("Failed to remove: " + testDir);
            }
        } else {
            System.err.println("Non-empty directory: " + testDir);
        }
        return b;
    }

    public SyntheticRepository createRepos(final String cpentry)
    {
        final ClassPath cp = new ClassPath("target" + File.separator + "testdata"
                + File.separator + cpentry + File.separator);
        return SyntheticRepository.getInstance(cp);
    }

    protected Attribute[] findAttribute(final String name, final JavaClass clazz)
    {
        final Attribute[] all = clazz.getAttributes();
        final List<Attribute> chosenAttrsList = new ArrayList<>();
        for (final Attribute element : all) {
            if (verbose) {
                System.err.println("Attribute: " + element.getName());
            }
            if (element.getName().equals(name)) {
                chosenAttrsList.add(element);
            }
        }
        return chosenAttrsList.toArray(new Attribute[] {});
    }

    protected Attribute findAttribute(final String name, final Attribute[] all)
    {
        final List<Attribute> chosenAttrsList = new ArrayList<>();
        for (final Attribute element : all) {
            if (verbose) {
                System.err.println("Attribute: " + element.getName());
            }
            if (element.getName().equals(name)) {
                chosenAttrsList.add(element);
            }
        }
        assertTrue("Should be one match: " + chosenAttrsList.size(),
                chosenAttrsList.size() == 1);
        return chosenAttrsList.get(0);
    }

    protected String dumpAttributes(final Attribute[] as)
    {
        final StringBuilder result = new StringBuilder();
        result.append("AttributeArray:[");
        for (int i = 0; i < as.length; i++)
        {
            final Attribute attr = as[i];
            result.append(attr.toString());
            if (i + 1 < as.length) {
                result.append(",");
            }
        }
        result.append("]");
        return result.toString();
    }

    protected String dumpAnnotationEntries(final AnnotationEntry[] as)
    {
        final StringBuilder result = new StringBuilder();
        result.append("[");
        for (int i = 0; i < as.length; i++)
        {
            final AnnotationEntry annotation = as[i];
            result.append(annotation.toShortString());
            if (i + 1 < as.length) {
                result.append(",");
            }
        }
        result.append("]");
        return result.toString();
    }

    protected String dumpAnnotationEntries(final AnnotationEntryGen[] as)
    {
        final StringBuilder result = new StringBuilder();
        result.append("[");
        for (int i = 0; i < as.length; i++)
        {
            final AnnotationEntryGen annotation = as[i];
            result.append(annotation.toShortString());
            if (i + 1 < as.length) {
                result.append(",");
            }
        }
        result.append("]");
        return result.toString();
    }

    public AnnotationEntryGen createFruitAnnotationEntry(final ConstantPoolGen cp,
            final String aFruit, final boolean visibility)
    {
        final SimpleElementValueGen evg = new SimpleElementValueGen(
                ElementValueGen.STRING, cp, aFruit);
        final ElementValuePairGen nvGen = new ElementValuePairGen("fruit", evg, cp);
        final ObjectType t = new ObjectType("SimpleStringAnnotation");
        final List<ElementValuePairGen> elements = new ArrayList<>();
        elements.add(nvGen);
        return new AnnotationEntryGen(t, elements, visibility, cp);
    }
}