Skip to content

Commit

Permalink
8273935: (zipfs) Files.getFileAttributeView() throws UOE instead of r…
Browse files Browse the repository at this point in the history
…eturning null when view not supported

Backport-of: 161fdb4afbc6e67cc7580fe753112c4d894a9b6b
  • Loading branch information
jerboaa committed Sep 30, 2021
1 parent 91dd244 commit de6899b
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ <V extends FileAttributeView> V getFileAttributeView(Class<V> type) {
if (type == FileOwnerAttributeView.class)
return (V)new ZipPosixFileAttributeView(this,true);
}
throw new UnsupportedOperationException("view <" + type + "> is not supported");
return null;
}

private ZipFileAttributeView getFileAttributeView(String type) {
Expand Down
6 changes: 3 additions & 3 deletions test/jdk/jdk/nio/zipfs/TestPosix.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 SAP SE. All rights reserved.
* Copyright (c) 2019, 2021, SAP SE. 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 Down Expand Up @@ -68,7 +68,7 @@

/**
* @test
* @bug 8213031
* @bug 8213031 8273935
* @modules jdk.zipfs
* jdk.jartool
* @run testng TestPosix
Expand Down Expand Up @@ -595,7 +595,7 @@ public void testPosixDefaults() throws IOException {
assertTrue(throwsUOE(()->Files.setPosixFilePermissions(entry, UW)));
assertTrue(throwsUOE(()->Files.getOwner(entry)));
assertTrue(throwsUOE(()->Files.setOwner(entry, DUMMY_USER)));
assertTrue(throwsUOE(()->Files.getFileAttributeView(entry, PosixFileAttributeView.class)));
assertNull(Files.getFileAttributeView(entry, PosixFileAttributeView.class));
}

// test with posix = true -> default values
Expand Down
102 changes: 102 additions & 0 deletions test/jdk/jdk/nio/zipfs/testng/test/PosixAttributeViewTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright (c) 2021, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
package test;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import util.ZipFsBaseTest;

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFileAttributeView;
import java.util.Map;
import java.util.zip.ZipEntry;

/**
* @test
* @bug 8273935
* @summary Validate that Files.getFileAttributeView will not throw an
* Exception when the attribute view PosixFileAttributeView is not available
*/
public class PosixAttributeViewTest extends ZipFsBaseTest {
public static final String ZIP_ENTRY = "Entry-0";
private static final Path ZIP_FILE = Path.of("posixTest.zip");

/**
* Create initial Zip File
* @throws IOException if an error occurs
*/
@BeforeTest
public void setup() throws IOException {
Files.deleteIfExists(ZIP_FILE);
Entry entry = Entry.of(ZIP_ENTRY, ZipEntry.DEFLATED,
"Tennis Anyone");
zip(ZIP_FILE, Map.of("create", "true"), entry);
}

/**
* Remove Zip File used by Test
* @throws IOException if an error occurs
*/
@AfterTest
public void cleanup() throws IOException {
Files.deleteIfExists(ZIP_FILE);
}

/**
* DataProvider used to specify the Map indicating whether Posix
* file attributes have been enabled
* @return map of the Zip FS properties to configure
*/
@DataProvider
protected Object[][] zipfsMap() {
return new Object[][]{
{Map.of()},
{Map.of("enablePosixFileAttributes", "true")}
};
}

/**
* Verify that Files.getFileAttributeView will not throw
* an Exception when the attribute view
* PosixFileAttributeView is not available
* @param env map of the Zip FS properties to configure
* @throws Exception if an error occurs
*/
@Test(dataProvider = "zipfsMap")
public void testPosixAttributeView(Map<String, String> env) throws Exception {
try (FileSystem fs = FileSystems.newFileSystem(ZIP_FILE, env)) {
Path entry = fs.getPath(ZIP_ENTRY);
PosixFileAttributeView view = Files.getFileAttributeView(entry,
PosixFileAttributeView.class);
System.out.printf("View returned: %s, Map= %s%n", view,
formatMap(env));
}
}
}
4 changes: 2 additions & 2 deletions test/jdk/jdk/nio/zipfs/testng/util/ZipFsBaseTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2021, 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 Down Expand Up @@ -105,7 +105,7 @@ protected Object[][] compressionMethods() {
* @param env Map to format
* @return Formatted string of the Map entries
*/
private static String formatMap(Map<String, ?> env) {
protected static String formatMap(Map<String, ?> env) {
return env.entrySet().stream()
.map(e -> format("(%s:%s)", e.getKey(), e.getValue()))
.collect(joining(", "));
Expand Down

1 comment on commit de6899b

@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.