From f204d9471e58abdc7b34ef41b3161d4bb5223566 Mon Sep 17 00:00:00 2001 From: Tim Molter <tim.molter@gmail.com> Date: Mon, 4 Mar 2013 20:57:38 +0100 Subject: [PATCH] clean up and added unit test --- .../internal/chartpart/DateFormatter.java | 4 +- .../java/com/xeiam/xchart/chart/NoBug.java | 70 --------- .../unit/DateAxisTickCalculatorTest.java | 17 ++- .../xeiam/xchart/unit/DateFormatterTest.java | 136 ++++++++++++++++++ ...tterTest.java => NumberFormatterTest.java} | 2 +- 5 files changed, 149 insertions(+), 80 deletions(-) delete mode 100644 xchart/src/test/java/com/xeiam/xchart/chart/NoBug.java create mode 100644 xchart/src/test/java/com/xeiam/xchart/unit/DateFormatterTest.java rename xchart/src/test/java/com/xeiam/xchart/unit/{ValueFormatterTest.java => NumberFormatterTest.java} (99%) diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/DateFormatter.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/DateFormatter.java index ad9f220c..c28a5bda 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/DateFormatter.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/DateFormatter.java @@ -64,7 +64,7 @@ public class DateFormatter { validTickStepsMap.put(YEAR_SCALE, new int[] { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000 }); } - long getTimeUnit(long gridStepHint) { + public long getTimeUnit(long gridStepHint) { for (Entry<Long, int[]> entry : validTickStepsMap.entrySet()) { @@ -86,7 +86,7 @@ public class DateFormatter { * @param max * @return */ - String formatDate(BigDecimal value, long timeUnit) { + public String formatDate(BigDecimal value, long timeUnit) { String datePattern; diff --git a/xchart/src/test/java/com/xeiam/xchart/chart/NoBug.java b/xchart/src/test/java/com/xeiam/xchart/chart/NoBug.java deleted file mode 100644 index 965bc737..00000000 --- a/xchart/src/test/java/com/xeiam/xchart/chart/NoBug.java +++ /dev/null @@ -1,70 +0,0 @@ -/** - * Copyright (C) 2013 Xeiam LLC http://xeiam.com - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies - * of the Software, and to permit persons to whom the Software is furnished to do - * so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package com.xeiam.xchart.chart; - -import java.util.ArrayList; -import java.util.List; - -import com.xeiam.xchart.Chart; -import com.xeiam.xchart.Series; -import com.xeiam.xchart.SwingWrapper; - -/** - * @author timmolter - */ -public class NoBug { - - static void plot(Chart chart, int n) { - - chart.setChartTitle("title"); - chart.setXAxisTitle("X"); - chart.setYAxisTitle("Y"); - List<Number> x = new ArrayList<Number>(); - List<Number> y = new ArrayList<Number>(); - String seriesName = addOneSeries(chart, n, x, y); - } - - private static String addOneSeries(Chart chart, int n, List<Number> x, List<Number> y) { - - for (int i = 0; i <= 10; i++) { - x.add(i / 10.); - y.add(i * n / 10.); - } - String seriesName = "series " + n; - Series series = chart.addSeries(seriesName, x, y); - chart.getStyleManager().setxAxisMin(0); - chart.getStyleManager().setxAxisMax(1); - chart.getStyleManager().setyAxisMin(-5); - chart.getStyleManager().setyAxisMax(5); - return seriesName; - } - - public static void main(String[] args) { - - for (int i = 0; i < 4; i++) { - Chart chart = new Chart(700, 500); - plot(chart, i); - new SwingWrapper(chart).displayChart(); - } - } - -} diff --git a/xchart/src/test/java/com/xeiam/xchart/unit/DateAxisTickCalculatorTest.java b/xchart/src/test/java/com/xeiam/xchart/unit/DateAxisTickCalculatorTest.java index a5d64604..081d8816 100644 --- a/xchart/src/test/java/com/xeiam/xchart/unit/DateAxisTickCalculatorTest.java +++ b/xchart/src/test/java/com/xeiam/xchart/unit/DateAxisTickCalculatorTest.java @@ -21,6 +21,9 @@ */ package com.xeiam.xchart.unit; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + import java.math.BigDecimal; import java.util.Arrays; import java.util.List; @@ -28,8 +31,8 @@ import java.util.List; import org.junit.Test; import com.xeiam.xchart.StyleManager; -import com.xeiam.xchart.internal.chartpart.AxisTickDateCalculator; import com.xeiam.xchart.internal.chartpart.Axis.Direction; +import com.xeiam.xchart.internal.chartpart.AxisTickDateCalculator; /** * @author timmolter @@ -43,12 +46,12 @@ public class DateAxisTickCalculatorTest { List<String> tickLabels = decimalAxisTickCalculator.getTickLabels(); System.out.println(Arrays.toString(tickLabels.toArray())); - // assertThat(tickLabels.size(), equalTo(7)); - // assertThat(tickLabels.get(0), equalTo("-15")); + assertThat(tickLabels.size(), equalTo(6)); + assertThat(tickLabels.get(0), equalTo("17:50")); - // List<Integer> tickLocations = decimalAxisTickCalculator.getTickLocations(); - // System.out.println(Arrays.toString(tickLocations.toArray())); - // assertThat(tickLocations.size(), equalTo(7)); - // assertThat(tickLocations.get(0), equalTo(15)); + List<Integer> tickLocations = decimalAxisTickCalculator.getTickLocations(); + System.out.println(Arrays.toString(tickLocations.toArray())); + assertThat(tickLocations.size(), equalTo(6)); + assertThat(tickLocations.get(0), equalTo(100)); } } diff --git a/xchart/src/test/java/com/xeiam/xchart/unit/DateFormatterTest.java b/xchart/src/test/java/com/xeiam/xchart/unit/DateFormatterTest.java new file mode 100644 index 00000000..3386bb37 --- /dev/null +++ b/xchart/src/test/java/com/xeiam/xchart/unit/DateFormatterTest.java @@ -0,0 +1,136 @@ +/** + * Copyright (C) 2013 Xeiam LLC http://xeiam.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.xeiam.xchart.unit; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.math.BigDecimal; +import java.util.Locale; +import java.util.TimeZone; + +import org.junit.Test; + +import com.xeiam.xchart.StyleManager; +import com.xeiam.xchart.internal.chartpart.DateFormatter; + +/** + * @author timmolter + */ +public class DateFormatterTest { + + private final Locale locale = Locale.US; + + @Test + public void testDateFormatting() { + + StyleManager styleManager = new StyleManager(); + DateFormatter dateFormatter = new DateFormatter(styleManager); + + TimeZone timeZone = TimeZone.getTimeZone("UTC"); + + styleManager.setLocale(locale); + styleManager.setTimezone(timeZone); + + // ms + BigDecimal value = new BigDecimal(1358108105531L); + BigDecimal min = new BigDecimal(1358108105100L); + BigDecimal max = new BigDecimal(1358108105900L); + long span = Math.abs(max.subtract(min).longValue()); // in data space + long gridStepHint = (long) (span / (double) 1000 * 74); + long timeUnit = dateFormatter.getTimeUnit(gridStepHint); + String stringValue = dateFormatter.formatDate(value, timeUnit); + assertThat(stringValue, equalTo("05.531")); + + // sec + value = new BigDecimal(1358108105000L); + min = new BigDecimal(1358108101000L); + max = new BigDecimal(1358108109000L); + span = Math.abs(max.subtract(min).longValue()); // in data space + gridStepHint = (long) (span / (double) 1000 * 74); + timeUnit = dateFormatter.getTimeUnit(gridStepHint); + stringValue = dateFormatter.formatDate(value, timeUnit); + assertThat(stringValue, equalTo("05.000")); + + // min + value = new BigDecimal(1358111750000L); + min = new BigDecimal(1358111690000L); + max = new BigDecimal(1358111870000L); + span = Math.abs(max.subtract(min).longValue()); // in data space + gridStepHint = (long) (span / (double) 1000 * 74); + timeUnit = dateFormatter.getTimeUnit(gridStepHint); + stringValue = dateFormatter.formatDate(value, timeUnit); + assertThat(stringValue, equalTo("15:50")); + + // hour + value = new BigDecimal(1358111870000L); + min = new BigDecimal(1358101070000L); + max = new BigDecimal(1358115470000L); + span = Math.abs(max.subtract(min).longValue()); // in data space + gridStepHint = (long) (span / (double) 1000 * 74); + timeUnit = dateFormatter.getTimeUnit(gridStepHint); + stringValue = dateFormatter.formatDate(value, timeUnit); + assertThat(stringValue, equalTo("21:17")); + + // day + value = new BigDecimal(1358112317000L); + min = new BigDecimal(1357939517000L); + max = new BigDecimal(1358285117000L); + span = Math.abs(max.subtract(min).longValue()); // in data space + gridStepHint = (long) (span / (double) 1000 * 74); + timeUnit = dateFormatter.getTimeUnit(gridStepHint); + stringValue = dateFormatter.formatDate(value, timeUnit); + assertThat(stringValue, equalTo("13-21")); + + // week + value = new BigDecimal(1358112317000L); + min = new BigDecimal(1357075517000L); + max = new BigDecimal(1359149117000L); + span = Math.abs(max.subtract(min).longValue()); // in data space + gridStepHint = (long) (span / (double) 1000 * 74); + timeUnit = dateFormatter.getTimeUnit(gridStepHint); + stringValue = dateFormatter.formatDate(value, timeUnit); + assertThat(stringValue, equalTo("01-13")); + + // month + value = new BigDecimal(1358112838000L); + min = new BigDecimal(1354397638000L); + max = new BigDecimal(1361223238000L); + span = Math.abs(max.subtract(min).longValue()); // in data space + gridStepHint = (long) (span / (double) 1000 * 74); + timeUnit = dateFormatter.getTimeUnit(gridStepHint); + stringValue = dateFormatter.formatDate(value, timeUnit); + assertThat(stringValue, equalTo("01-13")); + + // year + value = new BigDecimal(1358113402000L); + min = new BigDecimal(1263419002000L); + max = new BigDecimal(1421185402000L); + span = Math.abs(max.subtract(min).longValue()); // in data space + gridStepHint = (long) (span / (double) 1000 * 74); + timeUnit = dateFormatter.getTimeUnit(gridStepHint); + stringValue = dateFormatter.formatDate(value, timeUnit); + assertThat(stringValue, equalTo("2013-01")); + + } + +} diff --git a/xchart/src/test/java/com/xeiam/xchart/unit/ValueFormatterTest.java b/xchart/src/test/java/com/xeiam/xchart/unit/NumberFormatterTest.java similarity index 99% rename from xchart/src/test/java/com/xeiam/xchart/unit/ValueFormatterTest.java rename to xchart/src/test/java/com/xeiam/xchart/unit/NumberFormatterTest.java index 3c319b4e..12ad0922 100644 --- a/xchart/src/test/java/com/xeiam/xchart/unit/ValueFormatterTest.java +++ b/xchart/src/test/java/com/xeiam/xchart/unit/NumberFormatterTest.java @@ -35,7 +35,7 @@ import com.xeiam.xchart.internal.chartpart.NumberFormatter; /** * @author timmolter */ -public class ValueFormatterTest { +public class NumberFormatterTest { private final Locale locale = Locale.US; -- GitLab