001/*
002 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package jdk.internal.jvmci.options.test;
024
025import static jdk.internal.jvmci.options.test.TestOptionValue.Options.*;
026import static org.junit.Assert.*;
027
028import java.util.*;
029
030import jdk.internal.jvmci.options.*;
031import jdk.internal.jvmci.options.OptionValue.*;
032
033import org.junit.*;
034
035public class TestOptionValue {
036
037    public static class Options {
038        public static final OptionValue<Boolean> Stable = new StableOptionValue<>(true);
039        public static final OptionValue<String> Mutable = new OptionValue<>("original");
040        public static final OptionValue<String> SecondMutable = new OptionValue<>("second");
041    }
042
043    static final OptionDescriptor stable = new OptionDescriptor("Stable", Boolean.class, "", Options.class, "Stable", Stable);
044    static final OptionDescriptor mutable = new OptionDescriptor("Mutable", String.class, "", Options.class, "Mutable", Mutable);
045    static final OptionDescriptor secondMutable = new OptionDescriptor("SecondMutable", String.class, "", Options.class, "SecondMutable", SecondMutable);
046
047    @Test
048    public void testMutable() {
049        assertEquals("original", Mutable.getValue());
050        try (OverrideScope s1 = OptionValue.override(Mutable, "override1")) {
051            assertEquals("override1", Mutable.getValue());
052            try (OverrideScope s2 = OptionValue.override(Mutable, "override2")) {
053                assertEquals("override2", Mutable.getValue());
054            }
055            assertEquals("override1", Mutable.getValue());
056            try (OverrideScope s3 = OptionValue.override(Mutable, "override3")) {
057                assertEquals("override3", Mutable.getValue());
058            }
059            assertEquals("override1", Mutable.getValue());
060        }
061        assertEquals("original", Mutable.getValue());
062        try (OverrideScope s1 = OptionValue.override(Mutable, "original")) {
063            assertEquals("original", Mutable.getValue());
064        }
065    }
066
067    @Test
068    public void testMultiple() {
069        assertEquals("original", Mutable.getValue());
070        assertEquals("second", SecondMutable.getValue());
071        try (OverrideScope s1 = OptionValue.override(Mutable, "override1", SecondMutable, "secondOverride1")) {
072            assertEquals("override1", Mutable.getValue());
073            assertEquals("secondOverride1", SecondMutable.getValue());
074            try (OverrideScope s2 = OptionValue.override(Mutable, "override2", SecondMutable, "secondOverride2")) {
075                assertEquals("override2", Mutable.getValue());
076                assertEquals("secondOverride2", SecondMutable.getValue());
077            }
078            assertEquals("override1", Mutable.getValue());
079            assertEquals("secondOverride1", SecondMutable.getValue());
080            try (OverrideScope s3 = OptionValue.override(Mutable, "override3", SecondMutable, "secondOverride3")) {
081                assertEquals("override3", Mutable.getValue());
082                assertEquals("secondOverride3", SecondMutable.getValue());
083            }
084            assertEquals("override1", Mutable.getValue());
085            assertEquals("secondOverride1", SecondMutable.getValue());
086        }
087        assertEquals("original", Mutable.getValue());
088        assertEquals("second", SecondMutable.getValue());
089        try (OverrideScope s1 = OptionValue.override(Mutable, "original", SecondMutable, "second")) {
090            assertEquals("original", Mutable.getValue());
091            assertEquals("second", SecondMutable.getValue());
092        }
093    }
094
095    @Test
096    public void testStable() {
097        assertTrue(Stable.getValue());
098        try (OverrideScope s = OptionValue.override(Stable, false)) {
099            fail("cannot override stable option");
100        } catch (IllegalArgumentException e) {
101            // expected
102        }
103    }
104
105    @Test
106    public void toStringTest() {
107        assertEquals("jdk.internal.jvmci.options.test.TestOptionValue$Options.Mutable=original", Mutable.toString());
108        try (OverrideScope s1 = OptionValue.override(Mutable, "override1")) {
109            assertEquals("jdk.internal.jvmci.options.test.TestOptionValue$Options.Mutable=override1", Mutable.toString());
110            try (OverrideScope s2 = OptionValue.override(Mutable, "override2")) {
111                assertEquals("jdk.internal.jvmci.options.test.TestOptionValue$Options.Mutable=override2", Mutable.toString());
112            }
113        }
114    }
115
116    @Test
117    public void getValuesTest() {
118        assertEquals(Arrays.asList("original"), Mutable.getValues(null));
119        assertEquals(Arrays.asList(true), Stable.getValues(null));
120        try (OverrideScope s1 = OptionValue.override(Mutable, "override1")) {
121            assertEquals(Arrays.asList("override1", "original"), Mutable.getValues(null));
122            try (OverrideScope s2 = OptionValue.override(Mutable, "override2")) {
123                assertEquals(Arrays.asList("override2", "override1", "original"), Mutable.getValues(null));
124            }
125        }
126    }
127}