diff --git a/src/test/java/org/apache/harmony/pack200/tests/PackingOptionsTest.java b/src/test/java/org/apache/harmony/pack200/tests/PackingOptionsTest.java index 035d146..9bb1ca0 100644 --- a/src/test/java/org/apache/harmony/pack200/tests/PackingOptionsTest.java +++ b/src/test/java/org/apache/harmony/pack200/tests/PackingOptionsTest.java @@ -1,658 +1,658 @@ -/* - * 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.harmony.pack200.tests; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.net.URISyntaxException; -import java.util.Enumeration; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; -import java.util.jar.JarOutputStream; - -import junit.framework.TestCase; - -import org.apache.harmony.pack200.Archive; -import org.apache.harmony.pack200.Pack200Exception; -import org.apache.harmony.pack200.PackingOptions; - -/** - * Test different options for packing a Jar file - */ -public class PackingOptionsTest extends TestCase { - - JarFile in; - OutputStream out; - File file; - - public void testKeepFileOrder() throws Exception { - // Test default first - PackingOptions options = new PackingOptions(); - assertTrue(options.isKeepFileOrder()); - options.setKeepFileOrder(false); - assertFalse(options.isKeepFileOrder()); - - // Test option works correctly. Test 'True'. - in = new JarFile(new File(Archive.class.getResource( - "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI())); - file = File.createTempFile("sql", ".pack"); - file.deleteOnExit(); - out = new FileOutputStream(file); - options = new PackingOptions(); - options.setGzip(false); - Archive archive = new Archive(in, out, options); - archive.pack(); - in.close(); - out.close(); - - InputStream in2 = new FileInputStream(file); - File file2 = File.createTempFile("sql", ".jar"); - file2.deleteOnExit(); - JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file2)); - org.apache.harmony.unpack200.Archive u2archive = new org.apache.harmony.unpack200.Archive( - in2, out2); - u2archive.unpack(); - - File compareFile = new File(Archive.class.getResource( - "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI()); - JarFile jarFile = new JarFile(file2); - - JarFile jarFile2 = new JarFile(compareFile); - - // Check that both jars have the same entries in the same order - Enumeration entries = jarFile.entries(); - Enumeration entries2 = jarFile2.entries(); - while (entries.hasMoreElements()) { - - JarEntry entry = (JarEntry) entries.nextElement(); - assertNotNull(entry); - JarEntry entry2 = (JarEntry) entries2.nextElement(); - String name = entry.getName(); - String name2 = entry2.getName(); - assertEquals(name, name2); - } - - // Test 'false' - in = new JarFile(new File(Archive.class.getResource( - "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI())); - file = File.createTempFile("sql", ".pack"); - file.deleteOnExit(); - out = new FileOutputStream(file); - options = new PackingOptions(); - options.setKeepFileOrder(false); - options.setGzip(false); - archive = new Archive(in, out, options); - archive.pack(); - in.close(); - out.close(); - - in2 = new FileInputStream(file); - file2 = File.createTempFile("sql", ".jar"); - file2.deleteOnExit(); - out2 = new JarOutputStream(new FileOutputStream(file2)); - u2archive = new org.apache.harmony.unpack200.Archive(in2, out2); - u2archive.unpack(); - - compareFile = new File(Archive.class.getResource( - "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI()); - jarFile = new JarFile(file2); - - jarFile2 = new JarFile(compareFile); - // Check that both jars have the same entries (may be in a different - // order) - compareJarEntries(jarFile, jarFile2); - - // Check files are not in order this time - entries = jarFile.entries(); - entries2 = jarFile2.entries(); - boolean inOrder = true; - while (entries.hasMoreElements()) { - JarEntry entry = (JarEntry) entries.nextElement(); - assertNotNull(entry); - JarEntry entry2 = (JarEntry) entries2.nextElement(); - String name = entry.getName(); - String name2 = entry2.getName(); - if (!name.equals(name2)) { - inOrder = false; - break; - } - } - assertFalse("Files are not expected to be in order", inOrder); - } - - public void testDeflateHint() { - // Test default first - PackingOptions options = new PackingOptions(); - assertEquals("keep", options.getDeflateHint()); - options.setDeflateHint("true"); - assertEquals("true", options.getDeflateHint()); - options.setDeflateHint("false"); - assertEquals("false", options.getDeflateHint()); - try { - options.setDeflateHint("hello"); - fail("Should throw IllegalArgumentException for incorrect deflate hint"); - } catch (IllegalArgumentException iae) { - // pass - } - } - - public void testModificationTime() throws Exception { - // Test default first - PackingOptions options = new PackingOptions(); - assertEquals("keep", options.getModificationTime()); - options.setModificationTime("latest"); - assertEquals("latest", options.getModificationTime()); - try { - options.setModificationTime("true"); - fail("Should throw IllegalArgumentException for incorrect mod time"); - } catch (IllegalArgumentException iae) { - // pass - } - - // Test option works correctly. Test 'keep'. - in = new JarFile(new File(Archive.class.getResource( - "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI())); - file = File.createTempFile("sql", ".pack"); - file.deleteOnExit(); - out = new FileOutputStream(file); - options = new PackingOptions(); - options.setGzip(false); - Archive archive = new Archive(in, out, options); - archive.pack(); - in.close(); - out.close(); - - InputStream in2 = new FileInputStream(file); - File file2 = File.createTempFile("sql", ".jar"); - file2.deleteOnExit(); - JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file2)); - org.apache.harmony.unpack200.Archive u2archive = new org.apache.harmony.unpack200.Archive( - in2, out2); - u2archive.unpack(); - - File compareFile = new File(Archive.class.getResource( - "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI()); - JarFile jarFile = new JarFile(file2); - - JarFile jarFile2 = new JarFile(compareFile); - - // Check that both jars have the same entries in the same order - Enumeration entries = jarFile.entries(); - Enumeration entries2 = jarFile2.entries(); - while (entries.hasMoreElements()) { - - JarEntry entry = (JarEntry) entries.nextElement(); - assertNotNull(entry); - JarEntry entry2 = (JarEntry) entries2.nextElement(); - String name = entry.getName(); - String name2 = entry2.getName(); - assertEquals(name, name2); - assertEquals(entry.getTime(), entry2.getTime()); - } - - // Test option works correctly. Test 'latest'. - in = new JarFile(new File(Archive.class.getResource( - "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI())); - file = File.createTempFile("sql", ".pack"); - file.deleteOnExit(); - out = new FileOutputStream(file); - options = new PackingOptions(); - options.setGzip(false); - options.setModificationTime("latest"); - archive = new Archive(in, out, options); - archive.pack(); - in.close(); - out.close(); - - in2 = new FileInputStream(file); - file2 = File.createTempFile("sql", ".jar"); - file2.deleteOnExit(); - out2 = new JarOutputStream(new FileOutputStream(file2)); - u2archive = new org.apache.harmony.unpack200.Archive(in2, out2); - u2archive.unpack(); - - compareFile = new File(Archive.class.getResource( - "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI()); - jarFile = new JarFile(file2); - - jarFile2 = new JarFile(compareFile); - - // Check that all modtimes are the same and some are not the same as the - // original - entries = jarFile.entries(); - entries2 = jarFile2.entries(); - long modtime = -1; - boolean sameAsOriginal = true; - while (entries.hasMoreElements()) { - JarEntry entry = (JarEntry) entries.nextElement(); - assertNotNull(entry); - JarEntry entry2 = (JarEntry) entries2.nextElement(); - String name = entry.getName(); - if (!name.startsWith("META-INF")) { - if (modtime == -1) { - modtime = entry.getTime(); - } else { - assertEquals(modtime, entry.getTime()); - } - } - if (entry2.getTime() != entry.getTime()) { - sameAsOriginal = false; - } - } - assertFalse("Some modtimes should have changed", sameAsOriginal); - } - - // Test verbose, quiet and log file options. - public void testLoggingOptions() throws Exception { - // Test defaults - PackingOptions options = new PackingOptions(); - assertFalse(options.isVerbose()); - assertNull(options.getLogFile()); - options.setVerbose(true); - assertTrue(options.isVerbose()); - options.setQuiet(true); - assertFalse(options.isVerbose()); - - File logFile = File.createTempFile("logfile", ".txt"); - logFile.deleteOnExit(); - options.setLogFile(logFile.getPath()); - assertEquals(logFile.getPath(), options.getLogFile()); - - in = new JarFile(new File(Archive.class.getResource( - "/org/apache/harmony/pack200/tests/hw.jar").toURI())); - file = File.createTempFile("helloworld", ".pack.gz"); - file.deleteOnExit(); - out = new FileOutputStream(file); - new Archive(in, out, options).pack(); - in.close(); - out.close(); - - // log file should be empty - FileReader reader = new FileReader(logFile); - assertFalse(reader.ready()); - reader.close(); - - options.setVerbose(true); - in = new JarFile(new File(Archive.class.getResource( - "/org/apache/harmony/pack200/tests/hw.jar").toURI())); - file = File.createTempFile("helloworld", ".pack.gz"); - file.deleteOnExit(); - out = new FileOutputStream(file); - new Archive(in, out, options).pack(); - in.close(); - out.close(); - - // log file should not be empty - reader = new FileReader(logFile); - assertTrue(reader.ready()); - reader.close(); - } - - public void testSegmentLimits() throws IOException, Pack200Exception, - URISyntaxException { - in = new JarFile(new File(Archive.class.getResource( - "/org/apache/harmony/pack200/tests/hw.jar").toURI())); - file = File.createTempFile("helloworld", ".pack.gz"); - file.deleteOnExit(); - out = new FileOutputStream(file); - PackingOptions options = new PackingOptions(); - options.setSegmentLimit(0); - Archive archive = new Archive(in, out, options); - archive.pack(); - in.close(); - out.close(); - - in = new JarFile(new File(Archive.class.getResource( - "/org/apache/harmony/pack200/tests/hw.jar").toURI())); - file = File.createTempFile("helloworld", ".pack.gz"); - file.deleteOnExit(); - out = new FileOutputStream(file); - options = new PackingOptions(); - options.setSegmentLimit(-1); - archive = new Archive(in, out, options); - archive.pack(); - in.close(); - out.close(); - - in = new JarFile(new File(Archive.class.getResource( - "/org/apache/harmony/pack200/tests/hw.jar").toURI())); - file = File.createTempFile("helloworld", ".pack.gz"); - file.deleteOnExit(); - out = new FileOutputStream(file); - options = new PackingOptions(); - options.setSegmentLimit(5000); - archive = new Archive(in, out, options); - archive.pack(); - in.close(); - out.close(); - } - - public void testStripDebug() throws IOException, Pack200Exception, - URISyntaxException { - in = new JarFile(new File(Archive.class.getResource( - "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI())); - file = File.createTempFile("sql", ".pack"); - file.deleteOnExit(); - out = new FileOutputStream(file); - PackingOptions options = new PackingOptions(); - options.setGzip(false); - options.setStripDebug(true); - Archive archive = new Archive(in, out, options); - archive.pack(); - in.close(); - out.close(); - - // now unpack - InputStream in2 = new FileInputStream(file); - File file2 = File.createTempFile("sqloutNoDebug", ".jar"); - file2.deleteOnExit(); - JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file2)); - org.apache.harmony.unpack200.Archive u2archive = new org.apache.harmony.unpack200.Archive( - in2, out2); - u2archive.unpack(); - - File compareFile = new File(Archive.class.getResource( - "/org/apache/harmony/pack200/tests/sqlUnpackedNoDebug.jar") - .toURI()); - JarFile jarFile = new JarFile(file2); - assertTrue(file2.length() < 250000); - - JarFile jarFile2 = new JarFile(compareFile); - - compareFiles(jarFile, jarFile2); - } - - public void testPassFiles() throws IOException, URISyntaxException, - Pack200Exception { - // Don't pass any - in = new JarFile(new File(Archive.class.getResource( - "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI())); - File file0 = File.createTempFile("sql", ".pack"); - file0.deleteOnExit(); - out = new FileOutputStream(file0); - PackingOptions options = new PackingOptions(); - options.setGzip(false); - Archive archive = new Archive(in, out, options); - archive.pack(); - in.close(); - out.close(); - - // Pass one file - in = new JarFile(new File(Archive.class.getResource( - "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI())); - file = File.createTempFile("sql", ".pack"); - file.deleteOnExit(); - out = new FileOutputStream(file); - options = new PackingOptions(); - options.setGzip(false); - options.addPassFile("bin/test/org/apache/harmony/sql/tests/java/sql/DatabaseMetaDataTest.class"); - assertTrue(options - .isPassFile("bin/test/org/apache/harmony/sql/tests/java/sql/DatabaseMetaDataTest.class")); - archive = new Archive(in, out, options); - archive.pack(); - in.close(); - out.close(); - - // Pass a whole directory - in = new JarFile(new File(Archive.class.getResource( - "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI())); - File file2 = File.createTempFile("sql", ".pack"); - file2.deleteOnExit(); - out = new FileOutputStream(file2); - options = new PackingOptions(); - options.setGzip(false); - options.addPassFile("bin/test/org/apache/harmony/sql/tests/java/sql"); - assertTrue(options - .isPassFile("bin/test/org/apache/harmony/sql/tests/java/sql/DatabaseMetaDataTest.class")); - assertFalse(options - .isPassFile("bin/test/org/apache/harmony/sql/tests/java/sqldata/SqlData.class")); - archive = new Archive(in, out, options); - archive.pack(); - in.close(); - out.close(); - - assertTrue("If files are passed then the pack file should be larger", - file.length() > file0.length()); - assertTrue( - "If more files are passed then the pack file should be larger", - file2.length() > file.length()); - - // now unpack - InputStream in2 = new FileInputStream(file); - File file3 = File.createTempFile("sql", ".jar"); - file3.deleteOnExit(); - JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file3)); - org.apache.harmony.unpack200.Archive u2archive = new org.apache.harmony.unpack200.Archive( - in2, out2); - u2archive.unpack(); - - File compareFile = new File(Archive.class.getResource( - "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI()); - JarFile jarFile = new JarFile(file3); - - JarFile jarFile2 = new JarFile(compareFile); - // Check that both jars have the same entries - compareJarEntries(jarFile, jarFile2); - - // now unpack the file with lots of passed files - InputStream in3 = new FileInputStream(file2); - File file4 = File.createTempFile("sql", ".jar"); - file4.deleteOnExit(); - JarOutputStream out3 = new JarOutputStream(new FileOutputStream(file4)); - u2archive = new org.apache.harmony.unpack200.Archive(in3, out3); - u2archive.unpack(); - jarFile = new JarFile(file4); - jarFile2 = new JarFile(compareFile); - compareJarEntries(jarFile, jarFile2); - } - - public void testNewAttributes() throws Exception { - in = new JarFile( - new File( - Archive.class - .getResource( - "/org/apache/harmony/pack200/tests/jndiWithUnknownAttributes.jar") - .toURI())); - file = File.createTempFile("unknown", ".pack"); - file.deleteOnExit(); - out = new FileOutputStream(file); - PackingOptions options = new PackingOptions(); - options.addClassAttributeAction("Pack200", "I"); - Archive ar = new Archive(in, out, options); - ar.pack(); - in.close(); - out.close(); - - // unpack and check this was done right - InputStream in2 = new FileInputStream(file); - File file2 = File.createTempFile("unknown", ".jar"); - file2.deleteOnExit(); - JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file2)); - org.apache.harmony.unpack200.Archive u2archive = new org.apache.harmony.unpack200.Archive( - in2, out2); - u2archive.unpack(); - - // compare with original - File compareFile = new File(Archive.class.getResource( - "/org/apache/harmony/pack200/tests/jndiWithUnknownAttributes.jar").toURI()); - JarFile jarFile = new JarFile(file2); - - JarFile jarFile2 = new JarFile(compareFile); - assertEquals(jarFile2.size(), jarFile.size()); - compareJarEntries(jarFile, jarFile2); -// compareFiles(jarFile, jarFile2); - } - - public void testErrorAttributes() throws Exception { - in = new JarFile( - new File( - Archive.class - .getResource( - "/org/apache/harmony/pack200/tests/jndiWithUnknownAttributes.jar") - .toURI())); - file = File.createTempFile("unknown", ".pack"); - file.deleteOnExit(); - out = new FileOutputStream(file); - PackingOptions options = new PackingOptions(); - options.addClassAttributeAction("Pack200", "error"); - Archive ar = new Archive(in, out, options); - try { - ar.pack(); - in.close(); - out.close(); - fail("fail"); - } catch (Error e) { - // pass - assertEquals("Attribute Pack200 was found", e.getMessage()); - } - } - - public void testPassAttributes() throws Exception { - in = new JarFile( - new File( - Archive.class - .getResource( - "/org/apache/harmony/pack200/tests/jndiWithUnknownAttributes.jar") - .toURI())); - file = File.createTempFile("unknown", ".pack"); - file.deleteOnExit(); - out = new FileOutputStream(file); - PackingOptions options = new PackingOptions(); - options.addClassAttributeAction("Pack200", "pass"); - Archive ar = new Archive(in, out, options); - ar.pack(); - in.close(); - out.close(); - - // now unpack - InputStream in2 = new FileInputStream(file); - File file2 = File.createTempFile("unknown", ".jar"); - file2.deleteOnExit(); - JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file2)); - org.apache.harmony.unpack200.Archive u2archive = new org.apache.harmony.unpack200.Archive( - in2, out2); - u2archive.unpack(); - - // compare with original - File compareFile = new File(Archive.class.getResource( - "/org/apache/harmony/pack200/tests/jndiWithUnknownAttributes.jar").toURI()); - JarFile jarFile = new JarFile(file2); - - JarFile jarFile2 = new JarFile(compareFile); - assertEquals(jarFile2.size(), jarFile.size()); - compareJarEntries(jarFile, jarFile2); - } - - public void testE0() throws Pack200Exception, IOException, - URISyntaxException { - File f1 = new File(Archive.class.getResource( - "/org/apache/harmony/pack200/tests/jndi.jar").toURI()); - in = new JarFile(f1); - file = File.createTempFile("jndiE0", ".pack"); - file.deleteOnExit(); - out = new FileOutputStream(file); - PackingOptions options = new PackingOptions(); - options.setGzip(false); - options.setEffort(0); - Archive archive = new Archive(in, out, options); - archive.pack(); - in.close(); - out.close(); - compareFiles(new JarFile(f1), new JarFile(file)); - - } - - // public void testE0again() throws IOException, Pack200Exception, - // URISyntaxException { - // JarInputStream inputStream = new - // JarInputStream(Archive.class.getResourceAsStream("/org/apache/harmony/pack200/tests/jndi.jar")); - // file = File.createTempFile("jndiE0", ".pack"); - // out = new FileOutputStream(file); - // Archive archive = new Archive(inputStream, out, false); - // archive.setEffort(0); - // archive.pack(); - // inputStream.close(); - // out.close(); - // in = new JarFile(new File(Archive.class.getResource( - // "/org/apache/harmony/pack200/tests/jndi.jar").toURI())); - // compareFiles(in, new JarFile(file)); - // } - - private void compareJarEntries(JarFile jarFile, JarFile jarFile2) - throws IOException { - Enumeration entries = jarFile.entries(); - while (entries.hasMoreElements()) { - - JarEntry entry = (JarEntry) entries.nextElement(); - assertNotNull(entry); - - String name = entry.getName(); - JarEntry entry2 = jarFile2.getJarEntry(name); - assertNotNull("Missing Entry: " + name, entry2); - } - } - - private void compareFiles(JarFile jarFile, JarFile jarFile2) - throws IOException { - Enumeration entries = jarFile.entries(); - while (entries.hasMoreElements()) { - - JarEntry entry = (JarEntry) entries.nextElement(); - assertNotNull(entry); - - String name = entry.getName(); - JarEntry entry2 = jarFile2.getJarEntry(name); - assertNotNull("Missing Entry: " + name, entry2); - // assertEquals(entry.getTime(), entry2.getTime()); - if (!name.equals("META-INF/MANIFEST.MF")) { // Manifests aren't - // necessarily - // byte-for-byte - // identical - - InputStream ours = jarFile.getInputStream(entry); - InputStream expected = jarFile2.getInputStream(entry2); - - BufferedReader reader1 = new BufferedReader( - new InputStreamReader(ours)); - BufferedReader reader2 = new BufferedReader( - new InputStreamReader(expected)); - String line1 = reader1.readLine(); - String line2 = reader2.readLine(); - int i = 1; - while (line1 != null || line2 != null) { - assertEquals("Unpacked files differ for " + name, line2, - line1); - line1 = reader1.readLine(); - line2 = reader2.readLine(); - i++; - } - reader1.close(); - reader2.close(); - } - } - jarFile.close(); - jarFile2.close(); - } - -} +/* + * 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.harmony.pack200.tests; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.URISyntaxException; +import java.util.Enumeration; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; +import java.util.jar.JarOutputStream; + +import junit.framework.TestCase; + +import org.apache.harmony.pack200.Archive; +import org.apache.harmony.pack200.Pack200Exception; +import org.apache.harmony.pack200.PackingOptions; + +/** + * Test different options for packing a Jar file + */ +public class PackingOptionsTest extends TestCase { + + JarFile in; + OutputStream out; + File file; + + public void testKeepFileOrder() throws Exception { + // Test default first + PackingOptions options = new PackingOptions(); + assertTrue(options.isKeepFileOrder()); + options.setKeepFileOrder(false); + assertFalse(options.isKeepFileOrder()); + + // Test option works correctly. Test 'True'. + in = new JarFile(new File(Archive.class.getResource( + "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI())); + file = File.createTempFile("sql", ".pack"); + file.deleteOnExit(); + out = new FileOutputStream(file); + options = new PackingOptions(); + options.setGzip(false); + Archive archive = new Archive(in, out, options); + archive.pack(); + in.close(); + out.close(); + + InputStream in2 = new FileInputStream(file); + File file2 = File.createTempFile("sql", ".jar"); + file2.deleteOnExit(); + JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file2)); + org.apache.harmony.unpack200.Archive u2archive = new org.apache.harmony.unpack200.Archive( + in2, out2); + u2archive.unpack(); + + File compareFile = new File(Archive.class.getResource( + "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI()); + JarFile jarFile = new JarFile(file2); + + JarFile jarFile2 = new JarFile(compareFile); + + // Check that both jars have the same entries in the same order + Enumeration entries = jarFile.entries(); + Enumeration entries2 = jarFile2.entries(); + while (entries.hasMoreElements()) { + + JarEntry entry = (JarEntry) entries.nextElement(); + assertNotNull(entry); + JarEntry entry2 = (JarEntry) entries2.nextElement(); + String name = entry.getName(); + String name2 = entry2.getName(); + assertEquals(name, name2); + } + + // Test 'false' + in = new JarFile(new File(Archive.class.getResource( + "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI())); + file = File.createTempFile("sql", ".pack"); + file.deleteOnExit(); + out = new FileOutputStream(file); + options = new PackingOptions(); + options.setKeepFileOrder(false); + options.setGzip(false); + archive = new Archive(in, out, options); + archive.pack(); + in.close(); + out.close(); + + in2 = new FileInputStream(file); + file2 = File.createTempFile("sql", ".jar"); + file2.deleteOnExit(); + out2 = new JarOutputStream(new FileOutputStream(file2)); + u2archive = new org.apache.harmony.unpack200.Archive(in2, out2); + u2archive.unpack(); + + compareFile = new File(Archive.class.getResource( + "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI()); + jarFile = new JarFile(file2); + + jarFile2 = new JarFile(compareFile); + // Check that both jars have the same entries (may be in a different + // order) + compareJarEntries(jarFile, jarFile2); + + // Check files are not in order this time + entries = jarFile.entries(); + entries2 = jarFile2.entries(); + boolean inOrder = true; + while (entries.hasMoreElements()) { + JarEntry entry = (JarEntry) entries.nextElement(); + assertNotNull(entry); + JarEntry entry2 = (JarEntry) entries2.nextElement(); + String name = entry.getName(); + String name2 = entry2.getName(); + if (!name.equals(name2)) { + inOrder = false; + break; + } + } + assertFalse("Files are not expected to be in order", inOrder); + } + + public void testDeflateHint() { + // Test default first + PackingOptions options = new PackingOptions(); + assertEquals("keep", options.getDeflateHint()); + options.setDeflateHint("true"); + assertEquals("true", options.getDeflateHint()); + options.setDeflateHint("false"); + assertEquals("false", options.getDeflateHint()); + try { + options.setDeflateHint("hello"); + fail("Should throw IllegalArgumentException for incorrect deflate hint"); + } catch (IllegalArgumentException iae) { + // pass + } + } + + public void testModificationTime() throws Exception { + // Test default first + PackingOptions options = new PackingOptions(); + assertEquals("keep", options.getModificationTime()); + options.setModificationTime("latest"); + assertEquals("latest", options.getModificationTime()); + try { + options.setModificationTime("true"); + fail("Should throw IllegalArgumentException for incorrect mod time"); + } catch (IllegalArgumentException iae) { + // pass + } + + // Test option works correctly. Test 'keep'. + in = new JarFile(new File(Archive.class.getResource( + "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI())); + file = File.createTempFile("sql", ".pack"); + file.deleteOnExit(); + out = new FileOutputStream(file); + options = new PackingOptions(); + options.setGzip(false); + Archive archive = new Archive(in, out, options); + archive.pack(); + in.close(); + out.close(); + + InputStream in2 = new FileInputStream(file); + File file2 = File.createTempFile("sql", ".jar"); + file2.deleteOnExit(); + JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file2)); + org.apache.harmony.unpack200.Archive u2archive = new org.apache.harmony.unpack200.Archive( + in2, out2); + u2archive.unpack(); + + File compareFile = new File(Archive.class.getResource( + "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI()); + JarFile jarFile = new JarFile(file2); + + JarFile jarFile2 = new JarFile(compareFile); + + // Check that both jars have the same entries in the same order + Enumeration entries = jarFile.entries(); + Enumeration entries2 = jarFile2.entries(); + while (entries.hasMoreElements()) { + + JarEntry entry = (JarEntry) entries.nextElement(); + assertNotNull(entry); + JarEntry entry2 = (JarEntry) entries2.nextElement(); + String name = entry.getName(); + String name2 = entry2.getName(); + assertEquals(name, name2); + assertEquals(entry.getTime(), entry2.getTime()); + } + + // Test option works correctly. Test 'latest'. + in = new JarFile(new File(Archive.class.getResource( + "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI())); + file = File.createTempFile("sql", ".pack"); + file.deleteOnExit(); + out = new FileOutputStream(file); + options = new PackingOptions(); + options.setGzip(false); + options.setModificationTime("latest"); + archive = new Archive(in, out, options); + archive.pack(); + in.close(); + out.close(); + + in2 = new FileInputStream(file); + file2 = File.createTempFile("sql", ".jar"); + file2.deleteOnExit(); + out2 = new JarOutputStream(new FileOutputStream(file2)); + u2archive = new org.apache.harmony.unpack200.Archive(in2, out2); + u2archive.unpack(); + + compareFile = new File(Archive.class.getResource( + "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI()); + jarFile = new JarFile(file2); + + jarFile2 = new JarFile(compareFile); + + // Check that all modtimes are the same and some are not the same as the + // original + entries = jarFile.entries(); + entries2 = jarFile2.entries(); + long modtime = -1; + boolean sameAsOriginal = true; + while (entries.hasMoreElements()) { + JarEntry entry = (JarEntry) entries.nextElement(); + assertNotNull(entry); + JarEntry entry2 = (JarEntry) entries2.nextElement(); + String name = entry.getName(); + if (!name.startsWith("META-INF")) { + if (modtime == -1) { + modtime = entry.getTime(); + } else { + assertEquals(modtime, entry.getTime()); + } + } + if (entry2.getTime() != entry.getTime()) { + sameAsOriginal = false; + } + } + assertFalse("Some modtimes should have changed", sameAsOriginal); + } + + // Test verbose, quiet and log file options. + public void testLoggingOptions() throws Exception { + // Test defaults + PackingOptions options = new PackingOptions(); + assertFalse(options.isVerbose()); + assertNull(options.getLogFile()); + options.setVerbose(true); + assertTrue(options.isVerbose()); + options.setQuiet(true); + assertFalse(options.isVerbose()); + + File logFile = File.createTempFile("logfile", ".txt"); + logFile.deleteOnExit(); + options.setLogFile(logFile.getPath()); + assertEquals(logFile.getPath(), options.getLogFile()); + + in = new JarFile(new File(Archive.class.getResource( + "/org/apache/harmony/pack200/tests/hw.jar").toURI())); + file = File.createTempFile("helloworld", ".pack.gz"); + file.deleteOnExit(); + out = new FileOutputStream(file); + new Archive(in, out, options).pack(); + in.close(); + out.close(); + + // log file should be empty + FileReader reader = new FileReader(logFile); + assertFalse(reader.ready()); + reader.close(); + + options.setVerbose(true); + in = new JarFile(new File(Archive.class.getResource( + "/org/apache/harmony/pack200/tests/hw.jar").toURI())); + file = File.createTempFile("helloworld", ".pack.gz"); + file.deleteOnExit(); + out = new FileOutputStream(file); + new Archive(in, out, options).pack(); + in.close(); + out.close(); + + // log file should not be empty + reader = new FileReader(logFile); + assertTrue(reader.ready()); + reader.close(); + } + + public void testSegmentLimits() throws IOException, Pack200Exception, + URISyntaxException { + in = new JarFile(new File(Archive.class.getResource( + "/org/apache/harmony/pack200/tests/hw.jar").toURI())); + file = File.createTempFile("helloworld", ".pack.gz"); + file.deleteOnExit(); + out = new FileOutputStream(file); + PackingOptions options = new PackingOptions(); + options.setSegmentLimit(0); + Archive archive = new Archive(in, out, options); + archive.pack(); + in.close(); + out.close(); + + in = new JarFile(new File(Archive.class.getResource( + "/org/apache/harmony/pack200/tests/hw.jar").toURI())); + file = File.createTempFile("helloworld", ".pack.gz"); + file.deleteOnExit(); + out = new FileOutputStream(file); + options = new PackingOptions(); + options.setSegmentLimit(-1); + archive = new Archive(in, out, options); + archive.pack(); + in.close(); + out.close(); + + in = new JarFile(new File(Archive.class.getResource( + "/org/apache/harmony/pack200/tests/hw.jar").toURI())); + file = File.createTempFile("helloworld", ".pack.gz"); + file.deleteOnExit(); + out = new FileOutputStream(file); + options = new PackingOptions(); + options.setSegmentLimit(5000); + archive = new Archive(in, out, options); + archive.pack(); + in.close(); + out.close(); + } + + public void testStripDebug() throws IOException, Pack200Exception, + URISyntaxException { + in = new JarFile(new File(Archive.class.getResource( + "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI())); + file = File.createTempFile("sql", ".pack"); + file.deleteOnExit(); + out = new FileOutputStream(file); + PackingOptions options = new PackingOptions(); + options.setGzip(false); + options.setStripDebug(true); + Archive archive = new Archive(in, out, options); + archive.pack(); + in.close(); + out.close(); + + // now unpack + InputStream in2 = new FileInputStream(file); + File file2 = File.createTempFile("sqloutNoDebug", ".jar"); + file2.deleteOnExit(); + JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file2)); + org.apache.harmony.unpack200.Archive u2archive = new org.apache.harmony.unpack200.Archive( + in2, out2); + u2archive.unpack(); + + File compareFile = new File(Archive.class.getResource( + "/org/apache/harmony/pack200/tests/sqlUnpackedNoDebug.jar") + .toURI()); + JarFile jarFile = new JarFile(file2); + assertTrue(file2.length() < 250000); + + JarFile jarFile2 = new JarFile(compareFile); + + compareFiles(jarFile, jarFile2); + } + + public void testPassFiles() throws IOException, URISyntaxException, + Pack200Exception { + // Don't pass any + in = new JarFile(new File(Archive.class.getResource( + "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI())); + File file0 = File.createTempFile("sql", ".pack"); + file0.deleteOnExit(); + out = new FileOutputStream(file0); + PackingOptions options = new PackingOptions(); + options.setGzip(false); + Archive archive = new Archive(in, out, options); + archive.pack(); + in.close(); + out.close(); + + // Pass one file + in = new JarFile(new File(Archive.class.getResource( + "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI())); + file = File.createTempFile("sql", ".pack"); + file.deleteOnExit(); + out = new FileOutputStream(file); + options = new PackingOptions(); + options.setGzip(false); + options.addPassFile("bin/test/org/apache/harmony/sql/tests/java/sql/DatabaseMetaDataTest.class"); + assertTrue(options + .isPassFile("bin/test/org/apache/harmony/sql/tests/java/sql/DatabaseMetaDataTest.class")); + archive = new Archive(in, out, options); + archive.pack(); + in.close(); + out.close(); + + // Pass a whole directory + in = new JarFile(new File(Archive.class.getResource( + "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI())); + File file2 = File.createTempFile("sql", ".pack"); + file2.deleteOnExit(); + out = new FileOutputStream(file2); + options = new PackingOptions(); + options.setGzip(false); + options.addPassFile("bin/test/org/apache/harmony/sql/tests/java/sql"); + assertTrue(options + .isPassFile("bin/test/org/apache/harmony/sql/tests/java/sql/DatabaseMetaDataTest.class")); + assertFalse(options + .isPassFile("bin/test/org/apache/harmony/sql/tests/java/sqldata/SqlData.class")); + archive = new Archive(in, out, options); + archive.pack(); + in.close(); + out.close(); + + assertTrue("If files are passed then the pack file should be larger", + file.length() > file0.length()); + assertTrue( + "If more files are passed then the pack file should be larger", + file2.length() > file.length()); + + // now unpack + InputStream in2 = new FileInputStream(file); + File file3 = File.createTempFile("sql", ".jar"); + file3.deleteOnExit(); + JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file3)); + org.apache.harmony.unpack200.Archive u2archive = new org.apache.harmony.unpack200.Archive( + in2, out2); + u2archive.unpack(); + + File compareFile = new File(Archive.class.getResource( + "/org/apache/harmony/pack200/tests/sqlUnpacked.jar").toURI()); + JarFile jarFile = new JarFile(file3); + + JarFile jarFile2 = new JarFile(compareFile); + // Check that both jars have the same entries + compareJarEntries(jarFile, jarFile2); + + // now unpack the file with lots of passed files + InputStream in3 = new FileInputStream(file2); + File file4 = File.createTempFile("sql", ".jar"); + file4.deleteOnExit(); + JarOutputStream out3 = new JarOutputStream(new FileOutputStream(file4)); + u2archive = new org.apache.harmony.unpack200.Archive(in3, out3); + u2archive.unpack(); + jarFile = new JarFile(file4); + jarFile2 = new JarFile(compareFile); + compareJarEntries(jarFile, jarFile2); + } + + public void testNewAttributes() throws Exception { + in = new JarFile( + new File( + Archive.class + .getResource( + "/org/apache/harmony/pack200/tests/jndiWithUnknownAttributes.jar") + .toURI())); + file = File.createTempFile("unknown", ".pack"); + file.deleteOnExit(); + out = new FileOutputStream(file); + PackingOptions options = new PackingOptions(); + options.addClassAttributeAction("Pack200", "I"); + Archive ar = new Archive(in, out, options); + ar.pack(); + in.close(); + out.close(); + + // unpack and check this was done right + InputStream in2 = new FileInputStream(file); + File file2 = File.createTempFile("unknown", ".jar"); + file2.deleteOnExit(); + JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file2)); + org.apache.harmony.unpack200.Archive u2archive = new org.apache.harmony.unpack200.Archive( + in2, out2); + u2archive.unpack(); + + // compare with original + File compareFile = new File(Archive.class.getResource( + "/org/apache/harmony/pack200/tests/jndiWithUnknownAttributes.jar").toURI()); + JarFile jarFile = new JarFile(file2); + + JarFile jarFile2 = new JarFile(compareFile); + assertEquals(jarFile2.size(), jarFile.size()); + compareJarEntries(jarFile, jarFile2); +// compareFiles(jarFile, jarFile2); + } + + public void testErrorAttributes() throws Exception { + in = new JarFile( + new File( + Archive.class + .getResource( + "/org/apache/harmony/pack200/tests/jndiWithUnknownAttributes.jar") + .toURI())); + file = File.createTempFile("unknown", ".pack"); + file.deleteOnExit(); + out = new FileOutputStream(file); + PackingOptions options = new PackingOptions(); + options.addClassAttributeAction("Pack200", "error"); + Archive ar = new Archive(in, out, options); + try { + ar.pack(); + in.close(); + out.close(); + fail("fail"); + } catch (Error e) { + // pass + assertEquals("Attribute Pack200 was found", e.getMessage()); + } + } + + public void testPassAttributes() throws Exception { + in = new JarFile( + new File( + Archive.class + .getResource( + "/org/apache/harmony/pack200/tests/jndiWithUnknownAttributes.jar") + .toURI())); + file = File.createTempFile("unknown", ".pack"); + file.deleteOnExit(); + out = new FileOutputStream(file); + PackingOptions options = new PackingOptions(); + options.addClassAttributeAction("Pack200", "pass"); + Archive ar = new Archive(in, out, options); + ar.pack(); + in.close(); + out.close(); + + // now unpack + InputStream in2 = new FileInputStream(file); + File file2 = File.createTempFile("unknown", ".jar"); + file2.deleteOnExit(); + JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file2)); + org.apache.harmony.unpack200.Archive u2archive = new org.apache.harmony.unpack200.Archive( + in2, out2); + u2archive.unpack(); + + // compare with original + File compareFile = new File(Archive.class.getResource( + "/org/apache/harmony/pack200/tests/jndiWithUnknownAttributes.jar").toURI()); + JarFile jarFile = new JarFile(file2); + + JarFile jarFile2 = new JarFile(compareFile); + assertEquals(jarFile2.size(), jarFile.size()); + compareJarEntries(jarFile, jarFile2); + } + + public void testE0() throws Pack200Exception, IOException, + URISyntaxException { + File f1 = new File(Archive.class.getResource( + "/org/apache/harmony/pack200/tests/jndi.jar").toURI()); + in = new JarFile(f1); + file = File.createTempFile("jndiE0", ".pack"); + file.deleteOnExit(); + out = new FileOutputStream(file); + PackingOptions options = new PackingOptions(); + options.setGzip(false); + options.setEffort(0); + Archive archive = new Archive(in, out, options); + archive.pack(); + in.close(); + out.close(); + compareFiles(new JarFile(f1), new JarFile(file)); + + } + + // public void testE0again() throws IOException, Pack200Exception, + // URISyntaxException { + // JarInputStream inputStream = new + // JarInputStream(Archive.class.getResourceAsStream("/org/apache/harmony/pack200/tests/jndi.jar")); + // file = File.createTempFile("jndiE0", ".pack"); + // out = new FileOutputStream(file); + // Archive archive = new Archive(inputStream, out, false); + // archive.setEffort(0); + // archive.pack(); + // inputStream.close(); + // out.close(); + // in = new JarFile(new File(Archive.class.getResource( + // "/org/apache/harmony/pack200/tests/jndi.jar").toURI())); + // compareFiles(in, new JarFile(file)); + // } + + private void compareJarEntries(JarFile jarFile, JarFile jarFile2) + throws IOException { + Enumeration entries = jarFile.entries(); + while (entries.hasMoreElements()) { + + JarEntry entry = (JarEntry) entries.nextElement(); + assertNotNull(entry); + + String name = entry.getName(); + JarEntry entry2 = jarFile2.getJarEntry(name); + assertNotNull("Missing Entry: " + name, entry2); + } + } + + private void compareFiles(JarFile jarFile, JarFile jarFile2) + throws IOException { + Enumeration entries = jarFile.entries(); + while (entries.hasMoreElements()) { + + JarEntry entry = (JarEntry) entries.nextElement(); + assertNotNull(entry); + + String name = entry.getName(); + JarEntry entry2 = jarFile2.getJarEntry(name); + assertNotNull("Missing Entry: " + name, entry2); + // assertEquals(entry.getTime(), entry2.getTime()); + if (!name.equals("META-INF/MANIFEST.MF")) { // Manifests aren't + // necessarily + // byte-for-byte + // identical + + InputStream ours = jarFile.getInputStream(entry); + InputStream expected = jarFile2.getInputStream(entry2); + + BufferedReader reader1 = new BufferedReader( + new InputStreamReader(ours)); + BufferedReader reader2 = new BufferedReader( + new InputStreamReader(expected)); + String line1 = reader1.readLine(); + String line2 = reader2.readLine(); + int i = 1; + while (line1 != null || line2 != null) { + assertEquals("Unpacked files differ for " + name, line2, + line1); + line1 = reader1.readLine(); + line2 = reader2.readLine(); + i++; + } + reader1.close(); + reader2.close(); + } + } + jarFile.close(); + jarFile2.close(); + } + +} diff --git a/src/test/java/org/apache/harmony/unpack200/tests/NewAttributeBandsTest.java b/src/test/java/org/apache/harmony/unpack200/tests/NewAttributeBandsTest.java index c121e94..7aebc4f 100644 --- a/src/test/java/org/apache/harmony/unpack200/tests/NewAttributeBandsTest.java +++ b/src/test/java/org/apache/harmony/unpack200/tests/NewAttributeBandsTest.java @@ -1,256 +1,256 @@ -/* - * 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.harmony.unpack200.tests; - -import java.io.IOException; -import java.util.List; - -import org.apache.harmony.pack200.Pack200Exception; -import org.apache.harmony.unpack200.AttributeLayout; -import org.apache.harmony.unpack200.NewAttributeBands; -import org.apache.harmony.unpack200.NewAttributeBands.Call; -import org.apache.harmony.unpack200.NewAttributeBands.Callable; -import org.apache.harmony.unpack200.NewAttributeBands.Integral; -import org.apache.harmony.unpack200.NewAttributeBands.Reference; -import org.apache.harmony.unpack200.NewAttributeBands.Replication; -import org.apache.harmony.unpack200.NewAttributeBands.Union; -import org.apache.harmony.unpack200.NewAttributeBands.UnionCase; -import org.apache.harmony.unpack200.Segment; - -/** - * Tests for unpack200 support for non-predefined attributes - */ -public class NewAttributeBandsTest extends AbstractBandsTestCase { - - public void testEmptyLayout() throws IOException, Pack200Exception { - MockNewAttributeBands newAttributeBands = new MockNewAttributeBands( - new MockSegment(), new AttributeLayout("test", - AttributeLayout.CONTEXT_CLASS, "", 25)); - List layoutElements = newAttributeBands.getLayoutElements(); - assertEquals(0, layoutElements.size()); - } - - public void testIntegralLayout() throws IOException, Pack200Exception { - tryIntegral("B"); - tryIntegral("FB"); - tryIntegral("SB"); - tryIntegral("H"); - tryIntegral("FH"); - tryIntegral("SH"); - tryIntegral("I"); - tryIntegral("FI"); - tryIntegral("SI"); - tryIntegral("PB"); - tryIntegral("OB"); - tryIntegral("OSB"); - tryIntegral("POB"); - tryIntegral("PH"); - tryIntegral("OH"); - tryIntegral("OSH"); - tryIntegral("POH"); - tryIntegral("PI"); - tryIntegral("OI"); - tryIntegral("OSI"); - tryIntegral("POI"); - } - - public void tryIntegral(String layout) throws IOException, Pack200Exception { - MockNewAttributeBands newAttributeBands = new MockNewAttributeBands( - new MockSegment(), new AttributeLayout("test", - AttributeLayout.CONTEXT_CLASS, layout, 25)); - List layoutElements = newAttributeBands.getLayoutElements(); - assertEquals(1, layoutElements.size()); - Integral element = (Integral) layoutElements.get(0); - assertEquals(layout, element.getTag()); - } - - public void testReplicationLayout() throws IOException, Pack200Exception { - MockNewAttributeBands newAttributeBands = new MockNewAttributeBands( - new MockSegment(), new AttributeLayout("test", - AttributeLayout.CONTEXT_CLASS, "NH[PHOHRUHRSHH]", 25)); - List layoutElements = newAttributeBands.getLayoutElements(); - assertEquals(1, layoutElements.size()); - Replication element = (Replication) layoutElements.get(0); - Integral countElement = element.getCountElement(); - assertEquals("H", countElement.getTag()); - List replicatedElements = element.getLayoutElements(); - assertEquals(5, replicatedElements.size()); - Integral firstElement = (Integral) replicatedElements.get(0); - assertEquals("PH", firstElement.getTag()); - Integral secondElement = (Integral) replicatedElements.get(1); - assertEquals("OH", secondElement.getTag()); - Reference thirdElement = (Reference) replicatedElements.get(2); - assertEquals("RUH", thirdElement.getTag()); - Reference fourthElement = (Reference) replicatedElements.get(3); - assertEquals("RSH", fourthElement.getTag()); - Integral fifthElement = (Integral) replicatedElements.get(4); - assertEquals("H", fifthElement.getTag()); - } - - public void testReferenceLayouts() throws IOException, Pack200Exception { - tryReference("KIB"); - tryReference("KIH"); - tryReference("KII"); - tryReference("KINH"); - tryReference("KJH"); - tryReference("KDH"); - tryReference("KSH"); - tryReference("KQH"); - tryReference("RCH"); - tryReference("RSH"); - tryReference("RDH"); - tryReference("RFH"); - tryReference("RMH"); - tryReference("RIH"); - tryReference("RUH"); - tryReference("RQH"); - tryReference("RQNH"); - tryReference("RQNI"); - } - - private void tryReference(String layout) throws IOException, - Pack200Exception { - MockNewAttributeBands newAttributeBands = new MockNewAttributeBands( - new MockSegment(), new AttributeLayout("test", - AttributeLayout.CONTEXT_CODE, layout, 26)); - List layoutElements = newAttributeBands.getLayoutElements(); - assertEquals(1, layoutElements.size()); - Reference element = (Reference) layoutElements.get(0); - assertEquals(layout, element.getTag()); - } - - public void testUnionLayout() throws IOException, Pack200Exception { - MockNewAttributeBands newAttributeBands = new MockNewAttributeBands( - new MockSegment(), new AttributeLayout("test", - AttributeLayout.CONTEXT_CODE, - "TB(55)[FH](23)[]()[RSH]", 26)); - List layoutElements = newAttributeBands.getLayoutElements(); - assertEquals(1, layoutElements.size()); - Union element = (Union) layoutElements.get(0); - Integral tag = element.getUnionTag(); - assertEquals("B", tag.getTag()); - List unionCases = element.getUnionCases(); - assertEquals(2, unionCases.size()); - UnionCase firstCase = (UnionCase) unionCases.get(0); - assertTrue(firstCase.hasTag(55)); - assertFalse(firstCase.hasTag(23)); - List body = firstCase.getBody(); - assertEquals(1, body.size()); - Integral bodyElement = (Integral) body.get(0); - assertEquals("FH", bodyElement.getTag()); - UnionCase secondCase = (UnionCase) unionCases.get(1); - assertTrue(secondCase.hasTag(23)); - assertFalse(secondCase.hasTag(55)); - body = secondCase.getBody(); - assertEquals(0, body.size()); - List defaultBody = element.getDefaultCaseBody(); - assertEquals(1, defaultBody.size()); - Reference ref = (Reference) defaultBody.get(0); - assertEquals("RSH", ref.getTag()); - } - - public void testLayoutWithCalls() throws IOException, Pack200Exception { - MockNewAttributeBands newAttributeBands = new MockNewAttributeBands( - new MockSegment(), - new AttributeLayout( - "test", - AttributeLayout.CONTEXT_FIELD, - "[NH[(1)]][RSH NH[RUH(1)]][TB(66,67,73,83,90)[KIH](68)[KDH](70)[KFH](74)[KJH](99)[RSH](101)[RSH RUH](115)[RUH](91)[NH[(0)]](64)[RSH[RUH(0)]]()[]]", - 26)); - List layoutElements = newAttributeBands.getLayoutElements(); - assertEquals(3, layoutElements.size()); - Callable firstCallable = (Callable) layoutElements.get(0); - Callable secondCallable = (Callable) layoutElements.get(1); - Callable thirdCallable = (Callable) layoutElements.get(2); - List firstBody = firstCallable.getBody(); - assertEquals(1, firstBody.size()); - Replication rep = (Replication) firstBody.get(0); - List repBody = rep.getLayoutElements(); - assertEquals(1, repBody.size()); - Call call = (Call) repBody.get(0); - assertEquals(1, call.getCallableIndex()); - assertEquals(secondCallable, call.getCallable()); - assertFalse(firstCallable.isBackwardsCallable()); - assertFalse(secondCallable.isBackwardsCallable()); - assertFalse(thirdCallable.isBackwardsCallable()); - } - - public void testLayoutWithBackwardsCall() throws IOException, - Pack200Exception { - MockNewAttributeBands newAttributeBands = new MockNewAttributeBands( - new MockSegment(), new AttributeLayout("test", - AttributeLayout.CONTEXT_METHOD, "[NH[(1)]][KIH][(-1)]", - 20)); - List layoutElements = newAttributeBands.getLayoutElements(); - assertEquals(3, layoutElements.size()); - Callable firstCallable = (Callable) layoutElements.get(0); - Callable secondCallable = (Callable) layoutElements.get(1); - Callable thirdCallable = (Callable) layoutElements.get(2); - List thirdBody = thirdCallable.getBody(); - assertEquals(1, thirdBody.size()); - Call call = (Call) thirdBody.get(0); - assertEquals(secondCallable, call.getCallable()); - assertTrue(secondCallable.isBackwardsCallable()); - assertFalse(firstCallable.isBackwardsCallable()); - assertFalse(thirdCallable.isBackwardsCallable()); - - newAttributeBands = new MockNewAttributeBands(new MockSegment(), - new AttributeLayout("test", AttributeLayout.CONTEXT_METHOD, - "[NH[(1)]][KIH][(-2)]", 20)); - layoutElements = newAttributeBands.getLayoutElements(); - assertEquals(3, layoutElements.size()); - firstCallable = (Callable) layoutElements.get(0); - secondCallable = (Callable) layoutElements.get(1); - thirdCallable = (Callable) layoutElements.get(2); - thirdBody = thirdCallable.getBody(); - assertEquals(1, thirdBody.size()); - call = (Call) thirdBody.get(0); - assertEquals(firstCallable, call.getCallable()); - assertTrue(firstCallable.isBackwardsCallable()); - assertFalse(secondCallable.isBackwardsCallable()); - assertFalse(thirdCallable.isBackwardsCallable()); - - newAttributeBands = new MockNewAttributeBands(new MockSegment(), - new AttributeLayout("test", AttributeLayout.CONTEXT_METHOD, - "[NH[(1)]][KIH][(0)]", 20)); - layoutElements = newAttributeBands.getLayoutElements(); - assertEquals(3, layoutElements.size()); - firstCallable = (Callable) layoutElements.get(0); - secondCallable = (Callable) layoutElements.get(1); - thirdCallable = (Callable) layoutElements.get(2); - thirdBody = thirdCallable.getBody(); - assertEquals(1, thirdBody.size()); - call = (Call) thirdBody.get(0); - assertEquals(thirdCallable, call.getCallable()); - assertTrue(thirdCallable.isBackwardsCallable()); - assertFalse(firstCallable.isBackwardsCallable()); - assertFalse(secondCallable.isBackwardsCallable()); - assertFalse(firstCallable.isBackwardsCallable()); - } - - private class MockNewAttributeBands extends NewAttributeBands { - - public MockNewAttributeBands(Segment segment, AttributeLayout layout) - throws IOException { - super(segment, layout); - } - - public List getLayoutElements() { - return attributeLayoutElements; - } - } -} +/* + * 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.harmony.unpack200.tests; + +import java.io.IOException; +import java.util.List; + +import org.apache.harmony.pack200.Pack200Exception; +import org.apache.harmony.unpack200.AttributeLayout; +import org.apache.harmony.unpack200.NewAttributeBands; +import org.apache.harmony.unpack200.NewAttributeBands.Call; +import org.apache.harmony.unpack200.NewAttributeBands.Callable; +import org.apache.harmony.unpack200.NewAttributeBands.Integral; +import org.apache.harmony.unpack200.NewAttributeBands.Reference; +import org.apache.harmony.unpack200.NewAttributeBands.Replication; +import org.apache.harmony.unpack200.NewAttributeBands.Union; +import org.apache.harmony.unpack200.NewAttributeBands.UnionCase; +import org.apache.harmony.unpack200.Segment; + +/** + * Tests for unpack200 support for non-predefined attributes + */ +public class NewAttributeBandsTest extends AbstractBandsTestCase { + + public void testEmptyLayout() throws IOException, Pack200Exception { + MockNewAttributeBands newAttributeBands = new MockNewAttributeBands( + new MockSegment(), new AttributeLayout("test", + AttributeLayout.CONTEXT_CLASS, "", 25)); + List layoutElements = newAttributeBands.getLayoutElements(); + assertEquals(0, layoutElements.size()); + } + + public void testIntegralLayout() throws IOException, Pack200Exception { + tryIntegral("B"); + tryIntegral("FB"); + tryIntegral("SB"); + tryIntegral("H"); + tryIntegral("FH"); + tryIntegral("SH"); + tryIntegral("I"); + tryIntegral("FI"); + tryIntegral("SI"); + tryIntegral("PB"); + tryIntegral("OB"); + tryIntegral("OSB"); + tryIntegral("POB"); + tryIntegral("PH"); + tryIntegral("OH"); + tryIntegral("OSH"); + tryIntegral("POH"); + tryIntegral("PI"); + tryIntegral("OI"); + tryIntegral("OSI"); + tryIntegral("POI"); + } + + public void tryIntegral(String layout) throws IOException, Pack200Exception { + MockNewAttributeBands newAttributeBands = new MockNewAttributeBands( + new MockSegment(), new AttributeLayout("test", + AttributeLayout.CONTEXT_CLASS, layout, 25)); + List layoutElements = newAttributeBands.getLayoutElements(); + assertEquals(1, layoutElements.size()); + Integral element = (Integral) layoutElements.get(0); + assertEquals(layout, element.getTag()); + } + + public void testReplicationLayout() throws IOException, Pack200Exception { + MockNewAttributeBands newAttributeBands = new MockNewAttributeBands( + new MockSegment(), new AttributeLayout("test", + AttributeLayout.CONTEXT_CLASS, "NH[PHOHRUHRSHH]", 25)); + List layoutElements = newAttributeBands.getLayoutElements(); + assertEquals(1, layoutElements.size()); + Replication element = (Replication) layoutElements.get(0); + Integral countElement = element.getCountElement(); + assertEquals("H", countElement.getTag()); + List replicatedElements = element.getLayoutElements(); + assertEquals(5, replicatedElements.size()); + Integral firstElement = (Integral) replicatedElements.get(0); + assertEquals("PH", firstElement.getTag()); + Integral secondElement = (Integral) replicatedElements.get(1); + assertEquals("OH", secondElement.getTag()); + Reference thirdElement = (Reference) replicatedElements.get(2); + assertEquals("RUH", thirdElement.getTag()); + Reference fourthElement = (Reference) replicatedElements.get(3); + assertEquals("RSH", fourthElement.getTag()); + Integral fifthElement = (Integral) replicatedElements.get(4); + assertEquals("H", fifthElement.getTag()); + } + + public void testReferenceLayouts() throws IOException, Pack200Exception { + tryReference("KIB"); + tryReference("KIH"); + tryReference("KII"); + tryReference("KINH"); + tryReference("KJH"); + tryReference("KDH"); + tryReference("KSH"); + tryReference("KQH"); + tryReference("RCH"); + tryReference("RSH"); + tryReference("RDH"); + tryReference("RFH"); + tryReference("RMH"); + tryReference("RIH"); + tryReference("RUH"); + tryReference("RQH"); + tryReference("RQNH"); + tryReference("RQNI"); + } + + private void tryReference(String layout) throws IOException, + Pack200Exception { + MockNewAttributeBands newAttributeBands = new MockNewAttributeBands( + new MockSegment(), new AttributeLayout("test", + AttributeLayout.CONTEXT_CODE, layout, 26)); + List layoutElements = newAttributeBands.getLayoutElements(); + assertEquals(1, layoutElements.size()); + Reference element = (Reference) layoutElements.get(0); + assertEquals(layout, element.getTag()); + } + + public void testUnionLayout() throws IOException, Pack200Exception { + MockNewAttributeBands newAttributeBands = new MockNewAttributeBands( + new MockSegment(), new AttributeLayout("test", + AttributeLayout.CONTEXT_CODE, + "TB(55)[FH](23)[]()[RSH]", 26)); + List layoutElements = newAttributeBands.getLayoutElements(); + assertEquals(1, layoutElements.size()); + Union element = (Union) layoutElements.get(0); + Integral tag = element.getUnionTag(); + assertEquals("B", tag.getTag()); + List unionCases = element.getUnionCases(); + assertEquals(2, unionCases.size()); + UnionCase firstCase = (UnionCase) unionCases.get(0); + assertTrue(firstCase.hasTag(55)); + assertFalse(firstCase.hasTag(23)); + List body = firstCase.getBody(); + assertEquals(1, body.size()); + Integral bodyElement = (Integral) body.get(0); + assertEquals("FH", bodyElement.getTag()); + UnionCase secondCase = (UnionCase) unionCases.get(1); + assertTrue(secondCase.hasTag(23)); + assertFalse(secondCase.hasTag(55)); + body = secondCase.getBody(); + assertEquals(0, body.size()); + List defaultBody = element.getDefaultCaseBody(); + assertEquals(1, defaultBody.size()); + Reference ref = (Reference) defaultBody.get(0); + assertEquals("RSH", ref.getTag()); + } + + public void testLayoutWithCalls() throws IOException, Pack200Exception { + MockNewAttributeBands newAttributeBands = new MockNewAttributeBands( + new MockSegment(), + new AttributeLayout( + "test", + AttributeLayout.CONTEXT_FIELD, + "[NH[(1)]][RSH NH[RUH(1)]][TB(66,67,73,83,90)[KIH](68)[KDH](70)[KFH](74)[KJH](99)[RSH](101)[RSH RUH](115)[RUH](91)[NH[(0)]](64)[RSH[RUH(0)]]()[]]", + 26)); + List layoutElements = newAttributeBands.getLayoutElements(); + assertEquals(3, layoutElements.size()); + Callable firstCallable = (Callable) layoutElements.get(0); + Callable secondCallable = (Callable) layoutElements.get(1); + Callable thirdCallable = (Callable) layoutElements.get(2); + List firstBody = firstCallable.getBody(); + assertEquals(1, firstBody.size()); + Replication rep = (Replication) firstBody.get(0); + List repBody = rep.getLayoutElements(); + assertEquals(1, repBody.size()); + Call call = (Call) repBody.get(0); + assertEquals(1, call.getCallableIndex()); + assertEquals(secondCallable, call.getCallable()); + assertFalse(firstCallable.isBackwardsCallable()); + assertFalse(secondCallable.isBackwardsCallable()); + assertFalse(thirdCallable.isBackwardsCallable()); + } + + public void testLayoutWithBackwardsCall() throws IOException, + Pack200Exception { + MockNewAttributeBands newAttributeBands = new MockNewAttributeBands( + new MockSegment(), new AttributeLayout("test", + AttributeLayout.CONTEXT_METHOD, "[NH[(1)]][KIH][(-1)]", + 20)); + List layoutElements = newAttributeBands.getLayoutElements(); + assertEquals(3, layoutElements.size()); + Callable firstCallable = (Callable) layoutElements.get(0); + Callable secondCallable = (Callable) layoutElements.get(1); + Callable thirdCallable = (Callable) layoutElements.get(2); + List thirdBody = thirdCallable.getBody(); + assertEquals(1, thirdBody.size()); + Call call = (Call) thirdBody.get(0); + assertEquals(secondCallable, call.getCallable()); + assertTrue(secondCallable.isBackwardsCallable()); + assertFalse(firstCallable.isBackwardsCallable()); + assertFalse(thirdCallable.isBackwardsCallable()); + + newAttributeBands = new MockNewAttributeBands(new MockSegment(), + new AttributeLayout("test", AttributeLayout.CONTEXT_METHOD, + "[NH[(1)]][KIH][(-2)]", 20)); + layoutElements = newAttributeBands.getLayoutElements(); + assertEquals(3, layoutElements.size()); + firstCallable = (Callable) layoutElements.get(0); + secondCallable = (Callable) layoutElements.get(1); + thirdCallable = (Callable) layoutElements.get(2); + thirdBody = thirdCallable.getBody(); + assertEquals(1, thirdBody.size()); + call = (Call) thirdBody.get(0); + assertEquals(firstCallable, call.getCallable()); + assertTrue(firstCallable.isBackwardsCallable()); + assertFalse(secondCallable.isBackwardsCallable()); + assertFalse(thirdCallable.isBackwardsCallable()); + + newAttributeBands = new MockNewAttributeBands(new MockSegment(), + new AttributeLayout("test", AttributeLayout.CONTEXT_METHOD, + "[NH[(1)]][KIH][(0)]", 20)); + layoutElements = newAttributeBands.getLayoutElements(); + assertEquals(3, layoutElements.size()); + firstCallable = (Callable) layoutElements.get(0); + secondCallable = (Callable) layoutElements.get(1); + thirdCallable = (Callable) layoutElements.get(2); + thirdBody = thirdCallable.getBody(); + assertEquals(1, thirdBody.size()); + call = (Call) thirdBody.get(0); + assertEquals(thirdCallable, call.getCallable()); + assertTrue(thirdCallable.isBackwardsCallable()); + assertFalse(firstCallable.isBackwardsCallable()); + assertFalse(secondCallable.isBackwardsCallable()); + assertFalse(firstCallable.isBackwardsCallable()); + } + + private class MockNewAttributeBands extends NewAttributeBands { + + public MockNewAttributeBands(Segment segment, AttributeLayout layout) + throws IOException { + super(segment, layout); + } + + public List getLayoutElements() { + return attributeLayoutElements; + } + } +}