Reorganize existing Closeable/AutoCloseable.use tests
This commit is contained in:
@@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+15
-16
@@ -1,29 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2017 JetBrains s.r.o.
|
* 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.
|
||||||
* 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.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package kotlin.jdk7.test
|
package kotlin.jdk7.test
|
||||||
|
|
||||||
import java.io.*
|
import java.io.*
|
||||||
import org.junit.Test
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.test.*
|
import kotlin.test.*
|
||||||
|
|
||||||
class TryWithResourcesAutoCloseableTest {
|
class UseAutoCloseableResourceTest {
|
||||||
|
|
||||||
@Suppress("HasPlatformType") fun <T> platformNull() = Collections.singletonList(null as T).first()
|
@Suppress("HasPlatformType", "UNCHECKED_CAST") fun <T> platformNull() = Collections.singletonList(null as T).first()
|
||||||
|
|
||||||
class Resource(val faultyClose: Boolean = false) : AutoCloseable {
|
class Resource(val faultyClose: Boolean = false) : AutoCloseable {
|
||||||
|
|
||||||
@@ -125,4 +113,15 @@ class TryWithResourcesAutoCloseableTest {
|
|||||||
assertTrue(e.suppressed.isEmpty())
|
assertTrue(e.suppressed.isEmpty())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun contractCallsInPlace() {
|
||||||
|
val i: Int
|
||||||
|
Resource().use { _ ->
|
||||||
|
Resource().use { _ ->
|
||||||
|
i = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertEquals(1, i)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+15
-17
@@ -1,30 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2017 JetBrains s.r.o.
|
* 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.
|
||||||
* 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.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package kotlin.jdk7.test
|
package kotlin.jdk7.test
|
||||||
|
|
||||||
import org.junit.Ignore
|
|
||||||
import java.io.*
|
import java.io.*
|
||||||
import org.junit.Test
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.test.*
|
import kotlin.test.*
|
||||||
|
|
||||||
class TryWithResourcesCloseableTest {
|
class UseCloseableResourceTest {
|
||||||
|
|
||||||
@Suppress("HasPlatformType") fun <T> platformNull() = Collections.singletonList(null as T).first()
|
@Suppress("HasPlatformType", "UNCHECKED_CAST") fun <T> platformNull() = Collections.singletonList(null as T).first()
|
||||||
|
|
||||||
class Resource(val faultyClose: Boolean = false) : Closeable {
|
class Resource(val faultyClose: Boolean = false) : Closeable {
|
||||||
|
|
||||||
@@ -124,4 +111,15 @@ class TryWithResourcesCloseableTest {
|
|||||||
assertTrue(e is IllegalArgumentException)
|
assertTrue(e is IllegalArgumentException)
|
||||||
assertTrue(e.suppressed.isEmpty())
|
assertTrue(e.suppressed.isEmpty())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun contractCallsInPlace() {
|
||||||
|
val i: Int
|
||||||
|
Resource().use { _ ->
|
||||||
|
Resource().use { _ ->
|
||||||
|
i = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertEquals(1, i)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -12,7 +12,7 @@ import java.io.StringReader
|
|||||||
import java.net.URL
|
import java.net.URL
|
||||||
import java.util.ArrayList
|
import java.util.ArrayList
|
||||||
|
|
||||||
fun sample(): Reader = StringReader("Hello\nWorld");
|
private fun sample(): Reader = StringReader("Hello\nWorld");
|
||||||
|
|
||||||
class ReadWriteTest {
|
class ReadWriteTest {
|
||||||
@Test fun testAppendText() {
|
@Test fun testAppendText() {
|
||||||
@@ -124,34 +124,6 @@ class ReadWriteTest {
|
|||||||
file.deleteOnExit()
|
file.deleteOnExit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test fun testUse() {
|
|
||||||
val list = ArrayList<String>()
|
|
||||||
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 <T> platformNull() = @Suppress("UNCHECKED_CAST") java.util.Collections.singleton(null as T).first()
|
|
||||||
val resource = platformNull<java.io.Closeable>()
|
|
||||||
val result = resource.use {
|
|
||||||
"ok"
|
|
||||||
}
|
|
||||||
assertEquals("ok", result)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test fun testURL() {
|
@Test fun testURL() {
|
||||||
val url = URL("http://kotlinlang.org")
|
val url = URL("http://kotlinlang.org")
|
||||||
val text = url.readText()
|
val text = url.readText()
|
||||||
|
|||||||
@@ -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 <T> 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<Resource>()
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user