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