From a98f36bce4f9193a1fe721f421b6ffd8f2fd5532 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Fri, 27 Dec 2019 22:59:32 +0300 Subject: [PATCH] Reorganize existing Closeable/AutoCloseable.use tests --- .../jdk7/test/AutoCloseableContractsTest.kt | 25 ---- ...est.kt => UseAutoCloseableResourceTest.kt} | 31 ++-- ...bleTest.kt => UseCloseableResourceTest.kt} | 32 ++--- libraries/stdlib/jvm/test/io/Closeable.kt | 20 --- libraries/stdlib/jvm/test/io/ReadWrite.kt | 30 +--- .../jvm/test/io/UseCloseableResourceTest.kt | 135 ++++++++++++++++++ 6 files changed, 166 insertions(+), 107 deletions(-) delete mode 100644 libraries/stdlib/jdk7/test/AutoCloseableContractsTest.kt rename libraries/stdlib/jdk7/test/{TryWithResourcesAutoCloseableTest.kt => UseAutoCloseableResourceTest.kt} (79%) rename libraries/stdlib/jdk7/test/{TryWithResourcesCloseableTest.kt => UseCloseableResourceTest.kt} (78%) delete mode 100644 libraries/stdlib/jvm/test/io/Closeable.kt create mode 100644 libraries/stdlib/jvm/test/io/UseCloseableResourceTest.kt diff --git a/libraries/stdlib/jdk7/test/AutoCloseableContractsTest.kt b/libraries/stdlib/jdk7/test/AutoCloseableContractsTest.kt deleted file mode 100644 index ef5440c0550..00000000000 --- a/libraries/stdlib/jdk7/test/AutoCloseableContractsTest.kt +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package kotlin.jdk7.test - -import org.junit.Test -import kotlin.test.assertEquals - -class AutoCloseableContractsTest { - @Test - fun shouldHaveBlockExactlyOnceContract() { - class TestAutoCloseable : AutoCloseable { - override fun close() { - } - } - - val i: Int - TestAutoCloseable().use { - i = 1 - } - assertEquals(1, i) - } -} diff --git a/libraries/stdlib/jdk7/test/TryWithResourcesAutoCloseableTest.kt b/libraries/stdlib/jdk7/test/UseAutoCloseableResourceTest.kt similarity index 79% rename from libraries/stdlib/jdk7/test/TryWithResourcesAutoCloseableTest.kt rename to libraries/stdlib/jdk7/test/UseAutoCloseableResourceTest.kt index 9d60ffd7f56..3f58d8b3ef1 100644 --- a/libraries/stdlib/jdk7/test/TryWithResourcesAutoCloseableTest.kt +++ b/libraries/stdlib/jdk7/test/UseAutoCloseableResourceTest.kt @@ -1,29 +1,17 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ package kotlin.jdk7.test import java.io.* -import org.junit.Test import java.util.* import kotlin.test.* -class TryWithResourcesAutoCloseableTest { +class UseAutoCloseableResourceTest { - @Suppress("HasPlatformType") fun platformNull() = Collections.singletonList(null as T).first() + @Suppress("HasPlatformType", "UNCHECKED_CAST") fun platformNull() = Collections.singletonList(null as T).first() class Resource(val faultyClose: Boolean = false) : AutoCloseable { @@ -125,4 +113,15 @@ class TryWithResourcesAutoCloseableTest { assertTrue(e.suppressed.isEmpty()) } + + @Test + fun contractCallsInPlace() { + val i: Int + Resource().use { _ -> + Resource().use { _ -> + i = 1 + } + } + assertEquals(1, i) + } } diff --git a/libraries/stdlib/jdk7/test/TryWithResourcesCloseableTest.kt b/libraries/stdlib/jdk7/test/UseCloseableResourceTest.kt similarity index 78% rename from libraries/stdlib/jdk7/test/TryWithResourcesCloseableTest.kt rename to libraries/stdlib/jdk7/test/UseCloseableResourceTest.kt index 022170704bf..2a2e3a92ea2 100644 --- a/libraries/stdlib/jdk7/test/TryWithResourcesCloseableTest.kt +++ b/libraries/stdlib/jdk7/test/UseCloseableResourceTest.kt @@ -1,30 +1,17 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ package kotlin.jdk7.test -import org.junit.Ignore import java.io.* -import org.junit.Test import java.util.* import kotlin.test.* -class TryWithResourcesCloseableTest { +class UseCloseableResourceTest { - @Suppress("HasPlatformType") fun platformNull() = Collections.singletonList(null as T).first() + @Suppress("HasPlatformType", "UNCHECKED_CAST") fun platformNull() = Collections.singletonList(null as T).first() class Resource(val faultyClose: Boolean = false) : Closeable { @@ -124,4 +111,15 @@ class TryWithResourcesCloseableTest { assertTrue(e is IllegalArgumentException) assertTrue(e.suppressed.isEmpty()) } + + @Test + fun contractCallsInPlace() { + val i: Int + Resource().use { _ -> + Resource().use { _ -> + i = 1 + } + } + assertEquals(1, i) + } } diff --git a/libraries/stdlib/jvm/test/io/Closeable.kt b/libraries/stdlib/jvm/test/io/Closeable.kt deleted file mode 100644 index c81a8155dd8..00000000000 --- a/libraries/stdlib/jvm/test/io/Closeable.kt +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package test.io - -import org.junit.Test -import kotlin.test.assertEquals - -class CloseableTest { - @Test - fun shouldHaveBlockExactlyOnceContract() { - val i: Int - "".byteInputStream().use { - i = 1 - } - assertEquals(1, i) - } -} diff --git a/libraries/stdlib/jvm/test/io/ReadWrite.kt b/libraries/stdlib/jvm/test/io/ReadWrite.kt index e3d8a2f6723..a6bc38063a0 100644 --- a/libraries/stdlib/jvm/test/io/ReadWrite.kt +++ b/libraries/stdlib/jvm/test/io/ReadWrite.kt @@ -12,7 +12,7 @@ import java.io.StringReader import java.net.URL import java.util.ArrayList -fun sample(): Reader = StringReader("Hello\nWorld"); +private fun sample(): Reader = StringReader("Hello\nWorld"); class ReadWriteTest { @Test fun testAppendText() { @@ -124,34 +124,6 @@ class ReadWriteTest { file.deleteOnExit() } - - - @Test fun testUse() { - val list = ArrayList() - val reader = sample().buffered() - - reader.use { - while (true) { - val line = it.readLine() - if (line != null) - list.add(line) - else - break - } - } - - assertEquals(arrayListOf("Hello", "World"), list) - } - - @Test fun testPlatformNullUse() { - fun platformNull() = @Suppress("UNCHECKED_CAST") java.util.Collections.singleton(null as T).first() - val resource = platformNull() - val result = resource.use { - "ok" - } - assertEquals("ok", result) - } - @Test fun testURL() { val url = URL("http://kotlinlang.org") val text = url.readText() diff --git a/libraries/stdlib/jvm/test/io/UseCloseableResourceTest.kt b/libraries/stdlib/jvm/test/io/UseCloseableResourceTest.kt new file mode 100644 index 00000000000..bc433134531 --- /dev/null +++ b/libraries/stdlib/jvm/test/io/UseCloseableResourceTest.kt @@ -0,0 +1,135 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package test.io + +import java.io.* +import java.util.* +import kotlin.test.* + +class UseCloseableResourceTest { + + @Test + fun useReader() { + val reader = StringReader("Hello\nWorld").buffered() + val firstLine: String + val secondLine: String = reader.use { + firstLine = it.readLine() + it.readLine() + } + assertEquals("Hello", firstLine) + assertEquals("World", secondLine) + } + + + @Suppress("HasPlatformType", "UNCHECKED_CAST") fun platformNull() = Collections.singletonList(null as T).first() + + class Resource(val faultyClose: Boolean = false) : Closeable { + + var isClosed = false + private set + + override fun close() { + if (faultyClose) + throw IOException("Close failed") + isClosed = true + } + } + + @Test fun success() { + val resource = Resource() + val result = resource.use { "ok" } + assertEquals("ok", result) + assertTrue(resource.isClosed) + } + + @Test fun closeFails() { + val e = assertFails { + Resource(faultyClose = true).use { "" } + } + assertTrue(e is IOException) + } + + @Test fun opFailsCloseSuccess() { + val e = assertFails { + Resource().use { error("op fail") } + } + assertTrue(e is IllegalStateException) + // assertTrue(e.suppressed.isEmpty()) - no suppressed in JDK6 + } + + @Test fun opFailsCloseFails() { + val e = assertFails { + Resource(faultyClose = true).use { error("op fail") } + } + assertTrue(e is IllegalStateException) + // assertTrue(e.suppressed.single() is IOException) - no suppressed in JDK6 + } + + @Test fun opFailsCloseFailsTwice() { + val e = assertFails { + Resource(faultyClose = true).use { _ -> + Resource(faultyClose = true).use { _ -> + error("op fail") + } + } + } + assertTrue(e is IllegalStateException) + } + + @Test fun nonLocalReturnInBlock() { + fun Resource.operation(nonLocal: Boolean): String { + return use { if (nonLocal) return "nonLocal" else "local" } + } + + Resource().let { resource -> + val result = resource.operation(nonLocal = false) + assertEquals("local", result) + assertTrue(resource.isClosed) + } + + Resource().let { resource -> + val result = resource.operation(nonLocal = true) + assertEquals("nonLocal", result) + assertTrue(resource.isClosed) + } + + } + + @Test fun nullableResourceSuccess() { + val resource: Resource? = null + val result = resource.use { "ok" } + assertEquals("ok", result) + } + + @Test fun nullableResourceOpFails() { + val resource: Resource? = null + val e = assertFails { + resource.use { requireNotNull(it) } + } + assertTrue(e is IllegalArgumentException) + // assertTrue(e.suppressed.isEmpty()) - no suppressed in JDK6 + } + + @Test fun platformResourceOpFails() { + val resource = platformNull() + val e = assertFails { + resource.use { requireNotNull(it) } + } + assertTrue(e is IllegalArgumentException) + // assertTrue(e.suppressed.isEmpty()) - no suppressed in JDK6 + } + + @Test + fun contractCallsInPlace() { + val i: Int + Resource().use { _ -> + Resource().use { _ -> + i = 1 + } + } + assertEquals(1, i) + } +}