From bfbcacef6de970a10041675cea903159dedfa02f Mon Sep 17 00:00:00 2001 From: Chalard Jean Date: Mon, 22 Nov 2021 21:21:25 +0900 Subject: [PATCH] Make sure no suppressed expression is present when they shouldn't In fact the test already passes, because addSuppressed contains an explicit test that if this === argument, then it doesn't add it. But otherwise that's a bug in tryCleanup Test: NetworkStaticLibTests Change-Id: I202790bbe8d82445c5affdd9076561c2c6ea9b59 --- .../com/android/net/module/util/CleanupTest.kt | 17 ++++++++--------- .../hostdevice/com/android/testutils/Cleanup.kt | 2 +- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/staticlibs/tests/unit/src/com/android/net/module/util/CleanupTest.kt b/staticlibs/tests/unit/src/com/android/net/module/util/CleanupTest.kt index af4d818d94..05faed1cb0 100644 --- a/staticlibs/tests/unit/src/com/android/net/module/util/CleanupTest.kt +++ b/staticlibs/tests/unit/src/com/android/net/module/util/CleanupTest.kt @@ -18,14 +18,13 @@ package com.android.net.module.util import android.util.Log import com.android.testutils.tryTest -import kotlin.test.assertFailsWith import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 +import kotlin.test.assertFailsWith import kotlin.test.assertTrue -import kotlin.test.fail -private val TAG = CleanupTest::class.toString() +private val TAG = CleanupTest::class.simpleName @RunWith(JUnit4::class) class CleanupTest { @@ -49,7 +48,7 @@ class CleanupTest { @Test fun testThrowTry() { var x = 1 - assertFailsWith { + val thrown = assertFailsWith { tryTest { x = 2 throw TestException1() @@ -60,13 +59,14 @@ class CleanupTest { Log.e(TAG, "Do nothing") } } + assertTrue(thrown.suppressedExceptions.isEmpty()) assertTrue(x == 3) } @Test fun testThrowCleanup() { var x = 1 - assertFailsWith { + val thrown = assertFailsWith { tryTest { x = 2 Log.e(TAG, "Do nothing") @@ -77,13 +77,14 @@ class CleanupTest { x = 4 } } + assertTrue(thrown.suppressedExceptions.isEmpty()) assertTrue(x == 3) } @Test fun testThrowBoth() { var x = 1 - try { + val thrown = assertFailsWith { tryTest { x = 2 throw TestException1() @@ -94,10 +95,8 @@ class CleanupTest { throw TestException2() x = 5 } - fail("Expected failure with TestException1") - } catch (e: TestException1) { - assertTrue(e.suppressedExceptions[0] is TestException2) } + assertTrue(thrown.suppressedExceptions[0] is TestException2) assertTrue(x == 4) } } diff --git a/staticlibs/testutils/hostdevice/com/android/testutils/Cleanup.kt b/staticlibs/testutils/hostdevice/com/android/testutils/Cleanup.kt index 769d98068c..fb45582f97 100644 --- a/staticlibs/testutils/hostdevice/com/android/testutils/Cleanup.kt +++ b/staticlibs/testutils/hostdevice/com/android/testutils/Cleanup.kt @@ -63,7 +63,6 @@ class ExceptionCleanupBlock(val originalException: Exception?) { inline infix fun cleanup(block: () -> Unit) { try { block() - if (null != originalException) throw originalException } catch (e: Exception) { if (null == originalException) { throw e @@ -72,6 +71,7 @@ class ExceptionCleanupBlock(val originalException: Exception?) { throw originalException } } + if (null != originalException) throw originalException } }