< prev index next >

test/tools/jar/ChangeDir.java

Print this page




   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  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 4806786 8023113
  27  * @modules jdk.jartool/sun.tools.jar
  28  * @summary jar -C doesn't ignore multiple // in path
  29  */
  30 
  31 import java.io.*;
  32 import java.nio.file.*;
  33 import java.util.*;
  34 import java.util.jar.*;

  35 import java.util.stream.Stream;
  36 import sun.tools.jar.Main;
  37 
  38 public class ChangeDir {



  39     private final static String jarName = "test.jar";
  40     private final static String fileName = "hello.txt";
  41 
  42     /** Remove dirs & files needed for test. */
  43     private static void cleanup(Path dir) {
  44         try {
  45             if (Files.isDirectory(dir)) {
  46                 try (Stream<Path> s = Files.list(dir)) {
  47                     s.forEach( p -> cleanup(p));
  48                 }
  49             }
  50             Files.delete(dir);
  51         } catch (IOException x) {
  52             fail(x.toString());
  53         }
  54     }
  55 
  56     public static void realMain(String[] args) throws Throwable {
  57         doTest("/");
  58         doTest("//");


  71         Path topDir = Files.createTempDirectory("delete");
  72         try {
  73             Files.deleteIfExists(Paths.get(jarName));
  74 
  75             // Create a subdirectory "a/b"
  76             Path testDir = Files.createDirectories(topDir.resolve("a").resolve("b"));
  77 
  78             // Create file in that subdirectory
  79             Path testFile = testDir.resolve(fileName);
  80             Files.createFile(testFile);
  81 
  82             // Create a jar file from that subdirectory, but with a // in the
  83             // path  name.
  84             List<String> argList = new ArrayList<String>();
  85             argList.add("cf");
  86             argList.add(jarName);
  87             argList.add("-C");
  88             argList.add(topDir.toString() + sep + "a" + sep + sep + "b"); // Note double 'sep' is intentional
  89             argList.add(fileName);
  90 
  91             Main jarTool = new Main(System.out, System.err, "jar");
  92             if (!jarTool.run(argList.toArray(new String[argList.size()]))) {

  93                 fail("Could not create jar file.");
  94             }
  95 
  96             // Check that the entry for hello.txt does *not* have a pathname.
  97             try (JarFile jf = new JarFile(jarName)) {
  98                 for (Enumeration<JarEntry> i = jf.entries(); i.hasMoreElements();) {
  99                     JarEntry je = i.nextElement();
 100                     String name = je.getName();
 101                     if (name.indexOf(fileName) != -1) {
 102                         if (name.indexOf(fileName) != 0) {
 103                             fail(String.format(
 104                                      "Expected '%s' but got '%s'%n", fileName, name));
 105                         } else {
 106                             pass();
 107                         }
 108                     }
 109                 }
 110             }
 111         } finally {
 112             cleanup(topDir);


   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  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 4806786 8023113
  27  * @modules jdk.jartool
  28  * @summary jar -C doesn't ignore multiple // in path
  29  */
  30 
  31 import java.io.*;
  32 import java.nio.file.*;
  33 import java.util.*;
  34 import java.util.jar.*;
  35 import java.util.spi.ToolProvider;
  36 import java.util.stream.Stream;

  37 
  38 public class ChangeDir {
  39     private static final ToolProvider JAR_TOOL =
  40         ToolProvider.findFirst("jar").get();
  41 
  42     private final static String jarName = "test.jar";
  43     private final static String fileName = "hello.txt";
  44 
  45     /** Remove dirs & files needed for test. */
  46     private static void cleanup(Path dir) {
  47         try {
  48             if (Files.isDirectory(dir)) {
  49                 try (Stream<Path> s = Files.list(dir)) {
  50                     s.forEach( p -> cleanup(p));
  51                 }
  52             }
  53             Files.delete(dir);
  54         } catch (IOException x) {
  55             fail(x.toString());
  56         }
  57     }
  58 
  59     public static void realMain(String[] args) throws Throwable {
  60         doTest("/");
  61         doTest("//");


  74         Path topDir = Files.createTempDirectory("delete");
  75         try {
  76             Files.deleteIfExists(Paths.get(jarName));
  77 
  78             // Create a subdirectory "a/b"
  79             Path testDir = Files.createDirectories(topDir.resolve("a").resolve("b"));
  80 
  81             // Create file in that subdirectory
  82             Path testFile = testDir.resolve(fileName);
  83             Files.createFile(testFile);
  84 
  85             // Create a jar file from that subdirectory, but with a // in the
  86             // path  name.
  87             List<String> argList = new ArrayList<String>();
  88             argList.add("cf");
  89             argList.add(jarName);
  90             argList.add("-C");
  91             argList.add(topDir.toString() + sep + "a" + sep + sep + "b"); // Note double 'sep' is intentional
  92             argList.add(fileName);
  93 
  94             int rc = JAR_TOOL.run(System.out, System.err,
  95                                   argList.toArray(new String[argList.size()]));
  96             if (rc != 0) {
  97                 fail("Could not create jar file.");
  98             }
  99 
 100             // Check that the entry for hello.txt does *not* have a pathname.
 101             try (JarFile jf = new JarFile(jarName)) {
 102                 for (Enumeration<JarEntry> i = jf.entries(); i.hasMoreElements();) {
 103                     JarEntry je = i.nextElement();
 104                     String name = je.getName();
 105                     if (name.indexOf(fileName) != -1) {
 106                         if (name.indexOf(fileName) != 0) {
 107                             fail(String.format(
 108                                      "Expected '%s' but got '%s'%n", fileName, name));
 109                         } else {
 110                             pass();
 111                         }
 112                     }
 113                 }
 114             }
 115         } finally {
 116             cleanup(topDir);
< prev index next >