Improved tests.

This commit is contained in:
Erik C. Thauvin 2019-04-09 15:59:13 -07:00
parent b1641eba06
commit 76e7651f7c
6 changed files with 60 additions and 32 deletions

View file

@ -62,18 +62,31 @@ public class UtilsTest {
}
@Test
public void testBold() throws Exception {
public void testBold() {
assertThat(Utils.bold(1)).as("bold(1)").isEqualTo(Colors.BOLD + "1" + Colors.BOLD);
assertThat(Utils.bold(ASCII)).as("bold(ascii").isEqualTo(Colors.BOLD + ASCII + Colors.BOLD);
}
@Test
public void testCapitalize() throws Exception {
public void testCapitalize() {
assertThat(Utils.capitalize("this is a test.")).isEqualTo("This is a test.");
}
@Test
public void testEnsureDir() throws Exception {
public void testColorize() {
assertThat(Utils.colorize(ASCII, Colors.REVERSE)).as("reverse")
.isEqualTo(Colors.REVERSE + ASCII + Colors.REVERSE);
assertThat(Utils.colorize(ASCII, Colors.RED)).as("red")
.isEqualTo(Colors.RED + ASCII + Colors.NORMAL);
}
@Test
public void testCyan() {
assertThat(Utils.cyan(ASCII)).isEqualTo(Colors.CYAN + ASCII + Colors.NORMAL);
}
@Test
public void testEnsureDir() {
assertThat(Utils.ensureDir("dir", false)).as("ensureDir(dir, false)")
.isEqualTo("dir" + File.separatorChar);
assertThat(Utils.ensureDir("https://erik.thauvin.net", true))
@ -81,18 +94,18 @@ public class UtilsTest {
}
@Test
public void testGetIntProperty() throws Exception {
public void testGetIntProperty() {
assertThat(Utils.getIntProperty("10", 1)).as("getIntProperty(10, 1)").isEqualTo(10);
assertThat(Utils.getIntProperty("a", 1)).as("getIntProperty(a, 1)").isEqualTo(1);
}
@Test
public void testGreen() throws Exception {
public void testGreen() {
assertThat(Utils.green(ASCII)).isEqualTo(Colors.DARK_GREEN + ASCII + Colors.NORMAL);
}
@Test
public void testIsValidString() throws Exception {
public void testIsValidString() {
assertThat(Utils.isValidString(ASCII)).as("isValidString(ascii)").isTrue();
assertThat(Utils.isValidString("")).as("isValidString(empty)").isFalse();
assertThat(Utils.isValidString(" ")).as("isValidString( )").isFalse();
@ -101,13 +114,13 @@ public class UtilsTest {
}
@Test
public void testIsoLocalDate() throws Exception {
public void testIsoLocalDate() {
assertThat(Utils.isoLocalDate(cal.getTime())).as("isoLocalDate(date)").isEqualTo("1952-02-17");
assertThat(Utils.isoLocalDate(localDateTime)).as("isoLocalDate(localDate)").isEqualTo("1952-02-17");
}
@Test
public void testPlural() throws Exception {
public void testPlural() {
final String week = "week";
final String weeks = "weeks";
@ -118,23 +131,23 @@ public class UtilsTest {
}
@Test
public void testReverseColor() throws Exception {
public void testReverseColor() {
assertThat(Utils.reverseColor(ASCII)).isEqualTo(Colors.REVERSE + ASCII + Colors.REVERSE);
}
@Test
public void testToday() throws Exception {
public void testToday() {
assertThat(Utils.today()).isEqualTo(Utils.isoLocalDate(LocalDateTime.now()));
}
@Test
public void testUnescapeXml() throws Exception {
public void testUnescapeXml() {
assertThat(Utils.unescapeXml("<a name="test & ''">"))
.isEqualTo("<a name=\"test & ''\">");
}
@Test
public void testUtcDateTime() throws Exception {
public void testUtcDateTime() {
assertThat(Utils.utcDateTime(cal.getTime())).as("utcDateTime(date)").isEqualTo("1952-02-17 12:30");
assertThat(Utils.utcDateTime(localDateTime)).as("utcDateTime(localDate)")
.isEqualTo("1952-02-17 12:30");

View file

@ -41,7 +41,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @since 1.0
*/
final class AbstractModuleTest {
static void testAbstractModule(AbstractModule module) {
static void testAbstractModule(final AbstractModule module) {
final String name = module.getClass().getName();
assertThat(module.isEnabled()).as(name + ": enabled").isNotEqualTo(module.hasProperties());

View file

@ -1,5 +1,5 @@
/*
* LocalProperties.java
* properties.java
*
* Copyright (c) 2004-2019, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
@ -41,37 +41,37 @@ import java.nio.file.Paths;
import java.util.Properties;
/**
* The <code>LocalProperties</code> class.
* The <code>properties</code> class.
*
* @author <a href="https://erik.thauvin.net/" target="_blank">Erik C. Thauvin</a>
* @created 2019-04-08
* @since 1.0
*/
class LocalProperties {
private static Properties localProperties = new Properties();
private static final Properties localProps = new Properties();
static String getProperty(final String key) {
if (localProperties.containsKey(key)) {
return localProperties.getProperty(key);
if (localProps.containsKey(key)) {
return localProps.getProperty(key);
} else {
final String env = System.getenv(keyToEnv(key));
if (env != null) {
localProperties.setProperty(key, env);
localProps.setProperty(key, env);
}
return env;
}
}
private static String keyToEnv(String key) {
private static String keyToEnv(final String key) {
return key.replaceAll("-", "_").toUpperCase();
}
@BeforeSuite
static void loadLocalProperties() {
final Path localProps = Paths.get("local.properties");
if (Files.exists(localProps)) {
try (final InputStream stream = Files.newInputStream(localProps)) {
localProperties.load(stream);
@BeforeSuite(alwaysRun = true)
public void loadProperties() {
final Path localPath = Paths.get("local.properties");
if (Files.exists(localPath)) {
try (final InputStream stream = Files.newInputStream(localPath)) {
localProps.load(stream);
} catch (IOException ignore) {
// Do nothing
}

View file

@ -60,7 +60,7 @@ public class LookupTest {
public void testWhois() throws Exception {
final String[] result = Lookup.whois("17.178.96.59", Lookup.WHOIS_HOST);
assertThat(Arrays.asList(result).stream().anyMatch(m -> m.contains("Apple Inc.")))
assertThat(Arrays.stream(result).anyMatch(m -> m.contains("Apple Inc.")))
.as("whois(17.178.96.59/Apple Inc.").isTrue();
}
}

View file

@ -48,7 +48,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class ModuleExceptionTest {
@DataProvider(name = "dp")
Object[][] createData(Method m) {
Object[][] createData(final Method m) {
System.out.println(m.getName()); // print test method name
return new Object[][]{new Object[]{new ModuleException("debugMessage", "message",
new IOException("Secret URL http://foo.com?apiKey=sec&userID=me"))},
@ -57,17 +57,17 @@ public class ModuleExceptionTest {
}
@Test(dataProvider = "dp")
final void testGetDebugMessage(ModuleException e) {
final void testGetDebugMessage(final ModuleException e) {
assertThat(e.getDebugMessage()).as("get debug message").isEqualTo("debugMessage");
}
@Test(dataProvider = "dp")
final void testGetMessage(ModuleException e) {
final void testGetMessage(final ModuleException e) {
assertThat(e.getMessage()).as("get message").isEqualTo("message");
}
@Test(dataProvider = "dp")
final void testGetStanitizedMessage(ModuleException e) {
final void testGetStanitizedMessage(final ModuleException e) {
if (e.hasCause()) {
assertThat(e.getSanitizedMessage()).as("get sanitzed url")
.contains("http://foo.com?apiKey=[3]&userID=[2]");

View file

@ -45,7 +45,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @created 2019-04-07
* @since 1.0
*/
public class StockQuoteTest {
public class StockQuoteTest extends LocalProperties {
@Test
public void testGetQuote() throws ModuleException {
final String apiKey = LocalProperties.getProperty(StockQuote.ALPHAVANTAGE_API_KEY_PROP);
@ -56,6 +56,21 @@ public class StockQuoteTest {
messages = StockQuote.getQuote("012", apiKey);
assertThat(messages.get(0).isError()).as("invalid symbol error").isTrue();
try {
StockQuote.getQuote("test", "");
} catch (Exception e) {
assertThat(e).as("no API key").isInstanceOf(ModuleException.class);
assertThat(e).as("no API key exception has no cause").hasNoCause();
}
try {
StockQuote.getQuote("", "apikey");
} catch (Exception e) {
assertThat(e).as("no symbol").isInstanceOf(ModuleException.class);
assertThat(e).as("no symbol exception has no cause").hasNoCause();
}
} catch (ModuleException e) {
// Avoid displaying api keys in CI logs.
if ("true".equals(System.getenv("CI"))) {