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

Reviewed-by: alanb, bpb, sgehwolf
  • Loading branch information
Lance Andersen committed Sep 21, 2021
1 parent 0fc47e9 commit 161fdb4afbc6e67cc7580fe753112c4d894a9b6b
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(", "));

3 comments on commit 161fdb4

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 161fdb4 Sep 21, 2021

Choose a reason for hiding this comment

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

@jerboaa
Copy link
Contributor

@jerboaa jerboaa commented on 161fdb4 Sep 28, 2021

Choose a reason for hiding this comment

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

/backport jdk17u

@openjdk
Copy link

@openjdk openjdk bot commented on 161fdb4 Sep 28, 2021

Choose a reason for hiding this comment

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

@jerboaa the backport was successfully created on the branch jerboaa-backport-161fdb4a in my personal fork of openjdk/jdk17u. To create a pull request with this backport targeting openjdk/jdk17u:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

this pull request contains a backport of commit 161fdb4a from the openjdk/jdk repository.

The commit being backported was authored by Lance Andersen on 21 Sep 2021 and was reviewed by Alan Bateman, Brian Burkhalter and Severin Gehwolf.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u:

$ git fetch https://github.com/openjdk-bots/jdk17u jerboaa-backport-161fdb4a:jerboaa-backport-161fdb4a
$ git checkout jerboaa-backport-161fdb4a
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u jerboaa-backport-161fdb4a

Please sign in to comment.