mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-16 22:51:17 +07:00
[Java. Logging] Add basic tests
IDEA-331693 GitOrigin-RevId: 7e3418984b59f18411ce3be472637f5d0e8daa39
This commit is contained in:
committed by
intellij-monorepo-bot
parent
de5ea078b4
commit
a9ac56fa4b
@@ -0,0 +1,7 @@
|
|||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
class A {
|
||||||
|
|
||||||
|
private static final Log log<caret> = LogFactory.getLog(A.class);
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
|
class A {
|
||||||
|
|
||||||
|
private static final Logger log<caret> = Logger.getLogger(A.class);
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
class A {
|
||||||
|
|
||||||
|
private static final Logger log<caret> = LogManager.getLogger(A.class);
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
class Outer {
|
||||||
|
class Inner {
|
||||||
|
private static final Logger log<caret> = LoggerFactory.getLogger(Inner.class);
|
||||||
|
|
||||||
|
void foo() {
|
||||||
|
}
|
||||||
|
class InnerMost {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
class Outer {
|
||||||
|
class Inner {
|
||||||
|
private static final Logger log<caret> = LoggerFactory.getLogger(Inner.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
class Outer {
|
||||||
|
private static final Logger log<caret> = LoggerFactory.getLogger(Outer.class);
|
||||||
|
|
||||||
|
class Inner {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
|
class A {
|
||||||
|
|
||||||
|
private static final Logger log<caret> = Logger.getLogger(A.class);
|
||||||
|
}
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
class A {
|
|
||||||
private static final Logger LOGGER<caret> = Logger.getLogger(A.class.getName());
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
class A {
|
||||||
|
|
||||||
|
private static final Logger log<caret> = LoggerFactory.getLogger(A.class);
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
class A {
|
||||||
|
<caret>
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
class A {
|
||||||
|
<caret>
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
class Outer {
|
||||||
|
class Inner {
|
||||||
|
void foo() {<caret>
|
||||||
|
}
|
||||||
|
class InnerMost {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
class Outer {
|
||||||
|
class Inner {<caret>
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
class Outer {
|
||||||
|
class Inner {<caret>
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
class A {
|
||||||
|
<caret>
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
class A {
|
||||||
|
<caret>
|
||||||
|
}
|
||||||
@@ -3,29 +3,202 @@ package com.intellij.java.codeInsight
|
|||||||
|
|
||||||
import com.intellij.JavaTestUtil
|
import com.intellij.JavaTestUtil
|
||||||
import com.intellij.codeInsight.generation.GenerateLoggerHandler
|
import com.intellij.codeInsight.generation.GenerateLoggerHandler
|
||||||
|
import com.intellij.logging.UnspecifiedLogger
|
||||||
|
import com.intellij.openapi.components.service
|
||||||
|
import com.intellij.openapi.ui.popup.JBPopup
|
||||||
|
import com.intellij.openapi.util.Disposer
|
||||||
|
import com.intellij.psi.PsiClass
|
||||||
|
import com.intellij.refactoring.introduce.PsiIntroduceTarget
|
||||||
|
import com.intellij.settings.JavaSettingsStorage
|
||||||
import com.intellij.testFramework.LightProjectDescriptor
|
import com.intellij.testFramework.LightProjectDescriptor
|
||||||
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase
|
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase
|
||||||
|
import com.intellij.ui.UiInterceptors
|
||||||
|
import com.intellij.ui.UiInterceptors.UiInterceptor
|
||||||
|
import com.intellij.ui.components.JBList
|
||||||
|
import com.intellij.util.ui.UIUtil
|
||||||
|
import junit.framework.TestCase
|
||||||
|
import javax.swing.ListModel
|
||||||
|
|
||||||
class GenerateLoggerTest : LightJavaCodeInsightFixtureTestCase() {
|
class GenerateLoggerTest : LightJavaCodeInsightFixtureTestCase() {
|
||||||
override fun getBasePath(): String = JavaTestUtil.getRelativeJavaTestDataPath() + "/codeInsight/generateLogger"
|
override fun getBasePath(): String = JavaTestUtil.getRelativeJavaTestDataPath() + "/codeInsight/generateLogger"
|
||||||
|
|
||||||
override fun setUp() {
|
fun testSlf4j() {
|
||||||
super.setUp()
|
|
||||||
myFixture.addClass("""
|
myFixture.addClass("""
|
||||||
package java.util.logging;
|
package org.slf4j;
|
||||||
public class Logger {
|
|
||||||
public static Logger getLogger(String name) {}
|
interface Logger {}
|
||||||
|
""".trimIndent())
|
||||||
|
myFixture.addClass("""
|
||||||
|
package org.slf4j;
|
||||||
|
|
||||||
|
interface LoggerFactory{
|
||||||
|
static <T> Logger getLogger(Class<T> clazz) {}
|
||||||
}
|
}
|
||||||
""".trimIndent())
|
""".trimIndent())
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fun testSimple() {
|
|
||||||
doTest()
|
doTest()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getProjectDescriptor(): LightProjectDescriptor {
|
fun testLog4j2() {
|
||||||
return JAVA_LATEST_WITH_LATEST_JDK
|
myFixture.addClass("""
|
||||||
|
package org.apache.logging.log4j;
|
||||||
|
|
||||||
|
interface Logger {}
|
||||||
|
""".trimIndent())
|
||||||
|
myFixture.addClass("""
|
||||||
|
package org.apache.logging.log4j;
|
||||||
|
|
||||||
|
interface LogManager{
|
||||||
|
static <T> Logger getLogger(Class<T> clazz) {}
|
||||||
|
}
|
||||||
|
""".trimIndent())
|
||||||
|
doTest()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testLog4j() {
|
||||||
|
myFixture.addClass("""
|
||||||
|
package org.apache.log4j;
|
||||||
|
|
||||||
|
interface Logger {
|
||||||
|
static <T> Logger getLogger(Class<T> clazz) {}
|
||||||
|
}
|
||||||
|
""".trimIndent())
|
||||||
|
doTest()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testApacheCommons() {
|
||||||
|
myFixture.addClass("""
|
||||||
|
package org.apache.commons.logging;
|
||||||
|
|
||||||
|
interface Log {
|
||||||
|
}
|
||||||
|
""".trimIndent())
|
||||||
|
myFixture.addClass("""
|
||||||
|
package org.apache.commons.logging;
|
||||||
|
|
||||||
|
interface LogFactory {
|
||||||
|
static Log getLog(Class<?> clazz) {}
|
||||||
|
}
|
||||||
|
""".trimIndent())
|
||||||
|
doTest()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testNestedClassesOuterClass() {
|
||||||
|
myFixture.addClass("""
|
||||||
|
package org.slf4j;
|
||||||
|
|
||||||
|
interface Logger {}
|
||||||
|
""".trimIndent())
|
||||||
|
myFixture.addClass("""
|
||||||
|
package org.slf4j;
|
||||||
|
|
||||||
|
interface LoggerFactory{
|
||||||
|
static <T> Logger getLogger(Class<T> clazz) {}
|
||||||
|
}
|
||||||
|
""".trimIndent())
|
||||||
|
doTestWithMultiplePlaces(
|
||||||
|
listOf(
|
||||||
|
"class Outer",
|
||||||
|
"class Inner",
|
||||||
|
),
|
||||||
|
"class Outer"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testNestedClassesNestedClass() {
|
||||||
|
myFixture.addClass("""
|
||||||
|
package org.slf4j;
|
||||||
|
|
||||||
|
interface Logger {}
|
||||||
|
""".trimIndent())
|
||||||
|
myFixture.addClass("""
|
||||||
|
package org.slf4j;
|
||||||
|
|
||||||
|
interface LoggerFactory{
|
||||||
|
static <T> Logger getLogger(Class<T> clazz) {}
|
||||||
|
}
|
||||||
|
""".trimIndent())
|
||||||
|
doTestWithMultiplePlaces(
|
||||||
|
listOf(
|
||||||
|
"class Outer",
|
||||||
|
"class Inner",
|
||||||
|
),
|
||||||
|
"class Inner"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testMultipleNestedClasses() {
|
||||||
|
myFixture.addClass("""
|
||||||
|
package org.slf4j;
|
||||||
|
|
||||||
|
interface Logger {}
|
||||||
|
""".trimIndent())
|
||||||
|
myFixture.addClass("""
|
||||||
|
package org.slf4j;
|
||||||
|
|
||||||
|
interface LoggerFactory{
|
||||||
|
static <T> Logger getLogger(Class<T> clazz) {}
|
||||||
|
}
|
||||||
|
""".trimIndent())
|
||||||
|
doTestWithMultiplePlaces(
|
||||||
|
listOf(
|
||||||
|
"class Outer",
|
||||||
|
"class Inner",
|
||||||
|
),
|
||||||
|
"class Inner"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testSaveSettings() {
|
||||||
|
myFixture.addClass("""
|
||||||
|
package org.apache.log4j;
|
||||||
|
|
||||||
|
interface Logger {
|
||||||
|
static <T> Logger getLogger(Class<T> clazz) {}
|
||||||
|
}
|
||||||
|
""".trimIndent())
|
||||||
|
assertEquals(project.service<JavaSettingsStorage>().state.loggerName, UnspecifiedLogger.UNSPECIFIED_LOGGER_NAME)
|
||||||
|
doTest()
|
||||||
|
assertEquals(project.service<JavaSettingsStorage>().state.loggerName, "Log4j")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getProjectDescriptor(): LightProjectDescriptor = JAVA_LATEST_WITH_LATEST_JDK
|
||||||
|
|
||||||
|
private fun doTestWithMultiplePlaces(expectedClassNameList: List<String>, selectedClass: String) {
|
||||||
|
val name = getTestName(false)
|
||||||
|
myFixture.configureByFile("before$name.java")
|
||||||
|
|
||||||
|
UiInterceptors.register(object : UiInterceptor<JBPopup>(JBPopup::class.java) {
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
override fun doIntercept(popup: JBPopup) {
|
||||||
|
Disposer.register(myFixture.testRootDisposable, popup)
|
||||||
|
val content = UIUtil.findComponentOfType(popup.getContent(), JBList::class.java)
|
||||||
|
|
||||||
|
TestCase.assertTrue(content?.selectedIndex == 0)
|
||||||
|
|
||||||
|
val model: ListModel<PsiIntroduceTarget<PsiClass>> = content!!.model as ListModel<PsiIntroduceTarget<PsiClass>>
|
||||||
|
|
||||||
|
val actualClassNameList: MutableList<String> = mutableListOf()
|
||||||
|
|
||||||
|
(0..<model.size).forEach {
|
||||||
|
actualClassNameList.add(model.getElementAt(it).render())
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(expectedClassNameList, actualClassNameList)
|
||||||
|
|
||||||
|
val indexOfPreferredOption = actualClassNameList.indexOf(selectedClass)
|
||||||
|
|
||||||
|
TestCase.assertTrue(indexOfPreferredOption >= 0)
|
||||||
|
|
||||||
|
content.selectedIndex = indexOfPreferredOption
|
||||||
|
|
||||||
|
popup.closeOk(null)
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
GenerateLoggerHandler().invoke(project, editor, file)
|
||||||
|
|
||||||
|
myFixture.checkResultByFile("after$name.java")
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun doTest() {
|
private fun doTest() {
|
||||||
|
|||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package com.intellij.java.lomboktest
|
||||||
|
|
||||||
|
import com.intellij.codeInsight.generation.GenerateLoggerHandler
|
||||||
|
import com.intellij.codeInsight.generation.ui.ChooseLoggerDialogWrapper
|
||||||
|
import com.intellij.openapi.ui.DialogWrapper
|
||||||
|
import com.intellij.openapi.util.Disposer
|
||||||
|
import com.intellij.testFramework.LightProjectDescriptor
|
||||||
|
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase
|
||||||
|
import com.intellij.ui.UiInterceptors
|
||||||
|
import de.plushnikov.intellij.plugin.LombokTestUtil
|
||||||
|
|
||||||
|
class LombokGenerateLoggerTest : LightJavaCodeInsightFixtureTestCase() {
|
||||||
|
override fun getProjectDescriptor(): LightProjectDescriptor = LombokTestUtil.LOMBOK_DESCRIPTOR
|
||||||
|
|
||||||
|
override fun getBasePath(): String = "community/plugins/lombok/testData/intention/generateLogger"
|
||||||
|
|
||||||
|
fun testSlf4j() {
|
||||||
|
myFixture.addClass("""
|
||||||
|
package org.slf4j;
|
||||||
|
|
||||||
|
interface Logger {}
|
||||||
|
""".trimIndent())
|
||||||
|
|
||||||
|
doTest()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testLog4j() {
|
||||||
|
myFixture.addClass("""
|
||||||
|
package org.apache.log4j;
|
||||||
|
|
||||||
|
interface Logger {}
|
||||||
|
""".trimIndent())
|
||||||
|
|
||||||
|
doTest()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testLog4j2() {
|
||||||
|
myFixture.addClass("""
|
||||||
|
package org.apache.logging.log4j;
|
||||||
|
|
||||||
|
interface Logger {}
|
||||||
|
""".trimIndent())
|
||||||
|
|
||||||
|
doTest()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testApacheCommons() {
|
||||||
|
myFixture.addClass("""
|
||||||
|
package org.apache.commons.logging;
|
||||||
|
|
||||||
|
interface Log {
|
||||||
|
}
|
||||||
|
""".trimIndent())
|
||||||
|
|
||||||
|
doTest("Lombok Apache Commons Logging")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun doTest() {
|
||||||
|
doTest("Lombok ${getTestName(false)}")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun doTest(displayName : String) {
|
||||||
|
val name = getTestName(false)
|
||||||
|
myFixture.configureByFile("before$name.java")
|
||||||
|
UiInterceptors.register(
|
||||||
|
object : UiInterceptors.UiInterceptor<ChooseLoggerDialogWrapper>(ChooseLoggerDialogWrapper::class.java) {
|
||||||
|
override fun doIntercept(component: ChooseLoggerDialogWrapper) {
|
||||||
|
Disposer.register(myFixture.testRootDisposable, component.disposable)
|
||||||
|
component.setComboBoxItem(displayName)
|
||||||
|
component.close(DialogWrapper.OK_EXIT_CODE)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
GenerateLoggerHandler().invoke(project, editor, file)
|
||||||
|
|
||||||
|
myFixture.checkResultByFile("after$name.java")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import lombok.extern.apachecommons.CommonsLog;
|
||||||
|
|
||||||
|
@CommonsLog<caret>
|
||||||
|
class A {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import lombok.extern.log4j.Log4j;
|
||||||
|
|
||||||
|
@Log4j<caret>
|
||||||
|
class A {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
|
||||||
|
@Log4j2<caret>
|
||||||
|
class A {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
@Slf4j<caret>
|
||||||
|
class A {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
class A {
|
||||||
|
<caret>
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
class A {
|
||||||
|
<caret>
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
class A {
|
||||||
|
<caret>
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
class A {
|
||||||
|
<caret>
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user