1 /*
   2  * Copyright (c) 2014, 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 package printcodecache;
  24 
  25 import com.oracle.java.testlibrary.ExitCode;
  26 import com.oracle.java.testlibrary.cli.CommandLineOptionTest;
  27 import common.CodeCacheCLITestCase;
  28 import common.CodeCacheInfoFormatter;
  29 import common.CodeCacheOptions;
  30 import sun.hotspot.code.BlobType;
  31 
  32 import java.util.EnumSet;
  33 import java.util.stream.Collectors;
  34 
  35 /**
  36  * Runner implementation aimed to verify PrintCodeCache output.
  37  */
  38 public class PrintCodeCacheRunner implements CodeCacheCLITestCase.Runner {
  39     private final boolean printCodeCache;
  40 
  41     public PrintCodeCacheRunner(boolean printCodeCache) {
  42         this.printCodeCache = printCodeCache;
  43     }
  44 
  45     public PrintCodeCacheRunner() {
  46         this(true);
  47     }
  48 
  49     @Override
  50     public void run(CodeCacheCLITestCase.Description testCaseDescription,
  51             CodeCacheOptions options) throws Throwable {
  52         CodeCacheOptions expectedValues
  53                 = testCaseDescription.expectedValues(options);
  54 
  55         String[] expectedMessages
  56                 = testCaseDescription.involvedCodeHeaps.stream()
  57                         .map(heap -> CodeCacheInfoFormatter.forHeap(heap)
  58                                 .withSize(expectedValues.sizeForHeap(heap)))
  59                         .map(CodeCacheInfoFormatter::getInfoString)
  60                         .toArray(String[]::new);
  61 
  62         EnumSet<BlobType> unexpectedHeapsSet
  63                 = EnumSet.complementOf(testCaseDescription.involvedCodeHeaps);
  64 
  65         String[] unexpectedMessages = CodeCacheInfoFormatter.forHeaps(
  66                 unexpectedHeapsSet.toArray(
  67                         new BlobType[unexpectedHeapsSet.size()]));
  68 
  69         String description = String.format("JVM output should contain entries "
  70                 + "for following code heaps: [%s] and should not contain "
  71                 + "entries for following code heaps: [%s].",
  72                 testCaseDescription.involvedCodeHeaps.stream()
  73                         .map(BlobType::name)
  74                         .collect(Collectors.joining(", ")),
  75                 unexpectedHeapsSet.stream()
  76                         .map(BlobType::name)
  77                         .collect(Collectors.joining(", ")));
  78 
  79         CommandLineOptionTest.verifySameJVMStartup(expectedMessages,
  80                 unexpectedMessages, "JVM startup failure is not expected, "
  81                 + "since all options have allowed values", description,
  82                 ExitCode.OK,
  83                 testCaseDescription.getTestOptions(options,
  84                         CommandLineOptionTest.prepareBooleanFlag(
  85                                 "PrintCodeCache", printCodeCache)));
  86     }
  87 }