From 971861c54c5fd304c0daf78ca27e0731237633ed Mon Sep 17 00:00:00 2001 From: Konstantin Bulenkov Date: Wed, 4 Apr 2012 13:09:08 +0200 Subject: [PATCH] tests for java anonymous classes helper --- ...nymousParameterInAnonymousConstructor.java | 27 ++++++++++ ...ymousParameterInAnonymousConstructor2.java | 27 ++++++++++ .../anonymous/InsideAnonymousMethod.java | 29 ++++++++++ .../java-tests/testData/anonymous/Simple.java | 31 +++++++++++ .../anonymous/SimpleInConstructor.java | 26 +++++++++ .../util/JavaAnonymousClassesHelperTest.java | 53 +++++++++++++++++++ 6 files changed, 193 insertions(+) create mode 100644 java/java-tests/testData/anonymous/AnonymousParameterInAnonymousConstructor.java create mode 100644 java/java-tests/testData/anonymous/AnonymousParameterInAnonymousConstructor2.java create mode 100644 java/java-tests/testData/anonymous/InsideAnonymousMethod.java create mode 100644 java/java-tests/testData/anonymous/Simple.java create mode 100644 java/java-tests/testData/anonymous/SimpleInConstructor.java create mode 100644 java/java-tests/testSrc/com/intellij/ide/util/JavaAnonymousClassesHelperTest.java diff --git a/java/java-tests/testData/anonymous/AnonymousParameterInAnonymousConstructor.java b/java/java-tests/testData/anonymous/AnonymousParameterInAnonymousConstructor.java new file mode 100644 index 000000000000..f2a2729b355f --- /dev/null +++ b/java/java-tests/testData/anonymous/AnonymousParameterInAnonymousConstructor.java @@ -0,0 +1,27 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * 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. + */ +class AnonymousParameterInAnonymousConstructor { + AnonymousParameterInAnonymousConstructor() { + new Base(new Inter() {public void m() {}}){}; + }; +} + +class Base { + Base(Inter inter) {} +} +interface Inter { + void m(); +} \ No newline at end of file diff --git a/java/java-tests/testData/anonymous/AnonymousParameterInAnonymousConstructor2.java b/java/java-tests/testData/anonymous/AnonymousParameterInAnonymousConstructor2.java new file mode 100644 index 000000000000..588249b57a41 --- /dev/null +++ b/java/java-tests/testData/anonymous/AnonymousParameterInAnonymousConstructor2.java @@ -0,0 +1,27 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * 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. + */ +class AnonymousParameterInAnonymousConstructor2 { + AnonymousParameterInAnonymousConstructor2() { + new Base(new Inter() {public void m() {}}){}; + }; +} + +class Base { + Base(Inter inter) {} +} +interface Inter { + void m(); +} \ No newline at end of file diff --git a/java/java-tests/testData/anonymous/InsideAnonymousMethod.java b/java/java-tests/testData/anonymous/InsideAnonymousMethod.java new file mode 100644 index 000000000000..5ebe9ff8b0da --- /dev/null +++ b/java/java-tests/testData/anonymous/InsideAnonymousMethod.java @@ -0,0 +1,29 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * 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. + */ +class InsideAnonymousMethod { + InsideAnonymousMethod() { + new Object(){ + void my() { + Base b = new Base(){}; + } + }; + } +} + +class Base {} +interface Inter { + void m(); +} \ No newline at end of file diff --git a/java/java-tests/testData/anonymous/Simple.java b/java/java-tests/testData/anonymous/Simple.java new file mode 100644 index 000000000000..cbbf87f96555 --- /dev/null +++ b/java/java-tests/testData/anonymous/Simple.java @@ -0,0 +1,31 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * 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. + */ +import java.lang.Object; + +class Simple { + Object o1 = new Object(){}; + Object o2 = new Object(){{new Object(){};}}; + + Simple() { + new Object(){}; + new Object(){}; + new Object(){}; + new Object(){{new Object(){};}}; + new Object(){}; + new Object(){}; + new Object(){}; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/anonymous/SimpleInConstructor.java b/java/java-tests/testData/anonymous/SimpleInConstructor.java new file mode 100644 index 000000000000..3d9490a5d469 --- /dev/null +++ b/java/java-tests/testData/anonymous/SimpleInConstructor.java @@ -0,0 +1,26 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * 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. + */ +class SimpleInConstructor { + SimpleInConstructor() { + new Object(){}; + new Base(){}; + } +} + +class Base {} +interface Inter { + void m(); +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/ide/util/JavaAnonymousClassesHelperTest.java b/java/java-tests/testSrc/com/intellij/ide/util/JavaAnonymousClassesHelperTest.java new file mode 100644 index 000000000000..350915d400cb --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/ide/util/JavaAnonymousClassesHelperTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * 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 com.intellij.ide.util; + +import com.intellij.JavaTestUtil; +import com.intellij.psi.PsiAnonymousClass; +import com.intellij.psi.PsiElement; +import com.intellij.psi.util.PsiUtilBase; +import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase; + +/** + * @author Konstantin Bulenkov + */ +public class JavaAnonymousClassesHelperTest extends LightCodeInsightFixtureTestCase { + @Override + public void setUp() throws Exception { + super.setUp(); + myFixture.configureByFile(getTestName(false) + ".java"); + } + + public void testSimple() throws Exception {doTest(7);} + public void testSimpleInConstructor() throws Exception {doTest(2);} + public void testInsideAnonymousMethod() throws Exception {doTest(1);} + public void testAnonymousParameterInAnonymousConstructor() throws Exception {doTest(1);} + public void testAnonymousParameterInAnonymousConstructor2() throws Exception {doTest(2);} + + @SuppressWarnings("ConstantConditions") + private void doTest(int num) { + final PsiElement element = PsiUtilBase.getElementAtCaret(myFixture.getEditor()).getParent().getParent(); + + assert element instanceof PsiAnonymousClass : "There should be anonymous class at caret but " + element + " found"; + + assertEquals("$" + num, JavaAnonymousClassesHelper.getName((PsiAnonymousClass)element)); + } + + @Override + protected String getBasePath() { + return JavaTestUtil.getRelativeJavaTestDataPath() + "/anonymous/"; + } +}