/* * Copyright (c) 2015, 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. */ import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Paths; /** * This is a base class for any tests which are intended to test -XX:Print* * options by parsing GC log output. It encapsulates reading GC's log * output from the log file and provides that output to the tests. * * NOTE: It assumes that the log filename is determined from the test's class * name by removing leading 'Test' and adding '.txt' suffix. For instance, * the test class TestWhatever corresponds to the log filename Whatever.txt. * */ public abstract class AbstractPrintGCTest { private final String logFileName; /** * Constructs a test object. Binds the test's name with the GC's log * filename. */ public AbstractPrintGCTest() { String className = getClass().getName(); if (!className.startsWith("Test")) { throw new IllegalStateException( "The test's class name shall starts with 'Test'"); } logFileName = getClass().getName().substring("Test".length()) + ".txt"; } /** * Retrieves content of GC's log file. */ public final String getContent() { try { return new String(Files.readAllBytes(Paths.get(logFileName)), Charset.defaultCharset()); } catch (IOException e) { throw new RuntimeException( "Failed to retrieve content from the log file: " + logFileName, e); } } /** * The test code goes here. */ public abstract void runTest(); }