1 /*
   2  * Copyright (c) 2015, 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 
  24 import java.io.IOException;
  25 import java.nio.charset.Charset;
  26 import java.nio.file.Files;
  27 import java.nio.file.Paths;
  28 
  29 /**
  30  * This is a base class for any tests which are intended to test -XX:Print*
  31  * options by parsing GC log output. It encapsulates reading GC's log
  32  * output from the log file and provides that output to the tests.
  33  *
  34  * NOTE: It assumes that the log filename is determined from the test's class
  35  * name by removing leading 'Test' and adding '.txt' suffix. For instance,
  36  * the test class TestWhatever corresponds to the log filename Whatever.txt.
  37  *
  38  */
  39 public abstract class AbstractPrintGCTest {
  40 
  41     private final String logFileName;
  42 
  43     /**
  44      * Constructs a test object. Binds the test's name with the GC's log 
  45      * filename.
  46      */
  47     public AbstractPrintGCTest() {
  48         String className = getClass().getName();
  49         if (!className.startsWith("Test")) {
  50             throw new IllegalStateException(
  51                 "The test's class name shall starts with 'Test'");
  52         }
  53         logFileName = getClass().getName().substring("Test".length()) 
  54             + ".txt";
  55     }
  56 
  57     /**
  58      * Retrieves content of GC's log file.
  59      */
  60     public final String getContent() {
  61         try {
  62             return new String(Files.readAllBytes(Paths.get(logFileName)),
  63                 Charset.defaultCharset());
  64         } catch (IOException e) {
  65             throw new RuntimeException(
  66                 "Failed to retrieve content from the log file: "
  67                     + logFileName, e);
  68         }
  69     }
  70 
  71     /**
  72      * The test code goes here.
  73      */
  74     public abstract void runTest();
  75 }