Skip to content

Commit

Permalink
8318971: Better Error Handling for Jar Tool When Processing Non-exist…
Browse files Browse the repository at this point in the history
…ent Files

Backport-of: 8ae309ebacd6947bbad2ef168ca13702e1cba099
  • Loading branch information
GoeLin committed Jan 29, 2024
1 parent ec4e75d commit fbc6005
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/jdk.jartool/share/classes/sun/tools/jar/Main.java
Expand Up @@ -283,6 +283,9 @@ public synchronized boolean run(String args[]) {
}
}
expand();
if (!ok) {
return false;
}
if (!moduleInfos.isEmpty()) {
// All actual file entries (excl manifest and module-info.class)
Set<String> jentries = new HashSet<>();
Expand Down Expand Up @@ -357,6 +360,9 @@ public synchronized boolean run(String args[]) {
tmpFile = createTemporaryFile("tmpjar", ".jar");
}
expand();
if (!ok) {
return false;
}
try (FileInputStream in = (fname != null) ? new FileInputStream(inputFile)
: new FileInputStream(FileDescriptor.in);
FileOutputStream out = new FileOutputStream(tmpFile);
Expand Down
82 changes: 80 additions & 2 deletions test/jdk/tools/jar/InputFilesTest.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,7 +23,7 @@

/*
* @test
* @bug 8165944
* @bug 8165944 8318971
* @summary test several jar tool input file scenarios with variations on -C
* options with/without a --release option. Some input files are
* duplicates that sometimes cause exceptions and other times do not,
Expand Down Expand Up @@ -153,6 +153,84 @@ public void test6() throws IOException {
jar("cf test.jar --release 9 -C test1 a -C test2 a");
}

/**
* Containing non-existent file in the file list
* The final jar should not be created and correct error message should be caught.
* IOException is triggered as expected.
*/
@Test
public void testNonExistentFileInput() throws IOException {
touch("existingTestFile.txt");
onCompletion = () -> rm("existingTestFile.txt");
try {
jar("cf test.jar existingTestFile.txt nonExistentTestFile.txt");
Assert.fail("jar tool unexpectedly completed successfully");
} catch (IOException e) {
Assert.assertEquals(e.getMessage().trim(), "nonExistentTestFile.txt : no such file or directory");
Assert.assertTrue(Files.notExists(Path.of("test.jar")), "Jar file should not be created.");
}
}

/**
* With @File as a part of jar command line, where the File is containing one or more
* non-existent files or directories
* The final jar should not be created and correct error message should be caught.
* IOException is triggered as expected.
*/
@Test
public void testNonExistentFileInputClassList() throws IOException {
touch("existingTestFile.txt");
touch("classes.list");
Files.writeString(Path.of("classes.list"),
"existingTestFile.txt\n" +
"nonExistentTestFile.txt\n" +
"nonExistentDirectory\n"
);
onCompletion = () -> rm("existingTestFile.txt classes.list");
try {
jar("cf test.jar @classes.list");
Assert.fail("jar tool unexpectedly completed successfully");
} catch (IOException e) {
String msg = e.getMessage().trim();
Assert.assertTrue(msg.contains("nonExistentTestFile.txt : no such file or directory"));
Assert.assertTrue(msg.trim().contains("nonExistentDirectory : no such file or directory"));
Assert.assertTrue(Files.notExists(Path.of("test.jar")), "Jar file should not be created.");
}

}

/**
* Create a jar file; then with @File as a part of jar command line, where the File is containing one or more
* non-existent files or directories
* The final jar should not be created and correct error message should be caught.
* IOException is triggered as expected.
*/
@Test
public void testUpdateNonExistentFileInputClassList() throws IOException {
touch("existingTestFileUpdate.txt");
touch("existingTestFileUpdate2.txt");
touch("classesUpdate.list");
Files.writeString(Path.of("classesUpdate.list"),
"existingTestFileUpdate2.txt\n" +
"nonExistentTestFileUpdate.txt\n" +
"nonExistentDirectoryUpdate\n"
);
onCompletion = () -> rm("existingTestFileUpdate.txt existingTestFileUpdate2.txt " +
"classesUpdate.list testUpdate.jar");
try {
jar("cf testUpdate.jar existingTestFileUpdate.txt");
Assert.assertTrue(Files.exists(Path.of("testUpdate.jar")));
jar("uf testUpdate.jar @classesUpdate.list");
Assert.fail("jar tool unexpectedly completed successfully");
} catch (IOException e) {
String msg = e.getMessage().trim();
Assert.assertFalse(msg.contains("existingTestFileUpdate.txt : no such file or directory"));
Assert.assertTrue(msg.contains("nonExistentTestFileUpdate.txt : no such file or directory"));
Assert.assertTrue(msg.trim().contains("nonExistentDirectoryUpdate : no such file or directory"));
}

}

private Stream<Path> mkpath(String... args) {
return Arrays.stream(args).map(d -> Paths.get(".", d.split("/")));
}
Expand Down

1 comment on commit fbc6005

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.