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