Path: blob/master/test/jdk/sun/security/ssl/SSLLogger/LoggerDateFormatterTest.java
41152 views
/*1* Copyright (c) 2020, 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/**24* @test25* @bug 824530626* @summary Replace ThreadLocal date format with DateTimeFormatter27* @modules java.base/sun.security.ssl:+open28* @compile LoggerDateFormatterTest.java29* @run testng/othervm -Djavax.net.debug=all LoggerDateFormatterTest30*/3132import org.testng.annotations.BeforeTest;33import org.testng.annotations.Test;34import sun.security.ssl.SSLLogger;35import java.io.ByteArrayOutputStream;36import java.io.IOException;37import java.io.OutputStream;38import java.io.PrintStream;39import java.util.regex.Matcher;40import java.util.regex.Pattern;4142import static java.lang.System.out;43import static org.testng.Assert.fail;4445public class LoggerDateFormatterTest {4647SSLPrintStream sslStream;48static String year = "(\\|\\d\\d\\d\\d-\\d\\d-\\d\\d";49static String hour = "\\s\\d\\d:\\d\\d:\\d\\d\\.\\d\\d\\d\\s";50static String zone = "([A-Za-z]+([\\+\\-][0-2]?[0-9](\\:[0-5]?[0-9]))?))";51static Pattern pattern;52Matcher matcher;5354@BeforeTest55public void setUp() {56sslStream = new SSLPrintStream(System.err);57System.setErr(sslStream);58String format = year + hour + zone;59pattern = Pattern.compile(format);60}6162@Test63public void testDateFormat() {64SSLLogger.info("logging");65System.out.println("The value is: " + sslStream.bos.toString());66matcher = pattern.matcher(sslStream.bos.toString());67if (matcher.find()) {68out.println("Test Passed with value :" + matcher.group());69}70else {71fail("Test failed wrong SSL DateFormat");72}73}7475public static class SSLPrintStream extends PrintStream {7677public ByteArrayOutputStream bos; // Stream that accumulates System.err7879public SSLPrintStream(OutputStream out) {80super(out);81bos = new ByteArrayOutputStream();82}8384@Override85public void write(int b) {86super.write(b);87bos.write(b);88}8990@Override91public void write(byte[] buf, int off, int len) {92super.write(buf, off, len);93bos.write(buf, off, len);94}9596@Override97public void write(byte[] buf) throws IOException {98super.write(buf);99bos.write(buf);100}101102@Override103public void writeBytes(byte[] buf) {104super.writeBytes(buf);105bos.writeBytes(buf);106}107}108109}110111112