Commit c82a1b79 authored by Eric Bruneton's avatar Eric Bruneton

Refactor more tests to use AsmTest and JUnit 4.

parent d493987d
......@@ -169,7 +169,6 @@
<ant antfile="${test.conform}/basicverifier.xml" inheritRefs="true"/>
<ant antfile="${test.conform}/checkclassadapter.xml" inheritRefs="true"/>
<ant antfile="${test.conform}/checksignatureadapter.xml" inheritRefs="true"/>
<ant antfile="${test.conform}/classadapter.xml" inheritRefs="true"/>
<ant antfile="${test.conform}/classnode.xml" inheritRefs="true"/>
<ant antfile="${test.conform}/classreader.xml" inheritRefs="true"/>
<ant antfile="${test.conform}/classwriter.xml" inheritRefs="true"/>
......@@ -185,7 +184,6 @@
<ant antfile="${test.conform}/localvariablessorter2.xml" inheritRefs="true"/>
<ant antfile="${test.conform}/classremapper.xml" inheritRefs="true"/>
<ant antfile="${test.conform}/saxadapter.xml" inheritRefs="true"/>
<ant antfile="${test.conform}/signaturewriter.xml" inheritRefs="true"/>
<ant antfile="${test.conform}/simpleverifier.xml" inheritRefs="true"/>
<ant antfile="${test.conform}/sourceinterpreter.xml" inheritRefs="true"/>
<ant antfile="${test.conform}/staticinitmerger.xml" inheritRefs="true"/>
......
<!--
! ASM: a very small and fast Java bytecode manipulation framework
! Copyright (c) 2000-2011 INRIA, France Telecom
! All rights reserved.
!
! Redistribution and use in source and binary forms, with or without
! modification, are permitted provided that the following conditions
! are met:
! 1. Redistributions of source code must retain the above copyright
! notice, this list of conditions and the following disclaimer.
! 2. Redistributions in binary form must reproduce the above copyright
! notice, this list of conditions and the following disclaimer in the
! documentation and/or other materials provided with the distribution.
! 3. Neither the name of the copyright holders nor the names of its
! contributors may be used to endorse or promote products derived from
! this software without specific prior written permission.
!
! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
! THE POSSIBILITY OF SUCH DAMAGE.
-->
<project name="conform" default="test">
<target name="test">
<junit fork="yes"
printsummary="yes"
errorproperty="test.failed"
failureproperty="test.failed">
<batchtest fork="yes" todir="${out.test}/reports">
<fileset dir="${test}/conform">
<include name="**/ClassAdapterTest.java"/>
</fileset>
</batchtest>
<formatter type="xml"/>
<classpath refid="test.classpath"/>
<jvmarg value="-Dasm.test=${asm.test}"/>
<jvmarg value="-Dasm.test.class=${asm.test.class}"/>
</junit>
</target>
</project>
......@@ -34,7 +34,7 @@ import junit.framework.TestCase;
*
* @author Eric Bruneton
*/
public class AttributeUnitTest extends TestCase {
public class AttributeTest extends TestCase {
public void testUnknown() {
assertTrue(new Attribute("Comment").isUnknown());
......
// ASM: a very small and fast Java bytecode manipulation framework
// Copyright (c) 2000-2011 INRIA, France Telecom
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. Neither the name of the copyright holders nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
package org.objectweb.asm;
import junit.framework.TestSuite;
/**
* ClassVisitor tests.
*
* @author Eric Bruneton
*/
public class ClassAdapterTest extends AbstractTest {
private static final TestClassLoader LOADER = new TestClassLoader();
public static TestSuite suite() throws Exception {
return new ClassAdapterTest().getSuite();
}
@Override
public void test() throws Exception {
ClassReader cr = new ClassReader(is);
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
cr.accept(
new ClassVisitor(Opcodes.ASM5, cw) {
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
return new AnnotationAdapter(super.visitAnnotation(desc, visible));
}
@Override
public AnnotationVisitor visitTypeAnnotation(
int typeRef, TypePath typePath, String desc, boolean visible) {
return new AnnotationAdapter(
super.visitTypeAnnotation(typeRef, typePath, desc, visible));
}
@Override
public FieldVisitor visitField(
int access, String name, String desc, String signature, Object value) {
return new FieldVisitor(
Opcodes.ASM5, super.visitField(access, name, desc, signature, value)) {
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
return new AnnotationAdapter(super.visitAnnotation(desc, visible));
}
@Override
public AnnotationVisitor visitTypeAnnotation(
int typeRef, TypePath typePath, String desc, boolean visible) {
return new AnnotationAdapter(
super.visitTypeAnnotation(typeRef, typePath, desc, visible));
}
};
}
@Override
public MethodVisitor visitMethod(
int access, String name, String desc, String signature, String[] exceptions) {
return new MethodVisitor(
Opcodes.ASM5, super.visitMethod(access, name, desc, signature, exceptions)) {
@Override
public AnnotationVisitor visitAnnotationDefault() {
return new AnnotationAdapter(super.visitAnnotationDefault());
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
return new AnnotationAdapter(super.visitAnnotation(desc, visible));
}
@Override
public AnnotationVisitor visitTypeAnnotation(
int typeRef, TypePath typePath, String desc, boolean visible) {
return new AnnotationAdapter(
super.visitTypeAnnotation(typeRef, typePath, desc, visible));
}
@Override
public AnnotationVisitor visitParameterAnnotation(
int parameter, String desc, boolean visible) {
return new AnnotationAdapter(
super.visitParameterAnnotation(parameter, desc, visible));
}
@Override
public AnnotationVisitor visitInsnAnnotation(
int typeRef, TypePath typePath, String desc, boolean visible) {
return new AnnotationAdapter(
super.visitInsnAnnotation(typeRef, typePath, desc, visible));
}
@Override
public AnnotationVisitor visitTryCatchAnnotation(
int typeRef, TypePath typePath, String desc, boolean visible) {
return new AnnotationAdapter(
super.visitTryCatchAnnotation(typeRef, typePath, desc, visible));
}
@Override
public AnnotationVisitor visitLocalVariableAnnotation(
int typeRef,
TypePath typePath,
Label[] start,
Label[] end,
int[] index,
String desc,
boolean visible) {
return new AnnotationAdapter(
super.visitLocalVariableAnnotation(
typeRef, typePath, start, end, index, desc, visible));
}
};
}
},
0);
byte[] b = cw.toByteArray();
try {
LOADER.defineClass(n, b);
} catch (ClassFormatError cfe) {
fail(cfe.getMessage());
} catch (Throwable ignored) {
}
}
static class AnnotationAdapter extends AnnotationVisitor {
public AnnotationAdapter(final AnnotationVisitor av) {
super(Opcodes.ASM5, av);
}
@Override
public AnnotationVisitor visitAnnotation(String name, String desc) {
return new AnnotationAdapter(super.visitAnnotation(name, desc));
}
@Override
public AnnotationVisitor visitArray(String name) {
return new AnnotationAdapter(super.visitArray(name));
}
}
// ------------------------------------------------------------------------
static class TestClassLoader extends ClassLoader {
public Class<?> defineClass(final String name, final byte[] b) {
return defineClass(name, b, 0, b.length);
}
}
}
// ASM: a very small and fast Java bytecode manipulation framework
// Copyright (c) 2000-2011 INRIA, France Telecom
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. Neither the name of the copyright holders nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
package org.objectweb.asm;
import java.util.Collection;
import org.junit.Test;
import org.junit.runners.Parameterized.Parameters;
import org.objectweb.asm.test.AsmTest;
/**
* ClassVisitor tests. Also tests FieldVisitor, MethodVisitor, ModuleVisitor and AnnotationVisitor.
*
* @author Eric Bruneton
*/
public class ClassVisitorTest extends AsmTest {
/** @return test parameters to test all the precompiled classes with all the apis. */
@Parameters(name = NAME)
public static Collection<Object[]> data() {
return data(Api.ASM4, Api.ASM5, Api.ASM6);
}
/**
* Tests that classes are unchanged when transformed with a ClassReader -> class adapter ->
* ClassWriter chain, where "class adapter" is a ClassVisitor which returns FieldVisitor,
* MethodVisitor, ModuleVisitor and AnnotationVisitor instances.
*/
@Test
public void testReadAndWriteWithEmptyVisitor() {
byte[] classFile = classParameter.getBytes();
ClassReader classReader = new ClassReader(classFile);
ClassWriter classWriter = new ClassWriter(0);
ClassAdapter classAdapter = new ClassAdapter(apiParameter.value(), classWriter);
if (classParameter.isMoreRecentThan(apiParameter)) {
thrown.expect(RuntimeException.class);
}
classReader.accept(classAdapter, attributes(), 0);
assertThatClass(classWriter.toByteArray()).isEqualTo(classFile);
}
/**
* Tests that a ClassReader -> class adapter -> ClassWriter chain give the same result with or
* without the copy pool option.
*/
@Test
public void testReadAndWriteWithCopyPoolAndExceptionAdapter() {
byte[] classFile = classParameter.getBytes();
ClassReader classReader = new ClassReader(classFile);
ClassWriter classWriter = new ClassWriter(0);
ClassWriter classWriterWithCopyPool = new ClassWriter(classReader, 0);
classReader.accept(new ChangeExceptionAdapter(classWriter), attributes(), 0);
classReader.accept(new ChangeExceptionAdapter(classWriterWithCopyPool), attributes(), 0);
assertThatClass(classWriterWithCopyPool.toByteArray()).isEqualTo(classWriter.toByteArray());
}
static Attribute[] attributes() {
return new Attribute[] {new Comment(), new CodeComment()};
}
private static class AnnotationAdapter extends AnnotationVisitor {
AnnotationAdapter(int api, AnnotationVisitor av) {
super(api, av);
}
@Override
public AnnotationVisitor visitAnnotation(String name, String desc) {
return new AnnotationAdapter(api, super.visitAnnotation(name, desc));
}
@Override
public AnnotationVisitor visitArray(String name) {
return new AnnotationAdapter(api, super.visitArray(name));
}
}
private static class ClassAdapter extends ClassVisitor {
ClassAdapter(int api, ClassVisitor cv) {
super(api, cv);
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
return new AnnotationAdapter(api, super.visitAnnotation(desc, visible));
}
@Override
public AnnotationVisitor visitTypeAnnotation(
int typeRef, TypePath typePath, String desc, boolean visible) {
return new AnnotationAdapter(
api, super.visitTypeAnnotation(typeRef, typePath, desc, visible));
}
@Override
public FieldVisitor visitField(
int access, String name, String desc, String signature, Object value) {
return new FieldAdapter(api, super.visitField(access, name, desc, signature, value));
}
@Override
public MethodVisitor visitMethod(
int access, String name, String desc, String signature, String[] exceptions) {
return new MethodAdapter(api, super.visitMethod(access, name, desc, signature, exceptions));
}
@Override
public ModuleVisitor visitModule(String name, int access, String version) {
return new ModuleVisitor(api, super.visitModule(name, access, version)) {};
}
};
private static class FieldAdapter extends FieldVisitor {
FieldAdapter(int api, FieldVisitor fv) {
super(api, fv);
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
return new AnnotationAdapter(api, super.visitAnnotation(desc, visible));
}
@Override
public AnnotationVisitor visitTypeAnnotation(
int typeRef, TypePath typePath, String desc, boolean visible) {
return new AnnotationAdapter(
api, super.visitTypeAnnotation(typeRef, typePath, desc, visible));
}
}
private static class MethodAdapter extends MethodVisitor {
MethodAdapter(int api, MethodVisitor mv) {
super(api, mv);
}
@Override
public AnnotationVisitor visitAnnotationDefault() {
return new AnnotationAdapter(api, super.visitAnnotationDefault());
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
return new AnnotationAdapter(api, super.visitAnnotation(desc, visible));
}
@Override
public AnnotationVisitor visitTypeAnnotation(
int typeRef, TypePath typePath, String desc, boolean visible) {
return new AnnotationAdapter(
api, super.visitTypeAnnotation(typeRef, typePath, desc, visible));
}
@Override
public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) {
return new AnnotationAdapter(api, super.visitParameterAnnotation(parameter, desc, visible));
}
@Override
public AnnotationVisitor visitInsnAnnotation(
int typeRef, TypePath typePath, String desc, boolean visible) {
return new AnnotationAdapter(
api, super.visitInsnAnnotation(typeRef, typePath, desc, visible));
}
@Override
public AnnotationVisitor visitTryCatchAnnotation(
int typeRef, TypePath typePath, String desc, boolean visible) {
return new AnnotationAdapter(
api, super.visitTryCatchAnnotation(typeRef, typePath, desc, visible));
}
@Override
public AnnotationVisitor visitLocalVariableAnnotation(
int typeRef,
TypePath typePath,
Label[] start,
Label[] end,
int[] index,
String desc,
boolean visible) {
return new AnnotationAdapter(
api,
super.visitLocalVariableAnnotation(typeRef, typePath, start, end, index, desc, visible));
}
}
private static class ChangeExceptionAdapter extends ClassVisitor {
ChangeExceptionAdapter(final ClassVisitor cv) {
super(Opcodes.ASM6, cv);
}
@Override
public MethodVisitor visitMethod(
final int access,
final String name,
final String desc,
final String signature,
final String[] exceptions) {
if (exceptions != null && exceptions.length > 0) {
exceptions[0] = "java/lang/Throwable";
}
return super.visitMethod(access, name, desc, signature, exceptions);
}
}
}
......@@ -25,17 +25,60 @@
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
package org.objectweb.asm;
import junit.framework.TestCase;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ByteVector;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
/**
* A non standard code attribute used for testing purposes.
*
* @author Eric Bruneton
*/
public class CodeComment extends Attribute {
public CodeComment() {
super("CodeComment");
}
@Override
public boolean isUnknown() {
return false;
}
@Override
public boolean isCodeAttribute() {
return true;
}
@Override
protected Attribute read(
final ClassReader cr,
final int off,
final int len,
final char[] buf,
final int codeOff,
final Label[] labels) {
public class TypePathUnitTest extends TestCase {
return new CodeComment();
}
@Override
protected ByteVector write(
final ClassWriter cw,
final byte[] code,
final int len,
final int maxStack,
final int maxLocals) {
return new ByteVector();
}
public void testToAndFromString() {
assertEquals("[.[*0;", TypePath.fromString("[.[*0").toString());
assertEquals("[*0;*[", TypePath.fromString("[*0;*[").toString());
assertEquals("10;", TypePath.fromString("10;").toString());
assertEquals("1;0;", TypePath.fromString("1;0;").toString());
@Override
protected Label[] getLabels() {
super.getLabels();
return new Label[] {new Label()};
}
}
......@@ -27,33 +27,47 @@
// THE POSSIBILITY OF SUCH DAMAGE.
package org.objectweb.asm;
import junit.framework.TestCase;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ByteVector;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
/**
* Label unit tests.
* A non standard attribute used for testing purposes.
*
* @author Eric Bruneton
*/
public class LabelUnitTest extends TestCase {
public class Comment extends Attribute {
public void testToString() {
new Label().toString();
public Comment() {
super("Comment");
}
public void testGetOffset() {
Label l = new Label();
ClassWriter cw = new ClassWriter(0);
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "m", "()V", null, null);
mv.visitCode();
mv.visitLabel(l);
assertEquals(0, l.getOffset());
@Override
public boolean isUnknown() {
return false;
}
public void testIllegalGetOffsetState() {
try {
new Label().getOffset();
fail();
} catch (RuntimeException e) {
}
@Override
protected Attribute read(
final ClassReader cr,
final int off,
final int len,
final char[] buf,
final int codeOff,
final Label[] labels) {
return new Comment();
}
@Override
protected ByteVector write(
final ClassWriter cw,
final byte[] code,
final int len,
final int maxStack,
final int maxLocals) {
return new ByteVector();
}
}
......@@ -25,56 +25,48 @@
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
package org.objectweb.asm.signature;
package org.objectweb.asm;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.objectweb.asm.util.TraceSignatureVisitorUnitTest;
import org.objectweb.asm.util.TraceSignatureVisitorUnitTest.TestData;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
/**
* Signature tests.
* Label tests.
*
* @author Eugene Kuleshov
* @author Eric Bruneton
*/
public class SignatureUnitTest extends TestCase {
public class LabelTest {
public static TestSuite suite() {
TestSuite suite = new TestSuite(SignatureUnitTest.class.getName());
for (int i = 0; i < TraceSignatureVisitorUnitTest.DATA.length; i++) {
suite.addTest(new SignatureUnitTest(new TestData(TraceSignatureVisitorUnitTest.DATA[i])));
}
return suite;
}
private TestData data;
/** Rule that can be used in tests that expect some exceptions to be thrown. */
@Rule public ExpectedException thrown = ExpectedException.none();
private SignatureUnitTest(final TestData data) {
super("testSignature");
this.data = data;
/** Tests that {@link Label.toString()} returns strings starting with "L". */
@Test
public void testToString() {
assertTrue(new Label().toString().startsWith("L"));
}
public void testSignature() {
SignatureWriter wrt = new SignatureWriter();
SignatureReader rdr = new SignatureReader(data.signature);
switch (data.type) {
case 'C':
case 'M':
rdr.accept(wrt);
break;
case 'F':
rdr.acceptType(wrt);
break;
default:
return;
}
assertEquals(data.signature, wrt.toString());
/** Tests that {@link Label.getOffset()} returns a correct offset after the label is visited. */
@Test
public void testGetOffset() {
Label label = new Label();
ClassWriter classWriter = new ClassWriter(0);
MethodVisitor methodVisitor =
classWriter.visitMethod(Opcodes.ACC_PUBLIC, "m", "()V", null, null);
methodVisitor.visitCode();
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
methodVisitor.visitLabel(label);
assertEquals(1, label.getOffset());
}
@Override
public String getName() {
return super.getName() + " " + data.signature;