Path: blob/master/test/jdk/sun/rmi/runtime/Log/checkLogging/CheckLogging.java
41161 views
/*1* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 440264925* @summary RMI should use new logging APIs. Unit test to exercise26* RMI's use of the java.util.logging API.27* @author Laird Dornin28*29* @library ../../../../../java/rmi/testlibrary30* @modules java.rmi/sun.rmi.registry31* java.rmi/sun.rmi.server32* java.rmi/sun.rmi.transport33* java.rmi/sun.rmi.transport.tcp34* @build TestLibrary35* @run main/othervm -Djava.security.manager=allow CheckLogging36*/3738import java.util.logging.Level;39import java.util.logging.LogRecord;40import java.util.logging.Logger;41import java.util.logging.SimpleFormatter;42import java.util.logging.StreamHandler;4344import java.io.ByteArrayOutputStream;45import java.io.IOException;46import java.io.PrintStream;47import java.io.OutputStream;4849import java.rmi.RemoteException;50import java.rmi.Remote;51import java.rmi.Naming;52import java.rmi.registry.LocateRegistry;53import java.rmi.server.LogStream;54import java.rmi.server.RemoteServer;5556import java.rmi.registry.Registry;5758/**59* Perform following checks:60*61* 1. If using java.util.logging, turn on client call logger using62* system property, "sun.rmi.client.logCalls". Collect client call63* output using a custom stream handler. Verify client call output is64* generated and contains the string "outbound call".65*66* 2. Turn on server call using67* RemoteServer.setLog(ByteArrayOutputStream). Invoke some remote68* method calls verify logger output is non-null.69*70* Turn off server call log by doing setLog(null), verify output is71* zero length. Verify that RemoteServer.getLog == null72*73* Use setLog to turn call log back on. Invoke remote method that74* throws an exception and contains the string "exception".75*76* 3. Print directly to return value of RemoteServer.getLog(), verify77* logger output is non-null.78*/79public class CheckLogging {80private static int REGISTRY_PORT = -1;81private static String LOCATION;82private static Logger logger;8384private static final ByteArrayOutputStream clientCallOut =85new ByteArrayOutputStream();8687private static final boolean usingOld =88Boolean.getBoolean("sun.rmi.log.useOld");8990static {91System.setProperty("sun.rmi.client.logCalls", "true");92if (usingOld) {93System.err.println("set default stream");94LogStream.setDefaultStream(new PrintStream(clientCallOut));95} else {96logger = Logger.getLogger("sun.rmi.client.call");97logger.addHandler(new InternalStreamHandler(clientCallOut));98}99}100101/* use registry to generate client & server call log info */102private static Registry registry;103static {104try {105registry = TestLibrary.createRegistryOnEphemeralPort();106REGISTRY_PORT = TestLibrary.getRegistryPort(registry);107LOCATION = "rmi://localhost:" + REGISTRY_PORT + "/";108} catch (Exception e) {109TestLibrary.bomb("could not create registry");110}111}112113/**114* Used to collect output from specific loggers115*/116private static class InternalStreamHandler extends StreamHandler {117private InternalStreamHandler(OutputStream out) {118super(out, new SimpleFormatter());119setLevel(Level.ALL);120}121122public void publish(LogRecord record) {123super.publish(record);124flush();125}126127public void close() {128flush();129}130}131132/**133* Ensure that a log has some output and that it contains a134* certain string135*/136private static void verifyLog(ByteArrayOutputStream bout,137String mustContain)138{139byte[] bytes = bout.toByteArray();140if (bytes.length == 0) {141TestLibrary.bomb("log data length is zero");142} else if ((mustContain != null) &&143(bout.toString().indexOf(mustContain) < 0))144{145TestLibrary.bomb("log output did not contain: " + mustContain);146}147}148149/**150* Check serverCallLog output151*/152private static void checkServerCallLog() throws Exception {153ByteArrayOutputStream serverCallLog = new ByteArrayOutputStream();154RemoteServer.setLog(serverCallLog);155Naming.list(LOCATION);156verifyLog(serverCallLog, "list");157158serverCallLog.reset();159RemoteServer.setLog(null);160PrintStream callStream = RemoteServer.getLog();161if (callStream != null) {162TestLibrary.bomb("call stream not null after calling " +163"setLog(null)");164} else {165System.err.println("call stream should be null and it is");166}167Naming.list(LOCATION);168169if (usingOld) {170if (serverCallLog.toString().indexOf("UnicastServerRef") >= 0) {171TestLibrary.bomb("server call logging not turned off");172}173} else if (serverCallLog.toByteArray().length != 0) {174TestLibrary.bomb("call log contains output but it " +175"should be empty");176}177178serverCallLog.reset();179RemoteServer.setLog(serverCallLog);180try {181// generates a notbound exception182Naming.lookup(LOCATION + "notthere");183} catch (Exception e) {184}185verifyLog(serverCallLog, "exception");186187serverCallLog.reset();188RemoteServer.setLog(serverCallLog);189callStream = RemoteServer.getLog();190callStream.println("bingo, this is a getLog test");191verifyLog(serverCallLog, "bingo");192}193194private static void checkPermissions() {195SecurityException ex = null;196try {197// should fail for lack of LoggingPermission "control"198RemoteServer.setLog(System.err);199} catch (SecurityException e) {200System.err.println("security excepton caught correctly");201ex = e;202}203if (ex == null) {204TestLibrary.bomb("able to set log without permission");205}206}207208public static void main(String[] args) {209try {210checkServerCallLog();211212if (!usingOld) {213verifyLog(clientCallOut, "outbound call");214System.setSecurityManager(new java.lang.SecurityManager());215checkPermissions();216}217System.err.println("TEST PASSED");218219} catch (Exception e) {220if (e instanceof RuntimeException) {221throw (RuntimeException) e;222}223TestLibrary.bomb("unexpected exception", e);224} finally {225TestLibrary.unexport(registry);226}227}228}229230231