From 578d363e51c019988d03c19281c50674ad6a3342 Mon Sep 17 00:00:00 2001 From: Georgii Ustinov Date: Mon, 7 Oct 2024 13:51:57 +0300 Subject: [PATCH] [Groovy] Convert Groovy files to Java - IndexedPropertyTest - SingletonConstructionInspectionTest - SingletonNewInstanceInspectionTest - SingletonTransformationSupportTest IDEA-327339 GitOrigin-RevId: 9d12852e81e49b6e3d9a4ddb75a7c2fc3082c9d7 --- .../IndexedPropertyTest.groovy | 145 ------------------ .../indexedProperty/IndexedPropertyTest.java | 137 +++++++++++++++++ .../SingletonConstructorInspectionTest.groovy | 103 ------------- .../SingletonConstructorInspectionTest.java | 104 +++++++++++++ .../SingletonNewInstanceInspectionTest.groovy | 56 ------- .../SingletonNewInstanceInspectionTest.java | 61 ++++++++ .../SingletonTransformationSupportTest.groovy | 122 --------------- .../SingletonTransformationSupportTest.java | 123 +++++++++++++++ 8 files changed, 425 insertions(+), 426 deletions(-) delete mode 100644 plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/indexedProperty/IndexedPropertyTest.groovy create mode 100644 plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/indexedProperty/IndexedPropertyTest.java delete mode 100644 plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonConstructorInspectionTest.groovy create mode 100644 plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonConstructorInspectionTest.java delete mode 100644 plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonNewInstanceInspectionTest.groovy create mode 100644 plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonNewInstanceInspectionTest.java delete mode 100644 plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonTransformationSupportTest.groovy create mode 100644 plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonTransformationSupportTest.java diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/indexedProperty/IndexedPropertyTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/indexedProperty/IndexedPropertyTest.groovy deleted file mode 100644 index 3200e88b2010..000000000000 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/indexedProperty/IndexedPropertyTest.groovy +++ /dev/null @@ -1,145 +0,0 @@ -// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -package org.jetbrains.plugins.groovy.transformations.indexedProperty - -import com.intellij.testFramework.LightProjectDescriptor -import groovy.transform.CompileStatic -import org.jetbrains.plugins.groovy.GroovyProjectDescriptors -import org.jetbrains.plugins.groovy.LightGroovyTestCase -import org.jetbrains.plugins.groovy.codeInspection.GroovyUnusedDeclarationInspection -import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.GrUnresolvedAccessInspection - -@CompileStatic -class IndexedPropertyTest extends LightGroovyTestCase { - - final LightProjectDescriptor projectDescriptor = GroovyProjectDescriptors.GROOVY_LATEST - - void 'test all methods resolved'() { - fixture.with { - addFileToProject 'A.groovy', '''\ -import groovy.transform.IndexedProperty -class A { - @IndexedProperty List rawList - @IndexedProperty List stringList - @IndexedProperty Double[] doubleArray - @IndexedProperty long[] primitiveArray -} -''' - - configureByText '_.groovy', '''\ -def a = new A() -a.getRawList(0) -a.setRawList(1, new Object()) - -a.getStringList(0) -a.setStringList(1, "Hello") - -a.getDoubleArray(1) -a.setDoubleArray(0, 1) - -a.getPrimitiveArray(1) -a.setPrimitiveArray(1, 1) -''' - enableInspections(GrUnresolvedAccessInspection) - checkHighlighting() - - configureByText 'Main.java', '''\ -class Main { - void foo() { - A a = new A(); - a.getRawList(0); - a.setRawList(1, new Object()); - - a.getStringList(0); - a.setStringList(1, "Hello"); - - a.getDoubleArray(1); - a.setDoubleArray(0, (double)1); - - a.getPrimitiveArray(1); - a.setPrimitiveArray(1, 1L); - } -} -''' - checkHighlighting() - } - } - - void 'test errors highlighting'() { - fixture.with { - configureByText '_.groovy', '''\ -import groovy.transform.IndexedProperty -class A { - @IndexedProperty List rawList - @IndexedProperty List stringList - @IndexedProperty Double[] doubleArray - @IndexedProperty long[] primitiveArray - @IndexedProperty Collection numberCollection - @IndexedProperty untyped - @IndexedProperty Integer nonIndexable - private @IndexedProperty explicitVisibility -} -''' - checkHighlighting() - } - } - - void 'test indexed property unused'() { - fixture.with { - configureByText '_.groovy', '''\ -import groovy.transform.IndexedProperty -class A { - @IndexedProperty List stringList -} -new A() -''' - enableInspections(GroovyUnusedDeclarationInspection) - checkHighlighting() - } - } - - void 'test indexed property is used via generated method'() { - fixture.with { - configureByText '_.groovy', '''\ -import groovy.transform.IndexedProperty -class A { - @IndexedProperty List stringList -} -new A().getStringList(0) -''' - enableInspections(GroovyUnusedDeclarationInspection) - checkHighlighting() - } - } - - void 'test indexed property rename'() { - fixture.with { - configureByText '_.groovy', '''\ -import groovy.transform.IndexedProperty -class A { - @IndexedProperty List stringList -} -def a = new A() -a.stringList -a.getStringList() -a.stringList = [] -a.setStringList([]) -a.getStringList(0) -a.setStringList(0, "") -''' - renameElementAtCaret 'newName' - checkResult '''\ -import groovy.transform.IndexedProperty -class A { - @IndexedProperty List newName -} -def a = new A() -a.newName -a.getNewName() -a.newName = [] -a.setNewName([]) -a.getNewName(0) -a.setNewName(0, "") -''' - } - } -} diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/indexedProperty/IndexedPropertyTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/indexedProperty/IndexedPropertyTest.java new file mode 100644 index 000000000000..dc5acb148b61 --- /dev/null +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/indexedProperty/IndexedPropertyTest.java @@ -0,0 +1,137 @@ +// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package org.jetbrains.plugins.groovy.transformations.indexedProperty; + +import com.intellij.testFramework.LightProjectDescriptor; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.plugins.groovy.GroovyProjectDescriptors; +import org.jetbrains.plugins.groovy.LightGroovyTestCase; +import org.jetbrains.plugins.groovy.codeInspection.GroovyUnusedDeclarationInspection; +import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.GrUnresolvedAccessInspection; + +public class IndexedPropertyTest extends LightGroovyTestCase { + @Override + @NotNull + public final LightProjectDescriptor getProjectDescriptor() { + return GroovyProjectDescriptors.GROOVY_LATEST; + } + + public void testAllMethodsResolved() { + myFixture.addFileToProject("A.groovy", """ + import groovy.transform.IndexedProperty + class A { + @IndexedProperty List rawList + @IndexedProperty List stringList + @IndexedProperty Double[] doubleArray + @IndexedProperty long[] primitiveArray + } + """); + + myFixture.configureByText("_.groovy", """ + def a = new A() + a.getRawList(0) + a.setRawList(1, new Object()) + + a.getStringList(0) + a.setStringList(1, "Hello") + + a.getDoubleArray(1) + a.setDoubleArray(0, 1) + + a.getPrimitiveArray(1) + a.setPrimitiveArray(1, 1) + """); + myFixture.enableInspections(GrUnresolvedAccessInspection.class); + myFixture.checkHighlighting(); + + myFixture.configureByText("Main.java", """ + class Main { + void foo() { + A a = new A(); + a.getRawList(0); + a.setRawList(1, new Object()); + + a.getStringList(0); + a.setStringList(1, "Hello"); + + a.getDoubleArray(1); + a.setDoubleArray(0, (double)1); + + a.getPrimitiveArray(1); + a.setPrimitiveArray(1, 1L); + } + } + """); + myFixture.checkHighlighting(); + } + + public void testErrorsHighlighting() { + myFixture.configureByText("_.groovy", """ + import groovy.transform.IndexedProperty + class A { + @IndexedProperty List rawList + @IndexedProperty List stringList + @IndexedProperty Double[] doubleArray + @IndexedProperty long[] primitiveArray + @IndexedProperty Collection numberCollection + @IndexedProperty untyped + @IndexedProperty Integer nonIndexable + private @IndexedProperty explicitVisibility + } + """); + myFixture.checkHighlighting(); + } + + public void testIndexedPropertyUnused() { + myFixture.configureByText("_.groovy", """ + import groovy.transform.IndexedProperty + class A { + @IndexedProperty List stringList + } + new A() + """); + myFixture.enableInspections(GroovyUnusedDeclarationInspection.class); + myFixture.checkHighlighting(); + } + + public void testIndexedPropertyIsUsedViaGeneratedMethod() { + myFixture.configureByText("_.groovy", """ + import groovy.transform.IndexedProperty + class A { + @IndexedProperty List stringList + } + new A().getStringList(0) + """); + myFixture.enableInspections(GroovyUnusedDeclarationInspection.class); + myFixture.checkHighlighting(); + } + + public void testIndexedPropertyRename() { + myFixture.configureByText("_.groovy", """ + import groovy.transform.IndexedProperty + class A { + @IndexedProperty List stringList + } + def a = new A() + a.stringList + a.getStringList() + a.stringList = [] + a.setStringList([]) + a.getStringList(0) + a.setStringList(0, "") + """); + myFixture.renameElementAtCaret("newName"); + myFixture.checkResult(""" + import groovy.transform.IndexedProperty + class A { + @IndexedProperty List newName + } + def a = new A() + a.newName + a.getNewName() + a.newName = [] + a.setNewName([]) + a.getNewName(0) + a.setNewName(0, "") + """); + } +} diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonConstructorInspectionTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonConstructorInspectionTest.groovy deleted file mode 100644 index aef844bde5fd..000000000000 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonConstructorInspectionTest.groovy +++ /dev/null @@ -1,103 +0,0 @@ -// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -package org.jetbrains.plugins.groovy.transformations.singleton - -import com.intellij.testFramework.LightProjectDescriptor -import groovy.transform.CompileStatic -import org.jetbrains.plugins.groovy.GroovyBundle -import org.jetbrains.plugins.groovy.GroovyProjectDescriptors -import org.jetbrains.plugins.groovy.LightGroovyTestCase - -@CompileStatic -class SingletonConstructorInspectionTest extends LightGroovyTestCase { - - final LightProjectDescriptor projectDescriptor = GroovyProjectDescriptors.GROOVY_LATEST - - void setUp() { - super.setUp() - fixture.enableInspections(SingletonConstructorInspection) - } - - void 'test highlighting'() { - fixture.with { - configureByText '_.groovy', '''\ -@Singleton -class A { - A() {} - A(a) {} -} - -@Singleton(strict=true) -class ExplicitStrict { - ExplicitStrict() {} - ExplicitStrict(a) {} -} - -@Singleton(strict = false) -class NonStrict { - NonStrict() {} - NonStrict(a) {} -} -''' - checkHighlighting() - } - } - - void 'test make non strict fix'() { - fixture.with { - configureByText '_.groovy', '''\ -@Singleton -class A { - A() {} -} -''' - def intention = findSingleIntention(GroovyBundle.message("singleton.constructor.makeNonStrict")) - assert intention - launchAction(intention) - checkResult '''\ -@Singleton(strict = false) -class A { - A() {} -} -''' - } - } - - void 'test make non strict fix existing'() { - fixture.with { - configureByText '_.groovy', '''\ -@Singleton(strict = true, property = "lol") -class A { - A() {} -} -''' - def intention = findSingleIntention(GroovyBundle.message("singleton.constructor.makeNonStrict")) - assert intention - launchAction(intention) - checkResult '''\ -@Singleton(strict = false, property = "lol") -class A { - A() {} -} -''' - } - } - - void 'test remove constructor fix'() { - fixture.with { - configureByText '_.groovy', '''\ -@Singleton -class A { - A() {} -} -''' - def intention = findSingleIntention(GroovyBundle.message("singleton.constructor.remove")) - assert intention - launchAction(intention) - checkResult '''\ -@Singleton -class A { -} -''' - } - } -} diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonConstructorInspectionTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonConstructorInspectionTest.java new file mode 100644 index 000000000000..096c98d95826 --- /dev/null +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonConstructorInspectionTest.java @@ -0,0 +1,104 @@ +// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package org.jetbrains.plugins.groovy.transformations.singleton; + +import com.intellij.codeInsight.intention.IntentionAction; +import com.intellij.testFramework.LightProjectDescriptor; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.plugins.groovy.GroovyBundle; +import org.jetbrains.plugins.groovy.GroovyProjectDescriptors; +import org.jetbrains.plugins.groovy.LightGroovyTestCase; +import org.junit.Assert; + +public class SingletonConstructorInspectionTest extends LightGroovyTestCase { + @Override + @NotNull + public final LightProjectDescriptor getProjectDescriptor() { + return GroovyProjectDescriptors.GROOVY_LATEST; + } + + @Override + public void setUp() throws Exception { + super.setUp(); + getFixture().enableInspections(SingletonConstructorInspection.class); + } + + public void testHighlighting() { + myFixture.configureByText("_.groovy", """ + @Singleton + class A { + A() {} + A(a) {} + } + + @Singleton(strict=true) + class ExplicitStrict { + ExplicitStrict() {} + ExplicitStrict(a) {} + } + + @Singleton(strict = false) + class NonStrict { + NonStrict() {} + NonStrict(a) {} + } + """); + + myFixture.checkHighlighting(); + } + + public void testMakeNonStrictFix() { + myFixture.configureByText("_.groovy", """ + @Singleton + class A { + A() {} + } + """); + + IntentionAction intention = myFixture.findSingleIntention(GroovyBundle.message("singleton.constructor.makeNonStrict")); + Assert.assertNotNull(intention); + myFixture.launchAction(intention); + myFixture.checkResult(""" + @Singleton(strict = false) + class A { + A() {} + } + """); + } + + public void testMakeNonStrictFixExisting() { + myFixture.configureByText("_.groovy", """ + @Singleton(strict = true, property = "lol") + class A { + A() {} + } + """); + + IntentionAction intention = myFixture.findSingleIntention(GroovyBundle.message("singleton.constructor.makeNonStrict")); + Assert.assertNotNull(intention); + myFixture.launchAction(intention); + myFixture.checkResult(""" + @Singleton(strict = false, property = "lol") + class A { + A() {} + } + """); + } + + public void testRemoveConstructorFix() { + myFixture.configureByText("_.groovy", """ + @Singleton + class A { + A() {} + } + """); + + IntentionAction intention = myFixture.findSingleIntention(GroovyBundle.message("singleton.constructor.remove")); + Assert.assertNotNull(intention); + myFixture.launchAction(intention); + myFixture.checkResult(""" + @Singleton + class A { + } + """); + } +} diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonNewInstanceInspectionTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonNewInstanceInspectionTest.groovy deleted file mode 100644 index 801670ff9853..000000000000 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonNewInstanceInspectionTest.groovy +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -package org.jetbrains.plugins.groovy.transformations.singleton - -import com.intellij.testFramework.LightProjectDescriptor -import org.jetbrains.plugins.groovy.GroovyBundle -import org.jetbrains.plugins.groovy.GroovyProjectDescriptors -import org.jetbrains.plugins.groovy.LightGroovyTestCase -import org.jetbrains.plugins.groovy.codeInspection.bugs.NewInstanceOfSingletonInspection - -class SingletonNewInstanceInspectionTest extends LightGroovyTestCase { - - final LightProjectDescriptor projectDescriptor = GroovyProjectDescriptors.GROOVY_LATEST - - void setUp() { - super.setUp() - fixture.enableInspections NewInstanceOfSingletonInspection - } - - private void doTest(String before, String after) { - fixture.with { - configureByText '_.groovy', before - def action = findSingleIntention(GroovyBundle.message("replace.new.expression.with.instance.access")) - assert action - launchAction action - checkResult after - } - } - - void 'test fix simple'() { - doTest '''\ -@Singleton -class A {} - -new A() -''', '''\ -@Singleton -class A {} - -A.instance -''' - } - - void 'test fix custom property name'() { - doTest '''\ -@Singleton(property = "coolInstance") -class A {} - -new A() -''', '''\ -@Singleton(property = "coolInstance") -class A {} - -A.coolInstance -''' - } -} diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonNewInstanceInspectionTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonNewInstanceInspectionTest.java new file mode 100644 index 000000000000..9b2aeb3680c6 --- /dev/null +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonNewInstanceInspectionTest.java @@ -0,0 +1,61 @@ +// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package org.jetbrains.plugins.groovy.transformations.singleton; + +import com.intellij.codeInsight.intention.IntentionAction; +import com.intellij.testFramework.LightProjectDescriptor; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.plugins.groovy.GroovyBundle; +import org.jetbrains.plugins.groovy.GroovyProjectDescriptors; +import org.jetbrains.plugins.groovy.LightGroovyTestCase; +import org.jetbrains.plugins.groovy.codeInspection.bugs.NewInstanceOfSingletonInspection; +import org.junit.Assert; + +public class SingletonNewInstanceInspectionTest extends LightGroovyTestCase { + @Override + @NotNull + public final LightProjectDescriptor getProjectDescriptor() { + return GroovyProjectDescriptors.GROOVY_LATEST; + } + + @Override + public void setUp() throws Exception { + super.setUp(); + getFixture().enableInspections(NewInstanceOfSingletonInspection.class); + } + + private void doTest(final String before, final String after) { + myFixture.configureByText("_.groovy", before); + IntentionAction action = myFixture.findSingleIntention(GroovyBundle.message("replace.new.expression.with.instance.access")); + Assert.assertNotNull(action); + myFixture.launchAction(action); + myFixture.checkResult(after); + } + + public void testFixSimple() { + doTest(""" + @Singleton + class A {} + + new A() + """, """ + @Singleton + class A {} + + A.instance + """); + } + + public void testFixCustomPropertyName() { + doTest(""" + @Singleton(property = "coolInstance") + class A {} + + new A() + """, """ + @Singleton(property = "coolInstance") + class A {} + + A.coolInstance + """); + } +} diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonTransformationSupportTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonTransformationSupportTest.groovy deleted file mode 100644 index 3aa6b7eae37f..000000000000 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonTransformationSupportTest.groovy +++ /dev/null @@ -1,122 +0,0 @@ -// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -package org.jetbrains.plugins.groovy.transformations.singleton - -import com.intellij.testFramework.LightProjectDescriptor -import groovy.transform.CompileStatic -import org.jetbrains.plugins.groovy.GroovyProjectDescriptors -import org.jetbrains.plugins.groovy.LightGroovyTestCase -import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.GrUnresolvedAccessInspection -import org.jetbrains.plugins.groovy.lang.psi.impl.GroovyFileImpl - -@CompileStatic -class SingletonTransformationSupportTest extends LightGroovyTestCase { - - final LightProjectDescriptor projectDescriptor = GroovyProjectDescriptors.GROOVY_LATEST - - void 'test highlighting'() { - fixture.with { - def file = addFileToProject('singletons.groovy', '''\ -@Singleton -class Simple {} - -@Singleton(property = "coolInstance") -class CustomName {} - -@Singleton(strict = false) -class DefaultConstructor { - DefaultConstructor() {} -} - -@Singleton(strict = false) -class CustomConstructor { - CustomConstructor(int a, String b) {} -} - -@Singleton(lazy = true) -class Lazy {} - -@Singleton(strict = false, lazy = true) -class LazyDefaultConstructor { - LazyDefaultConstructor() {} -} -''') as GroovyFileImpl - - configureByText '_.groovy', '''\ -Simple.setInstance(null) -Simple simple -simple = Simple.getInstance() -simple = Simple.instance -simple = new Simple() - -CustomName.setCoolInstance(null) -CustomName customName -customName = CustomName.getCoolInstance() -customName = CustomName.coolInstance -customName = new CustomName() - -DefaultConstructor.setInstance(null) -DefaultConstructor defaultConstructor -defaultConstructor = DefaultConstructor.getInstance() -defaultConstructor = DefaultConstructor.instance -defaultConstructor = new DefaultConstructor() - -CustomConstructor customConstructor -customConstructor = CustomConstructor.getInstance() -customConstructor = CustomConstructor.instance -customConstructor = new CustomConstructor() -customConstructor = new CustomConstructor(1, "") - -Lazy lazy -lazy = Lazy.getInstance() -lazy = Lazy.instance -lazy = new Lazy() - -LazyDefaultConstructor lazyDefaultConstructor -lazyDefaultConstructor = LazyDefaultConstructor.getInstance() -lazyDefaultConstructor = LazyDefaultConstructor.instance -lazyDefaultConstructor = new LazyDefaultConstructor() -''' - enableInspections(GrUnresolvedAccessInspection) - checkHighlighting() - - configureByText 'Main.java', '''\ -public class Main { - public static void main(String[] args) { - Simple simple; - simple = Simple.getInstance(); - simple = Simple.instance; - simple = new Simple(); - - CustomName customName; - customName = CustomName.getCoolInstance(); - customName = CustomName.coolInstance; - customName = new CustomName(); - - DefaultConstructor defaultConstructor; - defaultConstructor = DefaultConstructor.getInstance(); - defaultConstructor = DefaultConstructor.instance; - defaultConstructor = new DefaultConstructor(); - - CustomConstructor customConstructor; - customConstructor = CustomConstructor.getInstance(); - customConstructor = CustomConstructor.instance; - customConstructor = new CustomConstructor(); - customConstructor = new CustomConstructor(1, ""); - - Lazy lazy; - lazy = Lazy.getInstance(); - lazy = Lazy.instance; - lazy = new Lazy(); - - LazyDefaultConstructor lazyDefaultConstructor; - lazyDefaultConstructor = LazyDefaultConstructor.getInstance(); - lazyDefaultConstructor = LazyDefaultConstructor.instance; - lazyDefaultConstructor = new LazyDefaultConstructor(); - } -} -''' - checkHighlighting() - assert !file.contentsLoaded - } - } -} diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonTransformationSupportTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonTransformationSupportTest.java new file mode 100644 index 000000000000..efb1c75ed03c --- /dev/null +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/singleton/SingletonTransformationSupportTest.java @@ -0,0 +1,123 @@ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package org.jetbrains.plugins.groovy.transformations.singleton; + +import com.intellij.testFramework.LightProjectDescriptor; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.plugins.groovy.GroovyProjectDescriptors; +import org.jetbrains.plugins.groovy.LightGroovyTestCase; +import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.GrUnresolvedAccessInspection; +import org.jetbrains.plugins.groovy.lang.psi.impl.GroovyFileImpl; +import org.junit.Assert; + +public class SingletonTransformationSupportTest extends LightGroovyTestCase { + @Override + @NotNull + public final LightProjectDescriptor getProjectDescriptor() { + return GroovyProjectDescriptors.GROOVY_LATEST; + } + + public void testHighlighting() { + GroovyFileImpl file = (GroovyFileImpl) myFixture.addFileToProject("singletons.groovy", """ + @Singleton + class Simple {} + + @Singleton(property = "coolInstance") + class CustomName {} + + @Singleton(strict = false) + class DefaultConstructor { + DefaultConstructor() {} + } + + @Singleton(strict = false) + class CustomConstructor { + CustomConstructor(int a, String b) {} + } + + @Singleton(lazy = true) + class Lazy {} + + @Singleton(strict = false, lazy = true) + class LazyDefaultConstructor { + LazyDefaultConstructor() {} + } + """); + + myFixture.configureByText("_.groovy", """ + Simple.setInstance(null) + Simple simple + simple = Simple.getInstance() + simple = Simple.instance + simple = new Simple() + + CustomName.setCoolInstance(null) + CustomName customName + customName = CustomName.getCoolInstance() + customName = CustomName.coolInstance + customName = new CustomName() + + DefaultConstructor.setInstance(null) + DefaultConstructor defaultConstructor + defaultConstructor = DefaultConstructor.getInstance() + defaultConstructor = DefaultConstructor.instance + defaultConstructor = new DefaultConstructor() + + CustomConstructor customConstructor + customConstructor = CustomConstructor.getInstance() + customConstructor = CustomConstructor.instance + customConstructor = new CustomConstructor() + customConstructor = new CustomConstructor(1, "") + + Lazy lazy + lazy = Lazy.getInstance() + lazy = Lazy.instance + lazy = new Lazy() + + LazyDefaultConstructor lazyDefaultConstructor + lazyDefaultConstructor = LazyDefaultConstructor.getInstance() + lazyDefaultConstructor = LazyDefaultConstructor.instance + lazyDefaultConstructor = new LazyDefaultConstructor() + """); + myFixture.enableInspections(GrUnresolvedAccessInspection.class); + myFixture.checkHighlighting(); + + myFixture.configureByText("Main.java", """ + public class Main { + public static void main(String[] args) { + Simple simple; + simple = Simple.getInstance(); + simple = Simple.instance; + simple = new Simple(); + + CustomName customName; + customName = CustomName.getCoolInstance(); + customName = CustomName.coolInstance; + customName = new CustomName(); + + DefaultConstructor defaultConstructor; + defaultConstructor = DefaultConstructor.getInstance(); + defaultConstructor = DefaultConstructor.instance; + defaultConstructor = new DefaultConstructor(); + + CustomConstructor customConstructor; + customConstructor = CustomConstructor.getInstance(); + customConstructor = CustomConstructor.instance; + customConstructor = new CustomConstructor(); + customConstructor = new CustomConstructor(1, ""); + + Lazy lazy; + lazy = Lazy.getInstance(); + lazy = Lazy.instance; + lazy = new Lazy(); + + LazyDefaultConstructor lazyDefaultConstructor; + lazyDefaultConstructor = LazyDefaultConstructor.getInstance(); + lazyDefaultConstructor = LazyDefaultConstructor.instance; + lazyDefaultConstructor = new LazyDefaultConstructor(); + } + } + """); + myFixture.checkHighlighting(); + Assert.assertFalse(file.isContentsLoaded()); + } +}