001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *   http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     * 
017     */
018    
019    package org.apache.bcel;
020    
021    import java.io.File;
022    import java.util.ArrayList;
023    import java.util.List;
024    
025    import junit.framework.TestCase;
026    
027    import org.apache.bcel.classfile.AnnotationEntry;
028    import org.apache.bcel.classfile.Attribute;
029    import org.apache.bcel.classfile.JavaClass;
030    import org.apache.bcel.classfile.Method;
031    import org.apache.bcel.generic.AnnotationEntryGen;
032    import org.apache.bcel.generic.ConstantPoolGen;
033    import org.apache.bcel.generic.ElementValueGen;
034    import org.apache.bcel.generic.ElementValuePairGen;
035    import org.apache.bcel.generic.ObjectType;
036    import org.apache.bcel.generic.SimpleElementValueGen;
037    import org.apache.bcel.util.ClassPath;
038    import org.apache.bcel.util.SyntheticRepository;
039    
040    public abstract class AbstractTestCase extends TestCase
041    {
042            private boolean verbose = false;
043    
044            protected File createTestdataFile(String name)
045            {
046                    return new File("target" + File.separator + "testdata" + File.separator
047                                    + name);
048            }
049    
050            protected JavaClass getTestClass(String name) throws ClassNotFoundException
051            {
052                    return SyntheticRepository.getInstance().loadClass(name);
053            }
054    
055            protected Method getMethod(JavaClass cl, String methodname)
056            {
057                    Method[] methods = cl.getMethods();
058                    for (int i = 0; i < methods.length; i++)
059                    {
060                            Method m = methods[i];
061                            if (m.getName().equals(methodname))
062                            {
063                                    return m;
064                            }
065                    }
066                    return null;
067            }
068    
069            protected boolean wipe(String name)
070            {
071                    return new File("target" + File.separator + "testdata" + File.separator
072                                    + name).delete();
073            }
074    
075            protected boolean wipe(String dir, String name)
076            {
077                    boolean b = wipe(dir + File.separator + name);
078                    String[] files = new File(dir).list();
079                    if (files == null || files.length == 0)
080                    {
081                            new File(dir).delete(); // Why does this not succeed? stupid thing
082                    }
083                    return b;
084            }
085    
086            public SyntheticRepository createRepos(String cpentry)
087            {
088                    ClassPath cp = new ClassPath("target" + File.separator + "testdata"
089                                    + File.separator + cpentry + File.separator);
090                    return SyntheticRepository.getInstance(cp);
091            }
092    
093            protected Attribute[] findAttribute(String name, JavaClass clazz)
094            {
095                    Attribute[] all = clazz.getAttributes();
096                    List<Attribute> chosenAttrsList = new ArrayList<Attribute>();
097                    for (int i = 0; i < all.length; i++)
098                    {
099                            if (verbose)
100                                    System.err.println("Attribute: " + all[i].getName());
101                            if (all[i].getName().equals(name))
102                                    chosenAttrsList.add(all[i]);
103                    }
104                    return chosenAttrsList.toArray(new Attribute[] {});
105            }
106    
107            protected Attribute findAttribute(String name, Attribute[] all)
108            {
109                    List<Attribute> chosenAttrsList = new ArrayList<Attribute>();
110                    for (int i = 0; i < all.length; i++)
111                    {
112                            if (verbose)
113                                    System.err.println("Attribute: " + all[i].getName());
114                            if (all[i].getName().equals(name))
115                                    chosenAttrsList.add(all[i]);
116                    }
117                    assertTrue("Should be one match: " + chosenAttrsList.size(),
118                                    chosenAttrsList.size() == 1);
119                    return chosenAttrsList.get(0);
120            }
121    
122            protected String dumpAttributes(Attribute[] as)
123            {
124                    StringBuffer result = new StringBuffer();
125                    result.append("AttributeArray:[");
126                    for (int i = 0; i < as.length; i++)
127                    {
128                            Attribute attr = as[i];
129                            result.append(attr.toString());
130                            if (i + 1 < as.length)
131                                    result.append(",");
132                    }
133                    result.append("]");
134                    return result.toString();
135            }
136    
137            protected String dumpAnnotationEntries(AnnotationEntry[] as)
138            {
139                    StringBuffer result = new StringBuffer();
140                    result.append("[");
141                    for (int i = 0; i < as.length; i++)
142                    {
143                            AnnotationEntry annotation = as[i];
144                            result.append(annotation.toShortString());
145                            if (i + 1 < as.length)
146                                    result.append(",");
147                    }
148                    result.append("]");
149                    return result.toString();
150            }
151    
152            protected String dumpAnnotationEntries(AnnotationEntryGen[] as)
153            {
154                    StringBuffer result = new StringBuffer();
155                    result.append("[");
156                    for (int i = 0; i < as.length; i++)
157                    {
158                            AnnotationEntryGen annotation = as[i];
159                            result.append(annotation.toShortString());
160                            if (i + 1 < as.length)
161                                    result.append(",");
162                    }
163                    result.append("]");
164                    return result.toString();
165            }
166    
167            public AnnotationEntryGen createFruitAnnotationEntry(ConstantPoolGen cp,
168                            String aFruit, boolean visibility)
169            {
170                    SimpleElementValueGen evg = new SimpleElementValueGen(
171                                    ElementValueGen.STRING, cp, aFruit);
172                    ElementValuePairGen nvGen = new ElementValuePairGen("fruit", evg, cp);
173                    ObjectType t = new ObjectType("SimpleStringAnnotation");
174                    List<ElementValuePairGen> elements = new ArrayList<ElementValuePairGen>();
175                    elements.add(nvGen);
176                    return new AnnotationEntryGen(t, elements, visibility, cp);
177            }
178    }