001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.harmony.pack200.tests;
018:
019: import java.io.BufferedReader;
020: import java.io.File;
021: import java.io.FileOutputStream;
022: import java.io.InputStream;
023: import java.io.InputStreamReader;
024: import java.util.jar.JarEntry;
025: import java.util.jar.JarFile;
026: import java.util.jar.JarOutputStream;
027:
028: import junit.framework.TestCase;
029:
030: import org.apache.harmony.pack200.Segment;
031:
032: /**
033: * Tests for org.apache.harmony.pack200.Segment.
034: */
035: public class SegmentTest extends TestCase {
036:
037: InputStream in;
038: JarOutputStream out;
039:
040: public void testHelloWorld() throws Exception {
041: in = Segment.class
042: .getResourceAsStream("/org/apache/harmony/pack200/tests/HelloWorld.pack");
043: Segment segment = Segment.parse(in);
044: assertNotNull(segment);
045: File file = File.createTempFile("hello", "world.jar");
046: out = new JarOutputStream(new FileOutputStream(file));
047: segment.writeJar(out);
048: out.close();
049: out = null;
050: JarFile jarFile = new JarFile(file);
051: JarEntry entry = jarFile
052: .getJarEntry("org/apache/harmony/archive/tests/internal/pack200/HelloWorld.class");
053: assertNotNull(entry);
054: Process process = Runtime
055: .getRuntime()
056: .exec(
057: "java -cp "
058: + file.getName()
059: + " org.apache.harmony.archive.tests.internal.pack200.HelloWorld",
060: new String[] {}, file.getParentFile());
061: BufferedReader reader = new BufferedReader(
062: new InputStreamReader(process.getInputStream()));
063: String line = reader.readLine();
064: assertEquals(line, "Hello world");
065: }
066:
067: public void testJustResources() throws Exception {
068: in = Segment.class
069: .getResourceAsStream("/org/apache/harmony/pack200/tests/JustResources.pack");
070: Segment segment = Segment.parse(in);
071: assertNotNull(segment);
072: out = new JarOutputStream(new FileOutputStream(File
073: .createTempFile("just", "resources.jar")));
074: segment.writeJar(out);
075: }
076:
077: public void testInterfaceOnly() throws Exception {
078: in = Segment.class
079: .getResourceAsStream("/org/apache/harmony/pack200/tests/InterfaceOnly.pack");
080: Segment segment = Segment.parse(in);
081: assertNotNull(segment);
082: out = new JarOutputStream(new FileOutputStream(File
083: .createTempFile("Interface", "Only.jar")));
084: segment.writeJar(out);
085: }
086:
087: protected void tearDown() throws Exception {
088: super.tearDown();
089: try {
090: if (in != null) {
091: in.close();
092: }
093: } finally {
094: if (out != null) {
095: out.close();
096: }
097: }
098: }
099:
100: }
|