Skip to content

Commit f7d190c

Browse files
Xinmeirfecher
authored andcommitted
unit tests added for GeoWaveMain, VersionUtils, ExplainCommand, HelpCommand, ListCommand, SetCommand, ConfigOptions, and OperationRegistry
1 parent 13f94c0 commit f7d190c

File tree

7 files changed

+312
-0
lines changed

7 files changed

+312
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package mil.nga.giat.geowave.core.cli;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.PrintStream;
7+
8+
import org.junit.Test;
9+
10+
public class VersionUtilsTest
11+
{
12+
13+
@Test
14+
public void testVersion() {
15+
String version = null; // change this value when it gives a version
16+
assertEquals(
17+
version, // change this value when it gives a version
18+
VersionUtils.getVersion());
19+
}
20+
21+
@Test
22+
public void testPrintVersionInfo() {
23+
ByteArrayOutputStream output = new ByteArrayOutputStream();
24+
System.setOut(new PrintStream(
25+
output));
26+
VersionUtils.printVersionInfo();
27+
String expectedoutput = "{}\n"; // change this value when it gives a version
28+
assertEquals(
29+
expectedoutput,
30+
output.toString());
31+
}
32+
33+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package mil.nga.giat.geowave.core.cli.operations;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.PrintStream;
7+
8+
import org.junit.Test;
9+
10+
import mil.nga.giat.geowave.core.cli.parser.CommandLineOperationParams;
11+
import mil.nga.giat.geowave.core.cli.parser.OperationParser;
12+
import mil.nga.giat.geowave.core.cli.spi.OperationRegistry;
13+
14+
public class ExplainCommandTest {
15+
16+
@Test
17+
public void testPrepare() {
18+
String[] args = {"explain"};
19+
OperationRegistry registry = OperationRegistry.getInstance();
20+
OperationParser parser = new OperationParser(registry);
21+
CommandLineOperationParams params = parser.parse(
22+
GeowaveTopLevelSection.class,
23+
args);
24+
25+
ExplainCommand expcommand = new ExplainCommand();
26+
expcommand.prepare(params);
27+
assertEquals(false, params.isValidate());
28+
assertEquals(true, params.isAllowUnknown());
29+
}
30+
31+
@Test
32+
public void testExecute() {
33+
ByteArrayOutputStream output = new ByteArrayOutputStream();
34+
System.setOut(new PrintStream(output));
35+
36+
String[] args = {"explain"};
37+
OperationRegistry registry = OperationRegistry.getInstance();
38+
OperationParser parser = new OperationParser(registry);
39+
CommandLineOperationParams params = parser.parse(
40+
GeowaveTopLevelSection.class,
41+
args);
42+
ExplainCommand expcommand = new ExplainCommand();
43+
expcommand.execute(params);
44+
45+
String expectedoutput =
46+
"Command: geowave [options] <subcommand> ...\n\n"
47+
+ " VALUE NEEDED PARAMETER NAMES \n"
48+
+ "----------------------------------------------\n"
49+
+ "{ } -cf, --config-file, \n"
50+
+ "{ } --debug, \n"
51+
+ "{ } --version,\n";
52+
assertEquals(expectedoutput, output.toString());
53+
}
54+
55+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package mil.nga.giat.geowave.core.cli.operations;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.PrintStream;
7+
8+
import org.junit.Test;
9+
10+
import mil.nga.giat.geowave.core.cli.parser.CommandLineOperationParams;
11+
import mil.nga.giat.geowave.core.cli.parser.OperationParser;
12+
import mil.nga.giat.geowave.core.cli.spi.OperationRegistry;
13+
14+
public class HelpCommandTest {
15+
16+
@Test
17+
public void testPrepare() {
18+
String[] args = {"help"};
19+
OperationRegistry registry = OperationRegistry.getInstance();
20+
OperationParser parser = new OperationParser(registry);
21+
final CommandLineOperationParams params = parser.parse(
22+
GeowaveTopLevelSection.class,
23+
args);
24+
25+
HelpCommand helpcommand = new HelpCommand();
26+
helpcommand.prepare(params);
27+
assertEquals(false, params.isValidate());
28+
assertEquals(true, params.isAllowUnknown());
29+
}
30+
31+
@Test
32+
public void testExecute() {
33+
ByteArrayOutputStream output = new ByteArrayOutputStream();
34+
System.setOut(new PrintStream(output));
35+
36+
String[] args = {"help"};
37+
OperationRegistry registry = OperationRegistry.getInstance();
38+
OperationParser parser = new OperationParser(registry);
39+
final CommandLineOperationParams params = parser.parse(
40+
GeowaveTopLevelSection.class,
41+
args);
42+
43+
HelpCommand helpcommand = new HelpCommand();
44+
helpcommand.execute(params);
45+
46+
String expectedoutput = "Usage: geowave help [options]\n";
47+
assertEquals(expectedoutput, output.toString());
48+
}
49+
50+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package mil.nga.giat.geowave.core.cli.operations.config;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.File;
7+
import java.io.PrintStream;
8+
import java.util.ArrayList;
9+
import java.util.Collections;
10+
import java.util.List;
11+
import java.util.Properties;
12+
13+
import org.junit.Test;
14+
15+
import mil.nga.giat.geowave.core.cli.operations.GeowaveTopLevelSection;
16+
import mil.nga.giat.geowave.core.cli.operations.config.options.ConfigOptions;
17+
import mil.nga.giat.geowave.core.cli.parser.CommandLineOperationParams;
18+
import mil.nga.giat.geowave.core.cli.parser.OperationParser;
19+
import mil.nga.giat.geowave.core.cli.spi.OperationRegistry;
20+
21+
public class ListCommandTest {
22+
23+
@Test
24+
public void testExecute() {
25+
ByteArrayOutputStream output = new ByteArrayOutputStream();
26+
System.setOut(new PrintStream(output));
27+
28+
String[] args = {"config", "list"};
29+
OperationRegistry registry = OperationRegistry.getInstance();
30+
OperationParser parser = new OperationParser(registry);
31+
CommandLineOperationParams params = parser.parse(
32+
GeowaveTopLevelSection.class,
33+
args);
34+
ListCommand lstcommand = new ListCommand();
35+
lstcommand.execute(params);
36+
37+
// can directly write to expectedoutput if knowing properties
38+
File f = (File) params.getContext().get(
39+
ConfigOptions.PROPERTIES_FILE_CONTEXT);
40+
Properties p = ConfigOptions.loadProperties(
41+
f,
42+
null);
43+
List<String> keys = new ArrayList<String>();
44+
keys.addAll(p.stringPropertyNames());
45+
Collections.sort(keys);
46+
StringBuffer expectedoutput =
47+
new StringBuffer("PROPERTIES (unknownversion-config.properties)\n");
48+
for (String key : keys) {
49+
String value = (String) p.get(key);
50+
expectedoutput.append(key + ": " + value+"\n");
51+
}
52+
53+
assertEquals(expectedoutput.toString(), output.toString());
54+
}
55+
56+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package mil.nga.giat.geowave.core.cli.operations.config;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.io.File;
6+
import java.util.Properties;
7+
8+
import org.junit.Test;
9+
10+
import mil.nga.giat.geowave.core.cli.operations.GeowaveTopLevelSection;
11+
import mil.nga.giat.geowave.core.cli.operations.config.options.ConfigOptions;
12+
import mil.nga.giat.geowave.core.cli.parser.CommandLineOperationParams;
13+
import mil.nga.giat.geowave.core.cli.parser.OperationParser;
14+
import mil.nga.giat.geowave.core.cli.spi.OperationRegistry;
15+
16+
public class SetCommandTest {
17+
18+
@Test
19+
public void testExecute() {
20+
String[] args = {"config", "set", "name", "value"};
21+
OperationRegistry registry = OperationRegistry.getInstance();
22+
OperationParser parser = new OperationParser(registry);
23+
CommandLineOperationParams params = parser.parse(
24+
GeowaveTopLevelSection.class,
25+
args);
26+
27+
SetCommand setcommand = new SetCommand();
28+
String name = "name";
29+
String value = "value";
30+
setcommand.setParameters(name, value);
31+
setcommand.execute(params);
32+
33+
File f = (File) params.getContext().get(
34+
ConfigOptions.PROPERTIES_FILE_CONTEXT);
35+
Properties p = ConfigOptions.loadProperties(
36+
f,
37+
null);
38+
assertEquals(value, p.getProperty(name));
39+
}
40+
41+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package mil.nga.giat.geowave.core.cli.operations.config.options;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.io.File;
6+
import java.util.Properties;
7+
8+
import org.junit.Test;
9+
10+
public class ConfigOptionsTest {
11+
12+
@Test
13+
public void testGetPropertyPath() {
14+
String propertypath = ConfigOptions.getDefaultPropertyPath().getAbsolutePath();
15+
String expectedoutput = String.format(
16+
"%s%s",
17+
System.getProperty("user.home"),
18+
"/.geowave");
19+
assertEquals(expectedoutput, propertypath);
20+
}
21+
22+
@Test
23+
public void testGetPropertyFile() {
24+
String propertyfile = ConfigOptions.getDefaultPropertyFile().getAbsolutePath();
25+
String expectedoutput = String.format(
26+
"%s%s",
27+
System.getProperty("user.home"),
28+
"/.geowave/unknownversion-config.properties");
29+
assertEquals(expectedoutput, propertyfile);
30+
}
31+
32+
@Test
33+
public void testWriteProperty() {
34+
String parent = String.format(
35+
"%s",
36+
System.getProperty("user.home"));
37+
File path = new File(parent);
38+
File configfile = ConfigOptions.formatConfigFile("0", path);
39+
Properties prop = new Properties();
40+
String key = "key";
41+
String value = "value";
42+
prop.setProperty(key, value);
43+
boolean success = ConfigOptions.writeProperties(configfile, prop);
44+
if (success) {
45+
Properties loadprop = ConfigOptions.loadProperties(configfile, key);
46+
assertEquals(value, loadprop.getProperty(key));
47+
}
48+
49+
}
50+
51+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package mil.nga.giat.geowave.core.cli.spi;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.util.ArrayList;
6+
import java.util.Collection;
7+
import java.util.List;
8+
9+
import org.junit.Test;
10+
11+
import mil.nga.giat.geowave.core.cli.operations.ExplainCommand;
12+
13+
public class OperationRegistryTest {
14+
15+
@Test
16+
public void testGetOperation() {
17+
OperationEntry optentry = new OperationEntry(ExplainCommand.class);
18+
List<OperationEntry> entries = new ArrayList<OperationEntry>();
19+
entries.add(optentry);
20+
OperationRegistry optreg = new OperationRegistry(entries);
21+
22+
assertEquals("explain", optreg.getOperation(ExplainCommand.class).getOperationName());
23+
assertEquals(true, optreg.getAllOperations().contains(optentry));
24+
}
25+
26+
}

0 commit comments

Comments
 (0)