1/* 2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* @test 25 * @bug 4313887 6881498 26 * @modules java.base/java.lang:open 27 * @summary Miscellenous tests on exceptions in java.nio.file 28 */ 29 30import java.nio.file.*; 31import java.io.*; 32import java.util.Objects; 33import java.lang.reflect.*; 34 35public class Exceptions { 36 37 public static void main(String[] args) throws Exception { 38 testFileSystemException(); 39 testDirectoryIteratorException(); 40 } 41 42 static void testFileSystemException() throws Exception { 43 String thisFile = "source"; 44 String otherFile = "target"; 45 String reason = "Access denied"; 46 47 // getFile/getOtherFile 48 testFileSystemException(thisFile, otherFile, reason); 49 testFileSystemException(null, otherFile, reason); 50 testFileSystemException(thisFile, null, reason); 51 testFileSystemException(thisFile, otherFile, null); 52 53 // serialization 54 FileSystemException exc; 55 exc = new FileSystemException(thisFile, otherFile, reason); 56 exc = (FileSystemException)deserialize(serialize(exc)); 57 if (!exc.getFile().equals(thisFile) || !exc.getOtherFile().equals(otherFile)) 58 throw new RuntimeException("Exception not reconstituted completely"); 59 } 60 61 static void testFileSystemException(String thisFile, 62 String otherFile, 63 String reason) 64 { 65 FileSystemException exc = new FileSystemException(thisFile, otherFile, reason); 66 if (!Objects.equals(thisFile, exc.getFile())) 67 throw new RuntimeException("getFile returned unexpected result"); 68 if (!Objects.equals(otherFile, exc.getOtherFile())) 69 throw new RuntimeException("getOtherFile returned unexpected result"); 70 if (!Objects.equals(reason, exc.getReason())) 71 throw new RuntimeException("getReason returned unexpected result"); 72 } 73 74 static void testDirectoryIteratorException() throws Exception { 75 // NullPointerException 76 try { 77 new DirectoryIteratorException(null); 78 throw new RuntimeException("NullPointerException expected"); 79 } catch (NullPointerException expected) { } 80 81 // serialization 82 DirectoryIteratorException exc; 83 exc = new DirectoryIteratorException(new IOException()); 84 exc = (DirectoryIteratorException)deserialize(serialize(exc)); 85 IOException ioe = exc.getCause(); 86 if (ioe == null) 87 throw new RuntimeException("Cause should not be null"); 88 89 // when deserializing then the cause should be an IOException 90 hackCause(exc, null); 91 try { 92 deserialize(serialize(exc)); 93 throw new RuntimeException("InvalidObjectException expected"); 94 } catch (InvalidObjectException expected) { } 95 96 hackCause(exc, new RuntimeException()); 97 try { 98 deserialize(serialize(exc)); 99 throw new RuntimeException("InvalidObjectException expected"); 100 } catch (InvalidObjectException expected) { } 101 } 102 103 104 // Use reflection to set a Throwable's cause. 105 static void hackCause(Throwable t, Throwable cause) 106 throws NoSuchFieldException, IllegalAccessException 107 { 108 Field f = Throwable.class.getDeclaredField("cause"); 109 f.setAccessible(true); 110 f.set(t, cause); 111 } 112 113 // Serialize the given object to a byte[] 114 static byte[] serialize(Object o) throws IOException { 115 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 116 ObjectOutputStream oos = new ObjectOutputStream(baos); 117 oos.writeObject(o); 118 oos.close(); 119 return baos.toByteArray(); 120 } 121 122 // Read an object from its serialized form 123 static Object deserialize(byte[] bytes) 124 throws IOException, ClassNotFoundException 125 { 126 ByteArrayInputStream in = new ByteArrayInputStream(bytes); 127 ObjectInputStream ois = new ObjectInputStream(in); 128 Object result = ois.readObject(); 129 ois.close(); 130 return result; 131 } 132} 133