Skip to content

Commit

Permalink
Merge pull request #711 from zhzzang/add-test
Browse files Browse the repository at this point in the history
Add CategoryChart test and set junit5
  • Loading branch information
timmolter committed Dec 15, 2022
2 parents 8a78d1c + 78a85f4 commit dda1ba9
Show file tree
Hide file tree
Showing 12 changed files with 794 additions and 73 deletions.
12 changes: 9 additions & 3 deletions pom.xml
Expand Up @@ -90,9 +90,15 @@

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.23.1</version>
<scope>test</scope>
</dependency>
<!-- FEST for fluent test assertions -->
Expand Down
11 changes: 7 additions & 4 deletions xchart/src/test/java/org/knowm/xchart/AnnotationLineTest.java
Expand Up @@ -3,8 +3,10 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.junit.Assert;
import org.junit.Test;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class AnnotationLineTest {

Expand All @@ -22,7 +24,8 @@ public void annotationLineShouldNotSpanOverWholeWidth() throws IOException {
BitmapEncoder.saveBitmap(chart, output, BitmapEncoder.BitmapFormat.PNG);

// test
Assert.assertTrue(annotation.getBounds().getX() > 50);
Assert.assertTrue(annotation.getBounds().getWidth() < 500);
assertTrue(annotation.getBounds().getX() > 50);
assertTrue(annotation.getBounds().getX() > 50);
assertTrue(annotation.getBounds().getWidth() < 500);
}
}
237 changes: 237 additions & 0 deletions xchart/src/test/java/org/knowm/xchart/CategoryChartTest.java
@@ -0,0 +1,237 @@
package org.knowm.xchart;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.knowm.xchart.custom.CustomGraphic;
import org.knowm.xchart.custom.CustomTheme;
import org.knowm.xchart.internal.series.Series;
import org.knowm.xchart.style.Styler;

import java.util.*;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.*;
import static org.knowm.xchart.style.Styler.ChartTheme.GGPlot2;
import static org.knowm.xchart.style.Styler.ChartTheme.XChart;

