- Notifications
You must be signed in to change notification settings - Fork 44
Config add getBoolean, getInt, getLong, getDouble, getString #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits Select commit Hold shift + click to select a range
7837ceb add getByte, getShort, getInt, getLong, getFloat, getDouble
houzhizhen 236b89f improve exception message
houzhizhen 70ec30c remove getByte, getShort, getFloat
houzhizhen bba66ef throw ComputerException in getBoolean if can't convert the parameter …
houzhizhen cf3361d add test for null as default value
houzhizhen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions 151 computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| /* | ||
| * Copyright 2017 HugeGraph Authors | ||
| * | ||
| * 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 com.baidu.hugegraph.computer.core.config; | ||
| | ||
| import java.io.IOException; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| | ||
| import org.junit.Test; | ||
| | ||
| import com.baidu.hugegraph.computer.core.common.exception.ComputerException; | ||
| import com.baidu.hugegraph.testutil.Assert; | ||
| | ||
| public class ConfigTest { | ||
| | ||
| private static final String KEY = "algorithm.page_rank.key"; | ||
| private static final String KEY_TRUE = "algorithm.page_rank.key_true"; | ||
| private static final String KEY_FALSE = "algorithm.page_rank.key_false"; | ||
| private static final String KEY_MAX = "algorithm.page_rank.key_max"; | ||
| private static final String KEY_MIN = "algorithm.page_rank.key_min"; | ||
| private static final String KEY_EMPTY = "algorithm.page_rank.no_key"; | ||
| private static final String KEY_ABC = "algorithm.page_rank.abc"; | ||
| private static final String VALUE_ABC = "abc"; | ||
| | ||
| @Test | ||
| public void testGetBoolean() { | ||
| final boolean defaultValue = false; | ||
| Map<String, String> options = this.initialOptions(); | ||
| options.put(KEY_TRUE, Boolean.toString(Boolean.TRUE)); | ||
| options.put(KEY_FALSE, Boolean.toString(Boolean.FALSE)); | ||
| Config config = new Config(options); | ||
| Assert.assertTrue(config.getBoolean(KEY_TRUE, defaultValue)); | ||
| Assert.assertFalse(config.getBoolean(KEY_FALSE, defaultValue)); | ||
| Assert.assertFalse(config.getBoolean(KEY_EMPTY, defaultValue)); | ||
| Assert.assertThrows(ComputerException.class, () -> { | ||
| config.getBoolean(KEY_ABC, Boolean.TRUE); | ||
| } ,e -> { | ||
| Assert.assertContains("Can't parse boolean value", e.getMessage()); | ||
| }); | ||
| } | ||
| | ||
| @Test | ||
| public void testGetInt() { | ||
| final int defaultValue = 1; | ||
| Map<String, String> options = this.initialOptions(); | ||
| options.put(KEY_MAX, Integer.toString(Integer.MAX_VALUE)); | ||
| options.put(KEY_MIN, Integer.toString(Integer.MIN_VALUE)); | ||
| Config config = new Config(options); | ||
| Assert.assertEquals(Integer.MAX_VALUE, | ||
| config.getInt(KEY_MAX, defaultValue)); | ||
| Assert.assertEquals(Integer.MIN_VALUE, | ||
| config.getInt(KEY_MIN, defaultValue)); | ||
| Assert.assertEquals(defaultValue, | ||
| config.getInt(KEY_EMPTY, defaultValue)); | ||
| Assert.assertThrows(ComputerException.class, () -> { | ||
| config.getInt(KEY_ABC, defaultValue); | ||
| } ,e -> { | ||
| Assert.assertContains("Can't parse int value", e.getMessage()); | ||
| }); | ||
| } | ||
| | ||
| @Test | ||
| public void testGetLong() { | ||
| final long defaultValue = 1L; | ||
| Map<String, String> options = this.initialOptions(); | ||
| options.put(KEY_MAX, Long.toString(Long.MAX_VALUE)); | ||
| options.put(KEY_MIN, Long.toString(Long.MIN_VALUE)); | ||
| Config config = new Config(options); | ||
| Assert.assertEquals(Long.MAX_VALUE, | ||
| config.getLong(KEY_MAX, defaultValue)); | ||
| Assert.assertEquals(Long.MIN_VALUE, | ||
| config.getLong(KEY_MIN, defaultValue)); | ||
| Assert.assertEquals(defaultValue, | ||
| config.getLong(KEY_EMPTY, defaultValue)); | ||
| Assert.assertThrows(ComputerException.class, () -> { | ||
| config.getLong(KEY_ABC, defaultValue); | ||
| } ,e -> { | ||
| Assert.assertContains("Can't parse long value", e.getMessage()); | ||
| }); | ||
| } | ||
| | ||
| @Test | ||
| public void testGetDouble() throws IOException { | ||
| final double defaultValue = 1.0D; | ||
| final double delta = 0.0D; | ||
| Map<String, String> options = this.initialOptions(); | ||
| options.put(KEY_MAX, Double.toString(Double.MAX_VALUE)); | ||
| options.put(KEY_MIN, Double.toString(Double.MIN_VALUE)); | ||
| Config config = new Config(options); | ||
| Assert.assertEquals(Double.MAX_VALUE, | ||
| config.getDouble(KEY_MAX, defaultValue), | ||
| delta); | ||
| Assert.assertEquals(Double.MIN_VALUE, | ||
| config.getDouble(KEY_MIN, defaultValue), | ||
| delta); | ||
| Assert.assertEquals(defaultValue, | ||
| config.getDouble(KEY_EMPTY, defaultValue), | ||
| delta); | ||
| Assert.assertThrows(ComputerException.class, () -> { | ||
| config.getDouble(KEY_ABC, defaultValue); | ||
| } ,e -> { | ||
| Assert.assertContains("Can't parse double value", e.getMessage()); | ||
| }); | ||
| } | ||
| | ||
| @Test | ||
| public void testString() throws IOException { | ||
| String value = "The value of string"; | ||
| final String defaultValue = "The default value of string"; | ||
| Map<String, String> options = this.initialOptions(); | ||
| options.put(KEY, value); | ||
| Config config = new Config(options); | ||
| Assert.assertEquals(value, config.getString(KEY, defaultValue)); | ||
| Assert.assertEquals(value, config.getString(KEY, null)); | ||
| Assert.assertEquals(defaultValue, | ||
| config.getString(KEY_EMPTY, defaultValue)); | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test pass defaultValue=null | ||
| Assert.assertNull(config.getString(KEY_EMPTY, null)); | ||
| } | ||
| | ||
| private Map<String, String> initialOptions() { | ||
| Map<String, String> options = new HashMap<>(); | ||
| options.put(ComputerOptions.ALGORITHM_NAME.name(), "page_rank"); | ||
| options.put(ComputerOptions.VALUE_NAME.name(), "rank"); | ||
| options.put(ComputerOptions.EDGES_NAME.name(), "value"); | ||
| options.put(ComputerOptions.VALUE_TYPE.name(), "LONG"); | ||
| options.put(ComputerOptions.OUTPUT_WITH_ADJACENT_EDGES.name(), "false"); | ||
| options.put(ComputerOptions.OUTPUT_WITH_VERTEX_PROPERTIES.name(), | ||
| "false"); | ||
| options.put(ComputerOptions.OUTPUT_WITH_EDGE_PROPERTIES.name(), | ||
| "false"); | ||
| options.put(KEY_ABC, VALUE_ABC); | ||
| return options; | ||
| } | ||
| } | ||
30 changes: 30 additions & 0 deletions 30 computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTestSuite.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| * Copyright 2017 HugeGraph Authors | ||
| * | ||
| * 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 com.baidu.hugegraph.computer.core.config; | ||
| | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.Suite; | ||
| | ||
| @RunWith(Suite.class) | ||
| @Suite.SuiteClasses({ | ||
| ConfigTest.class | ||
| }) | ||
| public class ConfigTestSuite { | ||
| } |
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also update PR title