From 7837ceb5c19a3d39aa7e94d47e31fae45fc40120 Mon Sep 17 00:00:00 2001 From: Hou Zhizhen Date: Fri, 12 Mar 2021 16:56:15 +0800 Subject: [PATCH 1/5] add getByte, getShort, getInt, getLong, getFloat, getDouble --- .../computer/core/config/Config.java | 58 +++++++ .../computer/core/UnitTestSuite.java | 2 + .../computer/core/config/ConfigTest.java | 160 ++++++++++++++++++ .../computer/core/config/ConfigTestSuite.java | 30 ++++ 4 files changed, 250 insertions(+) create mode 100644 computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTest.java create mode 100644 computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTestSuite.java diff --git a/computer-core/src/main/java/com/baidu/hugegraph/computer/core/config/Config.java b/computer-core/src/main/java/com/baidu/hugegraph/computer/core/config/Config.java index 8866579d5..c14004183 100644 --- a/computer-core/src/main/java/com/baidu/hugegraph/computer/core/config/Config.java +++ b/computer-core/src/main/java/com/baidu/hugegraph/computer/core/config/Config.java @@ -86,6 +86,64 @@ public R get(TypedOption option) { return this.allConfig.get(option); } + public int getByte(String key, byte defaultValue) { + String value = this.allConfig.getString(key); + if (value == null) { + return defaultValue; + } else { + return Byte.parseByte(value); + } + } + + public int getShort(String key, short defaultValue) { + String value = this.allConfig.getString(key); + if (value == null) { + return defaultValue; + } else { + return Short.parseShort(value); + } + } + + public int getInt(String key, int defaultValue) { + String value = this.allConfig.getString(key); + if (value == null) { + return defaultValue; + } else { + return Integer.parseInt(value); + } + } + + public long getLong(String key, long defaultValue) { + String value = this.allConfig.getString(key); + if (value == null) { + return defaultValue; + } else { + return Long.parseLong(value); + } + } + + public float getFloat(String key, float defaultValue) { + String value = this.allConfig.getString(key); + if (value == null) { + return defaultValue; + } else { + return Float.parseFloat(value); + } + } + + public double getDouble(String key, double defaultValue) { + String value = this.allConfig.getString(key); + if (value == null) { + return defaultValue; + } else { + return Double.parseDouble(value); + } + } + + public String getString(String key, String defaultValue) { + return this.allConfig.getString(key, defaultValue); + } + public String algorithmName() { return this.hotConfig.algorithmName(); } diff --git a/computer-test/src/main/java/com/baidu/hugegraph/computer/core/UnitTestSuite.java b/computer-test/src/main/java/com/baidu/hugegraph/computer/core/UnitTestSuite.java index e7b42632b..4826092a2 100644 --- a/computer-test/src/main/java/com/baidu/hugegraph/computer/core/UnitTestSuite.java +++ b/computer-test/src/main/java/com/baidu/hugegraph/computer/core/UnitTestSuite.java @@ -29,6 +29,7 @@ import com.baidu.hugegraph.computer.core.combiner.CombinerTestSuite; import com.baidu.hugegraph.computer.core.common.CommonTestSuite; import com.baidu.hugegraph.computer.core.config.ComputerOptions; +import com.baidu.hugegraph.computer.core.config.ConfigTestSuite; import com.baidu.hugegraph.computer.core.graph.GraphTestSuite; import com.baidu.hugegraph.computer.core.input.InputTestSuite; import com.baidu.hugegraph.computer.core.io.IOTestSuite; @@ -40,6 +41,7 @@ @Suite.SuiteClasses({ AllocatorTestSuite.class, CommonTestSuite.class, + ConfigTestSuite.class, BspTestSuite.class, CombinerTestSuite.class, GraphTestSuite.class, diff --git a/computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTest.java b/computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTest.java new file mode 100644 index 000000000..ece35eb60 --- /dev/null +++ b/computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTest.java @@ -0,0 +1,160 @@ +/* + * 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.Assert; +import org.junit.Test; + +public class ConfigTest { + + private static final String KEY = "algorithm.page_rank.key"; + 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"; + + @Test + public void testGetByte() throws IOException { + final byte defaultValue = (byte) 1; + Map options = this.initialOptions(); + options.put(KEY_MAX, Byte.toString(Byte.MAX_VALUE)); + options.put(KEY_MIN, Byte.toString(Byte.MIN_VALUE)); + Config config = new Config(options); + Assert.assertEquals(Byte.MAX_VALUE, + config.getByte(KEY_MAX, defaultValue)); + Assert.assertEquals(Byte.MIN_VALUE, + config.getByte(KEY_MIN, defaultValue)); + Assert.assertEquals(defaultValue, + config.getByte(KEY_EMPTY, defaultValue)); + } + + @Test + public void testGetShort() throws IOException { + final short defaultValue = (short) 1; + Map options = this.initialOptions(); + options.put(KEY_MAX, Short.toString(Short.MAX_VALUE)); + options.put(KEY_MIN, Short.toString(Short.MIN_VALUE)); + Config config = new Config(options); + Assert.assertEquals(Short.MAX_VALUE, + config.getShort(KEY_MAX, defaultValue)); + Assert.assertEquals(Short.MIN_VALUE, + config.getShort(KEY_MIN, defaultValue)); + Assert.assertEquals(defaultValue, + config.getShort(KEY_EMPTY, defaultValue)); + } + + @Test + public void testGetInt() throws IOException { + final int defaultValue = 1; + Map 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)); + } + + @Test + public void testGetLong() throws IOException { + final long defaultValue = 1L; + Map 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)); + } + + @Test + public void testGetFloat() throws IOException { + final float defaultValue = 1.0F; + final float delta = 0.0F; + Map options = this.initialOptions(); + options.put(KEY_MAX, Float.toString(Float.MAX_VALUE)); + options.put(KEY_MIN, Float.toString(Float.MIN_VALUE)); + Config config = new Config(options); + Assert.assertEquals(Float.MAX_VALUE, + config.getFloat(KEY_MAX, defaultValue), + delta); + Assert.assertEquals(Float.MIN_VALUE, + config.getFloat(KEY_MIN, defaultValue), + delta); + Assert.assertEquals(defaultValue, + config.getFloat(KEY_EMPTY, defaultValue), + delta); + } + + @Test + public void testGetDouble() throws IOException { + final double defaultValue = 1.0D; + final double delta = 0.0D; + Map 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); + } + + @Test + public void testString() throws IOException { + + String value = "The value of string"; + final String defaultValue = "The default value of string"; + Map options = this.initialOptions(); + options.put(KEY, value); + Config config = new Config(options); + Assert.assertEquals(value, config.getString(KEY, defaultValue)); + Assert.assertEquals(defaultValue, + config.getString(KEY_EMPTY, defaultValue)); + } + + private Map initialOptions() { + Map 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"); + return options; + } +} diff --git a/computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTestSuite.java b/computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTestSuite.java new file mode 100644 index 000000000..4bd132b2f --- /dev/null +++ b/computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTestSuite.java @@ -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 { +} From 236b89f822f05b1fb8b067c78435026bbf030e5f Mon Sep 17 00:00:00 2001 From: Hou Zhizhen Date: Tue, 16 Mar 2021 17:46:46 +0800 Subject: [PATCH 2/5] improve exception message --- .../computer/core/config/Config.java | 57 +++++++++++++++-- .../computer/core/config/ConfigTest.java | 63 ++++++++++++++++--- 2 files changed, 107 insertions(+), 13 deletions(-) diff --git a/computer-core/src/main/java/com/baidu/hugegraph/computer/core/config/Config.java b/computer-core/src/main/java/com/baidu/hugegraph/computer/core/config/Config.java index c14004183..d3fc85489 100644 --- a/computer-core/src/main/java/com/baidu/hugegraph/computer/core/config/Config.java +++ b/computer-core/src/main/java/com/baidu/hugegraph/computer/core/config/Config.java @@ -86,12 +86,27 @@ public R get(TypedOption option) { return this.allConfig.get(option); } + public boolean getBoolean(String key, boolean defaultValue) { + String value = this.allConfig.getString(key); + if (value == null) { + return defaultValue; + } else { + return Boolean.parseBoolean(value); + } + } + public int getByte(String key, byte defaultValue) { String value = this.allConfig.getString(key); if (value == null) { return defaultValue; } else { - return Byte.parseByte(value); + try { + return Byte.parseByte(value); + } catch (Exception e) { + throw new ComputerException( + "Can't parse '%s' into byte for key '%s'", + value, key); + } } } @@ -100,7 +115,13 @@ public int getShort(String key, short defaultValue) { if (value == null) { return defaultValue; } else { - return Short.parseShort(value); + try { + return Short.parseShort(value); + } catch (Exception e) { + throw new ComputerException( + "Can't parse '%s' into short for key '%s'", + value, key); + } } } @@ -109,7 +130,13 @@ public int getInt(String key, int defaultValue) { if (value == null) { return defaultValue; } else { - return Integer.parseInt(value); + try { + return Integer.parseInt(value); + } catch (Exception e) { + throw new ComputerException( + "Can't parse '%s' into int for key '%s'", + value, key); + } } } @@ -118,7 +145,13 @@ public long getLong(String key, long defaultValue) { if (value == null) { return defaultValue; } else { - return Long.parseLong(value); + try { + return Long.parseLong(value); + } catch (Exception e) { + throw new ComputerException( + "Can't parse '%s' into long for key '%s'", + value, key); + } } } @@ -127,7 +160,13 @@ public float getFloat(String key, float defaultValue) { if (value == null) { return defaultValue; } else { - return Float.parseFloat(value); + try { + return Float.parseFloat(value); + } catch (Exception e) { + throw new ComputerException( + "Can't parse '%s' into float for key '%s'", + value, key); + } } } @@ -136,7 +175,13 @@ public double getDouble(String key, double defaultValue) { if (value == null) { return defaultValue; } else { - return Double.parseDouble(value); + try { + return Double.parseDouble(value); + } catch (Exception e) { + throw new ComputerException( + "Can't parse '%s' into double for key '%s'", + value, key); + } } } diff --git a/computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTest.java b/computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTest.java index ece35eb60..936d15952 100644 --- a/computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTest.java +++ b/computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTest.java @@ -23,18 +23,37 @@ import java.util.HashMap; import java.util.Map; -import org.junit.Assert; 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 testGetByte() throws IOException { + public void testGetBoolean() { + final boolean defaultValue = false; + Map 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.assertFalse(config.getBoolean(KEY_ABC, Boolean.TRUE)); + } + + @Test + public void testGetByte() { final byte defaultValue = (byte) 1; Map options = this.initialOptions(); options.put(KEY_MAX, Byte.toString(Byte.MAX_VALUE)); @@ -46,10 +65,15 @@ public void testGetByte() throws IOException { config.getByte(KEY_MIN, defaultValue)); Assert.assertEquals(defaultValue, config.getByte(KEY_EMPTY, defaultValue)); + Assert.assertThrows(ComputerException.class, () -> { + config.getByte(KEY_ABC, defaultValue); + } ,e -> { + Assert.assertContains("into byte for key", e.getMessage()); + }); } @Test - public void testGetShort() throws IOException { + public void testGetShort() { final short defaultValue = (short) 1; Map options = this.initialOptions(); options.put(KEY_MAX, Short.toString(Short.MAX_VALUE)); @@ -61,10 +85,15 @@ public void testGetShort() throws IOException { config.getShort(KEY_MIN, defaultValue)); Assert.assertEquals(defaultValue, config.getShort(KEY_EMPTY, defaultValue)); + Assert.assertThrows(ComputerException.class, () -> { + config.getShort(KEY_ABC, defaultValue); + } ,e -> { + Assert.assertContains("into short for key", e.getMessage()); + }); } @Test - public void testGetInt() throws IOException { + public void testGetInt() { final int defaultValue = 1; Map options = this.initialOptions(); options.put(KEY_MAX, Integer.toString(Integer.MAX_VALUE)); @@ -76,10 +105,15 @@ public void testGetInt() throws IOException { 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("into int for key", e.getMessage()); + }); } @Test - public void testGetLong() throws IOException { + public void testGetLong() { final long defaultValue = 1L; Map options = this.initialOptions(); options.put(KEY_MAX, Long.toString(Long.MAX_VALUE)); @@ -91,10 +125,15 @@ public void testGetLong() throws IOException { 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("into long for key", e.getMessage()); + }); } @Test - public void testGetFloat() throws IOException { + public void testGetFloat() { final float defaultValue = 1.0F; final float delta = 0.0F; Map options = this.initialOptions(); @@ -110,6 +149,11 @@ public void testGetFloat() throws IOException { Assert.assertEquals(defaultValue, config.getFloat(KEY_EMPTY, defaultValue), delta); + Assert.assertThrows(ComputerException.class, () -> { + config.getFloat(KEY_ABC, defaultValue); + } ,e -> { + Assert.assertContains("into float for key", e.getMessage()); + }); } @Test @@ -129,11 +173,15 @@ public void testGetDouble() throws IOException { Assert.assertEquals(defaultValue, config.getDouble(KEY_EMPTY, defaultValue), delta); + Assert.assertThrows(ComputerException.class, () -> { + config.getDouble(KEY_ABC, defaultValue); + } ,e -> { + Assert.assertContains("into double for key", e.getMessage()); + }); } @Test public void testString() throws IOException { - String value = "The value of string"; final String defaultValue = "The default value of string"; Map options = this.initialOptions(); @@ -155,6 +203,7 @@ private Map initialOptions() { "false"); options.put(ComputerOptions.OUTPUT_WITH_EDGE_PROPERTIES.name(), "false"); + options.put(KEY_ABC, VALUE_ABC); return options; } } From 70ec30cbe1933f8f657e58a2716b8e9954d6049f Mon Sep 17 00:00:00 2001 From: Hou Zhizhen Date: Thu, 18 Mar 2021 10:51:28 +0800 Subject: [PATCH 3/5] remove getByte, getShort, getFloat --- .../computer/core/config/Config.java | 51 +------------- .../computer/core/config/ConfigTest.java | 70 +------------------ 2 files changed, 6 insertions(+), 115 deletions(-) diff --git a/computer-core/src/main/java/com/baidu/hugegraph/computer/core/config/Config.java b/computer-core/src/main/java/com/baidu/hugegraph/computer/core/config/Config.java index d3fc85489..e154c93fd 100644 --- a/computer-core/src/main/java/com/baidu/hugegraph/computer/core/config/Config.java +++ b/computer-core/src/main/java/com/baidu/hugegraph/computer/core/config/Config.java @@ -95,36 +95,6 @@ public boolean getBoolean(String key, boolean defaultValue) { } } - public int getByte(String key, byte defaultValue) { - String value = this.allConfig.getString(key); - if (value == null) { - return defaultValue; - } else { - try { - return Byte.parseByte(value); - } catch (Exception e) { - throw new ComputerException( - "Can't parse '%s' into byte for key '%s'", - value, key); - } - } - } - - public int getShort(String key, short defaultValue) { - String value = this.allConfig.getString(key); - if (value == null) { - return defaultValue; - } else { - try { - return Short.parseShort(value); - } catch (Exception e) { - throw new ComputerException( - "Can't parse '%s' into short for key '%s'", - value, key); - } - } - } - public int getInt(String key, int defaultValue) { String value = this.allConfig.getString(key); if (value == null) { @@ -134,7 +104,7 @@ public int getInt(String key, int defaultValue) { return Integer.parseInt(value); } catch (Exception e) { throw new ComputerException( - "Can't parse '%s' into int for key '%s'", + "Can't parse int value from '%s' for key '%s'", value, key); } } @@ -149,22 +119,7 @@ public long getLong(String key, long defaultValue) { return Long.parseLong(value); } catch (Exception e) { throw new ComputerException( - "Can't parse '%s' into long for key '%s'", - value, key); - } - } - } - - public float getFloat(String key, float defaultValue) { - String value = this.allConfig.getString(key); - if (value == null) { - return defaultValue; - } else { - try { - return Float.parseFloat(value); - } catch (Exception e) { - throw new ComputerException( - "Can't parse '%s' into float for key '%s'", + "Can't parse long value from '%s' for key '%s'", value, key); } } @@ -179,7 +134,7 @@ public double getDouble(String key, double defaultValue) { return Double.parseDouble(value); } catch (Exception e) { throw new ComputerException( - "Can't parse '%s' into double for key '%s'", + "Can't parse double value from '%s' for key '%s'", value, key); } } diff --git a/computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTest.java b/computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTest.java index 936d15952..9e0cf3d44 100644 --- a/computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTest.java +++ b/computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTest.java @@ -52,46 +52,6 @@ public void testGetBoolean() { Assert.assertFalse(config.getBoolean(KEY_ABC, Boolean.TRUE)); } - @Test - public void testGetByte() { - final byte defaultValue = (byte) 1; - Map options = this.initialOptions(); - options.put(KEY_MAX, Byte.toString(Byte.MAX_VALUE)); - options.put(KEY_MIN, Byte.toString(Byte.MIN_VALUE)); - Config config = new Config(options); - Assert.assertEquals(Byte.MAX_VALUE, - config.getByte(KEY_MAX, defaultValue)); - Assert.assertEquals(Byte.MIN_VALUE, - config.getByte(KEY_MIN, defaultValue)); - Assert.assertEquals(defaultValue, - config.getByte(KEY_EMPTY, defaultValue)); - Assert.assertThrows(ComputerException.class, () -> { - config.getByte(KEY_ABC, defaultValue); - } ,e -> { - Assert.assertContains("into byte for key", e.getMessage()); - }); - } - - @Test - public void testGetShort() { - final short defaultValue = (short) 1; - Map options = this.initialOptions(); - options.put(KEY_MAX, Short.toString(Short.MAX_VALUE)); - options.put(KEY_MIN, Short.toString(Short.MIN_VALUE)); - Config config = new Config(options); - Assert.assertEquals(Short.MAX_VALUE, - config.getShort(KEY_MAX, defaultValue)); - Assert.assertEquals(Short.MIN_VALUE, - config.getShort(KEY_MIN, defaultValue)); - Assert.assertEquals(defaultValue, - config.getShort(KEY_EMPTY, defaultValue)); - Assert.assertThrows(ComputerException.class, () -> { - config.getShort(KEY_ABC, defaultValue); - } ,e -> { - Assert.assertContains("into short for key", e.getMessage()); - }); - } - @Test public void testGetInt() { final int defaultValue = 1; @@ -108,7 +68,7 @@ public void testGetInt() { Assert.assertThrows(ComputerException.class, () -> { config.getInt(KEY_ABC, defaultValue); } ,e -> { - Assert.assertContains("into int for key", e.getMessage()); + Assert.assertContains("Can't parse int value", e.getMessage()); }); } @@ -128,31 +88,7 @@ public void testGetLong() { Assert.assertThrows(ComputerException.class, () -> { config.getLong(KEY_ABC, defaultValue); } ,e -> { - Assert.assertContains("into long for key", e.getMessage()); - }); - } - - @Test - public void testGetFloat() { - final float defaultValue = 1.0F; - final float delta = 0.0F; - Map options = this.initialOptions(); - options.put(KEY_MAX, Float.toString(Float.MAX_VALUE)); - options.put(KEY_MIN, Float.toString(Float.MIN_VALUE)); - Config config = new Config(options); - Assert.assertEquals(Float.MAX_VALUE, - config.getFloat(KEY_MAX, defaultValue), - delta); - Assert.assertEquals(Float.MIN_VALUE, - config.getFloat(KEY_MIN, defaultValue), - delta); - Assert.assertEquals(defaultValue, - config.getFloat(KEY_EMPTY, defaultValue), - delta); - Assert.assertThrows(ComputerException.class, () -> { - config.getFloat(KEY_ABC, defaultValue); - } ,e -> { - Assert.assertContains("into float for key", e.getMessage()); + Assert.assertContains("Can't parse long value", e.getMessage()); }); } @@ -176,7 +112,7 @@ public void testGetDouble() throws IOException { Assert.assertThrows(ComputerException.class, () -> { config.getDouble(KEY_ABC, defaultValue); } ,e -> { - Assert.assertContains("into double for key", e.getMessage()); + Assert.assertContains("Can't parse double value", e.getMessage()); }); } From bba66ef3f0e9187af861bb27a2673835ca4945de Mon Sep 17 00:00:00 2001 From: Hou Zhizhen Date: Thu, 18 Mar 2021 15:17:03 +0800 Subject: [PATCH 4/5] throw ComputerException in getBoolean if can't convert the parameter value to boolean --- .../com/baidu/hugegraph/computer/core/config/Config.java | 9 ++++++++- .../baidu/hugegraph/computer/core/config/ConfigTest.java | 6 +++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/computer-core/src/main/java/com/baidu/hugegraph/computer/core/config/Config.java b/computer-core/src/main/java/com/baidu/hugegraph/computer/core/config/Config.java index e154c93fd..eaef6e61b 100644 --- a/computer-core/src/main/java/com/baidu/hugegraph/computer/core/config/Config.java +++ b/computer-core/src/main/java/com/baidu/hugegraph/computer/core/config/Config.java @@ -90,9 +90,16 @@ public boolean getBoolean(String key, boolean defaultValue) { String value = this.allConfig.getString(key); if (value == null) { return defaultValue; + } else if (value.equalsIgnoreCase("true")) { + return true; + } else if (value.equalsIgnoreCase("false")) { + return false; } else { - return Boolean.parseBoolean(value); + throw new ComputerException( + "Can't parse boolean value from '%s' for key '%s'", + value, key); } + } public int getInt(String key, int defaultValue) { diff --git a/computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTest.java b/computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTest.java index 9e0cf3d44..ca658f449 100644 --- a/computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTest.java +++ b/computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTest.java @@ -49,7 +49,11 @@ public void testGetBoolean() { Assert.assertTrue(config.getBoolean(KEY_TRUE, defaultValue)); Assert.assertFalse(config.getBoolean(KEY_FALSE, defaultValue)); Assert.assertFalse(config.getBoolean(KEY_EMPTY, defaultValue)); - Assert.assertFalse(config.getBoolean(KEY_ABC, Boolean.TRUE)); + Assert.assertThrows(ComputerException.class, () -> { + config.getBoolean(KEY_ABC, Boolean.TRUE); + } ,e -> { + Assert.assertContains("Can't parse boolean value", e.getMessage()); + }); } @Test From cf3361dc9cf3a55a785102bc871676e0f9b9d54c Mon Sep 17 00:00:00 2001 From: Hou Zhizhen Date: Fri, 19 Mar 2021 14:13:06 +0800 Subject: [PATCH 5/5] add test for null as default value --- .../java/com/baidu/hugegraph/computer/core/config/Config.java | 1 - .../com/baidu/hugegraph/computer/core/config/ConfigTest.java | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/computer-core/src/main/java/com/baidu/hugegraph/computer/core/config/Config.java b/computer-core/src/main/java/com/baidu/hugegraph/computer/core/config/Config.java index eaef6e61b..afa9c4518 100644 --- a/computer-core/src/main/java/com/baidu/hugegraph/computer/core/config/Config.java +++ b/computer-core/src/main/java/com/baidu/hugegraph/computer/core/config/Config.java @@ -99,7 +99,6 @@ public boolean getBoolean(String key, boolean defaultValue) { "Can't parse boolean value from '%s' for key '%s'", value, key); } - } public int getInt(String key, int defaultValue) { diff --git a/computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTest.java b/computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTest.java index ca658f449..22ae6ebbe 100644 --- a/computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTest.java +++ b/computer-test/src/main/java/com/baidu/hugegraph/computer/core/config/ConfigTest.java @@ -128,8 +128,10 @@ public void testString() throws IOException { 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)); + Assert.assertNull(config.getString(KEY_EMPTY, null)); } private Map initialOptions() {