2748N/A/*
2748N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2748N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2748N/A *
2748N/A * This code is free software; you can redistribute it and/or modify it
2748N/A * under the terms of the GNU General Public License version 2 only, as
2748N/A * published by the Free Software Foundation.
2748N/A *
2748N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2748N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2748N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2748N/A * version 2 for more details (a copy is included in the LICENSE file that
2748N/A * accompanied this code).
2748N/A *
2748N/A * You should have received a copy of the GNU General Public License version
2748N/A * 2 along with this work; if not, write to the Free Software Foundation,
2748N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2748N/A *
2748N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2748N/A * or visit www.oracle.com if you need additional information or have any
2748N/A * questions.
2748N/A */
2748N/A
2748N/A/* @test
2748N/A * @bug 4313887 6881498
2748N/A * @summary Miscellenous tests on exceptions in java.nio.file
2748N/A */
2748N/A
2748N/Aimport java.nio.file.*;
2748N/Aimport java.io.*;
2748N/Aimport java.util.Objects;
2748N/Aimport java.lang.reflect.*;
2748N/A
2748N/Apublic class Exceptions {
2748N/A
2748N/A public static void main(String[] args) throws Exception {
2748N/A testFileSystemException();
2748N/A testDirectoryIteratorException();
2748N/A }
2748N/A
2748N/A static void testFileSystemException() throws Exception {
2748N/A String thisFile = "source";
2748N/A String otherFile = "target";
2748N/A String reason = "Access denied";
2748N/A
2748N/A // getFile/getOtherFile
2748N/A testFileSystemException(thisFile, otherFile, reason);
2748N/A testFileSystemException(null, otherFile, reason);
2748N/A testFileSystemException(thisFile, null, reason);
2748N/A testFileSystemException(thisFile, otherFile, null);
2748N/A
2748N/A // serialization
2748N/A FileSystemException exc;
2748N/A exc = new FileSystemException(thisFile, otherFile, reason);
2748N/A exc = (FileSystemException)deserialize(serialize(exc));
2748N/A if (!exc.getFile().equals(thisFile) || !exc.getOtherFile().equals(otherFile))
2748N/A throw new RuntimeException("Exception not reconstituted completely");
2748N/A }
2748N/A
2748N/A static void testFileSystemException(String thisFile,
2748N/A String otherFile,
2748N/A String reason)
2748N/A {
2748N/A FileSystemException exc = new FileSystemException(thisFile, otherFile, reason);
2748N/A if (!Objects.equals(thisFile, exc.getFile()))
2748N/A throw new RuntimeException("getFile returned unexpected result");
2748N/A if (!Objects.equals(otherFile, exc.getOtherFile()))
2748N/A throw new RuntimeException("getOtherFile returned unexpected result");
2748N/A if (!Objects.equals(reason, exc.getReason()))
2748N/A throw new RuntimeException("getReason returned unexpected result");
2748N/A }
2748N/A
2748N/A static void testDirectoryIteratorException() throws Exception {
2748N/A // NullPointerException
2748N/A try {
2748N/A new DirectoryIteratorException(null);
2748N/A throw new RuntimeException("NullPointerException expected");
2748N/A } catch (NullPointerException expected) { }
2748N/A
2748N/A // serialization
2748N/A DirectoryIteratorException exc;
2748N/A exc = new DirectoryIteratorException(new IOException());
2748N/A exc = (DirectoryIteratorException)deserialize(serialize(exc));
2748N/A IOException ioe = exc.getCause();
2748N/A if (ioe == null)
2748N/A throw new RuntimeException("Cause should not be null");
2748N/A
2748N/A // when deserializing then the cause should be an IOException
2748N/A hackCause(exc, null);
2748N/A try {
2748N/A deserialize(serialize(exc));
2748N/A throw new RuntimeException("InvalidObjectException expected");
2748N/A } catch (InvalidObjectException expected) { }
2748N/A
2748N/A hackCause(exc, new RuntimeException());
2748N/A try {
2748N/A deserialize(serialize(exc));
2748N/A throw new RuntimeException("InvalidObjectException expected");
2748N/A } catch (InvalidObjectException expected) { }
2748N/A }
2748N/A
2748N/A
2748N/A // Use reflection to set a Throwable's cause.
2748N/A static void hackCause(Throwable t, Throwable cause)
2748N/A throws NoSuchFieldException, IllegalAccessException
2748N/A {
2748N/A Field f = Throwable.class.getDeclaredField("cause");
2748N/A f.setAccessible(true);
2748N/A f.set(t, cause);
2748N/A }
2748N/A
2748N/A // Serialize the given object to a byte[]
2748N/A static byte[] serialize(Object o) throws IOException {
2748N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
2748N/A ObjectOutputStream oos = new ObjectOutputStream(baos);
2748N/A oos.writeObject(o);
2748N/A oos.close();
2748N/A return baos.toByteArray();
2748N/A }
2748N/A
2748N/A // Read an object from its serialized form
2748N/A static Object deserialize(byte[] bytes)
2748N/A throws IOException, ClassNotFoundException
2748N/A {
2748N/A ByteArrayInputStream in = new ByteArrayInputStream(bytes);
2748N/A ObjectInputStream ois = new ObjectInputStream(in);
2748N/A Object result = ois.readObject();
2748N/A ois.close();
2748N/A return result;
2748N/A }
2748N/A}