From 546fab644b6653d37c232846c4f8d5a4cfa80832 Mon Sep 17 00:00:00 2001 From: Eric Bruneton Date: Mon, 2 Oct 2017 15:37:57 +0200 Subject: [PATCH] Refactor SAXAdapterTest to use AsmTest. Also fix bugs found with these new tests (some of them were also detected by the previous tests). --- .../objectweb/asm/xml/ASMContentHandler.java | 8 +- .../objectweb/asm/xml/SAXModuleAdapter.java | 2 +- test/build.xml | 1 - .../org/objectweb/asm/xml/SAXAdapterTest.java | 93 +++++++------------ test/conform/saxadapter.xml | 50 ---------- test/conform/unit.xml | 1 + 6 files changed, 42 insertions(+), 113 deletions(-) delete mode 100644 test/conform/saxadapter.xml diff --git a/src/org/objectweb/asm/xml/ASMContentHandler.java b/src/org/objectweb/asm/xml/ASMContentHandler.java index 14b1300ab..35aab4280 100644 --- a/src/org/objectweb/asm/xml/ASMContentHandler.java +++ b/src/org/objectweb/asm/xml/ASMContentHandler.java @@ -84,13 +84,15 @@ public class ASMContentHandler extends DefaultHandler implements Opcodes { ModuleRule moduleRule = new ModuleRule(); RULES.add(BASE + "/module", moduleRule); RULES.add(BASE + "/module/main-class", moduleRule); - RULES.add(BASE + "/module/target", moduleRule); RULES.add(BASE + "/module/packages", moduleRule); RULES.add(BASE + "/module/requires", moduleRule); RULES.add(BASE + "/module/exports", moduleRule); RULES.add(BASE + "/module/exports/to", moduleRule); + RULES.add(BASE + "/module/opens", moduleRule); + RULES.add(BASE + "/module/opens/to", moduleRule); RULES.add(BASE + "/module/uses", moduleRule); RULES.add(BASE + "/module/provides", moduleRule); + RULES.add(BASE + "/module/provides/with", moduleRule); RULES.add(BASE + "/field", new FieldRule()); @@ -532,7 +534,7 @@ public class ASMContentHandler extends DefaultHandler implements Opcodes { int itfIndex = val.indexOf(' ', tagIndex + 1); boolean itf = itfIndex != -1; - int tag = Integer.parseInt(val.substring(tagIndex + 1, itf ? val.length() - 1 : itfIndex)); + int tag = Integer.parseInt(val.substring(tagIndex + 1, itf ? itfIndex : val.length() - 1)); String owner = val.substring(0, dotIndex); String name = val.substring(dotIndex + 1, descIndex); String desc = val.substring(descIndex, tagIndex - 1); @@ -775,7 +777,7 @@ public class ASMContentHandler extends DefaultHandler implements Opcodes { boolean exports = "exports".equals(element); boolean opens = "opens".equals(element); boolean provides = "provides".equals(element); - if (exports | opens | provides) { + if (exports || opens || provides) { @SuppressWarnings("unchecked") ArrayList list = (ArrayList) pop(); int access = (Integer) pop(); diff --git a/src/org/objectweb/asm/xml/SAXModuleAdapter.java b/src/org/objectweb/asm/xml/SAXModuleAdapter.java index 67f7498a5..4de1d055d 100644 --- a/src/org/objectweb/asm/xml/SAXModuleAdapter.java +++ b/src/org/objectweb/asm/xml/SAXModuleAdapter.java @@ -67,7 +67,7 @@ public final class SAXModuleAdapter extends ModuleVisitor { att.addAttribute("", "module", "module", "", module); att.addAttribute("", "access", "access", "", sb.toString()); if (version != null) { - att.addAttribute("", "access", "access", "", version); + att.addAttribute("", "version", "version", "", version); } sa.addElement("requires", att); } diff --git a/test/build.xml b/test/build.xml index dc98a258d..621787e13 100644 --- a/test/build.xml +++ b/test/build.xml @@ -176,7 +176,6 @@ - diff --git a/test/conform/org/objectweb/asm/xml/SAXAdapterTest.java b/test/conform/org/objectweb/asm/xml/SAXAdapterTest.java index 8713fee4d..416bb2f3a 100644 --- a/test/conform/org/objectweb/asm/xml/SAXAdapterTest.java +++ b/test/conform/org/objectweb/asm/xml/SAXAdapterTest.java @@ -27,78 +27,55 @@ // THE POSSIBILITY OF SUCH DAMAGE. package org.objectweb.asm.xml; -import java.io.ByteArrayOutputStream; +import java.util.Collection; +import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerFactory; +import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.sax.SAXResult; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.TransformerHandler; - -import junit.framework.TestSuite; - -import org.objectweb.asm.AbstractTest; -import org.objectweb.asm.Attribute; +import org.junit.Test; +import org.junit.runners.Parameterized.Parameters; import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassWriter; -import org.objectweb.asm.Label; +import org.objectweb.asm.test.AsmTest; +import org.xml.sax.SAXException; /** - * SAXAdapter tests + * SAXAdapter tests. * * @author Eugene Kuleshov */ -public class SAXAdapterTest extends AbstractTest { +public class SAXAdapterTest extends AsmTest { - public static TestSuite suite() throws Exception { - return new SAXAdapterTest().getSuite(); + /** @return test parameters to test all the precompiled classes with ASM6. */ + @Parameters(name = NAME) + public static Collection data() { + return data(Api.ASM6); } - @Override - public void test() throws Exception { - ClassReader cr = new ClassReader(is); - ClassWriter cw = new ClassWriter(0); - - SAXTransformerFactory saxtf = (SAXTransformerFactory) TransformerFactory.newInstance(); - TransformerHandler handler = saxtf.newTransformerHandler(); - handler.setResult(new SAXResult(new ASMContentHandler(cw))); - handler.startDocument(); - cr.accept(new SAXClassAdapter(handler, false), 0); - handler.endDocument(); - - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - bos.write(cw.toByteArray()); - - ClassWriter cw2 = new ClassWriter(0); - cr.accept( - cw2, - new Attribute[] { - new Attribute("Comment") { - @Override - protected Attribute read( - final ClassReader cr, - final int off, - final int len, - final char[] buf, - final int codeOff, - final Label[] labels) { - return null; // skip these attributes - } - }, - new Attribute("CodeComment") { - @Override - protected Attribute read( - final ClassReader cr, - final int off, - final int len, - final char[] buf, - final int codeOff, - final Label[] labels) { - return null; // skip these attributes - } - } - }, - 0); - - assertEquals(new ClassReader(cw2.toByteArray()), new ClassReader(bos.toByteArray())); + /** + * Tests that classes are unchanged with a ClassReader->SAXClassAdapter->ClassWriter transform. + * + * @throws TransformerFactoryConfigurationError + * @throws TransformerConfigurationException + * @throws SAXException + */ + @Test + public void testSAXAdapter_classUnchanged() + throws TransformerConfigurationException, TransformerFactoryConfigurationError, SAXException { + // Non standard attributes are not supported by the XML API. + if (classParameter == PrecompiledClass.JDK3_ATTRIBUTE) return; + byte[] classFile = classParameter.getBytes(); + ClassReader classReader = new ClassReader(classFile); + ClassWriter classWriter = new ClassWriter(0); + TransformerHandler transformerHandler = + ((SAXTransformerFactory) TransformerFactory.newInstance()).newTransformerHandler(); + transformerHandler.setResult(new SAXResult(new ASMContentHandler(classWriter))); + transformerHandler.startDocument(); + classReader.accept(new SAXClassAdapter(transformerHandler, false), 0); + transformerHandler.endDocument(); + assertThatClass(classWriter.toByteArray()).isEqualTo(classFile); } } diff --git a/test/conform/saxadapter.xml b/test/conform/saxadapter.xml deleted file mode 100644 index add68ce38..000000000 --- a/test/conform/saxadapter.xml +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - - diff --git a/test/conform/unit.xml b/test/conform/unit.xml index c3c49352b..06af97053 100644 --- a/test/conform/unit.xml +++ b/test/conform/unit.xml @@ -48,6 +48,7 @@ + -- GitLab