1/* 2 * Copyright (c) 2017, 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 24import java.io.ByteArrayOutputStream; 25import java.io.IOException; 26import java.io.OutputStream; 27import java.io.PrintStream; 28import java.lang.System.Logger; 29import java.lang.System.Logger.Level; 30import java.lang.System.LoggerFinder; 31import java.util.Collections; 32import java.util.Enumeration; 33import java.util.Locale; 34import java.util.Map; 35import java.util.Objects; 36import java.util.ResourceBundle; 37// Can't use testng because testng requires java.logging 38//import org.testng.annotations.Test; 39 40/** 41 * @test 42 * @bug 8177835 8179222 43 * @summary Checks that the DefaultLoggerFinder and LoggingProviderImpl 44 * implementations of the System.LoggerFinder conform to the 45 * LoggerFinder specification, in particular with respect to 46 * throwing NullPointerException. The test uses --limit-module 47 * to force the selection of one or the other. 48 * @author danielfuchs 49 * @build LoggerFinderAPI 50 * @run main/othervm --limit-modules java.base,java.logging 51 * -Djava.util.logging.SimpleFormatter.format=LOG-%4$s:-[%2$s]-%5$s%6$s%n 52 * LoggerFinderAPI 53 * @run main/othervm -Djdk.system.logger.format=LOG-%4$s:-[%2$s]-%5$s%6$s%n 54 * --limit-modules java.base 55 * LoggerFinderAPI 56 */ 57public class LoggerFinderAPI { 58 59 // Simplified log format string. No white space for main/othervm line 60 static final String TEST_FORMAT = "LOG-%4$s:-[%2$s]-%5$s%6$s%n"; 61 static final String JDK_FORMAT_PROP_KEY = "jdk.system.logger.format"; 62 static final String JUL_FORMAT_PROP_KEY = 63 "java.util.logging.SimpleFormatter.format"; 64 static final String MESSAGE = "{0} with {1}: PASSED"; 65 static final String LOCALIZED = "[localized] "; 66 67 static class RecordStream extends OutputStream { 68 static final Object LOCK = new Object[0]; 69 final PrintStream out; 70 final PrintStream err; 71 final ByteArrayOutputStream bos = new ByteArrayOutputStream(); 72 boolean record; 73 RecordStream(PrintStream out, PrintStream err) { 74 this.out = out; 75 this.err = err; 76 } 77 78 @Override 79 public void write(int i) throws IOException { 80 if (record) { 81 bos.write(i); 82 out.write(i); 83 } else { 84 err.write(i); 85 } 86 } 87 88 void startRecording() { 89 out.flush(); 90 err.flush(); 91 bos.reset(); 92 record = true; 93 } 94 byte[] stopRecording() { 95 out.flush(); 96 err.flush(); 97 record = false; 98 return bos.toByteArray(); 99 } 100 } 101 102 static final PrintStream ERR = System.err; 103 static final PrintStream OUT = System.out; 104 static final RecordStream LOG_STREAM = new RecordStream(OUT, ERR); 105 static { 106 Locale.setDefault(Locale.US); 107 PrintStream perr = new PrintStream(LOG_STREAM); 108 System.setErr(perr); 109 } 110 111 public static class MyResourceBundle extends ResourceBundle { 112 final Map<String, String> map = Map.of(MESSAGE, LOCALIZED + MESSAGE); 113 @Override 114 protected Object handleGetObject(String string) { 115 return map.get(string); 116 } 117 118 @Override 119 public Enumeration<String> getKeys() { 120 return Collections.enumeration(map.keySet()); 121 } 122 123 } 124 125 public static class EmptyResourceBundle extends ResourceBundle { 126 @Override 127 protected Object handleGetObject(String string) { 128 return null; 129 } 130 131 @Override 132 public Enumeration<String> getKeys() { 133 return Collections.emptyEnumeration(); 134 } 135 136 } 137 138 public static void main(String[] args) { 139 // Set on the command line, to ensure that the test will fail if 140 // the 'wrong' provider gets selected. 141 // System.setProperty(JDK_FORMAT_PROP_KEY, TEST_FORMAT); 142 // System.setProperty(JUL_FORMAT_PROP_KEY, TEST_FORMAT); 143 LoggerFinder finder = System.LoggerFinder.getLoggerFinder(); 144 System.out.println("LoggerFinder is " + finder.getClass().getName()); 145 146 LoggerFinderAPI apiTest = new LoggerFinderAPI(); 147 for (Object[] params : getLoggerDataProvider()) { 148 @SuppressWarnings("unchecked") 149 Class<? extends Throwable> throwableClass = 150 Throwable.class.getClass().cast(params[3]); 151 apiTest.testGetLogger((String)params[0], 152 (String)params[1], 153 (Module)params[2], 154 throwableClass); 155 } 156 for (Object[] params : getLocalizedLoggerDataProvider()) { 157 @SuppressWarnings("unchecked") 158 Class<? extends Throwable> throwableClass = 159 Throwable.class.getClass().cast(params[4]); 160 apiTest.testGetLocalizedLogger((String)params[0], 161 (String)params[1], 162 (ResourceBundle)params[2], 163 (Module)params[3], 164 throwableClass); 165 } 166 } 167 168 //Can't use testng because testng requires java.logging 169 //@Test(dataProvider = "testGetLoggerDataProvider") 170 void testGetLogger(String desc, String name, Module mod, Class<? extends Throwable> thrown) { 171 try { 172 LoggerFinder finder = System.LoggerFinder.getLoggerFinder(); 173 Logger logger = finder.getLogger(name, mod); 174 if (thrown != null) { 175 throw new AssertionError("Exception " + thrown.getName() 176 + " not thrown for " 177 + "LoggerFinder.getLogger" 178 + " with " + desc); 179 } 180 // Make sure we don't fail if tests are run in parallel 181 synchronized(RecordStream.LOCK) { 182 LOG_STREAM.startRecording(); 183 byte[] logged = null; 184 try { 185 logger.log(Level.INFO, "{0} with {1}: PASSED", 186 "LoggerFinder.getLogger", 187 desc); 188 } finally { 189 logged = LOG_STREAM.stopRecording(); 190 } 191 check(logged, "testGetLogger", desc, null, 192 "LoggerFinder.getLogger"); 193 } 194 } catch (Throwable x) { 195 if (thrown != null && thrown.isInstance(x)) { 196 System.out.printf("Got expected exception for %s with %s: %s\n", 197 "LoggerFinder.getLogger", desc, String.valueOf(x)); 198 } else throw x; 199 } 200 } 201 202 //Can't use testng because testng requires java.logging 203 //@Test(dataProvider = "getLocalizedLoggerDataProvider") 204 void testGetLocalizedLogger(String desc, String name, ResourceBundle bundle, 205 Module mod, Class<? extends Throwable> thrown) { 206 try { 207 LoggerFinder finder = System.LoggerFinder.getLoggerFinder(); 208 Logger logger = finder.getLocalizedLogger(name, bundle, mod); 209 if (thrown != null) { 210 throw new AssertionError("Exception " + thrown.getName() 211 + " not thrown for " 212 + "LoggerFinder.getLocalizedLogger" 213 + " with " + desc); 214 } 215 // Make sure we don't fail if tests are run in parallel 216 synchronized(RecordStream.LOCK) { 217 LOG_STREAM.startRecording(); 218 byte[] logged = null; 219 try { 220 logger.log(Level.INFO, MESSAGE, 221 "LoggerFinder.getLocalizedLogger", 222 desc); 223 } finally { 224 logged = LOG_STREAM.stopRecording(); 225 } 226 check(logged, "testGetLocalizedLogger", desc, bundle, 227 "LoggerFinder.getLocalizedLogger"); 228 } 229 } catch (Throwable x) { 230 if (thrown != null && thrown.isInstance(x)) { 231 System.out.printf("Got expected exception for %s with %s: %s\n", 232 "LoggerFinder.getLocalizedLogger", desc, String.valueOf(x)); 233 } else throw x; 234 } 235 } 236 237 private void check(byte[] logged, String test, String desc, 238 ResourceBundle bundle, String meth) { 239 String msg = new String(logged); 240 String localizedPrefix = 241 ((bundle==null || bundle==EMPTY_BUNDLE)?"":LOCALIZED); 242 String expected = String.format(TEST_FORMAT, null, 243 "LoggerFinderAPI " + test, null, Level.INFO.name(), 244 localizedPrefix + meth + " with " + desc + ": PASSED", 245 ""); 246 if (!Objects.equals(msg, expected)) { 247 throw new AssertionError("Expected log message not found: " 248 + "\n\texpected: " + expected 249 + "\n\tretrieved: " + msg); 250 } 251 } 252 253 254 static final Module MODULE = LoggerFinderAPI.class.getModule(); 255 static final ResourceBundle BUNDLE = new MyResourceBundle(); 256 static final ResourceBundle EMPTY_BUNDLE = new EmptyResourceBundle(); 257 static final Object[][] GET_LOGGER = { 258 {"null name", null, MODULE , NullPointerException.class}, 259 {"null module", "foo", null, NullPointerException.class}, 260 {"null name and module", null, null, NullPointerException.class}, 261 {"non null name and module", "foo", MODULE, null}, 262 }; 263 static final Object[][] GET_LOCALIZED_LOGGER = { 264 {"null name", null, BUNDLE, MODULE , NullPointerException.class}, 265 {"null module", "foo", BUNDLE, null, NullPointerException.class}, 266 {"null name and module, non null bundle", null, BUNDLE, null, NullPointerException.class}, 267 {"non null name, module, and bundle", "foo", BUNDLE, MODULE, null}, 268 {"null name and bundle", null, null, MODULE , NullPointerException.class}, 269 {"null module and bundle", "foo", null, null, NullPointerException.class}, 270 {"null name and module and bundle", null, null, null, NullPointerException.class}, 271 {"non null name and module, null bundle", "foo", null, MODULE, null}, 272 // tests that MissingResourceBundle is not propagated to the caller of 273 // logger.log() if the key is not found in the resource bundle 274 {"non null name, module, and empty bundle", "foo", EMPTY_BUNDLE, MODULE, null}, 275 }; 276 public static Object[][] getLoggerDataProvider() { 277 return GET_LOGGER; 278 } 279 public static Object[][] getLocalizedLoggerDataProvider() { 280 return GET_LOCALIZED_LOGGER; 281 } 282} 283