Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/rmi/runtime/Log/checkLogging/CheckLogging.java
38867 views
/*1* Copyright (c) 2001, 2013, 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* @build TestLibrary31* @run main/othervm CheckLogging32*/3334import java.util.logging.Level;35import java.util.logging.LogRecord;36import java.util.logging.Logger;37import java.util.logging.SimpleFormatter;38import java.util.logging.StreamHandler;3940import java.io.ByteArrayOutputStream;41import java.io.IOException;42import java.io.PrintStream;43import java.io.OutputStream;4445import java.rmi.RemoteException;46import java.rmi.Remote;47import java.rmi.Naming;48import java.rmi.registry.LocateRegistry;49import java.rmi.server.LogStream;50import java.rmi.server.RemoteServer;5152import java.rmi.registry.Registry;5354/**55* Perform following checks:56*57* 1. If using java.util.logging, turn on client call logger using58* system property, "sun.rmi.client.logCalls". Collect client call59* output using a custom stream handler. Verify client call output is60* generated and contains the string "outbound call".61*62* 2. Turn on server call using63* RemoteServer.setLog(ByteArrayOutputStream). Invoke some remote64* method calls verify logger output is non-null.65*66* Turn off server call log by doing setLog(null), verify output is67* zero length. Verify that RemoteServer.getLog == null68*69* Use setLog to turn call log back on. Invoke remote method that70* throws an exception and contains the string "exception".71*72* 3. Print directly to return value of RemoteServer.getLog(), verify73* logger output is non-null.74*/75public class CheckLogging {76private static int REGISTRY_PORT = -1;77private static String LOCATION;78private static Logger logger;7980private static final ByteArrayOutputStream clientCallOut =81new ByteArrayOutputStream();8283private static final boolean usingOld =84Boolean.getBoolean("sun.rmi.log.useOld");8586static {87System.setProperty("sun.rmi.client.logCalls", "true");88if (usingOld) {89System.err.println("set default stream");90LogStream.setDefaultStream(new PrintStream(clientCallOut));91} else {92logger = Logger.getLogger("sun.rmi.client.call");93logger.addHandler(new InternalStreamHandler(clientCallOut));94}95}9697/* use registry to generate client & server call log info */98private static Registry registry;99static {100try {101registry = TestLibrary.createRegistryOnUnusedPort();102REGISTRY_PORT = TestLibrary.getRegistryPort(registry);103LOCATION = "rmi://localhost:" + REGISTRY_PORT + "/";104} catch (Exception e) {105TestLibrary.bomb("could not create registry");106}107}108109/**110* Used to collect output from specific loggers111*/112private static class InternalStreamHandler extends StreamHandler {113private InternalStreamHandler(OutputStream out) {114super(out, new SimpleFormatter());115setLevel(Level.ALL);116}117118public void publish(LogRecord record) {119super.publish(record);120flush();121}122123public void close() {124flush();125}126}127128/**129* Ensure that a log has some output and that it contains a130* certain string131*/132private static void verifyLog(ByteArrayOutputStream bout,133String mustContain)134{135byte[] bytes = bout.toByteArray();136if (bytes.length == 0) {137TestLibrary.bomb("log data length is zero");138} else if ((mustContain != null) &&139(bout.toString().indexOf(mustContain) < 0))140{141TestLibrary.bomb("log output did not contain: " + mustContain);142}143}144145/**146* Check serverCallLog output147*/148private static void checkServerCallLog() throws Exception {149ByteArrayOutputStream serverCallLog = new ByteArrayOutputStream();150RemoteServer.setLog(serverCallLog);151Naming.list(LOCATION);152verifyLog(serverCallLog, "list");153154serverCallLog.reset();155RemoteServer.setLog(null);156PrintStream callStream = RemoteServer.getLog();157if (callStream != null) {158TestLibrary.bomb("call stream not null after calling " +159"setLog(null)");160} else {161System.err.println("call stream should be null and it is");162}163Naming.list(LOCATION);164165if (usingOld) {166if (serverCallLog.toString().indexOf("UnicastServerRef") >= 0) {167TestLibrary.bomb("server call logging not turned off");168}169} else if (serverCallLog.toByteArray().length != 0) {170TestLibrary.bomb("call log contains output but it " +171"should be empty");172}173174serverCallLog.reset();175RemoteServer.setLog(serverCallLog);176try {177// generates a notbound exception178Naming.lookup(LOCATION + "notthere");179} catch (Exception e) {180}181verifyLog(serverCallLog, "exception");182183serverCallLog.reset();184RemoteServer.setLog(serverCallLog);185callStream = RemoteServer.getLog();186callStream.println("bingo, this is a getLog test");187verifyLog(serverCallLog, "bingo");188}189190private static void checkPermissions() {191SecurityException ex = null;192try {193// should fail for lack of LoggingPermission "control"194RemoteServer.setLog(System.err);195} catch (SecurityException e) {196System.err.println("security excepton caught correctly");197ex = e;198}199if (ex == null) {200TestLibrary.bomb("able to set log without permission");201}202}203204public static void main(String[] args) {205try {206checkServerCallLog();207208if (!usingOld) {209verifyLog(clientCallOut, "outbound call");210System.setSecurityManager(new java.lang.SecurityManager());211checkPermissions();212}213System.err.println("TEST PASSED");214215} catch (Exception e) {216if (e instanceof RuntimeException) {217throw (RuntimeException) e;218}219TestLibrary.bomb("unexpected exception", e);220} finally {221TestLibrary.unexport(registry);222}223}224}225226227