public class CategoryChartTest {

private CategoryChart chart;

@BeforeEach
void setUp() {
chart = new CategoryChart(800, 600, GGPlot2);
}

@Test
void constructor() {
CategoryChartBuilder builder = new CategoryChartBuilder()
.width(800)
.height(600)
.theme(Styler.ChartTheme.GGPlot2)
.title("CategoryChart")
.xAxisTitle("x-axis")
.yAxisTitle("y-axis");

assertAll(
() -> assertDoesNotThrow(() -> new CategoryChart(800, 600)),
() -> assertDoesNotThrow(() -> new CategoryChart(800, 600, new CustomTheme())),
() -> assertDoesNotThrow(() -> new CategoryChart(800, 600, XChart)),
() -> assertDoesNotThrow(() -> new CategoryChart(builder))
);
}

@Test
void alreadyContainsSeriesName() {
assertThatThrownBy(() -> {
chart.addSeries("a",
Arrays.asList(1, 2, 3, 4, 5),
Arrays.asList(10, 2, 30, 40, 50),
Arrays.asList(1, 3, 2, 1, 2));

chart.addSeries("a",
Arrays.asList("A", "B", "C", "D", "E"),
Arrays.asList(10, 25, 30., 4, 5));
}).isInstanceOf(IllegalArgumentException.class)
.hasMessageMatching("Series name >a< has already been used. Use unique names for each series!!!");
}

@Test
void yDataIsNull() {
assertThatThrownBy(() -> {
chart.addSeries("a",
Arrays.asList("A", "B", "C", "D", "E"),
null,
Arrays.asList(1, 2, 3, 4, 5));
}).isInstanceOf(IllegalArgumentException.class)
.hasMessageMatching("Y-Axis data cannot be null!!!");
}

@Test
void yDataIsEmpty() {
assertThatThrownBy(() -> {
chart.addSeries("a",
Arrays.asList("A", "B", "C", "D", "E"),
Arrays.asList(),
Arrays.asList(1, 2, 3, 4, 5));
}).isInstanceOf(IllegalArgumentException.class)
.hasMessageMatching("Y-Axis data cannot be empty!!!");
}

@Test
void xDataIsNull() {
assertThatThrownBy(() -> {
chart.addSeries("a",
Arrays.asList(),
Arrays.asList(1, 2, 3, 4, 5),
Arrays.asList(10, 20, 30, 40, 50));
}).isInstanceOf(IllegalArgumentException.class)
.hasMessageMatching("X-Axis data cannot be empty!!!");
}

@Test
void errorBarSizeIsNotEqualToYDataSize() {
assertThatThrownBy(() -> {
chart.addSeries("a",
Arrays.asList("A", "B", "C", "D", "E"),
Arrays.asList(1, 2),
Arrays.asList(1, 3, 2, 1, 2));
}).isInstanceOf(IllegalArgumentException.class)
.hasMessageMatching("Error bars and Y-Axis sizes are not the same!!!");
}

@Test
void xDataSizeIsNotEqualToYDataSize() {
assertThatThrownBy(() -> {
chart.addSeries("a",
Arrays.asList("A", "B", "C", "D"),
Arrays.asList(1, 2, 3, 4, 5),
Arrays.asList(1, 3, 2, 1, 2));
}).isInstanceOf(IllegalArgumentException.class)
.hasMessageMatching("X and Y-Axis sizes are not the same!!!");
}

@Test
void checkAddSeries() {
CategorySeries series = chart.addSeries(
"fruit",
Arrays.asList("Blue", "Red", "Green", "Yellow", "Orange"),
Arrays.asList(-40, 40.8, 20, 60, 60),
Arrays.asList(3, 3, 4, 3, 5));

assertAll(
() -> assertThat(series.getName()).isEqualTo("fruit"),
() -> assertThat(series.getxAxisDataType()).isEqualTo(Series.DataType.String),
() -> assertThat(series.getXData()).isEqualTo(Arrays.asList("Blue", "Red", "Green", "Yellow", "Orange")),
() -> assertThat(series.getYData()).isEqualTo(Arrays.asList(-40, 40.8, 20, 60, 60)),
() -> assertThat(series.getExtraValues()).isEqualTo(Arrays.asList(3, 3, 4, 3, 5)),
() -> assertThat(series.getYData()).hasSize(5)
);
}

@Test
void updateNonExistentSeries() {
chart.addSeries(
"fruit",
Arrays.asList("Blue", "Red", "Green", "Yellow", "Orange"),
Arrays.asList(-40, 30, 20, 60, 60));
chart.addSeries(
"food",
Arrays.asList("Blue", "Red", "Green", "Yellow", "Orange"),
Arrays.asList(50, 10, -20, 40, 60));

assertThatThrownBy(() -> {
chart.updateCategorySeries("a",
Arrays.asList(1, 2, 3, 4, 5),
null,
null);
}).isInstanceOf(IllegalArgumentException.class)
.hasMessageMatching("Series name >a< not found!!!");

}

@Test
void updateXData() {
CategorySeries fruit = chart.addSeries(
"fruit",
Arrays.asList("Blue", "Red", "Green", "Yellow", "Orange"),
Arrays.asList(-40, 30, 20, 60, 60));

chart.updateCategorySeries("fruit",
Arrays.asList("a", "b", "c", "d", "e"),
Arrays.asList(-40, 30, 20, 60, 60),
null);

assertThat(fruit.getXData()).isEqualTo(Arrays.asList("a", "b", "c", "d", "e"));

}

@Test
void updateYData() {
CategorySeries fruit = chart.addSeries(
"fruit",
Arrays.asList("Blue", "Red", "Green", "Yellow", "Orange"),
Arrays.asList(-40, 30, 20, 60, 60));

chart.updateCategorySeries("fruit",
Arrays.asList("Blue", "Red", "Green", "Yellow", "Orange"),
Arrays.asList(50, 10, -20, 40, 60),
null);

assertThat(fruit.getYData()).isEqualTo(Arrays.asList(50, 10, -20, 40, 60));
}

@Test
void updateErrorBar() {
CategorySeries fruit = chart.addSeries(
"fruit",
Arrays.asList("Blue", "Red", "Green", "Yellow", "Orange"),
Arrays.asList(-40, 30, 20, 60, 60),
Arrays.asList(5, 5, 10, 5, 5));

chart.updateCategorySeries("fruit",
Arrays.asList("Blue", "Red", "Green", "Yellow", "Orange"),
Arrays.asList(-40, 30, 20, 60, 60),
Arrays.asList(3, 1, 2, 1, 2));

assertThat(fruit.getExtraValues()).isEqualTo(Arrays.asList(3, 1, 2, 1, 2));
}

@Test
void automaticallyCreatedWhenXDataIsNull() {
CategorySeries fruit = chart.addSeries(
"fruit",
Arrays.asList("Blue", "Red", "Green", "Yellow", "Orange"),
Arrays.asList(-40, 30, 20, 60, 60));

chart.updateCategorySeries("fruit",
null,
Arrays.asList(-40, 30, 20, 60, 60),
null);

assertThat(fruit.getXData()).isEqualTo(Arrays.asList(1, 2, 3, 4, 5));
}

@Test
void paint() {
chart.addSeries(
"fruit",
Arrays.asList("Blue", "Red", "Green", "Yellow", "Orange"),
Arrays.asList(-40, 30, 20, 60, 60));
chart.addSeries(
"food",
Arrays.asList("Blue", "Red", "Green", "Yellow", "Orange"),
Arrays.asList(-40, 30, 20, 60, 60));

chart.paint(new CustomGraphic(), 20, 20);

for (CategorySeries series : chart.getSeriesMap().values()) {
CategorySeries.CategorySeriesRenderStyle seriesType = series.getChartCategorySeriesRenderStyle();
if (seriesType != null) {
assertThat(series.getChartCategorySeriesRenderStyle())
.isEqualTo(chart.getStyler().getDefaultSeriesRenderStyle());
}
}
}
}
5 changes: 3 additions & 2 deletions xchart/src/test/java/org/knowm/xchart/HistogramTest.java
@@ -1,9 +1,10 @@
package org.knowm.xchart;

