IJPL-453 migrate from OnlyThrowLoggedErrorProcessor to assertErrorLogged

GitOrigin-RevId: cf789d36a8f09c7fd562c42584c2e1fc2f4a1792
This commit is contained in:
Daniil Ovchinnikov
2024-10-05 20:02:29 +02:00
committed by intellij-monorepo-bot
parent 18a265bd2d
commit 5db74b516f
3 changed files with 7 additions and 42 deletions

View File

@@ -14,6 +14,7 @@ import com.intellij.platform.workspace.storage.testEntities.entities.*
import com.intellij.platform.workspace.storage.toBuilder
import com.intellij.platform.workspace.storage.url.VirtualFileUrlManager
import com.intellij.testFramework.UsefulTestCase.assertOneElement
import com.intellij.testFramework.assertErrorLogged
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.RepeatedTest
import org.junit.jupiter.api.RepetitionInfo
@@ -314,7 +315,7 @@ class ApplyChangesFromTest {
children = emptyList()
}
assertThrowsLogError<ApplyChangesFromException> {
assertErrorLogged<ApplyChangesFromException> {
source.applyChanges(target)
}
}
@@ -332,7 +333,7 @@ class ApplyChangesFromTest {
this.myName = "Name"
}
assertThrowsLogError<ApplyChangesFromException> {
assertErrorLogged<ApplyChangesFromException> {
source.applyChanges(target)
}
}

View File

@@ -7,6 +7,7 @@ import com.intellij.platform.workspace.storage.impl.exceptions.SymbolicIdAlready
import com.intellij.platform.workspace.storage.testEntities.entities.*
import com.intellij.testFramework.UsefulTestCase.assertEmpty
import com.intellij.testFramework.UsefulTestCase.assertOneElement
import com.intellij.testFramework.assertErrorLogged
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Disabled
@@ -83,7 +84,7 @@ class EntityWithSymbolicIdInPStorageTest {
@Test
fun `add entity with existing persistent id`() {
builder = createEmptyBuilder()
assertThrowsLogError<SymbolicIdAlreadyExistsException> {
assertErrorLogged<SymbolicIdAlreadyExistsException> {
builder addEntity NamedEntity("MyName", MySource) {
this.additionalProperty = null
children = emptyList()
@@ -118,7 +119,7 @@ class EntityWithSymbolicIdInPStorageTest {
@Test
fun `modify entity to repeat persistent id`() {
builder = createEmptyBuilder()
assertThrowsLogError<SymbolicIdAlreadyExistsException> {
assertErrorLogged<SymbolicIdAlreadyExistsException> {
builder addEntity NamedEntity("MyName", MySource) {
this.additionalProperty = null
children = emptyList()
@@ -136,7 +137,7 @@ class EntityWithSymbolicIdInPStorageTest {
@Test
fun `modify entity to repeat persistent id - restoring after exception`() {
builder = createEmptyBuilder()
assertThrowsLogError<SymbolicIdAlreadyExistsException> {
assertErrorLogged<SymbolicIdAlreadyExistsException> {
builder addEntity NamedEntity("MyName", MySource) {
this.additionalProperty = null
children = emptyList()

View File

@@ -1,37 +0,0 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.platform.workspace.storage.tests
import com.intellij.testFramework.LoggedErrorProcessor
import com.intellij.testFramework.TestLoggerFactory.TestLoggerAssertionError
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.fail
/**
* By default, LOG.error does three things in tests:
* - rethrows the exception
* - logs error
* - prints to stderr
*
* The problem is that if we catch exception in tests, such an approach will print the exception to stderr and it will
* look like the exception is not processed.
* I don't see a need for printing these caught exceptions, so we can use this processor to only rethrow them.
*/
internal object OnlyThrowLoggedErrorProcessor : LoggedErrorProcessor() {
override fun processError(category: String, message: String, details: Array<out String>, t: Throwable?): Set<Action> {
return setOf(Action.RETHROW)
}
}
/**
* Asserts that [T] was thrown via `LOG.error("message", e)` call where `e` has a type of [T].
*/
internal inline fun <reified T: Throwable> assertThrowsLogError(crossinline action: () -> Unit): T {
val exception = assertThrows<TestLoggerAssertionError> {
LoggedErrorProcessor.executeWith<Throwable>(OnlyThrowLoggedErrorProcessor) {
action()
}
}
val cause = exception.cause
if (cause !is T) fail("Expected ${T::class.java} exception in LOG.error")
return cause
}