|
| 1 | +package lwm.plugin.intention; |
| 2 | + |
| 3 | +import com.intellij.codeInsight.intention.IntentionAction; |
| 4 | +import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase; |
| 5 | +import java.util.List; |
| 6 | +import java.util.stream.Collectors; |
| 7 | + |
| 8 | +public class AddFinalIntentionTest extends LightJavaCodeInsightFixtureTestCase { |
| 9 | + |
| 10 | + private static final String INTENTION_TEXT = "Add final modifier(添加final修饰)"; |
| 11 | + |
| 12 | + private void doTest(final String before, final String after) { |
| 13 | + assertTrue("Test code must contain <caret>", before.contains("<caret>")); |
| 14 | + myFixture.configureByText("Test.java", before); |
| 15 | + final IntentionAction intention = myFixture.findSingleIntention(INTENTION_TEXT); |
| 16 | + assertNotNull("Intention '" + INTENTION_TEXT + "' not found", intention); |
| 17 | + myFixture.launchAction(intention); |
| 18 | + myFixture.checkResult(after); |
| 19 | + } |
| 20 | + |
| 21 | + private void doTestNotAvailable(final String content) { |
| 22 | + assertTrue("Test code must contain <caret>", content.contains("<caret>")); |
| 23 | + myFixture.configureByText("Test.java", content); |
| 24 | + final List<IntentionAction> intentions = myFixture.getAvailableIntentions(); |
| 25 | + final List<String> intentionTexts = intentions.stream() |
| 26 | + .map(IntentionAction::getText) |
| 27 | + .collect(Collectors.toList()); |
| 28 | + assertFalse("Intention '" + INTENTION_TEXT + "' should not be available. Available: " + intentionTexts, |
| 29 | + intentionTexts.contains(INTENTION_TEXT)); |
| 30 | + } |
| 31 | + |
| 32 | + private void doTestNoChange(final String contentWithCaret) { |
| 33 | + assertTrue("Test code must contain <caret>", contentWithCaret.contains("<caret>")); |
| 34 | + final String contentWithoutCaret = contentWithCaret.replace("<caret>", ""); |
| 35 | + myFixture.configureByText("Test.java", contentWithCaret); |
| 36 | + // Attempt to find the intention. It should be available. |
| 37 | + final IntentionAction intention = myFixture.findSingleIntention(INTENTION_TEXT); |
| 38 | + assertNotNull("Intention '" + INTENTION_TEXT + "' should be available but was not found.", intention); |
| 39 | + myFixture.launchAction(intention); |
| 40 | + myFixture.checkResult(contentWithoutCaret); // Ensure no change happened |
| 41 | + } |
| 42 | + |
| 43 | + public void testAddFinalToSingleParameter() { |
| 44 | + final String before = "class Test {\n" + |
| 45 | + " void method(String pa<caret>ram1, int param2) {}\n" + |
| 46 | + "}"; |
| 47 | + final String after = "class Test {\n" + |
| 48 | + " void method(final String param1, int param2) {}\n" + |
| 49 | + "}"; |
| 50 | + doTest(before, after); |
| 51 | + } |
| 52 | + |
| 53 | + public void testAddFinalToSingleLocalVariable() { |
| 54 | + final String before = "class Test {\n" + |
| 55 | + " void method() {\n" + |
| 56 | + " String l<caret>ocal1 = \"hello\";\n" + |
| 57 | + " int local2 = 10;\n" + |
| 58 | + " }\n" + |
| 59 | + "}"; |
| 60 | + final String after = "class Test {\n" + |
| 61 | + " void method() {\n" + |
| 62 | + " final String local1 = \"hello\";\n" + |
| 63 | + " int local2 = 10;\n" + |
| 64 | + " }\n" + |
| 65 | + "}"; |
| 66 | + doTest(before, after); |
| 67 | + } |
| 68 | + |
| 69 | + public void testAddFinalToAllInMethodScope() { |
| 70 | + // Caret is removed in 'after' string by checkResult implicitly if not present. |
| 71 | + final String before = "class Test {\n" + |
| 72 | + " void method(String param1, int param2) {\n" + |
| 73 | + " <caret>\n" + |
| 74 | + " String local1 = \"hello\";\n" + |
| 75 | + " int local2 = 10;\n" + |
| 76 | + " final String alreadyFinal = \"done\";\n" + |
| 77 | + " }\n" + |
| 78 | + "}"; |
| 79 | + final String after = "class Test {\n" + |
| 80 | + " void method(final String param1, final int param2) {\n" + |
| 81 | + " \n" + |
| 82 | + " final String local1 = \"hello\";\n" + |
| 83 | + " final int local2 = 10;\n" + |
| 84 | + " final String alreadyFinal = \"done\";\n" + |
| 85 | + " }\n" + |
| 86 | + "}"; |
| 87 | + doTest(before, after); |
| 88 | + } |
| 89 | + |
| 90 | + public void testNotAvailableOnAlreadyFinalParameter() { |
| 91 | + final String content = "class Test {\n" + |
| 92 | + " void method(final String pa<caret>ram1) {}\n" + |
| 93 | + "}"; |
| 94 | + // As per current isAvailable & invoke logic, intention is available but makes no change. |
| 95 | + doTestNoChange(content); |
| 96 | + } |
| 97 | + |
| 98 | + public void testNotAvailableOnAlreadyFinalLocalVariable() { |
| 99 | + final String content = "class Test {\n" + |
| 100 | + " void method() {\n" + |
| 101 | + " final String lo<caret>cal1 = \"hello\";\n" + |
| 102 | + " }\n" + |
| 103 | + "}"; |
| 104 | + // As per current isAvailable & invoke logic, intention is available but makes no change. |
| 105 | + doTestNoChange(content); |
| 106 | + } |
| 107 | + |
| 108 | + public void testNotAvailableOnClassDeclaration() { |
| 109 | + final String content = "class Te<caret>st {\n" + |
| 110 | + " void method(String param1) {}\n" + |
| 111 | + "}"; |
| 112 | + doTestNotAvailable(content); |
| 113 | + } |
| 114 | + |
| 115 | + public void testNotAvailableInImportStatement() { |
| 116 | + final String content = "import ja<caret>va.util.List;\n" + |
| 117 | + "class Test {\n" + |
| 118 | + " void method(String param1) {}\n" + |
| 119 | + "}"; |
| 120 | + doTestNotAvailable(content); |
| 121 | + } |
| 122 | + |
| 123 | + public void testNotAvailableOnFieldName() { |
| 124 | + // The current isAvailable will make it available for fields. |
| 125 | + // The invoke method will make the field final. |
| 126 | + final String before = "class Test {\n" + |
| 127 | + " String myFi<caret>eld = \"value\";\n" + |
| 128 | + "}"; |
| 129 | + final String after = "class Test {\n" + |
| 130 | + " final String myField = \"value\";\n" + |
| 131 | + "}"; |
| 132 | + doTest(before, after); |
| 133 | + } |
| 134 | + |
| 135 | + public void testNotAvailableOnAlreadyFinalFieldName() { |
| 136 | + final String content = "class Test {\n" + |
| 137 | + " final String myFi<caret>eld = \"value\";\n" + |
| 138 | + "}"; |
| 139 | + // Intention should be available (based on isAvailable) but do nothing (based on invoke) |
| 140 | + doTestNoChange(content); |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | + @Override |
| 145 | + protected String getTestDataPath() { |
| 146 | + // Not using testData files for these tests, so path can be empty or root. |
| 147 | + return ""; |
| 148 | + } |
| 149 | +} |
0 commit comments