< prev index next >

jdk/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 = ToolProvider.findFirst("jar")
  58         .orElseThrow(() ->
  59             new RuntimeException("jar tool not found")
  60         );
  61 
  62     private final String nl = System.lineSeparator();
  63     private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  64     private final PrintStream out = new PrintStream(baos);
  65     private Runnable onCompletion;
  66 
  67     @BeforeMethod
  68     public void reset() {
  69         onCompletion = null;
  70     }
  71 
  72     @AfterMethod
  73     public void run() {
  74         if (onCompletion != null) {
  75             onCompletion.run();
  76         }
  77     }
  78 
  79     @Test
  80     public void test1() throws IOException {
  81         mkdir("test1 test2");


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