Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/file/etc/Exceptions.java
38828 views
/*1* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4313887 688149825* @summary Miscellenous tests on exceptions in java.nio.file26*/2728import java.nio.file.*;29import java.io.*;30import java.util.Objects;31import java.lang.reflect.*;3233public class Exceptions {3435public static void main(String[] args) throws Exception {36testFileSystemException();37testDirectoryIteratorException();38}3940static void testFileSystemException() throws Exception {41String thisFile = "source";42String otherFile = "target";43String reason = "Access denied";4445// getFile/getOtherFile46testFileSystemException(thisFile, otherFile, reason);47testFileSystemException(null, otherFile, reason);48testFileSystemException(thisFile, null, reason);49testFileSystemException(thisFile, otherFile, null);5051// serialization52FileSystemException exc;53exc = new FileSystemException(thisFile, otherFile, reason);54exc = (FileSystemException)deserialize(serialize(exc));55if (!exc.getFile().equals(thisFile) || !exc.getOtherFile().equals(otherFile))56throw new RuntimeException("Exception not reconstituted completely");57}5859static void testFileSystemException(String thisFile,60String otherFile,61String reason)62{63FileSystemException exc = new FileSystemException(thisFile, otherFile, reason);64if (!Objects.equals(thisFile, exc.getFile()))65throw new RuntimeException("getFile returned unexpected result");66if (!Objects.equals(otherFile, exc.getOtherFile()))67throw new RuntimeException("getOtherFile returned unexpected result");68if (!Objects.equals(reason, exc.getReason()))69throw new RuntimeException("getReason returned unexpected result");70}7172static void testDirectoryIteratorException() throws Exception {73// NullPointerException74try {75new DirectoryIteratorException(null);76throw new RuntimeException("NullPointerException expected");77} catch (NullPointerException expected) { }7879// serialization80DirectoryIteratorException exc;81exc = new DirectoryIteratorException(new IOException());82exc = (DirectoryIteratorException)deserialize(serialize(exc));83IOException ioe = exc.getCause();84if (ioe == null)85throw new RuntimeException("Cause should not be null");8687// when deserializing then the cause should be an IOException88hackCause(exc, null);89try {90deserialize(serialize(exc));91throw new RuntimeException("InvalidObjectException expected");92} catch (InvalidObjectException expected) { }9394hackCause(exc, new RuntimeException());95try {96deserialize(serialize(exc));97throw new RuntimeException("InvalidObjectException expected");98} catch (InvalidObjectException expected) { }99}100101102// Use reflection to set a Throwable's cause.103static void hackCause(Throwable t, Throwable cause)104throws NoSuchFieldException, IllegalAccessException105{106Field f = Throwable.class.getDeclaredField("cause");107f.setAccessible(true);108f.set(t, cause);109}110111// Serialize the given object to a byte[]112static byte[] serialize(Object o) throws IOException {113ByteArrayOutputStream baos = new ByteArrayOutputStream();114ObjectOutputStream oos = new ObjectOutputStream(baos);115oos.writeObject(o);116oos.close();117return baos.toByteArray();118}119120// Read an object from its serialized form121static Object deserialize(byte[] bytes)122throws IOException, ClassNotFoundException123{124ByteArrayInputStream in = new ByteArrayInputStream(bytes);125ObjectInputStream ois = new ObjectInputStream(in);126Object result = ois.readObject();127ois.close();128return result;129}130}131132133