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