Skip to content

Commit 6adb2a9

Browse files
committed
Get test coverage up to 99%
1 parent b7f39a9 commit 6adb2a9

17 files changed

+550
-11
lines changed

src/main/java/org/geometrycommands/ProjectCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected void processGeometry(Geometry geometry, ProjectOptions options, Reader
8282
targetCrs = csFactory.createFromName(options.getTarget());
8383
} else {
8484
targetCrs = csFactory.createFromParameters(null, options.getTarget());
85-
}
85+
}
8686

8787
CoordinateTransform transform = ctFactory.createTransform(sourceCrs, targetCrs);
8888
Geometry projectedGeometry = transformGeometry(transform, geometry);

src/main/java/org/geometrycommands/SubLineCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public SubLineOptions getOptions() {
5252
@Override
5353
protected void processGeometry(Geometry geometry, SubLineOptions options, Reader reader, Writer writer) throws Exception {
5454
if (!(geometry instanceof Lineal)) {
55-
throw new IllegalArgumentException("The input geometry a LineString or a MultiLineString!");
55+
throw new IllegalArgumentException("The input geometry must be a LineString or a MultiLineString!");
5656
}
5757
if (options.getStartPosition() > options.getEndPosition()) {
5858
throw new IllegalArgumentException("The start position must be less than the end position!");

src/main/java/org/geometrycommands/ToWkbCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public ToWkbOptions getOptions() {
5151
*/
5252
@Override
5353
protected void processGeometry(Geometry geometry, ToWkbOptions options, Reader reader, Writer writer) throws Exception {
54-
WKBWriter wkbWriter = new WKBWriter();
54+
WKBWriter wkbWriter = new WKBWriter(options.getOutputDimension(), options.getByteOrder());
5555
writer.write(WKBWriter.toHex(wkbWriter.write(geometry)));
5656
}
5757

src/test/java/org/geometrycommands/DrawCommandTest.java

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ public class DrawCommandTest extends BaseTest {
1818

1919
@Test
2020
public void execute() throws Exception {
21-
2221
String inputGeometry = "POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))";
2322
File file = File.createTempFile("image", ".png");
2423
DrawOptions options = new DrawOptions();
2524
options.setGeometry(inputGeometry);
25+
options.setWidth(400);
26+
options.setHeight(400);
2627
options.setFile(file);
2728

2829
Reader reader = new StringReader(inputGeometry);
@@ -33,6 +34,53 @@ public void execute() throws Exception {
3334
assertTrue(file.exists());
3435
}
3536

37+
@Test
38+
public void executeDrawingCoordinates() throws Exception {
39+
String inputGeometry = "POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))";
40+
File file = File.createTempFile("image", ".png");
41+
DrawOptions options = new DrawOptions();
42+
options.setGeometry(inputGeometry);
43+
options.setBounds("0,0,10,10");
44+
options.setBackgroundColor("255,0,255");
45+
options.setDrawingCoordinates(true);
46+
options.setMarkerSize(12);
47+
options.setFillColor("0,0,255");
48+
options.setFillOpacity(0.5f);
49+
options.setStrokeColor("255,255,0");
50+
options.setStrokeOpacity(0.95f);
51+
options.setStrokeWidth(1.2f);
52+
options.setShape("square");
53+
options.setBackgroundImage(" ");
54+
options.setFile(file);
55+
56+
Reader reader = new StringReader(inputGeometry);
57+
StringWriter writer = new StringWriter();
58+
59+
DrawCommand command = new DrawCommand();
60+
command.execute(options, reader, writer);
61+
assertTrue(file.exists());
62+
}
63+
64+
@Test
65+
public void executeWithBackgroundImage() throws Exception {
66+
String inputGeometry = "POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))";
67+
File file = File.createTempFile("image", ".png");
68+
DrawOptions options = new DrawOptions();
69+
options.setGeometry(inputGeometry);
70+
options.setWidth(400);
71+
options.setHeight(400);
72+
options.setFile(file);
73+
File backgroundImageFile = new File(getClass().getClassLoader().getResource("grid.png").toURI());
74+
options.setBackgroundImage(backgroundImageFile.getAbsolutePath());
75+
76+
Reader reader = new StringReader(inputGeometry);
77+
StringWriter writer = new StringWriter();
78+
79+
DrawCommand command = new DrawCommand();
80+
command.execute(options, reader, writer);
81+
assertTrue(file.exists());
82+
}
83+
3684
@Test
3785
public void run() throws Exception {
3886
// Geometry from options
@@ -57,11 +105,56 @@ public void run() throws Exception {
57105
"-t", "0.75",
58106
"-l", "255,255,0",
59107
"-o", "0.55",
60-
"-m", "square",
108+
"-m", "star",
61109
"-z", "12",
62110
"-e", "0,0,10,10"
63111
}, "POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))");
64112
assertTrue(file.exists());
65113
assertTrue(file.length() > 0);
114+
115+
file = File.createTempFile("image", ".png");
116+
result = runApp(new String[]{
117+
"draw",
118+
"-g", "POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))",
119+
"-f", file.getAbsolutePath(),
120+
"-e", "0,0,10,0",
121+
"-m", "cross"
122+
}, null);
123+
assertTrue(file.exists());
124+
assertTrue(file.length() > 0);
125+
126+
file = File.createTempFile("image", ".png");
127+
result = runApp(new String[]{
128+
"draw",
129+
"-g", "POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))",
130+
"-f", file.getAbsolutePath(),
131+
"-e", "0,0,0,10",
132+
"-m", "triangle"
133+
}, null);
134+
assertTrue(file.exists());
135+
assertTrue(file.length() > 0);
136+
137+
file = File.createTempFile("image", ".png");
138+
result = runApp(new String[]{
139+
"draw",
140+
"-g", "POINT (5 5)",
141+
"-f", file.getAbsolutePath(),
142+
"-e", "5,5,5,5",
143+
"-m", "square"
144+
}, null);
145+
assertTrue(file.exists());
146+
assertTrue(file.length() > 0);
147+
148+
file = File.createTempFile("image", ".png");
149+
result = runApp(new String[]{
150+
"draw",
151+
"-g", "LINESTRING (0 0, 10 20)",
152+
"-f", file.getAbsolutePath(),
153+
"-c",
154+
"-e", "0,0,10", // Bad bounds
155+
"-m", "x"
156+
}, null);
157+
assertTrue(file.exists());
158+
assertTrue(file.length() > 0);
66159
}
67160
}

src/test/java/org/geometrycommands/ProjectCommandTest.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
import java.io.Reader;
55
import java.io.StringReader;
66
import java.io.StringWriter;
7+
78
import org.junit.Test;
89
import static org.junit.Assert.assertEquals;
9-
import static org.junit.Assert.assertFalse;
10-
import static org.junit.Assert.assertTrue;
1110

1211
/**
1312
* The ProjectCommand UnitTest
@@ -50,6 +49,20 @@ public void run() throws Exception {
5049
"-t", "EPSG:4326"
5150
}, "POINT (1186683.01 641934.58)");
5251
assertEquals("POINT (-122.32131937934592 47.07927009358412)", result);
52+
53+
result = runApp(new String[]{
54+
"project",
55+
"-s", "EPSG:2927",
56+
"-t", "+title=long/lat:WGS84 +proj=longlat +ellps=WGS84 +datum=WGS84 +units=degrees"
57+
}, "POINT (1186683.009999998 641934.5799999853)");
58+
assertEquals("POINT (-122.32131937934592 47.07927009358407)", result);
59+
60+
result = runApp(new String[]{
61+
"project",
62+
"-s", "+title=long/lat:WGS84 +proj=longlat +ellps=WGS84 +datum=WGS84 +units=degrees",
63+
"-t", "EPSG:2927"
64+
}, "POINT (-122.32131937934592 47.07927009358412)");
65+
assertEquals("POINT (1186683.009999998 641934.5799999853)", result);
5366
}
5467

5568
@Test

src/test/java/org/geometrycommands/RandomCommandTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.io.Reader;
77
import java.io.StringReader;
88
import java.io.StringWriter;
9+
import java.util.Map;
10+
911
import org.junit.Test;
1012
import static org.junit.Assert.assertEquals;
1113
import static org.junit.Assert.assertFalse;
@@ -24,6 +26,8 @@ public void execute() throws Exception {
2426
RandomOptions options = new RandomOptions();
2527
options.setGeometry(inputGeometry);
2628
options.setNumber(10);
29+
options.setConstrainedToCircle(false);
30+
options.setGutterFraction(Double.NaN);
2731

2832
Reader reader = new StringReader(inputGeometry);
2933
StringWriter writer = new StringWriter();
@@ -75,5 +79,37 @@ public void run() throws Exception {
7579
geometry = new WKTReader().read(result);
7680
assertEquals("MultiPoint", geometry.getGeometryType());
7781
assertEquals(10, geometry.getNumGeometries());
82+
83+
// Geometry from input stream
84+
result = runApp(new String[]{
85+
"random",
86+
"-n", "10",
87+
"-r",
88+
"-c",
89+
"-f", "0.25"
90+
}, "POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))");
91+
geometry = new WKTReader().read(result);
92+
assertEquals("MultiPoint", geometry.getGeometryType());
93+
assertEquals(16, geometry.getNumGeometries());
94+
}
95+
96+
@Test
97+
public void runWithWrongGeometryType() throws Exception {
98+
Map<String,String> result = runAppWithOutAndErr(new String[]{
99+
"random",
100+
"-g", "POINT (1 1)",
101+
"-n", "10"
102+
}, null);
103+
assertEquals("Geometry must be a Polygon or MultiPolygon!" + NEW_LINE +
104+
"Usage: geom <command> <args>" + NEW_LINE +
105+
" --help : Print help message" + NEW_LINE +
106+
" -c (--constrained) : The flag for whether the random points should be" + NEW_LINE +
107+
" constrained to a circle when gridded." + NEW_LINE +
108+
" -f (--gutterFraction) N : The gutter distance or padding for random points" + NEW_LINE +
109+
" when gridded." + NEW_LINE +
110+
" -g (--geometry) VAL : The input geometry" + NEW_LINE +
111+
" -n (--number) N : The number of points" + NEW_LINE +
112+
" -r (--gridded) : The flag for whether the random points should be" + NEW_LINE +
113+
" gridded.", result.get("err"));
78114
}
79115
}

src/test/java/org/geometrycommands/RectangleCommandTest.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.io.Reader;
44
import java.io.StringReader;
55
import java.io.StringWriter;
6+
import java.util.Map;
7+
68
import org.junit.Test;
79
import static org.junit.Assert.assertEquals;
810
import org.geometrycommands.RectangleCommand.RectangleOptions;
@@ -22,6 +24,8 @@ public void execute() throws Exception {
2224
options.setWidth(50);
2325
options.setHeight(40);
2426
options.setNumberOfPoints(10);
27+
options.setRotation(0.0);
28+
options.setCenter(false);
2529

2630
Reader reader = new StringReader(inputGeometry);
2731
StringWriter writer = new StringWriter();
@@ -55,5 +59,76 @@ public void run() throws Exception {
5559
}, "POINT (100 100)");
5660
assertEquals("POLYGON ((100 100, 125 100, 150 100, 150 120, 150 140, "
5761
+ "125 140, 100 140, 100 120, 100 100))", result);
62+
63+
result = runApp(new String[]{
64+
"rectangle",
65+
"-w", "50",
66+
"-h", "40",
67+
"-p", "10",
68+
"-c"
69+
}, "POINT (100 100)");
70+
assertEquals("POLYGON ((75 80, 100 80, 125 80, 125 100, " +
71+
"125 120, 100 120, 75 120, 75 100, 75 80))", result);
72+
73+
result = runApp(new String[]{
74+
"rectangle",
75+
"-w", "90",
76+
"-h", "70",
77+
"-p", "10",
78+
"-c"
79+
}, "POLYGON ((75 80, 100 80, 125 80, 125 100, " +
80+
"125 120, 100 120, 75 120, 75 100, 75 80))");
81+
assertEquals("POLYGON ((75 80, 100 80, 125 80, 125 100, " +
82+
"125 120, 100 120, 75 120, 75 100, 75 80))", result);
83+
}
84+
85+
@Test
86+
public void runWithInvalidAlgorithm() throws Exception {
87+
Map<String,String> result = runAppWithOutAndErr(new String[]{
88+
"rectangle",
89+
"-g", "POINT (100 100)",
90+
"-p", "10"
91+
}, null);
92+
assertEquals("When the Geometry is a point, then width and height are required!" + NEW_LINE +
93+
"Usage: geom <command> <args>" + NEW_LINE +
94+
" --help : Print help message" + NEW_LINE +
95+
" -c (--center) : The flag to use center (true) or the base (false)" + NEW_LINE +
96+
" -g (--geometry) VAL : The input geometry" + NEW_LINE +
97+
" -h (--height) N : The height" + NEW_LINE +
98+
" -p (--numberOfPoints) N : The number of points" + NEW_LINE +
99+
" -r (--rotation) N : The rotation" + NEW_LINE +
100+
" -w (--width) N : The width", result.get("err"));
101+
102+
result = runAppWithOutAndErr(new String[]{
103+
"rectangle",
104+
"-g", "POINT (100 100)",
105+
"-p", "10",
106+
"-w", "10"
107+
}, null);
108+
assertEquals("When the Geometry is a point, then width and height are required!" + NEW_LINE +
109+
"Usage: geom <command> <args>" + NEW_LINE +
110+
" --help : Print help message" + NEW_LINE +
111+
" -c (--center) : The flag to use center (true) or the base (false)" + NEW_LINE +
112+
" -g (--geometry) VAL : The input geometry" + NEW_LINE +
113+
" -h (--height) N : The height" + NEW_LINE +
114+
" -p (--numberOfPoints) N : The number of points" + NEW_LINE +
115+
" -r (--rotation) N : The rotation" + NEW_LINE +
116+
" -w (--width) N : The width", result.get("err"));
117+
118+
result = runAppWithOutAndErr(new String[]{
119+
"rectangle",
120+
"-g", "POINT (100 100)",
121+
"-p", "10",
122+
"-h", "10"
123+
}, null);
124+
assertEquals("When the Geometry is a point, then width and height are required!" + NEW_LINE +
125+
"Usage: geom <command> <args>" + NEW_LINE +
126+
" --help : Print help message" + NEW_LINE +
127+
" -c (--center) : The flag to use center (true) or the base (false)" + NEW_LINE +
128+
" -g (--geometry) VAL : The input geometry" + NEW_LINE +
129+
" -h (--height) N : The height" + NEW_LINE +
130+
" -p (--numberOfPoints) N : The number of points" + NEW_LINE +
131+
" -r (--rotation) N : The rotation" + NEW_LINE +
132+
" -w (--width) N : The width", result.get("err"));
58133
}
59134
}

src/test/java/org/geometrycommands/ReflectCommandTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,34 @@ public void execute() throws Exception {
3030
assertEquals("POLYGON ((0 0, 6.8965517241379315 -7.241379310344829, "
3131
+ "14.137931034482762 -0.3448275862068977, "
3232
+ "7.241379310344829 6.8965517241379315, 0 0))", writer.getBuffer().toString());
33+
34+
options = new ReflectOptions();
35+
options.setGeometry(inputGeometry);
36+
options.setX0(5);
37+
options.setY0(2);
38+
options.setX1(2);
39+
options.setY1(1);
40+
41+
reader = new StringReader(inputGeometry);
42+
writer = new StringWriter();
43+
44+
command.execute(options, reader, writer);
45+
assertEquals("POLYGON ((-0.1999999999999993 0.5999999999999996, 5.800000000000001 -7.3999999999999995, " +
46+
"13.8 -1.3999999999999995, 7.8 6.6, " +
47+
"-0.1999999999999993 0.5999999999999996))", writer.getBuffer().toString());
48+
49+
options = new ReflectOptions();
50+
options.setGeometry(inputGeometry);
51+
options.setX0(5);
52+
options.setY0(2);
53+
options.setX1(2);
54+
55+
reader = new StringReader(inputGeometry);
56+
writer = new StringWriter();
57+
58+
command.execute(options, reader, writer);
59+
assertEquals("POLYGON ((0 0, 6.8965517241379315 -7.241379310344829, 14.137931034482762 -0.3448275862068977, " +
60+
"7.241379310344829 6.8965517241379315, 0 0))", writer.getBuffer().toString());
3361
}
3462

3563
@Test

0 commit comments

Comments
 (0)