xiazcy commented on code in PR #3512: URL: https://github.com/apache/tinkerpop/pull/3512#discussion_r3604220754
########## gremlin-console/src/test/groovy/org/apache/tinkerpop/gremlin/console/PreferencesTest.groovy: ########## @@ -0,0 +1,198 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tinkerpop.gremlin.console + +import org.codehaus.groovy.tools.shell.util.Preferences as ShellPreferences +import org.codehaus.groovy.tools.shell.IO +import org.fusesource.jansi.Ansi +import org.junit.AfterClass +import org.junit.BeforeClass +import org.junit.Test + +import static org.junit.Assert.assertEquals + +/** + * Exercises the {@link Preferences} change listeners installed by {@code expandoMagic()}. Changing a preference + * through {@code org.codehaus.groovy.tools.shell.util.Preferences.put(key, value)} writes to the backing + * {@code java.util.prefs} store which asynchronously notifies the listeners, so the assertions poll for the + * expected static field update. + */ +class PreferencesTest { + + private static boolean originalAnsiEnabled + + // A known-valid jansi color name and a name guaranteed to make Colorizer.render throw. + private static final String VALID_COLOR = "red" + private static final String INVALID_COLOR = "notavalidcolorname" + + // Maps each color preference key to a closure that reads the corresponding static field. + private static final Map<String, Closure<String>> COLOR_FIELDS = [ + (Preferences.PREF_GREMLIN_COLOR) : { Preferences.gremlinColor }, + (Preferences.PREF_VERTEX_COLOR) : { Preferences.vertexColor }, + (Preferences.PREF_EDGE_COLOR) : { Preferences.edgeColor }, + (Preferences.PREF_ERROR_COLOR) : { Preferences.errorColor }, + (Preferences.PREF_INFO_COLOR) : { Preferences.infoColor }, + (Preferences.PREF_STRING_COLOR) : { Preferences.stringColor }, + (Preferences.PREF_NUMBER_COLOR) : { Preferences.numberColor }, + (Preferences.PREF_T_COLOR) : { Preferences.tColor }, + (Preferences.PREF_INPUT_PROMPT_COLOR) : { Preferences.inputPromptColor }, + (Preferences.PREF_RESULT_PROMPT_COLOR): { Preferences.resultPromptColor } + ] + + // Maps each color preference key to its default value used for the invalid-color fallback. + private static final Map<String, String> COLOR_DEFAULTS = [ + (Preferences.PREF_GREMLIN_COLOR) : Preferences.PREF_GREMLIN_COLOR_DEFAULT, + (Preferences.PREF_VERTEX_COLOR) : Preferences.PREF_VERTEX_COLOR_DEFAULT, + (Preferences.PREF_EDGE_COLOR) : Preferences.PREF_EDGE_COLOR_DEFAULT, + (Preferences.PREF_ERROR_COLOR) : Preferences.PREF_ERROR_COLOR_DEFAULT, + (Preferences.PREF_INFO_COLOR) : Preferences.PREF_INFO_COLOR_DEFAULT, + (Preferences.PREF_STRING_COLOR) : Preferences.PREF_STRING_COLOR_DEFAULT, + (Preferences.PREF_NUMBER_COLOR) : Preferences.PREF_NUMBER_COLOR_DEFAULT, + (Preferences.PREF_T_COLOR) : Preferences.PREF_T_COLOR_DEFAULT, + (Preferences.PREF_INPUT_PROMPT_COLOR) : Preferences.PREF_INPUT_PROMPT_COLOR_DEFAULT, + (Preferences.PREF_RESULT_PROMPT_COLOR): Preferences.PREF_RESULT_PROMPT_COLOR_DEFAULT + ] + + @BeforeClass + static void setUpClass() { + // Enable ansi so that Colorizer.render actually validates color names (and can throw on invalid ones), + // which drives the getValidColor catch/fallback branch. Preserve the prior state for restoration. + originalAnsiEnabled = Ansi.isEnabled() + Ansi.setEnabled(true) + Preferences.colors = true + + // Installs the change listeners AND loads default values. + Preferences.expandoMagic() + } + + @AfterClass + static void tearDownClass() { + // These tests write to the real per-user java.util.prefs store. Clear it so developer prefs aren't polluted. + ShellPreferences.clear() Review Comment: Just confirming that these won't clear existing user preferences? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
