Skip to content

Commit

Permalink
BAEL-102141 Review warnings in kotlin-build job
Browse files Browse the repository at this point in the history
  • Loading branch information
timis1 authored and n committed Feb 20, 2024
1 parent b5bafd1 commit 33da56a
Show file tree
Hide file tree
Showing 80 changed files with 209 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class KClassUnitTest {
val listClass = ArrayList::class

val list = listClass.createInstance()
@Suppress("USELESS_IS_CHECK")
assertTrue(list is ArrayList)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class KMethodUnitTest {
assertEquals("mutableProperty", mProperty.name)
assertTrue(mProperty.isLateinit)
assertFalse(mProperty.isConst)
@Suppress("USELESS_IS_CHECK")
assertTrue(mProperty is KMutableProperty<*>)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.baeldung.array

fun main(args: Array<String>) {
fun main() {
val fruits = arrayOf("Pear", "Apple", "Papaya", "Banana")
println(fruits.associate { Pair(it, it.length) })
println(fruits.associateBy { it.length })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fun printArray(array: Array<Int>) {
println()
}

fun main(args: Array<String>) {
fun main() {
var numbers = initArray()
printArray(numbers)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.baeldung.array

val dice = arrayOf(1, 2, 3, 4, 5, 6)

fun main(args: Array<String>) {
fun main() {
// Traversal using for loop
for (faceValue in dice) {
println(faceValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class RemoveNullAndEmptyValuesUnitTest {
iterator.remove()
}
}
@Suppress("UNCHECKED_CAST")
return listWithNullsAndEmpty as List<String>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fun <T : kotlin.Comparable<T>> getSimpleComparator() : Comparator<T> {

fun getComplexComparator() {
val complexComparator = compareBy<Pair<Int, String>>({it.first}, {it.second})
print("Complex comparator result: $complexComparator" )
}

fun nullHandlingUsage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,31 @@ package com.baeldung.deletefile
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import java.io.File
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.deleteRecursively
import kotlin.io.path.exists
import kotlin.io.path.pathString

class DeleteFileUnitTest {

@Test
fun `given file path when deleteFile called then file is deleted`() {
val tempFile = createTempFile()
val tempFile = kotlin.io.path.createTempFile()
assertTrue(tempFile.exists())

deleteFile(tempFile.absolutePath)
deleteFile(tempFile.pathString)

assertFalse(tempFile.exists())
}

@Test
fun `given directory when deleteDirectory called then directory and its contents are deleted`() {
val tempDir = createTempDir()
val tempFileInDir = File(tempDir, "tempFile.txt").apply { createNewFile() }
val tempDir = kotlin.io.path.createTempDirectory()
val tempFileInDir = File(tempDir.toFile(), "tempFile.txt").apply { createNewFile() }
assertTrue(tempDir.exists())
assertTrue(tempFileInDir.exists())

deleteDirectory(tempDir)
deleteDirectory(tempDir.toFile())

assertFalse(tempDir.exists())
assertFalse(tempFileInDir.exists())
Expand All @@ -38,17 +42,18 @@ class DeleteFileUnitTest {
}
}

@OptIn(ExperimentalPathApi::class)
@Test
fun `given directory when deleteDirectory called then directory and its contents are deleted recursively`() {
val tempDir = createTempDir()
val innerTempDir = File(tempDir, "innerTempDir").apply { mkdir() }
val tempDir = kotlin.io.path.createTempDirectory()
val innerTempDir = File(tempDir.toFile(), "innerTempDir").apply { mkdir() }
val tempFileInDir = File(innerTempDir, "tempFile.txt").apply { createNewFile() }

assertTrue(tempDir.exists())
assertTrue(innerTempDir.exists())
assertTrue(tempFileInDir.exists())

tempDir.deleteContentsRecursively()
tempDir.deleteRecursively()

assertFalse(tempDir.exists())
assertFalse(innerTempDir.exists())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class NullSafetyUnitTest {
@Test
fun givenNonNullableField_whenAssignValueToIt_thenNotNeedToCheckAgainstNull() {
//given
var a: String = "value"
val a: String = "value"
//a = null compilation error

//then
Expand All @@ -21,15 +21,11 @@ class NullSafetyUnitTest {
@Test
fun givenNullableField_whenReadValue_thenNeedToCheckAgainstNull() {
//given
var b: String? = "value"
val b: String?
b = null

//when
if (b != null) {

} else {
assertNull(b)
}
assertNull(b)
}

@Test
Expand Down Expand Up @@ -122,7 +118,7 @@ class NullSafetyUnitTest {
@Test
fun givenNullableField_whenUsingDoubleExclamationMarkOperatorOnNull_thenThrowNPE() {
//given
var b: String? = "value"
val b: String?
b = null

//when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class ScopeFunctionsUnitTest {
var called : Boolean = false

fun info(message: String) {
println("Message is: $message")
called = true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class NullabilityTests {
fun testNullPointerException() {
val name: String? = null
assertFailsWith<NullPointerException> {
val length = name!!.length
name!!
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class PairUnitTest{
fun `when using infix function then should create a Pair instance`(){
val pairInfixFunction = 1 to "value"

@Suppress("USELESS_IS_CHECK")
assertTrue(pairInfixFunction is Pair)
}

Expand Down Expand Up @@ -51,6 +52,7 @@ class PairUnitTest{
val pair = Pair(first = "Hello", second = "World")
val listFromPair = pair.toList()

@Suppress("USELESS_IS_CHECK")
assertTrue(listFromPair is List<String>)
assertEquals(2, listFromPair.size)
assertEquals("World", listFromPair.sortedDescending().first())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ class WorkingWithTripleUnitTest {
@Test
fun `when assignment with deconstruction, then get the expected result`() {
val (a, b, c) = Triple(42, "Kotlin", Long.MAX_VALUE)
@Suppress("USELESS_IS_CHECK")
assertTrue { a is Int }
assertEquals(42, a)

@Suppress("USELESS_IS_CHECK")
assertTrue { b is String }
assertEquals("Kotlin", b)

@Suppress("USELESS_IS_CHECK")
assertTrue { c is Long }
assertEquals(Long.MAX_VALUE, c)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ import java.io.Serializable
import java.nio.channels.Channel

fun main() {
val channel = object : Channel {
object : Channel {
override fun isOpen() = false

override fun close() {
}
}

val maxEntries = 10
val lruCache = object : LinkedHashMap<String, Int>(10, 0.75f) {
object : LinkedHashMap<String, Int>(10, 0.75f) {

override fun removeEldestEntry(eldest: MutableMap.MutableEntry<String, Int>?): Boolean {
return size > maxEntries
}
}

val map = object : LinkedHashMap<String, Int>() {
object : LinkedHashMap<String, Int>() {
// omitted
}

val serializableChannel = object : Channel, Serializable {
object : Channel, Serializable {
override fun isOpen(): Boolean {
TODO("Not yet implemented")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.baeldung.generic

fun <T> sort(xs: List<T>) where T : CharSequence, T : Comparable<T> {
fun <T> sort() where T : CharSequence, T : Comparable<T> {
// sort the collection in place
}

class StringCollection<T>(xs: List<T>) where T : CharSequence, T : Comparable<T>
class StringCollection<T>() where T : CharSequence, T : Comparable<T>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
inline fun <reified T> Iterable<*>.filterIsInstance() = filter { it is T }

fun main(args: Array<String>) {
fun main() {
val set = setOf("1984", 2, 3, "Brave new world", 11)
println(set.filterIsInstance<Int>())
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

class DatabaseDelegate<in R, T>(private val field: String, private val id: Int) : ReadWriteProperty<R, T> {
@Suppress("UNCHECKED_CAST")
override fun getValue(thisRef: R, property: KProperty<*>): T =
queryForValue(field, id) as T

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ class A
class B
class C

fun f(a: A): B = TODO()
fun g(b: B): C = TODO()
fun f(a: A): B {
println("Input value of f is: $a")
return B()
}
fun g(b: B): C {
println("Input value of g is: $b")
return C()
}
fun h(a: A): C = g(f(a))

fun interface G {
fun invoke(b: B): C
}

fun H(a: A, f: (A) -> B, g: G) = g(f(a))
fun H(a: A, f: (A) -> B) = g(f(a))
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ data class Employee(

companion object {
fun loadFromDb(id: UUID): Employee {
println("Provided id is: $id")
TODO("Go to DB and create an Employee object")
}
}
Expand All @@ -31,11 +32,14 @@ data class PureEmployee(
)

class EmployeeRepository(dataSource: DataSource) {
val dt = dataSource
fun upsert(employee: PureEmployee) {
println("Upsert Pure employee: $employee")
TODO("Save Employee to the database")
}

fun findById(id: UUID): PureEmployee {
println("The given id is: $id")
TODO("Go to DB and create an Employee object")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class GenericsUnitTest {
val res = parameterizedClass.getValue()

//then
@Suppress("USELESS_IS_CHECK")
assertTrue(res is String)
}

Expand All @@ -27,6 +28,7 @@ class GenericsUnitTest {
val res = parameterizedClass.getValue()

//then
@Suppress("USELESS_IS_CHECK")
assertTrue(res is String)
}

Expand All @@ -39,6 +41,7 @@ class GenericsUnitTest {
val ref: ParameterizedProducer<Any> = parameterizedProducer

//then
@Suppress("USELESS_IS_CHECK")
assertTrue(ref is ParameterizedProducer<Any>)
}

Expand All @@ -51,6 +54,7 @@ class GenericsUnitTest {
val ref: ParameterizedConsumer<Double> = parameterizedConsumer

//then
@Suppress("USELESS_IS_CHECK")
assertTrue(ref is ParameterizedConsumer<Double>)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import kotlin.test.assertFailsWith

class DelegateProvider<in T, D>(private val field: String, private val id: Int)
: PropertyDelegateProvider<T, DatabaseDelegate<T, D>> {
override operator fun provideDelegate(thisRef: T, prop: KProperty<*>): DatabaseDelegate<T, D> {
override operator fun provideDelegate(thisRef: T, property: KProperty<*>): DatabaseDelegate<T, D> {
println("Providing delegate for field $field and ID $id")
prop.returnType.isMarkedNullable
property.returnType.isMarkedNullable
return DatabaseDelegate(field, id)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.baeldung.extendDataClass

data class Vehicle(open val age: Int, open val numberOfWheels: Int)
data class Vehicle(val age: Int, val numberOfWheels: Int)
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class ObjectComparison {
if (other == null) return false
if (this === other) return true
if (other !is StorageOverriddenEqualsAndHashCode) return false
other as StorageOverriddenEqualsAndHashCode
if (name != other.name || capacity != other.capacity) return false
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class Car {

}

fun main(args: Array<String>) {
fun main() {
val car = Car("1", "sport")
val s= Car("2", "suv")
println("Car is: $car")
val car1 = Car("2", "suv")
println("Second car is: $car1")
}
Loading

0 comments on commit 33da56a

Please sign in to comment.