0N/A/*
5266N/A * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 4402649
0N/A * @summary RMI should use new logging APIs. Unit test to exercise
0N/A * RMI's use of the java.util.logging API.
0N/A * @author Laird Dornin
0N/A *
0N/A * @library ../../../../../java/rmi/testlibrary
0N/A * @build TestLibrary
0N/A * @run main/othervm CheckLogging
0N/A */
0N/A
0N/Aimport java.util.logging.Level;
0N/Aimport java.util.logging.LogRecord;
0N/Aimport java.util.logging.Logger;
0N/Aimport java.util.logging.SimpleFormatter;
0N/Aimport java.util.logging.StreamHandler;
0N/A
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.io.PrintStream;
0N/Aimport java.io.OutputStream;
0N/A
0N/Aimport java.rmi.RemoteException;
0N/Aimport java.rmi.Remote;
0N/Aimport java.rmi.Naming;
0N/Aimport java.rmi.registry.LocateRegistry;
0N/Aimport java.rmi.server.LogStream;
0N/Aimport java.rmi.server.RemoteServer;
0N/A
0N/Aimport java.rmi.registry.Registry;
0N/A
0N/A/**
0N/A * Perform following checks:
0N/A *
0N/A * 1. If using java.util.logging, turn on client call logger using
0N/A * system property, "sun.rmi.client.logCalls". Collect client call
0N/A * output using a custom stream handler. Verify client call output is
0N/A * generated and contains the string "outbound call".
0N/A *
0N/A * 2. Turn on server call using
0N/A * RemoteServer.setLog(ByteArrayOutputStream). Invoke some remote
0N/A * method calls verify logger output is non-null.
0N/A *
0N/A * Turn off server call log by doing setLog(null), verify output is
0N/A * zero length. Verify that RemoteServer.getLog == null
0N/A *
0N/A * Use setLog to turn call log back on. Invoke remote method that
0N/A * throws an exception and contains the string "exception".
0N/A *
0N/A * 3. Print directly to return value of RemoteServer.getLog(), verify
0N/A * logger output is non-null.
0N/A */
0N/Apublic class CheckLogging {
5266N/A private static int REGISTRY_PORT = -1;
5266N/A private static String LOCATION;
5266N/A
0N/A private static final ByteArrayOutputStream clientCallOut =
0N/A new ByteArrayOutputStream();
0N/A
0N/A private static final boolean usingOld =
0N/A Boolean.getBoolean("sun.rmi.log.useOld");
0N/A
0N/A static {
0N/A System.setProperty("sun.rmi.client.logCalls", "true");
0N/A if (usingOld) {
0N/A System.err.println("set default stream");
0N/A LogStream.setDefaultStream(new PrintStream(clientCallOut));
0N/A } else {
0N/A Logger.getLogger("sun.rmi.client.call").
0N/A addHandler(new InternalStreamHandler(clientCallOut));
0N/A }
0N/A }
0N/A
0N/A /* use registry to generate client & server call log info */
0N/A private static Registry registry;
0N/A static {
0N/A try {
5266N/A registry = TestLibrary.createRegistryOnUnusedPort();
5266N/A REGISTRY_PORT = TestLibrary.getRegistryPort(registry);
5266N/A LOCATION = "rmi://localhost:" + REGISTRY_PORT + "/";
0N/A } catch (Exception e) {
0N/A TestLibrary.bomb("could not create registry");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Used to collect output from specific loggers
0N/A */
0N/A private static class InternalStreamHandler extends StreamHandler {
0N/A private InternalStreamHandler(OutputStream out) {
0N/A super(out, new SimpleFormatter());
0N/A setLevel(Level.ALL);
0N/A }
0N/A
0N/A public void publish(LogRecord record) {
0N/A super.publish(record);
0N/A flush();
0N/A }
0N/A
0N/A public void close() {
0N/A flush();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Ensure that a log has some output and that it contains a
0N/A * certain string
0N/A */
0N/A private static void verifyLog(ByteArrayOutputStream bout,
0N/A String mustContain)
0N/A {
0N/A byte[] bytes = bout.toByteArray();
0N/A if (bytes.length == 0) {
0N/A TestLibrary.bomb("log data length is zero");
0N/A } else if ((mustContain != null) &&
0N/A (bout.toString().indexOf(mustContain) < 0))
0N/A {
0N/A TestLibrary.bomb("log output did not contain: " + mustContain);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Check serverCallLog output
0N/A */
0N/A private static void checkServerCallLog() throws Exception {
0N/A ByteArrayOutputStream serverCallLog = new ByteArrayOutputStream();
0N/A RemoteServer.setLog(serverCallLog);
0N/A Naming.list(LOCATION);
0N/A verifyLog(serverCallLog, "list");
0N/A
0N/A serverCallLog.reset();
0N/A RemoteServer.setLog(null);
0N/A PrintStream callStream = RemoteServer.getLog();
0N/A if (callStream != null) {
0N/A TestLibrary.bomb("call stream not null after calling " +
0N/A "setLog(null)");
0N/A } else {
0N/A System.err.println("call stream should be null and it is");
0N/A }
0N/A Naming.list(LOCATION);
0N/A
0N/A if (usingOld) {
0N/A if (serverCallLog.toString().indexOf("UnicastServerRef") >= 0) {
0N/A TestLibrary.bomb("server call logging not turned off");
0N/A }
0N/A } else if (serverCallLog.toByteArray().length != 0) {
0N/A TestLibrary.bomb("call log contains output but it " +
0N/A "should be empty");
0N/A }
0N/A
0N/A serverCallLog.reset();
0N/A RemoteServer.setLog(serverCallLog);
0N/A try {
0N/A // generates a notbound exception
0N/A Naming.lookup(LOCATION + "notthere");
0N/A } catch (Exception e) {
0N/A }
0N/A verifyLog(serverCallLog, "exception");
0N/A
0N/A serverCallLog.reset();
0N/A RemoteServer.setLog(serverCallLog);
0N/A callStream = RemoteServer.getLog();
0N/A callStream.println("bingo, this is a getLog test");
0N/A verifyLog(serverCallLog, "bingo");
0N/A }
0N/A
0N/A private static void checkPermissions() {
0N/A SecurityException ex = null;
0N/A try {
0N/A // should fail for lack of LoggingPermission "control"
0N/A RemoteServer.setLog(System.err);
0N/A } catch (SecurityException e) {
0N/A System.err.println("security excepton caught correctly");
0N/A ex = e;
0N/A }
0N/A if (ex == null) {
0N/A TestLibrary.bomb("able to set log without permission");
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) {
0N/A try {
0N/A checkServerCallLog();
0N/A
0N/A if (!usingOld) {
0N/A verifyLog(clientCallOut, "outbound call");
0N/A System.setSecurityManager(new java.lang.SecurityManager());
0N/A checkPermissions();
0N/A }
0N/A System.err.println("TEST PASSED");
0N/A
0N/A } catch (Exception e) {
0N/A if (e instanceof RuntimeException) {
0N/A throw (RuntimeException) e;
0N/A }
0N/A TestLibrary.bomb("unexpected exception", e);
0N/A } finally {
0N/A TestLibrary.unexport(registry);
0N/A }
0N/A }
0N/A}