From e64ee044c84d04db791bc95abaf729bd922a8afb Mon Sep 17 00:00:00 2001 From: Urs Joss Date: Sat, 8 Jun 2024 15:58:29 +0200 Subject: [PATCH] refactor: Resolve some sonar and IntelliJ reported minor issues (#259) --- README.adoc | 2 +- .../src/main/kotlin/CollectSarifPlugin.kt | 2 +- subprojects/kris-core/module.md | 4 +- .../kotlin/ch/difty/kris/domain/RisTag.kt | 2 +- .../kotlin/ch/difty/kris/domain/RisType.kt | 2 +- .../kris/domain/RisRecordBuilderTest.java | 160 +++++++++--------- .../ch/difty/kris/usage/KRisUsageTest.java | 20 +-- .../ch/difty/kris/guide/GuideExamples.java | 12 +- .../ch/difty/kris/KRisIOIntegrationTest.kt | 2 +- .../difty/kris/usage/KRisIoUsageImportSpec.kt | 3 +- .../src/main/kotlin/ch/difty/kris/KRisIO.kt | 5 +- .../test/kotlin/ch/difty/kris/KRisIoTest.kt | 2 - 12 files changed, 112 insertions(+), 104 deletions(-) diff --git a/README.adoc b/README.adoc index db9d20df..c5fc99d2 100644 --- a/README.adoc +++ b/README.adoc @@ -8,7 +8,7 @@ image:https://github.com/ursjoss/KRis/actions/workflows/codeql.yml/badge.svg?bra KRis is a Kotlin implementation of RIS Format -- forked from fastlucas Java implementation https://github.com/fastluca/JRis[JRis]. KRis has the goal to give an easy way to parse RIS format stream and to build them. -From https://en.wikipedia.org/wiki/RIS_(file_format)[Wikipiedia]: +From https://en.wikipedia.org/wiki/RIS_(file_format)[Wikipedia]: ==== RIS is a standardized tag format developed by Research Information Systems, diff --git a/gradle-plugins/verification/src/main/kotlin/CollectSarifPlugin.kt b/gradle-plugins/verification/src/main/kotlin/CollectSarifPlugin.kt index cac74b1a..71e0e767 100644 --- a/gradle-plugins/verification/src/main/kotlin/CollectSarifPlugin.kt +++ b/gradle-plugins/verification/src/main/kotlin/CollectSarifPlugin.kt @@ -3,7 +3,7 @@ import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.plugins.JavaBasePlugin -@Suppress("unused", "LackOfCohesionMethods") +@Suppress("LackOfCohesionMethods") class CollectSarifPlugin : Plugin { override fun apply(target: Project) { diff --git a/subprojects/kris-core/module.md b/subprojects/kris-core/module.md index 01798121..ebc7264c 100644 --- a/subprojects/kris-core/module.md +++ b/subprojects/kris-core/module.md @@ -10,9 +10,9 @@ like writing to file or reading to file. # Package ch.difty.kris -`KRis` is the object at the core of the KRis library. If offers a number of accessor methods +`KRis` is the object at the core of the KRis library. If provides a number of accessor methods to Java Clients that offer to convert `String`s to `RisRecord`s or vice versa, in different -flavors of data input, non-blockingly using kotlin flows or RxJava observables or in a blocking +flavors of data input, non-blocking usage kotlin flows or RxJava observables or in a blocking manner accepting lists. `KrisExtensions` offers a number of handy extension functions to kotlin clients. diff --git a/subprojects/kris-core/src/main/kotlin/ch/difty/kris/domain/RisTag.kt b/subprojects/kris-core/src/main/kotlin/ch/difty/kris/domain/RisTag.kt index d7dbb512..3f10f7bf 100644 --- a/subprojects/kris-core/src/main/kotlin/ch/difty/kris/domain/RisTag.kt +++ b/subprojects/kris-core/src/main/kotlin/ch/difty/kris/domain/RisTag.kt @@ -14,7 +14,7 @@ import kotlin.reflect.KClass * share the same value, resulting in either being sorted by tag name alphabetically * or by a custom sort order specified by the caller. * - * The class was composed from information available on + * The class was composed of information available on * [Wikipedia](https://en.wikipedia.org/wiki/RIS_(file_format)). */ public enum class RisTag( diff --git a/subprojects/kris-core/src/main/kotlin/ch/difty/kris/domain/RisType.kt b/subprojects/kris-core/src/main/kotlin/ch/difty/kris/domain/RisType.kt index abf2a54a..b4412865 100644 --- a/subprojects/kris-core/src/main/kotlin/ch/difty/kris/domain/RisType.kt +++ b/subprojects/kris-core/src/main/kotlin/ch/difty/kris/domain/RisType.kt @@ -3,7 +3,7 @@ package ch.difty.kris.domain /** * Types of References - as used in [RisTag] `TY`. * - * The class was composed from information available on + * The class was composed of information available on * [Wikipedia](https://en.wikipedia.org/wiki/RIS_(file_format)). */ @Suppress("SpellCheckingInspection") diff --git a/subprojects/kris-core/src/test/java/ch/difty/kris/domain/RisRecordBuilderTest.java b/subprojects/kris-core/src/test/java/ch/difty/kris/domain/RisRecordBuilderTest.java index 012488a2..d017e179 100644 --- a/subprojects/kris-core/src/test/java/ch/difty/kris/domain/RisRecordBuilderTest.java +++ b/subprojects/kris-core/src/test/java/ch/difty/kris/domain/RisRecordBuilderTest.java @@ -175,85 +175,85 @@ void canFillAllFieldsIntoDataClassAppropriately() { @Test void emptyRecord() { - final RisRecord risRecord = new RisRecord.Builder().build(); - assertThat(risRecord.getType()).isNull(); - assertThat(risRecord.getFirstAuthors()).isEmpty(); - assertThat(risRecord.getSecondaryAuthors()).isEmpty(); - assertThat(risRecord.getTertiaryAuthors()).isEmpty(); - assertThat(risRecord.getSubsidiaryAuthors()).isEmpty(); - assertThat(risRecord.getAuthors()).isEmpty(); - assertThat(risRecord.getAbstr()).isNull(); - assertThat(risRecord.getAuthorAddress()).isNull(); - assertThat(risRecord.getAccessionNumber()).isNull(); - assertThat(risRecord.getArchivesLocation()).isNull(); - assertThat(risRecord.getBt()).isNull(); - assertThat(risRecord.getCustom1()).isNull(); - assertThat(risRecord.getCustom2()).isNull(); - assertThat(risRecord.getCustom3()).isNull(); - assertThat(risRecord.getCustom4()).isNull(); - assertThat(risRecord.getCustom5()).isNull(); - assertThat(risRecord.getCustom6()).isNull(); - assertThat(risRecord.getCustom7()).isNull(); - assertThat(risRecord.getCustom8()).isNull(); - assertThat(risRecord.getCaption()).isNull(); - assertThat(risRecord.getCallNumber()).isNull(); - assertThat(risRecord.getCp()).isNull(); - assertThat(risRecord.getUnpublishedReferenceTitle()).isNull(); - assertThat(risRecord.getPlacePublished()).isNull(); - assertThat(risRecord.getDate()).isNull(); - assertThat(risRecord.getDatabaseName()).isNull(); - assertThat(risRecord.getDoi()).isNull(); - assertThat(risRecord.getDatabaseProvider()).isNull(); - assertThat(risRecord.getEditor()).isNull(); - assertThat(risRecord.getEndPage()).isNull(); - assertThat(risRecord.getEdition()).isNull(); - assertThat(risRecord.getReferenceId()).isNull(); - assertThat(risRecord.getIssue()).isNull(); - assertThat(risRecord.getPeriodicalNameUserAbbrevation()).isNull(); - assertThat(risRecord.getAlternativeTitle()).isNull(); - assertThat(risRecord.getPeriodicalNameStandardAbbrevation()).isNull(); - assertThat(risRecord.getPeriodicalNameFullFormatJF()).isNull(); - assertThat(risRecord.getPeriodicalNameFullFormatJO()).isNull(); - assertThat(risRecord.getKeywords()).isEmpty(); - assertThat(risRecord.getPdfLinks()).isEmpty(); - assertThat(risRecord.getFullTextLinks()).isEmpty(); - assertThat(risRecord.getRelatedRecords()).isEmpty(); - assertThat(risRecord.getImages()).isEmpty(); - assertThat(risRecord.getLanguage()).isNull(); - assertThat(risRecord.getLabel()).isNull(); - assertThat(risRecord.getWebsiteLink()).isNull(); - assertThat(risRecord.getMiscellaneous1()).isNull(); - assertThat(risRecord.getMiscellaneous2()).isNull(); - assertThat(risRecord.getMiscellaneous3()).isNull(); - assertThat(risRecord.getNotes()).isNull(); - assertThat(risRecord.getAbstr2()).isNull(); - assertThat(risRecord.getNumberOfVolumes()).isNull(); - assertThat(risRecord.getOriginalPublication()).isNull(); - assertThat(risRecord.getPublisher()).isNull(); - assertThat(risRecord.getPublishingPlace()).isNull(); - assertThat(risRecord.getPublicationYear()).isNull(); - assertThat(risRecord.getReviewedItem()).isNull(); - assertThat(risRecord.getResearchNotes()).isNull(); - assertThat(risRecord.getReprintEdition()).isNull(); - assertThat(risRecord.getSection()).isNull(); - assertThat(risRecord.getIsbnIssn()).isNull(); - assertThat(risRecord.getStartPage()).isNull(); - assertThat(risRecord.getShortTitle()).isNull(); - assertThat(risRecord.getPrimaryTitle()).isNull(); - assertThat(risRecord.getSecondaryTitle()).isNull(); - assertThat(risRecord.getTertiaryTitle()).isNull(); - assertThat(risRecord.getTranslatedAuthor()).isNull(); - assertThat(risRecord.getTitle()).isNull(); - assertThat(risRecord.getTranslatedTitle()).isNull(); - assertThat(risRecord.getUserDefinable1()).isNull(); - assertThat(risRecord.getUserDefinable2()).isNull(); - assertThat(risRecord.getUserDefinable3()).isNull(); - assertThat(risRecord.getUserDefinable4()).isNull(); - assertThat(risRecord.getUserDefinable5()).isNull(); - assertThat(risRecord.getUrl()).isNull(); - assertThat(risRecord.getVolumeNumber()).isNull(); - assertThat(risRecord.getPublisherStandardNumber()).isNull(); - assertThat(risRecord.getPrimaryDate()).isNull(); - assertThat(risRecord.getAccessDate()).isNull(); + final RisRecord emptyRecord = new RisRecord.Builder().build(); + assertThat(emptyRecord.getType()).isNull(); + assertThat(emptyRecord.getFirstAuthors()).isEmpty(); + assertThat(emptyRecord.getSecondaryAuthors()).isEmpty(); + assertThat(emptyRecord.getTertiaryAuthors()).isEmpty(); + assertThat(emptyRecord.getSubsidiaryAuthors()).isEmpty(); + assertThat(emptyRecord.getAuthors()).isEmpty(); + assertThat(emptyRecord.getAbstr()).isNull(); + assertThat(emptyRecord.getAuthorAddress()).isNull(); + assertThat(emptyRecord.getAccessionNumber()).isNull(); + assertThat(emptyRecord.getArchivesLocation()).isNull(); + assertThat(emptyRecord.getBt()).isNull(); + assertThat(emptyRecord.getCustom1()).isNull(); + assertThat(emptyRecord.getCustom2()).isNull(); + assertThat(emptyRecord.getCustom3()).isNull(); + assertThat(emptyRecord.getCustom4()).isNull(); + assertThat(emptyRecord.getCustom5()).isNull(); + assertThat(emptyRecord.getCustom6()).isNull(); + assertThat(emptyRecord.getCustom7()).isNull(); + assertThat(emptyRecord.getCustom8()).isNull(); + assertThat(emptyRecord.getCaption()).isNull(); + assertThat(emptyRecord.getCallNumber()).isNull(); + assertThat(emptyRecord.getCp()).isNull(); + assertThat(emptyRecord.getUnpublishedReferenceTitle()).isNull(); + assertThat(emptyRecord.getPlacePublished()).isNull(); + assertThat(emptyRecord.getDate()).isNull(); + assertThat(emptyRecord.getDatabaseName()).isNull(); + assertThat(emptyRecord.getDoi()).isNull(); + assertThat(emptyRecord.getDatabaseProvider()).isNull(); + assertThat(emptyRecord.getEditor()).isNull(); + assertThat(emptyRecord.getEndPage()).isNull(); + assertThat(emptyRecord.getEdition()).isNull(); + assertThat(emptyRecord.getReferenceId()).isNull(); + assertThat(emptyRecord.getIssue()).isNull(); + assertThat(emptyRecord.getPeriodicalNameUserAbbrevation()).isNull(); + assertThat(emptyRecord.getAlternativeTitle()).isNull(); + assertThat(emptyRecord.getPeriodicalNameStandardAbbrevation()).isNull(); + assertThat(emptyRecord.getPeriodicalNameFullFormatJF()).isNull(); + assertThat(emptyRecord.getPeriodicalNameFullFormatJO()).isNull(); + assertThat(emptyRecord.getKeywords()).isEmpty(); + assertThat(emptyRecord.getPdfLinks()).isEmpty(); + assertThat(emptyRecord.getFullTextLinks()).isEmpty(); + assertThat(emptyRecord.getRelatedRecords()).isEmpty(); + assertThat(emptyRecord.getImages()).isEmpty(); + assertThat(emptyRecord.getLanguage()).isNull(); + assertThat(emptyRecord.getLabel()).isNull(); + assertThat(emptyRecord.getWebsiteLink()).isNull(); + assertThat(emptyRecord.getMiscellaneous1()).isNull(); + assertThat(emptyRecord.getMiscellaneous2()).isNull(); + assertThat(emptyRecord.getMiscellaneous3()).isNull(); + assertThat(emptyRecord.getNotes()).isNull(); + assertThat(emptyRecord.getAbstr2()).isNull(); + assertThat(emptyRecord.getNumberOfVolumes()).isNull(); + assertThat(emptyRecord.getOriginalPublication()).isNull(); + assertThat(emptyRecord.getPublisher()).isNull(); + assertThat(emptyRecord.getPublishingPlace()).isNull(); + assertThat(emptyRecord.getPublicationYear()).isNull(); + assertThat(emptyRecord.getReviewedItem()).isNull(); + assertThat(emptyRecord.getResearchNotes()).isNull(); + assertThat(emptyRecord.getReprintEdition()).isNull(); + assertThat(emptyRecord.getSection()).isNull(); + assertThat(emptyRecord.getIsbnIssn()).isNull(); + assertThat(emptyRecord.getStartPage()).isNull(); + assertThat(emptyRecord.getShortTitle()).isNull(); + assertThat(emptyRecord.getPrimaryTitle()).isNull(); + assertThat(emptyRecord.getSecondaryTitle()).isNull(); + assertThat(emptyRecord.getTertiaryTitle()).isNull(); + assertThat(emptyRecord.getTranslatedAuthor()).isNull(); + assertThat(emptyRecord.getTitle()).isNull(); + assertThat(emptyRecord.getTranslatedTitle()).isNull(); + assertThat(emptyRecord.getUserDefinable1()).isNull(); + assertThat(emptyRecord.getUserDefinable2()).isNull(); + assertThat(emptyRecord.getUserDefinable3()).isNull(); + assertThat(emptyRecord.getUserDefinable4()).isNull(); + assertThat(emptyRecord.getUserDefinable5()).isNull(); + assertThat(emptyRecord.getUrl()).isNull(); + assertThat(emptyRecord.getVolumeNumber()).isNull(); + assertThat(emptyRecord.getPublisherStandardNumber()).isNull(); + assertThat(emptyRecord.getPrimaryDate()).isNull(); + assertThat(emptyRecord.getAccessDate()).isNull(); } } diff --git a/subprojects/kris-core/src/test/java/ch/difty/kris/usage/KRisUsageTest.java b/subprojects/kris-core/src/test/java/ch/difty/kris/usage/KRisUsageTest.java index 743a8df4..f76addc9 100644 --- a/subprojects/kris-core/src/test/java/ch/difty/kris/usage/KRisUsageTest.java +++ b/subprojects/kris-core/src/test/java/ch/difty/kris/usage/KRisUsageTest.java @@ -42,21 +42,21 @@ class KRisUsageTest { @Test void whenProcessingRisLinesAsList_willReturnListOfRisRecords() { - final List risRecords = KRis.processList(risLines); - assertThat(risRecords).hasSize(2); + final List records = KRis.processList(risLines); + assertThat(records).hasSize(2); } @Test void whenProcessingRisLinesAsObservable_willReturnObservableOfRisRecords() { - final List risRecords = new ArrayList<>(); + final List records = new ArrayList<>(); final Observable observable = Observable.fromIterable(risLines); KRis .processObservables(observable) - .blockingSubscribe(risRecords::add); + .blockingSubscribe(records::add); - assertThat(risRecords).hasSize(2); + assertThat(records).hasSize(2); } private final RisRecord risRecord = new RisRecord.Builder() @@ -76,20 +76,20 @@ void whenProcessingRisLinesAsObservable_willReturnObservableOfRisRecords() { @Test void whenProcessingRisRecordsAsList_willReturnListOfRisLines() { - final List risLines = KRis.buildFromList(risRecords); - assertThat(risLines).hasSize(expectedLineCount); + final List lines = KRis.buildFromList(risRecords); + assertThat(lines).hasSize(expectedLineCount); } @Test void whenProcessingRisRecordsAsObservable_willReturnObservableOfString() { - final List risLines = new ArrayList<>(); + final List lines = new ArrayList<>(); final Observable observable = Observable.fromIterable(risRecords); KRis .exportObservable(observable) - .blockingSubscribe(risLines::add); + .blockingSubscribe(lines::add); - assertThat(risLines).hasSize(expectedLineCount); + assertThat(lines).hasSize(expectedLineCount); } } diff --git a/subprojects/kris-guide/src/test/java/ch/difty/kris/guide/GuideExamples.java b/subprojects/kris-guide/src/test/java/ch/difty/kris/guide/GuideExamples.java index c0ed2937..65e64354 100644 --- a/subprojects/kris-guide/src/test/java/ch/difty/kris/guide/GuideExamples.java +++ b/subprojects/kris-guide/src/test/java/ch/difty/kris/guide/GuideExamples.java @@ -49,6 +49,7 @@ void fromList() { List lines = KRis.buildFromList(records); // end::fromList[] + System.out.println(lines); } void fromListCustomSort() { @@ -58,6 +59,7 @@ void fromListCustomSort() { List lines = KRis.buildFromList(records, sort); // end::fromListCustomSort[] + System.out.println(lines); } // tag::fromObservable[] @@ -91,7 +93,7 @@ void writerExport() throws IOException { // end::writerExport[] } - void fileExport() throws IOException { + void fileExport() { // tag::fileExport[] final List records = List.of(record1, record2); @@ -111,7 +113,7 @@ void streamExport() throws IOException { // end::streamExport[] } - void pathExport() throws IOException { + void pathExport() { // tag::pathExport[] final List records = List.of(record1, record2); @@ -135,6 +137,7 @@ void processFile() throws IOException { final File file = new File("import.ris"); final List records = KRisIO.process(file); // end::processFile[] + System.out.println(records); } void processInputStream() throws IOException { @@ -149,6 +152,7 @@ void processPath() throws IOException { // tag::processPath[] final List records = KRisIO.process("import.ris"); // end::processPath[] + System.out.println(records); } void processReaderAsStream() throws IOException { @@ -164,6 +168,7 @@ void processFileAsStream() throws IOException { final File file = new File("import.ris"); final Stream records = KRisIO.processToStream(file); // end::processFileAsStream[] + System.out.println(records); } void processInputStreamAsStream() throws IOException { @@ -178,6 +183,7 @@ void processPathAsStream() throws IOException { // tag::processPathAsStream[] final Stream records = KRisIO.processToStream("import.ris"); // end::processPathAsStream[] + System.out.println(records); } void passRisLinesAsList() throws IOException { @@ -185,6 +191,7 @@ void passRisLinesAsList() throws IOException { final List lines = List.of(); // TODO List records = KRis.processList(lines); // end::passRisLinesAsList[] + System.out.println(records); } void processObservables() throws IOException { @@ -205,5 +212,6 @@ void getListOfRisTagsAsString() { // tag::getListOfRisTagsAsString[] List namesOfAllRisTags = KRis.risTagNames(); // end::getListOfRisTagsAsString[] + System.out.println(namesOfAllRisTags); } } diff --git a/subprojects/kris-io/src/integrationTest/java/ch/difty/kris/KRisIOIntegrationTest.kt b/subprojects/kris-io/src/integrationTest/java/ch/difty/kris/KRisIOIntegrationTest.kt index 0f95cb1e..196a36a0 100644 --- a/subprojects/kris-io/src/integrationTest/java/ch/difty/kris/KRisIOIntegrationTest.kt +++ b/subprojects/kris-io/src/integrationTest/java/ch/difty/kris/KRisIOIntegrationTest.kt @@ -11,7 +11,7 @@ import java.util.stream.Collectors.toList private const val FILE_PATH = "src/integrationTest/resources/sample.ris" private const val PAPER_COUNT = 4 -@Suppress("FunctionName", "S100", "SpellCheckingInspection", "MaxLineLength", "TooManyFunctions") +@Suppress("FunctionName", "S100", "SpellCheckingInspection", "MaxLineLength", "TooManyFunctions", "RedundantSuppression") internal class KRisIOIntegrationTest { private val expectedLines = setOf( diff --git a/subprojects/kris-io/src/integrationTest/kotlin/ch/difty/kris/usage/KRisIoUsageImportSpec.kt b/subprojects/kris-io/src/integrationTest/kotlin/ch/difty/kris/usage/KRisIoUsageImportSpec.kt index d5349e5a..a44a0a7b 100644 --- a/subprojects/kris-io/src/integrationTest/kotlin/ch/difty/kris/usage/KRisIoUsageImportSpec.kt +++ b/subprojects/kris-io/src/integrationTest/kotlin/ch/difty/kris/usage/KRisIoUsageImportSpec.kt @@ -10,7 +10,6 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.amshove.kluent.shouldHaveSize import java.io.File -import kotlin.streams.toList object KRisIoUsageImportSpec : DescribeSpec({ @@ -22,7 +21,7 @@ object KRisIoUsageImportSpec : DescribeSpec({ it.deleteOnExit() } - describe("list mehtods") { + describe("list methods") { it("can read from File") { file.process() shouldHaveSize 1 } diff --git a/subprojects/kris-io/src/main/kotlin/ch/difty/kris/KRisIO.kt b/subprojects/kris-io/src/main/kotlin/ch/difty/kris/KRisIO.kt index b4cdd7e4..7596a0e0 100644 --- a/subprojects/kris-io/src/main/kotlin/ch/difty/kris/KRisIO.kt +++ b/subprojects/kris-io/src/main/kotlin/ch/difty/kris/KRisIO.kt @@ -10,6 +10,7 @@ import kotlinx.coroutines.flow.lastOrNull import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.toList import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withContext import java.io.BufferedReader import java.io.File import java.io.FileOutputStream @@ -147,7 +148,9 @@ public object KRisIO { KRis.build(records.asFlow(), sort) .buffer(onBufferOverflow = BufferOverflow.SUSPEND) .collect { line -> - w.write(line) + withContext(dispatchers.io) { + w.write(line) + } } } } diff --git a/subprojects/kris-io/src/test/kotlin/ch/difty/kris/KRisIoTest.kt b/subprojects/kris-io/src/test/kotlin/ch/difty/kris/KRisIoTest.kt index 6e36fc49..cbd5d1c5 100644 --- a/subprojects/kris-io/src/test/kotlin/ch/difty/kris/KRisIoTest.kt +++ b/subprojects/kris-io/src/test/kotlin/ch/difty/kris/KRisIoTest.kt @@ -5,7 +5,6 @@ import ch.difty.kris.domain.RisType import io.mockk.every import io.mockk.mockk import io.mockk.verify -import kotlinx.coroutines.ExperimentalCoroutinesApi import org.junit.jupiter.api.Test import java.io.Writer @@ -22,7 +21,6 @@ internal class KRisIoTest { every { close() } returns Unit } - @OptIn(ExperimentalCoroutinesApi::class) KRisIO.export(records, writer = writer, dispatchers = TestDispatcherProvider()) verify { writer.write("TY - BOOK\n") }