diff --git a/build.gradle b/build.gradle index edacab1..2fdec7d 100644 --- a/build.gradle +++ b/build.gradle @@ -74,6 +74,13 @@ dependencies { compile files('lib/owm-japis-2.5.0.5.jar') compileOnly semverJar + + testCompile 'org.testng:testng:6.11' + testCompile 'org.assertj:assertj-core:3.8.0' +} + +test { + useTestNG() } annotationProcessor { diff --git a/kobalt/src/Build.kt b/kobalt/src/Build.kt index 14b107e..cfce410 100644 --- a/kobalt/src/Build.kt +++ b/kobalt/src/Build.kt @@ -9,6 +9,7 @@ import com.beust.kobalt.plugin.java.javadoc import com.beust.kobalt.plugin.packaging.assemble import com.beust.kobalt.plugin.packaging.install import com.beust.kobalt.plugin.publish.autoGitTag +import net.thauvin.erik.kobalt.plugin.versioneye.versionEye import java.io.File import java.io.FileInputStream import java.util.* @@ -80,6 +81,11 @@ val p = project { compileOnly(processorJar) } + dependenciesTest { + compile("org.testng:testng:6.11") + compile("org.assertj:assertj-core:3.8.0") + } + apt { outputDir = "../src/generated/java/" } @@ -124,6 +130,11 @@ val p = project { author = true links("http://www.jibble.org/javadocs/pircbot/", "http://docs.oracle.com/javase/8/docs/api/") } + + versionEye { + org = "thauvin" + team = "Owners" + } } @Task(name = "deploy", dependsOn = arrayOf("assemble", "install"), description = "Deploy application") diff --git a/src/main/java/net/thauvin/erik/mobibot/Utils.java b/src/main/java/net/thauvin/erik/mobibot/Utils.java index 2610f20..ecf43ff 100644 --- a/src/main/java/net/thauvin/erik/mobibot/Utils.java +++ b/src/main/java/net/thauvin/erik/mobibot/Utils.java @@ -143,6 +143,16 @@ final public class Utils { return (Commands.LINK_CMD + (entryIndex + 1) + "T: " + entry.getPinboardTags().replaceAll(",", ", ")); } + /** + * Capitalize a string. + * + * @param s The string. + * @return The capitalized string. + */ + public static String capitalize(final String s) { + return s.substring(0, 1).toUpperCase() + s.substring(1); + } + /** * Ensures that the given location (File/URL) has a trailing slash (/) to indicate a directory. * @@ -169,7 +179,7 @@ final public class Utils { /** * Returns a property as an int. * - * @param property The port property value. + * @param property The property value. * @param def The default property value. * @return The port or default value if invalid. */ @@ -300,7 +310,7 @@ final public class Utils { * Returns the specified date formatted as yyyy-MM-dd HH:mm * * @param date The date. - * @return The fromatted date. + * @return The formatted date. */ public static String utcDateTime(final LocalDateTime date) { return date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")); diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.java b/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.java index db91e46..99fd2f1 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.java @@ -63,10 +63,6 @@ public class Weather2 extends AbstractModule { properties.put(OWM_API_KEY_PROP, ""); } - private String capitalize(final String s) { - return s.substring(0, 1).toUpperCase() + s.substring(1); - } - /** * {@inheritDoc} */ @@ -147,7 +143,7 @@ public class Weather2 extends AbstractModule { if (i != 0) { condition.append(", ").append(w.getWeatherDescription()); } else { - condition.append(capitalize(w.getWeatherDescription())); + condition.append(Utils.capitalize(w.getWeatherDescription())); } } bot.send(sender, condition.toString(), isPrivate); diff --git a/src/test/java/net/thauvin/erik/mobibot/UtilsTest.java b/src/test/java/net/thauvin/erik/mobibot/UtilsTest.java new file mode 100644 index 0000000..cedac76 --- /dev/null +++ b/src/test/java/net/thauvin/erik/mobibot/UtilsTest.java @@ -0,0 +1,137 @@ +/* + * UtilsTest.java + * + * Copyright (c) 2004-2017, Erik C. Thauvin (erik@thauvin.net) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of this project nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.thauvin.erik.mobibot; + +import org.jibble.pircbot.Colors; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import java.io.File; +import java.time.LocalDateTime; +import java.util.Calendar; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * The Utils Test class. + * + * @author Erik C. Thauvin + * @created 2017-05-30 + * @since 1.0 + */ +public class UtilsTest { + final Calendar cal = Calendar.getInstance(); + final LocalDateTime localDateTime = + LocalDateTime.of(1952, 2, 17, 12, 30, 0); + + @BeforeClass + public void setUp() { + cal.set(1952, Calendar.FEBRUARY, 17, 12, 30, 0); + } + + + @Test + public void testBold() throws Exception { + assertThat(Utils.bold(1)).as("bold(1)").isEqualTo(Colors.BOLD + "1" + Colors.BOLD); + assertThat(Utils.bold("test")).as("bold(test").isEqualTo(Colors.BOLD + "test" + Colors.BOLD); + } + + @Test + public void testCapitalize() throws Exception { + assertThat(Utils.capitalize("test")).isEqualTo("Test"); + } + + @Test + public void testEnsureDir() throws Exception { + assertThat(Utils.ensureDir("test", false)).as("ensureDir(test, false)") + .isEqualTo("test" + File.separatorChar); + assertThat(Utils.ensureDir("http://erik.thauvin.net", true)) + .as("ensureDir(erik.thauvin.net, true)").isEqualTo("http://erik.thauvin.net/"); + } + + @Test + public void testGetIntProperty() throws Exception { + 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 { + assertThat(Utils.green("test")).isEqualTo(Colors.DARK_GREEN + "test" + Colors.NORMAL); + } + + @Test + public void testIsValidString() throws Exception { + assertThat(Utils.isValidString("test")).as("isValidString(test)").isTrue(); + assertThat(Utils.isValidString("")).as("isValidString(empty)").isFalse(); + assertThat(Utils.isValidString(" ")).as("isValidString( )").isFalse(); + assertThat(Utils.isValidString(" \t ")).as("isValidString(tab)").isFalse(); + assertThat(Utils.isValidString(null)).as("isValidString(null)").isFalse(); + } + + @Test + public void testIsoLocalDate() throws Exception { + 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 { + assertThat(Utils.plural(1, "test", "tests")).as("plural(1, test, tests)") + .isEqualTo("test"); + assertThat(Utils.plural(2, "test", "tests")).as("plural(2, test, tests)") + .isEqualTo("tests"); + } + + @Test + public void testReverseColor() throws Exception { + assertThat(Utils.reverseColor("test")).isEqualTo(Colors.REVERSE + "test" + Colors.REVERSE); + } + + @Test + public void testToday() throws Exception { + assertThat(Utils.today()).isEqualTo(Utils.isoLocalDate(LocalDateTime.now())); + } + + @Test + public void testUnescapeXml() throws Exception { + assertThat(Utils.unescapeXml("<a name="test & ''">")) + .isEqualTo(""); + } + + @Test + public void testUtcDateTime() throws Exception { + 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"); + } +} \ No newline at end of file diff --git a/src/test/java/net/thauvin/erik/mobibot/modules/LookupTest.java b/src/test/java/net/thauvin/erik/mobibot/modules/LookupTest.java new file mode 100644 index 0000000..494c0d4 --- /dev/null +++ b/src/test/java/net/thauvin/erik/mobibot/modules/LookupTest.java @@ -0,0 +1,58 @@ +/* + * LookupTest.java + * + * Copyright (c) 2004-2017, Erik C. Thauvin (erik@thauvin.net) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of this project nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.thauvin.erik.mobibot.modules; + +import org.testng.annotations.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * The Lookup Test class. + * + * @author Erik C. Thauvin + * @created 2017-05-30 + * @since 1.0 + */ +public class LookupTest { + @Test + public void testLookup() throws Exception { + final String result = Lookup.lookup("erik.thauvin.net"); + assertThat(result).as("lookup(erik.thauvin.net)").contains("104.31.77.12"); + } + + @Test + public void testWhois() throws Exception { + final String[] result = Lookup.whois("17.178.96.59", Lookup.WHOIS_HOST); + assertThat(result).as("whois(17.178.96.59)") + .contains("Apple Inc. APPLE-WWNET (NET-17-0-0-0-1) 17.0.0.0 - 17.255.255.255"); + } +} \ No newline at end of file