Path: blob/master/test/jdk/java/nio/file/etc/Exceptions.java
41153 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* @modules java.base/java.lang:open26* @summary Miscellenous tests on exceptions in java.nio.file27*/2829import java.nio.file.*;30import java.io.*;31import java.util.Objects;32import java.lang.reflect.*;3334public class Exceptions {3536public static void main(String[] args) throws Exception {37testFileSystemException();38testDirectoryIteratorException();39}4041static void testFileSystemException() throws Exception {42String thisFile = "source";43String otherFile = "target";44String reason = "Access denied";4546// getFile/getOtherFile47testFileSystemException(thisFile, otherFile, reason);48testFileSystemException(null, otherFile, reason);49testFileSystemException(thisFile, null, reason);50testFileSystemException(thisFile, otherFile, null);5152// serialization53FileSystemException exc;54exc = new FileSystemException(thisFile, otherFile, reason);55exc = (FileSystemException)deserialize(serialize(exc));56if (!exc.getFile().equals(thisFile) || !exc.getOtherFile().equals(otherFile))57throw new RuntimeException("Exception not reconstituted completely");58}5960static void testFileSystemException(String thisFile,61String otherFile,62String reason)63{64FileSystemException exc = new FileSystemException(thisFile, otherFile, reason);65if (!Objects.equals(thisFile, exc.getFile()))66throw new RuntimeException("getFile returned unexpected result");67if (!Objects.equals(otherFile, exc.getOtherFile()))68throw new RuntimeException("getOtherFile returned unexpected result");69if (!Objects.equals(reason, exc.getReason()))70throw new RuntimeException("getReason returned unexpected result");71}7273static void testDirectoryIteratorException() throws Exception {74// NullPointerException75try {76new DirectoryIteratorException(null);77throw new RuntimeException("NullPointerException expected");78} catch (NullPointerException expected) { }7980// serialization81DirectoryIteratorException exc;82exc = new DirectoryIteratorException(new IOException());83exc = (DirectoryIteratorException)deserialize(serialize(exc));84IOException ioe = exc.getCause();85if (ioe == null)86throw new RuntimeException("Cause should not be null");8788// when deserializing then the cause should be an IOException89hackCause(exc, null);90try {91deserialize(serialize(exc));92throw new RuntimeException("InvalidObjectException expected");93} catch (InvalidObjectException expected) { }9495hackCause(exc, new RuntimeException());96try {97deserialize(serialize(exc));98throw new RuntimeException("InvalidObjectException expected");99} catch (InvalidObjectException expected) { }100}101102103// Use reflection to set a Throwable's cause.104static void hackCause(Throwable t, Throwable cause)105throws NoSuchFieldException, IllegalAccessException106{107Field f = Throwable.class.getDeclaredField("cause");108f.setAccessible(true);109f.set(t, cause);110}111112// Serialize the given object to a byte[]113static byte[] serialize(Object o) throws IOException {114ByteArrayOutputStream baos = new ByteArrayOutputStream();115ObjectOutputStream oos = new ObjectOutputStream(baos);116oos.writeObject(o);117oos.close();118return baos.toByteArray();119}120121// Read an object from its serialized form122static Object deserialize(byte[] bytes)123throws IOException, ClassNotFoundException124{125ByteArrayInputStream in = new ByteArrayInputStream(bytes);126ObjectInputStream ois = new ObjectInputStream(in);127Object result = ois.readObject();128ois.close();129return result;130}131}132133134