Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/rmi/runtime/Log/checkLogging/CheckLogging.java
38867 views
1
/*
2
* Copyright (c) 2001, 2013, 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 4402649
26
* @summary RMI should use new logging APIs. Unit test to exercise
27
* RMI's use of the java.util.logging API.
28
* @author Laird Dornin
29
*
30
* @library ../../../../../java/rmi/testlibrary
31
* @build TestLibrary
32
* @run main/othervm CheckLogging
33
*/
34
35
import java.util.logging.Level;
36
import java.util.logging.LogRecord;
37
import java.util.logging.Logger;
38
import java.util.logging.SimpleFormatter;
39
import java.util.logging.StreamHandler;
40
41
import java.io.ByteArrayOutputStream;
42
import java.io.IOException;
43
import java.io.PrintStream;
44
import java.io.OutputStream;
45
46
import java.rmi.RemoteException;
47
import java.rmi.Remote;
48
import java.rmi.Naming;
49
import java.rmi.registry.LocateRegistry;
50
import java.rmi.server.LogStream;
51
import java.rmi.server.RemoteServer;
52
53
import java.rmi.registry.Registry;
54
55
/**
56
* Perform following checks:
57
*
58
* 1. If using java.util.logging, turn on client call logger using
59
* system property, "sun.rmi.client.logCalls". Collect client call
60
* output using a custom stream handler. Verify client call output is
61
* generated and contains the string "outbound call".
62
*
63
* 2. Turn on server call using
64
* RemoteServer.setLog(ByteArrayOutputStream). Invoke some remote
65
* method calls verify logger output is non-null.
66
*
67
* Turn off server call log by doing setLog(null), verify output is
68
* zero length. Verify that RemoteServer.getLog == null
69
*
70
* Use setLog to turn call log back on. Invoke remote method that
71
* throws an exception and contains the string "exception".
72
*
73
* 3. Print directly to return value of RemoteServer.getLog(), verify
74
* logger output is non-null.
75
*/
76
public class CheckLogging {
77
private static int REGISTRY_PORT = -1;
78
private static String LOCATION;
79
private static Logger logger;
80
81
private static final ByteArrayOutputStream clientCallOut =
82
new ByteArrayOutputStream();
83
84
private static final boolean usingOld =
85
Boolean.getBoolean("sun.rmi.log.useOld");
86
87
static {
88
System.setProperty("sun.rmi.client.logCalls", "true");
89
if (usingOld) {
90
System.err.println("set default stream");
91
LogStream.setDefaultStream(new PrintStream(clientCallOut));
92
} else {
93
logger = Logger.getLogger("sun.rmi.client.call");
94
logger.addHandler(new InternalStreamHandler(clientCallOut));
95
}
96
}
97
98
/* use registry to generate client & server call log info */
99
private static Registry registry;
100
static {
101
try {
102
registry = TestLibrary.createRegistryOnUnusedPort();
103
REGISTRY_PORT = TestLibrary.getRegistryPort(registry);
104
LOCATION = "rmi://localhost:" + REGISTRY_PORT + "/";
105
} catch (Exception e) {
106
TestLibrary.bomb("could not create registry");
107
}
108
}
109
110
/**
111
* Used to collect output from specific loggers
112
*/
113
private static class InternalStreamHandler extends StreamHandler {
114
private InternalStreamHandler(OutputStream out) {
115
super(out, new SimpleFormatter());
116
setLevel(Level.ALL);
117
}
118
119
public void publish(LogRecord record) {
120
super.publish(record);
121
flush();
122
}
123
124
public void close() {
125
flush();
126
}
127
}
128
129
/**
130
* Ensure that a log has some output and that it contains a
131
* certain string
132
*/
133
private static void verifyLog(ByteArrayOutputStream bout,
134
String mustContain)
135
{
136
byte[] bytes = bout.toByteArray();
137
if (bytes.length == 0) {
138
TestLibrary.bomb("log data length is zero");
139
} else if ((mustContain != null) &&
140
(bout.toString().indexOf(mustContain) < 0))
141
{
142
TestLibrary.bomb("log output did not contain: " + mustContain);
143
}
144
}
145
146
/**
147
* Check serverCallLog output
148
*/
149
private static void checkServerCallLog() throws Exception {
150
ByteArrayOutputStream serverCallLog = new ByteArrayOutputStream();
151
RemoteServer.setLog(serverCallLog);
152
Naming.list(LOCATION);
153
verifyLog(serverCallLog, "list");
154
155
serverCallLog.reset();
156
RemoteServer.setLog(null);
157
PrintStream callStream = RemoteServer.getLog();
158
if (callStream != null) {
159
TestLibrary.bomb("call stream not null after calling " +
160
"setLog(null)");
161
} else {
162
System.err.println("call stream should be null and it is");
163
}
164
Naming.list(LOCATION);
165
166
if (usingOld) {
167
if (serverCallLog.toString().indexOf("UnicastServerRef") >= 0) {
168
TestLibrary.bomb("server call logging not turned off");
169
}
170
} else if (serverCallLog.toByteArray().length != 0) {
171
TestLibrary.bomb("call log contains output but it " +
172
"should be empty");
173
}
174
175
serverCallLog.reset();
176
RemoteServer.setLog(serverCallLog);
177
try {
178
// generates a notbound exception
179
Naming.lookup(LOCATION + "notthere");
180
} catch (Exception e) {
181
}
182
verifyLog(serverCallLog, "exception");
183
184
serverCallLog.reset();
185
RemoteServer.setLog(serverCallLog);
186
callStream = RemoteServer.getLog();
187
callStream.println("bingo, this is a getLog test");
188
verifyLog(serverCallLog, "bingo");
189
}
190
191
private static void checkPermissions() {
192
SecurityException ex = null;
193
try {
194
// should fail for lack of LoggingPermission "control"
195
RemoteServer.setLog(System.err);
196
} catch (SecurityException e) {
197
System.err.println("security excepton caught correctly");
198
ex = e;
199
}
200
if (ex == null) {
201
TestLibrary.bomb("able to set log without permission");
202
}
203
}
204
205
public static void main(String[] args) {
206
try {
207
checkServerCallLog();
208
209
if (!usingOld) {
210
verifyLog(clientCallOut, "outbound call");
211
System.setSecurityManager(new java.lang.SecurityManager());
212
checkPermissions();
213
}
214
System.err.println("TEST PASSED");
215
216
} catch (Exception e) {
217
if (e instanceof RuntimeException) {
218
throw (RuntimeException) e;
219
}
220
TestLibrary.bomb("unexpected exception", e);
221
} finally {
222
TestLibrary.unexport(registry);
223
}
224
}
225
}
226
227