From 168071c6cbed33308e2ca78f4b6a04bc1507cf5d Mon Sep 17 00:00:00 2001 From: Michael Hausegger Date: Thu, 9 Nov 2017 22:49:22 +0100 Subject: [PATCH 1/2] add-tests Added new Unit Tests. --- .../common/config/AbstractConfigTest.java | 129 ++++++++++++++++++ .../common/metrics/stats/PercentilesTest.java | 79 +++++++++++ 2 files changed, 208 insertions(+) create mode 100644 config/src/test/java/io/confluent/common/config/AbstractConfigTest.java create mode 100644 metrics/src/test/java/io/confluent/common/metrics/stats/PercentilesTest.java diff --git a/config/src/test/java/io/confluent/common/config/AbstractConfigTest.java b/config/src/test/java/io/confluent/common/config/AbstractConfigTest.java new file mode 100644 index 00000000000..faf4ed582f1 --- /dev/null +++ b/config/src/test/java/io/confluent/common/config/AbstractConfigTest.java @@ -0,0 +1,129 @@ +/** + * Copyright 2015 Confluent Inc. + * + * Licensed 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. + **/ + +/** + * Original license: + * + * 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 io.confluent.common.config; + +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import static org.junit.Assert.*; + +/** + * Unit tests for class {@link AbstractConfig}. + * + * @see AbstractConfig + */ +public class AbstractConfigTest { + + @Test + public void testGetPropsFromFileReturningEmptyProperties() { + Properties properties = AbstractConfig.getPropsFromFile(null); + + assertTrue(properties.isEmpty()); + } + + @Test + public void testWithPrefixWithNonEmptyStringReturnsEmptyMap() { + ConfigDef configDef = new ConfigDef(); + Map hashMap = new HashMap<>(); + AbstractConfig abstractConfig = new AbstractConfig(configDef, hashMap); + Map hashMapTwo = new HashMap<>(); + hashMapTwo.put(",..", ",.."); + Map map = abstractConfig.withPrefix(",..", hashMapTwo); + + assertEquals(0, map.size()); + } + + @Test + public void testWithPrefixWithNonEmptyStrings() { + ConfigDef configDef = new ConfigDef(); + Map hashMap = new HashMap<>(); + hashMap.put("- asd1", "-asd"); + hashMap.put(" asd1", "-asd"); + AbstractConfig abstractConfig = new AbstractConfig(configDef, hashMap); + Map map = abstractConfig.withPrefix("-", hashMap); + + assertEquals(1, map.size()); + assertEquals(" asd1", map.keySet().iterator().next()); + assertEquals(" asd1=-asd", map.entrySet().iterator().next().toString()); + } + + @Test + public void testFailsToCreateAbstractConfigThrowsConfigException() { + ConfigDef configDef = new ConfigDef(); + Map hashMap = new HashMap<>(); + hashMap.put(configDef, new Long((-1995L))); + + try { + new AbstractConfig(configDef, hashMap); + fail("Expecting exception: ConfigException"); + } catch (ConfigException e) { + assertEquals(AbstractConfig.class.getName(), e.getStackTrace()[0].getClassName()); + } + } + + @Test + public void testGetStringThrowsConfigException() { + ConfigDef configDef = new ConfigDef(); + Map hashMap = new HashMap<>(); + AbstractConfig abstractConfig = new AbstractConfig(configDef, hashMap); + + try { + abstractConfig.getString(" could not be found."); + fail("Expecting exception: ConfigException"); + } catch (ConfigException e) { + assertEquals(AbstractConfig.class.getName(), e.getStackTrace()[0].getClassName()); + } + } + + @Test + public void testValuesWithPrefix() { + ConfigDef configDef = new ConfigDef(); + Map hashMap = new HashMap<>(); + AbstractConfig abstractConfig = new AbstractConfig(configDef, hashMap); + Map map = abstractConfig.valuesWithPrefix(""); + + assertEquals(0, map.size()); + } + + @Test + public void testOriginalsWithPrefix() { + ConfigDef configDef = new ConfigDef(); + Map hashMap = new HashMap<>(); + AbstractConfig abstractConfig = new AbstractConfig(configDef, hashMap); + Map map = abstractConfig.originalsWithPrefix("~cOo+qt{k]k}#%y"); + + assertEquals(0, map.size()); + } + +} \ No newline at end of file diff --git a/metrics/src/test/java/io/confluent/common/metrics/stats/PercentilesTest.java b/metrics/src/test/java/io/confluent/common/metrics/stats/PercentilesTest.java new file mode 100644 index 00000000000..b8cb0fbb083 --- /dev/null +++ b/metrics/src/test/java/io/confluent/common/metrics/stats/PercentilesTest.java @@ -0,0 +1,79 @@ +package io.confluent.common.metrics.stats; + +import io.confluent.common.metrics.MetricConfig; +import io.confluent.common.metrics.MetricName; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +/** + * Unit tests for class {@link Percentiles}. + * + * @see Percentiles + */ +public class PercentilesTest { + + @Test + public void testValue() { + Percentiles.BucketSizing percentiles_BucketSizing = Percentiles.BucketSizing.LINEAR; + Percentiles percentiles = new Percentiles(1846, 1846, percentiles_BucketSizing, new Percentile[0]); + MetricConfig metricConfig = new MetricConfig(); + percentiles.record(metricConfig, 28L, 1792L); + + assertEquals(Double.POSITIVE_INFINITY, percentiles.value(metricConfig, 1846, 27.78662642648307), 0.01); + } + + @Test + public void testFailsToCreatePercentilesTaking5ArgumentsThrowsIllegalArgumentException() { + try { + new Percentiles(44, (-121.169), 44, Percentiles.BucketSizing.LINEAR, null); + fail("Expecting exception: IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals(Percentiles.class.getName(), e.getStackTrace()[0].getClassName()); + } + } + + @Test + public void testFailsToCreatePercentilesTaking4ArgumentsThrowsIllegalArgumentException() { + try { + new Percentiles(58, 58, null, new Percentile[7]); + fail("Expecting exception: IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals(Percentiles.class.getName(), e.getStackTrace()[0].getClassName()); + } + } + + @Test + public void testCombineWithEmptyList() { + Percentiles.BucketSizing percentiles_BucketSizing = Percentiles.BucketSizing.LINEAR; + Percentile[] percentileArray = new Percentile[0]; + Percentiles percentiles = new Percentiles(146, 1846, percentiles_BucketSizing, percentileArray); + MetricConfig metricConfig = new MetricConfig(); + List list = percentiles.samples; + + assertEquals(Double.NaN, percentiles.combine(list, metricConfig, 1846), 0.01); + } + + @Test + public void testStatsThrowsNullPointerException() { + Percentiles.BucketSizing percentiles_BucketSizing = Percentiles.BucketSizing.LINEAR; + Percentile[] percentileArray = new Percentile[2]; + Map hashMap = new HashMap<>(); + MetricName metricName = new MetricName("|~a`", "|~a`", hashMap); + Percentile percentile = new Percentile(metricName, (-961)); + percentileArray[0] = percentile; + Percentiles percentiles = new Percentiles((-961), 0.0, percentiles_BucketSizing, percentileArray); + + try { + percentiles.stats(); + fail("Expecting exception: NullPointerException"); + } catch (NullPointerException e) { + assertEquals(Percentiles.class.getName(), e.getStackTrace()[0].getClassName()); + } + } +} \ No newline at end of file From 87e58ffccd207d787d08837fb1973844b2081baf Mon Sep 17 00:00:00 2001 From: Michael Hausegger Date: Thu, 9 Nov 2017 22:51:59 +0100 Subject: [PATCH 2/2] add-tests Improved license header information. --- .../common/config/AbstractConfigTest.java | 17 +---------------- .../common/metrics/stats/PercentilesTest.java | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/config/src/test/java/io/confluent/common/config/AbstractConfigTest.java b/config/src/test/java/io/confluent/common/config/AbstractConfigTest.java index faf4ed582f1..e8c686fa7c4 100644 --- a/config/src/test/java/io/confluent/common/config/AbstractConfigTest.java +++ b/config/src/test/java/io/confluent/common/config/AbstractConfigTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2015 Confluent Inc. + * Copyright 2017 Confluent Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,21 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. **/ - -/** - * Original license: - * - * 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 io.confluent.common.config; import org.junit.Test; diff --git a/metrics/src/test/java/io/confluent/common/metrics/stats/PercentilesTest.java b/metrics/src/test/java/io/confluent/common/metrics/stats/PercentilesTest.java index b8cb0fbb083..5fa89c06c47 100644 --- a/metrics/src/test/java/io/confluent/common/metrics/stats/PercentilesTest.java +++ b/metrics/src/test/java/io/confluent/common/metrics/stats/PercentilesTest.java @@ -1,3 +1,18 @@ +/** + * Copyright 2017 Confluent Inc. + * + * Licensed 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 io.confluent.common.metrics.stats; import io.confluent.common.metrics.MetricConfig;