Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/file/etc/Exceptions.java
38828 views
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
* @summary Miscellenous tests on exceptions in java.nio.file
27
*/
28
29
import java.nio.file.*;
30
import java.io.*;
31
import java.util.Objects;
32
import java.lang.reflect.*;
33
34
public class Exceptions {
35
36
public static void main(String[] args) throws Exception {
37
testFileSystemException();
38
testDirectoryIteratorException();
39
}
40
41
static void testFileSystemException() throws Exception {
42
String thisFile = "source";
43
String otherFile = "target";
44
String reason = "Access denied";
45
46
// getFile/getOtherFile
47
testFileSystemException(thisFile, otherFile, reason);
48
testFileSystemException(null, otherFile, reason);
49
testFileSystemException(thisFile, null, reason);
50
testFileSystemException(thisFile, otherFile, null);
51
52
// serialization
53
FileSystemException exc;
54
exc = new FileSystemException(thisFile, otherFile, reason);
55
exc = (FileSystemException)deserialize(serialize(exc));
56
if (!exc.getFile().equals(thisFile) || !exc.getOtherFile().equals(otherFile))
57
throw new RuntimeException("Exception not reconstituted completely");
58
}
59
60
static void testFileSystemException(String thisFile,
61
String otherFile,
62
String reason)
63
{
64
FileSystemException exc = new FileSystemException(thisFile, otherFile, reason);
65
if (!Objects.equals(thisFile, exc.getFile()))
66
throw new RuntimeException("getFile returned unexpected result");
67
if (!Objects.equals(otherFile, exc.getOtherFile()))
68
throw new RuntimeException("getOtherFile returned unexpected result");
69
if (!Objects.equals(reason, exc.getReason()))
70
throw new RuntimeException("getReason returned unexpected result");
71
}
72
73
static void testDirectoryIteratorException() throws Exception {
74
// NullPointerException
75
try {
76
new DirectoryIteratorException(null);
77
throw new RuntimeException("NullPointerException expected");
78
} catch (NullPointerException expected) { }
79
80
// serialization
81
DirectoryIteratorException exc;
82
exc = new DirectoryIteratorException(new IOException());
83
exc = (DirectoryIteratorException)deserialize(serialize(exc));
84
IOException ioe = exc.getCause();
85
if (ioe == null)
86
throw new RuntimeException("Cause should not be null");
87
88
// when deserializing then the cause should be an IOException
89
hackCause(exc, null);
90
try {
91
deserialize(serialize(exc));
92
throw new RuntimeException("InvalidObjectException expected");
93
} catch (InvalidObjectException expected) { }
94
95
hackCause(exc, new RuntimeException());
96
try {
97
deserialize(serialize(exc));
98
throw new RuntimeException("InvalidObjectException expected");
99
} catch (InvalidObjectException expected) { }
100
}
101
102
103
// Use reflection to set a Throwable's cause.
104
static void hackCause(Throwable t, Throwable cause)
105
throws NoSuchFieldException, IllegalAccessException
106
{
107
Field f = Throwable.class.getDeclaredField("cause");
108
f.setAccessible(true);
109
f.set(t, cause);
110
}
111
112
// Serialize the given object to a byte[]
113
static byte[] serialize(Object o) throws IOException {
114
ByteArrayOutputStream baos = new ByteArrayOutputStream();
115
ObjectOutputStream oos = new ObjectOutputStream(baos);
116
oos.writeObject(o);
117
oos.close();
118
return baos.toByteArray();
119
}
120
121
// Read an object from its serialized form
122
static Object deserialize(byte[] bytes)
123
throws IOException, ClassNotFoundException
124
{
125
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
126
ObjectInputStream ois = new ObjectInputStream(in);
127
Object result = ois.readObject();
128
ois.close();
129
return result;
130
}
131
}
132
133