< prev index next >

test/tools/jar/InputFilesTest.java

Print this page




  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8165944
  27  * @summary test several jar tool input file scenarios with variations on -C
  28  *          options with/without a --release option.  Some input files are
  29  *          duplicates that sometimes cause exceptions and other times do not,
  30  *          demonstrating identical behavior to JDK 8 jar tool.
  31  * @library /lib/testlibrary
  32  * @modules jdk.jartool/sun.tools.jar
  33  * @build jdk.testlibrary.FileUtils
  34  * @run testng InputFilesTest
  35  */
  36 
  37 import org.testng.Assert;
  38 import org.testng.annotations.AfterMethod;
  39 import org.testng.annotations.BeforeMethod;
  40 import org.testng.annotations.Test;
  41 
  42 import java.io.ByteArrayOutputStream;
  43 import java.io.IOException;
  44 import java.io.PrintStream;
  45 import java.io.UncheckedIOException;
  46 import java.nio.file.Files;
  47 import java.nio.file.Path;
  48 import java.nio.file.Paths;
  49 import java.util.Arrays;

  50 import java.util.stream.Stream;
  51 import java.util.zip.ZipException;
  52 
  53 import jdk.testlibrary.FileUtils;
  54 
  55 public class InputFilesTest {



  56     private final String nl = System.lineSeparator();
  57     private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  58     private final PrintStream out = new PrintStream(baos);
  59     private Runnable onCompletion;
  60 
  61     @BeforeMethod
  62     public void reset() {
  63         onCompletion = null;
  64     }
  65 
  66     @AfterMethod
  67     public void run() {
  68         if (onCompletion != null) {
  69             onCompletion.run();
  70         }
  71     }
  72 
  73     @Test
  74     public void test1() throws IOException {
  75         mkdir("test1 test2");


 178                 if (Files.isDirectory(p)) {
 179                     FileUtils.deleteFileTreeWithRetry(p);
 180                 } else {
 181                     FileUtils.deleteFileIfExistsWithRetry(p);
 182                 }
 183             } catch (IOException x) {
 184                 throw new UncheckedIOException(x);
 185             }
 186         });
 187     }
 188 
 189     private void jar(String cmdline) throws IOException {
 190         System.out.println("jar " + cmdline);
 191         baos.reset();
 192 
 193         // the run method catches IOExceptions, we need to expose them
 194         ByteArrayOutputStream baes = new ByteArrayOutputStream();
 195         PrintStream err = new PrintStream(baes);
 196         PrintStream saveErr = System.err;
 197         System.setErr(err);
 198         boolean ok = new sun.tools.jar.Main(out, err, "jar").run(cmdline.split(" +"));
 199         System.setErr(saveErr);
 200         if (!ok) {
 201             String s = baes.toString();
 202             if (s.startsWith("java.util.zip.ZipException: duplicate entry: ")) {
 203                 throw new ZipException(s);
 204             }
 205             throw new IOException(s);
 206         }
 207     }
 208 
 209     private void println() throws IOException {
 210         System.out.println(new String(baos.toByteArray()));
 211     }
 212 }


  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8165944
  27  * @summary test several jar tool input file scenarios with variations on -C
  28  *          options with/without a --release option.  Some input files are
  29  *          duplicates that sometimes cause exceptions and other times do not,
  30  *          demonstrating identical behavior to JDK 8 jar tool.
  31  * @library /lib/testlibrary
  32  * @modules jdk.jartool
  33  * @build jdk.testlibrary.FileUtils
  34  * @run testng InputFilesTest
  35  */
  36 
  37 import org.testng.Assert;
  38 import org.testng.annotations.AfterMethod;
  39 import org.testng.annotations.BeforeMethod;
  40 import org.testng.annotations.Test;
  41 
  42 import java.io.ByteArrayOutputStream;
  43 import java.io.IOException;
  44 import java.io.PrintStream;
  45 import java.io.UncheckedIOException;
  46 import java.nio.file.Files;
  47 import java.nio.file.Path;
  48 import java.nio.file.Paths;
  49 import java.util.Arrays;
  50 import java.util.spi.ToolProvider;
  51 import java.util.stream.Stream;
  52 import java.util.zip.ZipException;
  53 
  54 import jdk.testlibrary.FileUtils;
  55 
  56 public class InputFilesTest {
  57     private static final ToolProvider JAR_TOOL =
  58         ToolProvider.findFirst("jar").get();
  59 
  60     private final String nl = System.lineSeparator();
  61     private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  62     private final PrintStream out = new PrintStream(baos);
  63     private Runnable onCompletion;
  64 
  65     @BeforeMethod
  66     public void reset() {
  67         onCompletion = null;
  68     }
  69 
  70     @AfterMethod
  71     public void run() {
  72         if (onCompletion != null) {
  73             onCompletion.run();
  74         }
  75     }
  76 
  77     @Test
  78     public void test1() throws IOException {
  79         mkdir("test1 test2");


 182                 if (Files.isDirectory(p)) {
 183                     FileUtils.deleteFileTreeWithRetry(p);
 184                 } else {
 185                     FileUtils.deleteFileIfExistsWithRetry(p);
 186                 }
 187             } catch (IOException x) {
 188                 throw new UncheckedIOException(x);
 189             }
 190         });
 191     }
 192 
 193     private void jar(String cmdline) throws IOException {
 194         System.out.println("jar " + cmdline);
 195         baos.reset();
 196 
 197         // the run method catches IOExceptions, we need to expose them
 198         ByteArrayOutputStream baes = new ByteArrayOutputStream();
 199         PrintStream err = new PrintStream(baes);
 200         PrintStream saveErr = System.err;
 201         System.setErr(err);
 202         int rc = JAR_TOOL.run(out, err, cmdline.split(" +"));
 203         System.setErr(saveErr);
 204         if (rc != 0) {
 205             String s = baes.toString();
 206             if (s.startsWith("java.util.zip.ZipException: duplicate entry: ")) {
 207                 throw new ZipException(s);
 208             }
 209             throw new IOException(s);
 210         }
 211     }
 212 
 213     private void println() throws IOException {
 214         System.out.println(new String(baos.toByteArray()));
 215     }
 216 }
< prev index next >