import static org.fest.assertions.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.*;

import java.util.Arrays;
import org.junit.Test;

import org.junit.jupiter.api.Test;

/** @author timmolter */
public class HistogramTest {
Expand Down
@@ -1,9 +1,10 @@
package org.knowm.xchart;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.knowm.xchart.VectorGraphicsEncoder.VectorGraphicsFormat;

import static org.assertj.core.api.Assertions.*;

/** @author ekleinod */
public class VectorGraphicsEncoderTest {

Expand All @@ -13,32 +14,27 @@ public void testAddFileExtension() {
for (VectorGraphicsFormat format : VectorGraphicsFormat.values()) {

// test -> test.svg
Assert.assertEquals(
String.format("test.%s", format.toString().toLowerCase()),
VectorGraphicsEncoder.addFileExtension("test", format));
assertThat(VectorGraphicsEncoder.addFileExtension("test", format))
.isEqualTo(String.format("test.%s", format.toString().toLowerCase()));

// test.svg -> test.svg
Assert.assertEquals(
String.format("test.%s", format.toString().toLowerCase()),
VectorGraphicsEncoder.addFileExtension(
String.format("test.%s", format.toString().toLowerCase()), format));
assertThat(VectorGraphicsEncoder.addFileExtension(
String.format("test.%s", format.toString().toLowerCase()), format))
.isEqualTo(String.format("test.%s", format.toString().toLowerCase()));

// test.svgsvg -> test.svgsvg.svg
Assert.assertEquals(
String.format("test.%1$s%1$s.%1$s", format.toString().toLowerCase()),
VectorGraphicsEncoder.addFileExtension(
String.format("test.%1$s%1$s", format.toString().toLowerCase()), format));
assertThat(VectorGraphicsEncoder.addFileExtension(
String.format("test.%1$s%1$s", format.toString().toLowerCase()), format))
.isEqualTo(String.format("test.%1$s%1$s.%1$s", format.toString().toLowerCase()));

// test.svg.svg -> test.svg.svg
Assert.assertEquals(
String.format("test.%1$s.%1$s", format.toString().toLowerCase()),
VectorGraphicsEncoder.addFileExtension(
String.format("test.%1$s.%1$s", format.toString().toLowerCase()), format));
assertThat(VectorGraphicsEncoder.addFileExtension(
String.format("test.%1$s.%1$s", format.toString().toLowerCase()), format))
.isEqualTo(String.format("test.%1$s.%1$s", format.toString().toLowerCase()));

// test.txt -> test.txt.svg
Assert.assertEquals(
String.format("test.txt.%1$s", format.toString().toLowerCase()),
VectorGraphicsEncoder.addFileExtension("test.txt", format));
assertThat(VectorGraphicsEncoder.addFileExtension("test.txt", format))
.isEqualTo(String.format("test.txt.%1$s", format.toString().toLowerCase()));
}
}
}

0 comments on commit dda1ba9

Please sign in to comment.