1 /*
   2  * Copyright (c) 2013, 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 /**
  25  * @test
  26  * @run junit org.graalvm.compiler.options.test.TestOptionValue
  27  */
  28 
  29 package org.graalvm.compiler.options.test;
  30 
  31 import static org.graalvm.compiler.options.test.TestOptionValue.Options.Mutable;
  32 import static org.graalvm.compiler.options.test.TestOptionValue.Options.SecondMutable;
  33 import static org.graalvm.compiler.options.test.TestOptionValue.Options.Stable;
  34 import static org.junit.Assert.assertEquals;
  35 import static org.junit.Assert.assertTrue;
  36 import static org.junit.Assert.fail;
  37 
  38 import java.util.Arrays;
  39 
  40 import org.junit.Test;
  41 
  42 import org.graalvm.compiler.options.OptionDescriptor;
  43 import org.graalvm.compiler.options.OptionValue;
  44 import org.graalvm.compiler.options.StableOptionValue;
  45 import org.graalvm.compiler.options.OptionValue.OverrideScope;
  46 
  47 @SuppressWarnings("try")
  48 public class TestOptionValue {
  49 
  50     public static class Options {
  51         public static final OptionValue<Boolean> Stable = new StableOptionValue<>(true);
  52         public static final OptionValue<String> Mutable = new OptionValue<>("original");
  53         public static final OptionValue<String> SecondMutable = new OptionValue<>("second");
  54     }
  55 
  56     static final OptionDescriptor stable = OptionDescriptor.create("Stable", Boolean.class, "", Options.class, "Stable", Stable);
  57     static final OptionDescriptor mutable = OptionDescriptor.create("Mutable", String.class, "", Options.class, "Mutable", Mutable);
  58     static final OptionDescriptor secondMutable = OptionDescriptor.create("SecondMutable", String.class, "", Options.class, "SecondMutable", SecondMutable);
  59 
  60     @Test
  61     public void testMutable() {
  62         assertEquals("original", Mutable.getValue());
  63         try (OverrideScope s1 = OptionValue.override(Mutable, "override1")) {
  64             assertEquals("override1", Mutable.getValue());
  65             try (OverrideScope s2 = OptionValue.override(Mutable, "override2")) {
  66                 assertEquals("override2", Mutable.getValue());
  67             }
  68             assertEquals("override1", Mutable.getValue());
  69             try (OverrideScope s3 = OptionValue.override(Mutable, "override3")) {
  70                 assertEquals("override3", Mutable.getValue());
  71             }
  72             assertEquals("override1", Mutable.getValue());
  73         }
  74         assertEquals("original", Mutable.getValue());
  75         try (OverrideScope s1 = OptionValue.override(Mutable, "original")) {
  76             assertEquals("original", Mutable.getValue());
  77         }
  78     }
  79 
  80     @Test
  81     public void testMultiple() {
  82         assertEquals("original", Mutable.getValue());
  83         assertEquals("second", SecondMutable.getValue());
  84         try (OverrideScope s1 = OptionValue.override(Mutable, "override1", SecondMutable, "secondOverride1")) {
  85             assertEquals("override1", Mutable.getValue());
  86             assertEquals("secondOverride1", SecondMutable.getValue());
  87             try (OverrideScope s2 = OptionValue.override(Mutable, "override2", SecondMutable, "secondOverride2")) {
  88                 assertEquals("override2", Mutable.getValue());
  89                 assertEquals("secondOverride2", SecondMutable.getValue());
  90             }
  91             assertEquals("override1", Mutable.getValue());
  92             assertEquals("secondOverride1", SecondMutable.getValue());
  93             try (OverrideScope s3 = OptionValue.override(Mutable, "override3", SecondMutable, "secondOverride3")) {
  94                 assertEquals("override3", Mutable.getValue());
  95                 assertEquals("secondOverride3", SecondMutable.getValue());
  96             }
  97             assertEquals("override1", Mutable.getValue());
  98             assertEquals("secondOverride1", SecondMutable.getValue());
  99         }
 100         assertEquals("original", Mutable.getValue());
 101         assertEquals("second", SecondMutable.getValue());
 102         try (OverrideScope s1 = OptionValue.override(Mutable, "original", SecondMutable, "second")) {
 103             assertEquals("original", Mutable.getValue());
 104             assertEquals("second", SecondMutable.getValue());
 105         }
 106     }
 107 
 108     @Test
 109     public void testStable() {
 110         assertTrue(Stable.getValue());
 111         try (OverrideScope s = OptionValue.override(Stable, false)) {
 112             fail("cannot override stable option");
 113         } catch (IllegalArgumentException e) {
 114             // expected
 115         }
 116     }
 117 
 118     @Test
 119     public void toStringTest() {
 120         assertEquals("Mutable=original", Mutable.toString());
 121         try (OverrideScope s1 = OptionValue.override(Mutable, "override1")) {
 122             assertEquals("Mutable=override1", Mutable.toString());
 123             try (OverrideScope s2 = OptionValue.override(Mutable, "override2")) {
 124                 assertEquals("Mutable=override2", Mutable.toString());
 125             }
 126         }
 127     }
 128 
 129     @Test
 130     public void getValuesTest() {
 131         assertEquals(Arrays.asList("original"), Mutable.getValues(null));
 132         assertEquals(Arrays.asList(true), Stable.getValues(null));
 133         try (OverrideScope s1 = OptionValue.override(Mutable, "override1")) {
 134             assertEquals(Arrays.asList("override1", "original"), Mutable.getValues(null));
 135             try (OverrideScope s2 = OptionValue.override(Mutable, "override2")) {
 136                 assertEquals(Arrays.asList("override2", "override1", "original"), Mutable.getValues(null));
 137             }
 138         }
 139     }
 140 }