1 /*
   2  * Copyright (c) 2013, 2018, 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 
  25 package org.graalvm.compiler.hotspot.test;
  26 
  27 import static org.graalvm.compiler.debug.DebugOptions.Dump;
  28 import static org.graalvm.compiler.debug.DebugOptions.MethodFilter;
  29 import static org.graalvm.compiler.debug.DebugOptions.PrintGraph;
  30 import static org.graalvm.compiler.test.SubprocessUtil.getVMCommandLine;
  31 import static org.graalvm.compiler.test.SubprocessUtil.withoutDebuggerArguments;
  32 
  33 import java.io.File;
  34 import java.io.FileOutputStream;
  35 import java.io.IOException;
  36 import java.io.PrintStream;
  37 import java.util.List;
  38 
  39 import org.graalvm.compiler.core.test.GraalCompilerTest;
  40 import org.graalvm.compiler.test.SubprocessUtil;
  41 import org.graalvm.compiler.test.SubprocessUtil.Subprocess;
  42 import org.junit.Assert;
  43 import org.junit.Test;
  44 
  45 /**
  46  * Tests reading options from a file specified by the {@code graal.options.file}.
  47  */
  48 public class OptionsInFileTest extends GraalCompilerTest {
  49     @Test
  50     public void test() throws IOException, InterruptedException {
  51         String methodFilterValue = "a very unlikely method name";
  52         String debugFilterValue = "a very unlikely debug scope";
  53         File optionsFile = File.createTempFile("options", ".properties").getAbsoluteFile();
  54         try {
  55             Assert.assertFalse(methodFilterValue.equals(MethodFilter.getDefaultValue()));
  56             Assert.assertFalse(debugFilterValue.equals(Dump.getDefaultValue()));
  57             Assert.assertTrue(PrintGraph.getDefaultValue());
  58 
  59             try (PrintStream out = new PrintStream(new FileOutputStream(optionsFile))) {
  60                 out.println(MethodFilter.getName() + "=" + methodFilterValue);
  61                 out.println(Dump.getName() + "=" + debugFilterValue);
  62                 out.println(PrintGraph.getName() + " = false");
  63             }
  64 
  65             List<String> vmArgs = withoutDebuggerArguments(getVMCommandLine());
  66             vmArgs.removeIf(a -> a.startsWith("-Dgraal."));
  67             vmArgs.add("-Dgraal.options.file=" + optionsFile);
  68             vmArgs.add("-XX:+JVMCIPrintProperties");
  69             Subprocess proc = SubprocessUtil.java(vmArgs);
  70             String[] expected = {
  71                             "graal.MethodFilter := \"a very unlikely method name\"",
  72                             "graal.Dump := \"a very unlikely debug scope\"",
  73                             "graal.PrintGraph := false"};
  74             for (String line : proc.output) {
  75                 for (int i = 0; i < expected.length; i++) {
  76                     if (expected[i] != null && line.contains(expected[i])) {
  77                         expected[i] = null;
  78                     }
  79                 }
  80             }
  81 
  82             for (int i = 0; i < expected.length; i++) {
  83                 if (expected[i] != null) {
  84                     Assert.fail(String.format("Did not find '%s' in output of command:%n%s", expected[i], proc));
  85                 }
  86             }
  87         } finally {
  88             optionsFile.delete();
  89         }
  90     }
  91 }