Moved from Gradle to bld

This commit is contained in:
Erik C. Thauvin 2023-11-12 15:12:11 -08:00
parent 7ef1c441e6
commit d5e21dd3a1
194 changed files with 2185 additions and 1983 deletions

View file

@ -0,0 +1,196 @@
/*
* AkismetBuild.java
*
* Copyright 2019-2023 Erik C. Thauvin (erik@thauvin.net)
*
* 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;
import rife.bld.BuildCommand;
import rife.bld.Project;
import rife.bld.extension.CompileKotlinOperation;
import rife.bld.extension.CompileKotlinOptions;
import rife.bld.extension.GeneratedVersionOperation;
import rife.bld.extension.JacocoReportOperation;
import rife.bld.extension.dokka.DokkaOperation;
import rife.bld.extension.dokka.LoggingLevel;
import rife.bld.extension.dokka.OutputFormat;
import rife.bld.extension.dokka.SourceSet;
import rife.bld.operations.exceptions.ExitStatusException;
import rife.bld.publish.PomBuilder;
import rife.bld.publish.PublishDeveloper;
import rife.bld.publish.PublishLicense;
import rife.bld.publish.PublishScm;
import rife.tools.exceptions.FileUtilsErrorException;
import java.io.File;
import java.io.IOException;
import java.util.List;
import static rife.bld.dependencies.Repository.*;
import static rife.bld.dependencies.Scope.*;
public class AkismetBuild extends Project {
public AkismetBuild() {
pkg = "net.thauvin.erik";
name = "akismet-kotlin";
version = version(1, 0, 1, "SNAPSHOT");
javaRelease = 11;
downloadSources = true;
autoDownloadPurge = true;
repositories = List.of(MAVEN_LOCAL, MAVEN_CENTRAL);
var okHttp = version(4, 12, 0);
scope(compile)
.include(dependency("org.jetbrains.kotlin", "kotlin-stdlib", version(1, 9, 20)))
.include(dependency("com.squareup.okhttp3", "okhttp", okHttp))
.include(dependency("com.squareup.okhttp3", "logging-interceptor", okHttp))
.include(dependency("jakarta.servlet", "jakarta.servlet-api", version(6, 0, 0)))
.include(dependency("org.jetbrains.kotlinx", "kotlinx-serialization-json-jvm", version(1, 6, 0)));
scope(provided)
.include(dependency("org.jetbrains.kotlin:kotlin-serialization-compiler-plugin:1.9.20"));
scope(test)
.include(dependency("org.mockito", "mockito-core", version(5, 6, 0)))
.include(dependency("org.jetbrains.kotlin", "kotlin-test-junit5", version(1, 9, 20)))
.include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 10, 1)))
.include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 10, 1)))
.include(dependency("com.willowtreeapps.assertk", "assertk-jvm", version(0, 27, 0)));
publishOperation()
.repository(version.isSnapshot() ? repository(SONATYPE_SNAPSHOTS_LEGACY.location())
.withCredentials(property("sonatype.user"), property("sonatype.password"))
: repository(SONATYPE_RELEASES_LEGACY.location())
.withCredentials(property("sonatype.user"), property("sonatype.password")))
.info()
.groupId(pkg)
.artifactId(name)
.description("A client library for accessing the Automattic Kismet (Akismet) spam comments filtering service.")
.url("https://github.com/ethauvin/" + name)
.developer(new PublishDeveloper()
.id("ethauvin")
.name("Erik C. Thauvin")
.email("erik@thauvin.net")
.url("https://erik.thauvin.net/"))
.license(new PublishLicense()
.name("BSD 3-Clause")
.url("https://opensource.org/licenses/BSD-3-Clause"))
.scm(new PublishScm()
.connection("scm:git:https://github.com/ethauvin/" + name)
.developerConnection("scm:git:git@github.com:ethauvin/" + name + ".git")
.url("https://github.com/ethauvin/" + name))
.signKey(property("sign.key"))
.signPassphrase(property("sign.passphrase"));
jarSourcesOperation().sourceDirectories(new File(srcMainDirectory(), "kotlin"));
}
public static void main(String[] args) {
new AkismetBuild().start(args);
}
@BuildCommand(summary = "Compiles the Kotlin project")
@Override
public void compile() throws IOException {
genver();
new CompileKotlinOperation()
.fromProject(this)
.plugins(CompileKotlinOperation.getJarList(libCompileDirectory(),
"^.*kotlin-serialization-compiler-plugin-.*$"))
.compileOptions(
new CompileKotlinOptions()
.jdkRelease(javaRelease)
.verbose(true)
)
.execute();
}
@BuildCommand(summary = "Generates documentation in HTML format")
public void docs() throws ExitStatusException, IOException, InterruptedException {
var kotlin = new File(srcMainDirectory(), "kotlin").getAbsolutePath();
new DokkaOperation()
.fromProject(this)
.loggingLevel(LoggingLevel.INFO)
.moduleName("Akismet Kotlin")
.moduleVersion(version.toString())
.outputDir("docs")
.outputFormat(OutputFormat.HTML)
.sourceSet(
new SourceSet()
.src(kotlin)
.srcLink(kotlin, "https://github.com/ethauvin/" + name +
"/tree/master/src/main/kotlin/", "#L")
.includes("config/dokka/packages.md")
.jdkVersion(javaRelease)
)
.execute();
}
@BuildCommand(summary = "Generates version class")
public void genver() {
new GeneratedVersionOperation()
.fromProject(this)
.projectName("Akismet Kotlin")
.packageName(pkg + ".akismet")
.classTemplate(new File(workDirectory(), "version.txt"))
.directory(new File(srcMainDirectory(), "kotlin"))
.extension(".kt")
.execute();
}
@BuildCommand(summary = "Generates JaCoCo Reports")
public void jacoco() throws IOException {
new JacocoReportOperation()
.fromProject(this)
.execute();
}
@Override
public void javadoc() throws ExitStatusException, IOException, InterruptedException {
new DokkaOperation()
.fromProject(this)
.loggingLevel(LoggingLevel.INFO)
.moduleName("Bitly Shorten")
.moduleVersion(version.toString())
.outputDir(new File(buildDirectory(), "javadoc"))
.outputFormat(OutputFormat.JAVADOC)
.execute();
}
@Override
public void publish() throws Exception {
super.publish();
pomRoot();
}
@BuildCommand(value = "pom-root", summary = "Generates the POM file in the root directory")
public void pomRoot() throws FileUtilsErrorException {
PomBuilder.generateInto(publishOperation().fromProject(this).info(), dependencies(),
new File(workDirectory, "pom.xml"));
}
}

View file

@ -1,8 +1,7 @@
/*
* Akismet.kt
*
* Copyright (c) 2019-2023, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
* Copyright 2019-2023 Erik C. Thauvin (erik@thauvin.net)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@ -32,7 +31,6 @@
package net.thauvin.erik.akismet
import kotlinx.serialization.json.Json
import net.thauvin.erik.semver.Version
import okhttp3.FormBody
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrl
@ -45,7 +43,7 @@ import java.time.OffsetDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
import java.util.Date
import java.util.*
import java.util.logging.Level
import java.util.logging.Logger
@ -54,7 +52,6 @@ import java.util.logging.Logger
*
* @constructor Creates a new instance using the provided [Akismet](https://www.askimet.com/) API key.
*/
@Version(properties = "version.properties", type = "kt")
open class Akismet(apiKey: String) {
companion object {
/**
@ -135,7 +132,6 @@ open class Akismet(apiKey: String) {
/**
* The [HTTP status code](https://www.restapitutorial.com/httpstatuscodes.html) of the last operation.
*/
@Suppress("MemberVisibilityCanBePrivate")
var httpStatusCode: Int = 0
private set
@ -144,7 +140,6 @@ open class Akismet(apiKey: String) {
*
* For example: `true`, `false`, `valid`, `invalid`, etc.
*/
@Suppress("MemberVisibilityCanBePrivate")
var response: String = ""
private set
@ -168,7 +163,6 @@ open class Akismet(apiKey: String) {
*
* @see [Akismet.isDiscard]
*/
@Suppress("MemberVisibilityCanBePrivate")
var proTip: String = ""
private set
@ -180,7 +174,6 @@ open class Akismet(apiKey: String) {
*
* @see [Akismet.proTip]
*/
@Suppress("MemberVisibilityCanBePrivate")
var isDiscard: Boolean = false
private set

View file

@ -1,8 +1,7 @@
/*
* AkismetComment.kt
*
* Copyright (c) 2019-2023, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
* Copyright 2019-2023 Erik C. Thauvin (erik@thauvin.net)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@ -58,7 +57,6 @@ private fun String?.ifNull() = this ?: ""
*/
@Serializable
open class AkismetComment(val userIp: String, val userAgent: String) {
@Suppress("unused")
companion object {
/** A blog comment. */
const val TYPE_COMMENT = "comment"

View file

@ -0,0 +1,14 @@
/*
* This file is automatically generated.
* Do not modify! -- ALL CHANGES WILL BE ERASED!
*/
package net.thauvin.erik.akismet
/**
* Provides semantic version information.
*/
internal object GeneratedVersion {
const val PROJECT = "Akismet Kotlin"
const val VERSION = "1.0.1-SNAPSHOT"
}

View file

@ -0,0 +1,46 @@
/*
* AkismetTest.java
*
* Copyright 2019-2023 Erik C. Thauvin (erik@thauvin.net)
*
* 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;
public class AkismetTest {
public static void main(String[] args) {
new AkismetTest().verifyHello();
}
void verifyHello() {
if (!"Hello World!".equals(new AkismetLib().getMessage())) {
throw new AssertionError();
} else {
System.out.println("Succeeded");
}
}
}

View file

@ -1,8 +1,7 @@
/*
* AkismetTest.kt
*
* Copyright (c) 2019-2023, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
* Copyright 2019-2023 Erik C. Thauvin (erik@thauvin.net)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@ -34,36 +33,27 @@ package net.thauvin.erik.akismet
import assertk.all
import assertk.assertThat
import assertk.assertions.contains
import assertk.assertions.isEmpty
import assertk.assertions.isEqualTo
import assertk.assertions.isFalse
import assertk.assertions.isNotEmpty
import assertk.assertions.isTrue
import assertk.assertions.key
import assertk.assertions.prop
import assertk.assertions.size
import assertk.assertions.*
import jakarta.servlet.http.HttpServletRequest
import okhttp3.FormBody
import okhttp3.HttpUrl.Companion.toHttpUrl
import org.junit.Assert.assertThrows
import org.junit.BeforeClass
import org.junit.jupiter.api.Test
import org.mockito.Mockito
import org.mockito.Mockito.`when`
import org.testng.Assert.assertEquals
import org.testng.Assert.assertFalse
import org.testng.Assert.assertNotEquals
import org.testng.Assert.assertTrue
import org.testng.Assert.expectThrows
import org.testng.annotations.BeforeClass
import org.testng.annotations.Test
import java.io.File
import java.io.FileInputStream
import java.time.LocalDateTime
import java.time.ZoneId
import java.util.Collections
import java.util.Date
import java.util.Properties
import java.util.*
import java.util.logging.ConsoleHandler
import java.util.logging.Level
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotEquals
import kotlin.test.assertTrue
fun getKey(key: String): String {
var value = System.getenv(key) ?: ""
@ -88,84 +78,40 @@ fun getKey(key: String): String {
* AKISMET_API_KEY and AKISMET_BLOG should be in env vars or local.properties
*/
class AkismetTest {
private val apiKey = getKey("AKISMET_API_KEY")
private val blog = getKey("AKISMET_BLOG")
private val referer = "http://www.google.com"
private val date = Date()
private val comment = AkismetComment(
userIp = "127.0.0.1",
userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6"
)
private val akismet = Akismet(apiKey, blog)
private val mockComment: AkismetComment = AkismetComment(request = getMockRequest())
private val emptyFormBody = FormBody.Builder().build()
@BeforeClass
fun beforeClass() {
with(akismet.logger) {
addHandler(ConsoleHandler().apply { level = Level.FINE })
level = Level.FINE
}
with(comment) {
referrer = referer
permalink = "http://yourblogdomainname.com/blog/post=1"
type = AkismetComment.TYPE_COMMENT
author = "admin"
authorEmail = "test@test.com"
authorUrl = "http://www.CheckOutMyCoolSite.com"
content = "It means a lot that you would take the time to review our software. Thanks again."
dateGmt = Akismet.dateToGmt(date)
postModifiedGmt = dateGmt
blogLang = "en"
blogCharset = "UTF-8"
userRole = AkismetComment.ADMIN_ROLE
isTest = true
}
akismet.logger.info(comment.toString())
with(mockComment) {
permalink = comment.permalink
type = comment.type
authorEmail = comment.authorEmail
author = comment.author
authorUrl = comment.authorUrl
content = comment.content
dateGmt = comment.dateGmt
postModifiedGmt = comment.dateGmt
blogLang = comment.blogLang
blogCharset = comment.blogCharset
userRole = comment.userRole
recheckReason = "edit"
isTest = true
}
akismet.logger.info(mockComment.toJson())
}
@Test
fun constructorsTest() {
expectThrows(IllegalArgumentException::class.java) {
assertThrows(
IllegalArgumentException::class.java
) {
Akismet("")
}
expectThrows(IllegalArgumentException::class.java) {
assertThrows(
IllegalArgumentException::class.java
) {
Akismet("1234")
}
expectThrows(IllegalArgumentException::class.java) {
assertThrows(
IllegalArgumentException::class.java
) {
Akismet("123456789 12")
}
expectThrows(IllegalArgumentException::class.java) {
assertThrows(
IllegalArgumentException::class.java
) {
Akismet("123456789012", "")
}
expectThrows(IllegalArgumentException::class.java) {
assertThrows(
IllegalArgumentException::class.java
) {
Akismet("1234", "foo")
}
}
@Test
fun blogPropertyTest() {
expectThrows(IllegalArgumentException::class.java) {
assertThrows(IllegalArgumentException::class.java) {
akismet.blog = ""
}
@ -208,9 +154,10 @@ class AkismetTest {
@Test
fun emptyCommentTest() {
expectThrows(IllegalArgumentException::class.java) {
akismet.checkComment(AkismetComment("", ""))
}
assertThrows(
java.lang.IllegalArgumentException::class.java
) { akismet.checkComment(AkismetComment("", "")) }
val empty = AkismetComment("", "")
assertThat(empty, "AkismetComment(empty)").all {
@ -308,6 +255,7 @@ class AkismetTest {
emptyFormBody
)
)
assertThat(akismet, "executeMethod(pro-tip)").all {
prop(Akismet::proTip).isEqualTo("discard")
prop(Akismet::isDiscard).isTrue()
@ -358,9 +306,12 @@ class AkismetTest {
}
}
@Test(expectedExceptions = [IllegalArgumentException::class])
@Test
fun invalidApiTest() {
akismet.executeMethod("https://.com".toHttpUrl(), emptyFormBody)
assertThrows(
java.lang.IllegalArgumentException::class.java
) { akismet.executeMethod("https://.com".toHttpUrl(), emptyFormBody) }
}
@Test
@ -395,7 +346,7 @@ class AkismetTest {
jsonComment.recheckReason = ""
assertNotEquals(jsonComment, mockComment, "jsonComment != jsonComment")
assertNotEquals(this, comment, "this != comment")
assertThat(this, "this != comment").isNotEqualTo(comment)
}
@Test
@ -415,19 +366,78 @@ class AkismetTest {
assertThat(comment::dateGmt).isEqualTo(utcDate)
}
private fun getMockRequest(): HttpServletRequest {
val request = Mockito.mock(HttpServletRequest::class.java)
with(request) {
`when`(remoteAddr).thenReturn(comment.userIp)
`when`(requestURI).thenReturn("/blog/post=1")
`when`(getHeader("referer")).thenReturn(referer)
`when`(getHeader("Cookie")).thenReturn("name=value; name2=value2; name3=value3")
`when`(getHeader("User-Agent")).thenReturn(comment.userAgent)
`when`(getHeader("Accept-Encoding")).thenReturn("gzip")
`when`(headerNames).thenReturn(
Collections.enumeration(listOf("User-Agent", "referer", "Cookie", "Accept-Encoding", "Null"))
)
companion object {
private val apiKey = getKey("AKISMET_API_KEY")
private val blog = getKey("AKISMET_BLOG")
private val akismet = Akismet(apiKey, blog)
private val comment = AkismetComment(
userIp = "127.0.0.1",
userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6"
)
private val date = Date()
private val mockComment: AkismetComment = AkismetComment(request = getMockRequest())
private const val REFERER = "http://www.google.com"
init {
with(comment) {
referrer = REFERER
permalink = "http://yourblogdomainname.com/blog/post=1"
type = AkismetComment.TYPE_COMMENT
author = "admin"
authorEmail = "test@test.com"
authorUrl = "http://www.CheckOutMyCoolSite.com"
content = "It means a lot that you would take the time to review our software. Thanks again."
dateGmt = Akismet.dateToGmt(date)
postModifiedGmt = dateGmt
blogLang = "en"
blogCharset = "UTF-8"
userRole = AkismetComment.ADMIN_ROLE
isTest = true
}
with(mockComment) {
permalink = comment.permalink
type = comment.type
authorEmail = comment.authorEmail
author = comment.author
authorUrl = comment.authorUrl
content = comment.content
dateGmt = comment.dateGmt
postModifiedGmt = comment.dateGmt
blogLang = comment.blogLang
blogCharset = comment.blogCharset
userRole = comment.userRole
recheckReason = "edit"
isTest = true
}
}
@JvmStatic
@BeforeClass
fun beforeClass() {
with(akismet.logger) {
addHandler(ConsoleHandler().apply { level = Level.FINE })
level = Level.FINE
}
akismet.logger.info(comment.toString())
akismet.logger.info(mockComment.toJson())
}
private fun getMockRequest(): HttpServletRequest {
val request = Mockito.mock(HttpServletRequest::class.java)
with(request) {
`when`(remoteAddr).thenReturn(comment.userIp)
`when`(requestURI).thenReturn("/blog/post=1")
`when`(getHeader("referer")).thenReturn(REFERER)
`when`(getHeader("Cookie")).thenReturn("name=value; name2=value2; name3=value3")
`when`(getHeader("User-Agent")).thenReturn(comment.userAgent)
`when`(getHeader("Accept-Encoding")).thenReturn("gzip")
`when`(headerNames).thenReturn(
Collections.enumeration(listOf("User-Agent", "referer", "Cookie", "Accept-Encoding", "Null"))
)
}
return request
}
return request
}
}