/* * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * @test TestExistingLogFile * @summary Ensure log files are archived/appended/truncated properly. * @library /testlibrary * @run driver TestExistingLogFile */ import java.nio.file.Files; import java.nio.file.Paths; import jdk.test.lib.ProcessTools; import jdk.test.lib.OutputAnalyzer; import static jdk.test.lib.Asserts.assertEquals; import static jdk.test.lib.Asserts.assertTrue; import static jdk.test.lib.Asserts.assertFalse; public class TestExistingLogFile { private static final String ARCHIVED_LOG_SUFFIX = ".0"; private static final String BASE_XLOG_CMD = "-Xlog:logging=trace:"; private static final String EXPECTED_LINE = "Log configuration fully initialized."; public static void main(String[] args) throws Exception { testArchiveMode(); testAppendMode(); testTruncateMode(); } private static void testArchiveMode() throws Exception { final String logFile = "archived.log"; final String archivedLogFile = logFile + ARCHIVED_LOG_SUFFIX; System.out.println("Testing archive mode"); runVMTwice(BASE_XLOG_CMD + logFile); assertTrue(Files.exists(Paths.get(archivedLogFile)), "Archived log file not found."); assertEquals(1L, occurencesInFile(EXPECTED_LINE, logFile), "Log file should contain exactly one occurence of the expected line."); assertEquals(1L, occurencesInFile(EXPECTED_LINE, archivedLogFile), "Archived log file should contain exactly one occurence of the expected line."); // Run it twice again make sure two new archive files are created runVMTwice(BASE_XLOG_CMD + logFile); assertTrue(Files.exists(Paths.get(logFile + ".1")), "Second archived log file not found."); assertTrue(Files.exists(Paths.get(logFile + ".2")), "Third archived log file not found."); } private static void testAppendMode() throws Exception { final String logFile = "appended.log"; final String archivedLogFile = logFile + ARCHIVED_LOG_SUFFIX; System.out.println("Testing append mode"); runVMTwice(BASE_XLOG_CMD + logFile + "::mode=append"); assertFalse(Files.exists(Paths.get(archivedLogFile)), "Log file was archived when it should have been appended."); assertEquals(2L, occurencesInFile(EXPECTED_LINE, logFile), "Appended log file should contain exactly two occurences of the expected line."); } private static void testTruncateMode() throws Exception { final String logFile = "truncated.log"; final String archivedLogFile = logFile + ARCHIVED_LOG_SUFFIX; System.out.println("Testing truncate mode"); runVMTwice(BASE_XLOG_CMD + logFile + "::mode=truncate"); assertFalse(Files.exists(Paths.get(archivedLogFile)), "Log file was archived when it should have been truncated."); assertEquals(1L, occurencesInFile(EXPECTED_LINE, logFile), "Truncated log file should contain exactly one occurence of the expected line."); } private static long occurencesInFile(final String string, final String file) throws Exception { return Files.lines(Paths.get(file)) .filter(s -> s.contains(string)).count(); } private static void runVMTwice(final String options) throws Exception { for (int i = 0; i < 2; i++) { ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(options, "-version"); Process p = pb.start(); OutputAnalyzer output = new OutputAnalyzer(p); output.shouldHaveExitValue(0); } } }