Converter Entry Links manipulation classes to Kotlin.
This commit is contained in:
parent
310ffb91da
commit
7759e278e8
22 changed files with 818 additions and 1213 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* LocalProperties.java
|
||||
* LocalProperties.kt
|
||||
*
|
||||
* Copyright (c) 2004-2020, Erik C. Thauvin (erik@thauvin.net)
|
||||
* All rights reserved.
|
||||
|
@ -29,54 +29,47 @@
|
|||
* 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
|
||||
|
||||
package net.thauvin.erik.mobibot;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.testng.annotations.BeforeSuite;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Properties;
|
||||
import org.testng.annotations.BeforeSuite
|
||||
import java.io.IOException
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Paths
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* 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
|
||||
* Access to `local.properties`.
|
||||
*/
|
||||
public class LocalProperties {
|
||||
private static final Properties localProps = new Properties();
|
||||
|
||||
public static String getProperty(final String key) {
|
||||
if (localProps.containsKey(key)) {
|
||||
return localProps.getProperty(key);
|
||||
} else {
|
||||
final String env = System.getenv(keyToEnv(key));
|
||||
if (env != null) {
|
||||
localProps.setProperty(key, env);
|
||||
}
|
||||
return env;
|
||||
}
|
||||
}
|
||||
|
||||
private static String keyToEnv(final String key) {
|
||||
return StringUtils.upperCase(key.replace('-', '_'));
|
||||
}
|
||||
|
||||
open class LocalProperties {
|
||||
@BeforeSuite(alwaysRun = true)
|
||||
public void loadProperties() {
|
||||
final Path localPath = Paths.get("local.properties");
|
||||
fun loadProperties() {
|
||||
val localPath = Paths.get("local.properties")
|
||||
if (Files.exists(localPath)) {
|
||||
try (final InputStream stream = Files.newInputStream(localPath)) {
|
||||
localProps.load(stream);
|
||||
} catch (IOException ignore) {
|
||||
try {
|
||||
Files.newInputStream(localPath).use { stream -> localProps.load(stream) }
|
||||
} catch (ignore: IOException) {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val localProps = Properties()
|
||||
|
||||
fun getProperty(key: String): String {
|
||||
return if (localProps.containsKey(key)) {
|
||||
localProps.getProperty(key)
|
||||
} else {
|
||||
val env = System.getenv(keyToEnv(key))
|
||||
if (env != null) {
|
||||
localProps.setProperty(key, env)
|
||||
}
|
||||
env
|
||||
}
|
||||
}
|
||||
|
||||
private fun keyToEnv(key: String): String {
|
||||
return key.replace('-', '_').toUpperCase()
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,100 +0,0 @@
|
|||
/*
|
||||
* EntryLinkTest.java
|
||||
*
|
||||
* Copyright (c) 2004-2020, 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.entries;
|
||||
|
||||
import com.rometools.rome.feed.synd.SyndCategory;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* The <code>EntryUtilsTest</code> class.
|
||||
*
|
||||
* @author <a href="https://erik.thauvin.net/" target="_blank">Erik C. Thauvin</a>
|
||||
* @created 2019-04-19
|
||||
* @since 1.0
|
||||
*/
|
||||
public class EntryLinkTest {
|
||||
private final EntryLink entryLink = new EntryLink("https://www.mobitopia.org/", "Mobitopia", "Skynx",
|
||||
"JimH", "#mobitopia", List.of("tag1", "tag2", "tag3", "TAG4", "Tag5"));
|
||||
|
||||
|
||||
@Test
|
||||
public void testAddDeleteComment() {
|
||||
int i = 0;
|
||||
for (; i < 5; i++) {
|
||||
entryLink.addComment("c" + i, "u" + i);
|
||||
}
|
||||
|
||||
i = 0;
|
||||
for (final EntryComment comment : entryLink.getComments()) {
|
||||
assertThat(comment.getComment()).as("getComment(" + i + ')').isEqualTo("c" + i);
|
||||
assertThat(comment.getNick()).as("getNick(" + i + ')').isEqualTo("u" + i);
|
||||
i++;
|
||||
}
|
||||
|
||||
final SecureRandom r = new SecureRandom();
|
||||
while (entryLink.getCommentsCount() > 0) {
|
||||
entryLink.deleteComment(r.nextInt(entryLink.getCommentsCount()));
|
||||
}
|
||||
assertThat(entryLink.hasComments()).as("hasComments()").isFalse();
|
||||
|
||||
entryLink.addComment("nothing", "nobody");
|
||||
entryLink.setComment(0, "something", "somebody");
|
||||
assertThat(entryLink.getComment(0).getNick()).as("getNick(somebody)").isEqualTo("somebody");
|
||||
assertThat(entryLink.getComment(0).getComment()).as("getComment(something)").isEqualTo("something");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTags() {
|
||||
final List<SyndCategory> tags = entryLink.getTags();
|
||||
|
||||
int i = 0;
|
||||
for (final SyndCategory tag : tags) {
|
||||
assertThat(tag.getName()).as("tag.getName(" + i + ')').isEqualTo("tag" + (i + 1));
|
||||
i++;
|
||||
}
|
||||
|
||||
entryLink.setTags("-tag5");
|
||||
entryLink.setTags("+mobitopia");
|
||||
entryLink.setTags("tag4");
|
||||
entryLink.setTags("-mobitopia");
|
||||
|
||||
assertThat(entryLink.getPinboardTags()).as("getPinboardTags()")
|
||||
.isEqualTo(entryLink.getNick() + ",tag1,tag2,tag3,tag4,mobitopia");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
/*
|
||||
* EntryLinkTest.kt
|
||||
*
|
||||
* Copyright (c) 2004-2020, 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.entries
|
||||
|
||||
import com.rometools.rome.feed.synd.SyndCategory
|
||||
import org.assertj.core.api.Assertions
|
||||
import org.testng.annotations.Test
|
||||
import java.security.SecureRandom
|
||||
|
||||
/**
|
||||
* The `EntryUtilsTest` class.
|
||||
*
|
||||
* @author [Erik C. Thauvin](https://erik.thauvin.net/)
|
||||
* @created 2019-04-19
|
||||
* @since 1.0
|
||||
*/
|
||||
class EntryLinkTest {
|
||||
private val entryLink = EntryLink(
|
||||
"https://www.mobitopia.org/", "Mobitopia", "Skynx", "JimH", "#mobitopia",
|
||||
listOf("tag1", "tag2", "tag3", "TAG4", "Tag5")
|
||||
)
|
||||
|
||||
@Test
|
||||
fun testAddDeleteComment() {
|
||||
var i = 0
|
||||
while (i < 5) {
|
||||
entryLink.addComment("c$i", "u$i")
|
||||
i++
|
||||
}
|
||||
Assertions.assertThat(entryLink.comments.size).`as`("getComments().size() == 5").isEqualTo(i)
|
||||
i = 0
|
||||
for (comment in entryLink.comments) {
|
||||
Assertions.assertThat(comment.comment).`as`("getComment($i)").isEqualTo("c$i")
|
||||
Assertions.assertThat(comment.nick).`as`("getNick($i)").isEqualTo("u$i")
|
||||
i++
|
||||
}
|
||||
val r = SecureRandom()
|
||||
while (entryLink.comments.size > 0) {
|
||||
entryLink.deleteComment(r.nextInt(entryLink.comments.size))
|
||||
}
|
||||
Assertions.assertThat(entryLink.hasComments()).`as`("hasComments()").isFalse
|
||||
entryLink.addComment("nothing", "nobody")
|
||||
entryLink.setComment(0, "something", "somebody")
|
||||
Assertions.assertThat(entryLink.getComment(0).nick).`as`("getNick(somebody)").isEqualTo("somebody")
|
||||
Assertions.assertThat(entryLink.getComment(0).comment).`as`("getComment(something)").isEqualTo("something")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testTags() {
|
||||
val tags: List<SyndCategory> = entryLink.tags
|
||||
for ((i, tag) in tags.withIndex()) {
|
||||
Assertions.assertThat(tag.name).`as`("tag.getName($i)").isEqualTo("tag" + (i + 1))
|
||||
}
|
||||
Assertions.assertThat(entryLink.tags.size).`as`("getTags().size() is 5").isEqualTo(5)
|
||||
Assertions.assertThat(entryLink.hasTags()).`as`("hasTags() is true").isTrue
|
||||
entryLink.setTags("-tag5")
|
||||
entryLink.setTags("+mobitopia")
|
||||
entryLink.setTags("tag4")
|
||||
entryLink.setTags("-mobitopia")
|
||||
Assertions.assertThat(entryLink.pinboardTags).`as`("getPinboardTags()")
|
||||
.isEqualTo(entryLink.nick + ",tag1,tag2,tag3,tag4,mobitopia")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue