Skip to content
Permalink
Browse files
8273935: (zipfs) Files.getFileAttributeView() throws UOE instead of r…
…eturning null when view not supported

Reviewed-by: vkempik
Backport-of: 161fdb4afbc6e67cc7580fe753112c4d894a9b6b
  • Loading branch information
Yuri Nesterenko committed Apr 25, 2022
1 parent 3817c5d commit 1d29a2ff5413e1dfef105b1083e7724658e52f79
Showing 4 changed files with 108 additions and 6 deletions.
@@ -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) {
@@ -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
@@ -68,7 +68,7 @@

/**
* @test
* @bug 8213031
* @bug 8213031 8273935
* @modules jdk.zipfs
* jdk.jartool
* @run testng TestPosix
@@ -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
@@ -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));
}
}
}
@@ -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
@@ -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(", "));

1 comment on commit 1d29a2f

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 1d29a2f Apr 25, 2022

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.