|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.palantir.javaformat.intellij; |
| 16 | + |
| 17 | +import static java.util.Objects.requireNonNull; |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import com.intellij.application.options.CodeStyle; |
| 21 | +import com.intellij.openapi.command.WriteCommandAction; |
| 22 | +import com.intellij.psi.PsiFile; |
| 23 | +import com.intellij.psi.PsiJavaFile; |
| 24 | +import com.intellij.psi.codeStyle.CodeStyleSettings; |
| 25 | +import com.intellij.psi.codeStyle.JavaCodeStyleManager; |
| 26 | +import com.intellij.psi.codeStyle.JavaCodeStyleSettings; |
| 27 | +import com.intellij.psi.codeStyle.PackageEntry; |
| 28 | +import com.intellij.psi.codeStyle.modifier.CodeStyleSettingsModifier; |
| 29 | +import com.intellij.psi.codeStyle.modifier.TransientCodeStyleSettings; |
| 30 | +import com.intellij.testFramework.fixtures.DefaultLightProjectDescriptor; |
| 31 | +import com.intellij.testFramework.fixtures.IdeaProjectTestFixture; |
| 32 | +import com.intellij.testFramework.fixtures.IdeaTestFixtureFactory; |
| 33 | +import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture; |
| 34 | +import com.intellij.testFramework.fixtures.JavaTestFixtureFactory; |
| 35 | +import com.intellij.testFramework.fixtures.TestFixtureBuilder; |
| 36 | +import com.intellij.testFramework.fixtures.impl.LightTempDirTestFixtureImpl; |
| 37 | +import com.palantir.javaformat.intellij.PalantirJavaFormatSettings.StoredSettings; |
| 38 | +import java.util.List; |
| 39 | +import org.junit.jupiter.api.AfterEach; |
| 40 | +import org.junit.jupiter.api.BeforeEach; |
| 41 | +import org.junit.jupiter.api.Test; |
| 42 | + |
| 43 | +final class PalantirJavaFormatCodeStyleSettingsModifierTest { |
| 44 | + private final PalantirJavaFormatCodeStyleSettingsModifier modifier = |
| 45 | + new PalantirJavaFormatCodeStyleSettingsModifier(); |
| 46 | + |
| 47 | + private JavaCodeInsightTestFixture fixture; |
| 48 | + private PalantirJavaFormatSettings settings; |
| 49 | + |
| 50 | + @BeforeEach |
| 51 | + void setUp() throws Exception { |
| 52 | + TestFixtureBuilder<IdeaProjectTestFixture> projectBuilder = IdeaTestFixtureFactory.getFixtureFactory() |
| 53 | + .createLightFixtureBuilder( |
| 54 | + new DefaultLightProjectDescriptor(), getClass().getName()); |
| 55 | + // Files in the light project's source root, so that the imports between them resolve. |
| 56 | + fixture = JavaTestFixtureFactory.getFixtureFactory() |
| 57 | + .createCodeInsightFixture(projectBuilder.getFixture(), new LightTempDirTestFixtureImpl(true)); |
| 58 | + fixture.setUp(); |
| 59 | + settings = PalantirJavaFormatSettings.getInstance(fixture.getProject()); |
| 60 | + var resetState = new StoredSettings(); |
| 61 | + resetState.setEnabled("true"); |
| 62 | + settings.loadState(resetState); |
| 63 | + } |
| 64 | + |
| 65 | + @AfterEach |
| 66 | + void tearDown() throws Exception { |
| 67 | + fixture.tearDown(); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void java_files_get_the_import_settings_that_open_java_format_writes_by() { |
| 72 | + var transientSettings = transientSettings(fixture.configureByText("A.java", "class A {}")); |
| 73 | + |
| 74 | + assertThat(modifier.modifySettings(transientSettings, fixture.getFile())) |
| 75 | + .isTrue(); |
| 76 | + |
| 77 | + var javaSettings = transientSettings.getCustomSettings(JavaCodeStyleSettings.class); |
| 78 | + assertThat(javaSettings.USE_SINGLE_CLASS_IMPORTS).isTrue(); |
| 79 | + assertThat(javaSettings.CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND).isEqualTo(999); |
| 80 | + assertThat(javaSettings.NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND).isEqualTo(999); |
| 81 | + assertThat(javaSettings.PACKAGES_TO_USE_IMPORT_ON_DEMAND.getEntries()).isEmpty(); |
| 82 | + assertThat(javaSettings.LAYOUT_STATIC_IMPORTS_SEPARATELY).isTrue(); |
| 83 | + assertThat(javaSettings.IMPORT_LAYOUT_TABLE.getEntries()) |
| 84 | + .containsExactly( |
| 85 | + PackageEntry.ALL_OTHER_STATIC_IMPORTS_ENTRY, |
| 86 | + PackageEntry.BLANK_LINE_ENTRY, |
| 87 | + PackageEntry.ALL_OTHER_IMPORTS_ENTRY); |
| 88 | + // The project's own settings stay as they were. |
| 89 | + assertThat(JavaCodeStyleSettings.getInstance(fixture.getProject()).CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND) |
| 90 | + .isEqualTo(5); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void the_settings_stay_as_they_are_with_the_plugin_disabled() { |
| 95 | + settings.setEnabled(false); |
| 96 | + var transientSettings = transientSettings(fixture.configureByText("A.java", "class A {}")); |
| 97 | + |
| 98 | + assertThat(modifier.modifySettings(transientSettings, fixture.getFile())) |
| 99 | + .isFalse(); |
| 100 | + assertThat(modifier.mayOverrideSettingsOf(fixture.getProject())).isFalse(); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void files_of_other_languages_keep_their_settings() { |
| 105 | + var transientSettings = transientSettings(fixture.configureByText("a.txt", "text")); |
| 106 | + |
| 107 | + assertThat(modifier.modifySettings(transientSettings, fixture.getFile())) |
| 108 | + .isFalse(); |
| 109 | + assertThat(modifier.mayOverrideSettingsOf(fixture.getProject())).isTrue(); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void the_disable_link_of_the_code_style_page_switches_the_plugin_off() { |
| 114 | + requireNonNull(modifier.getDisablingFunction(fixture.getProject())) |
| 115 | + .accept(CodeStyle.getSettings(fixture.getProject())); |
| 116 | + |
| 117 | + assertThat(settings.isEnabled()).isFalse(); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void an_import_the_ide_adds_is_a_single_class_import_however_many_come_from_its_package() { |
| 122 | + assertThat(CodeStyleSettingsModifier.EP_NAME.getExtensionList()) |
| 123 | + .hasAtLeastOneElementOfType(PalantirJavaFormatCodeStyleSettingsModifier.class); |
| 124 | + |
| 125 | + assertThat(addFifthImportFromOnePackage()).isEqualTo(""" |
| 126 | + import p.A; |
| 127 | + import p.B; |
| 128 | + import p.C; |
| 129 | + import p.D; |
| 130 | + import p.E; |
| 131 | +
|
| 132 | + class Main {} |
| 133 | + """); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + void with_the_plugin_disabled_the_ide_imports_a_fifth_class_from_a_package_on_demand() { |
| 138 | + settings.setEnabled(false); |
| 139 | + |
| 140 | + assertThat(addFifthImportFromOnePackage()).isEqualTo(""" |
| 141 | + import p.*; |
| 142 | +
|
| 143 | + class Main {} |
| 144 | + """); |
| 145 | + } |
| 146 | + |
| 147 | + /** Adds an import as auto-import, Alt+Enter and completion do, through the IDE's ImportHelper. */ |
| 148 | + private String addFifthImportFromOnePackage() { |
| 149 | + // The test fixture gives every file the same temporary settings, which no modifier sees. |
| 150 | + CodeStyle.dropTemporarySettings(fixture.getProject()); |
| 151 | + for (var name : List.of("A", "B", "C", "D")) { |
| 152 | + fixture.addClass("package p; public class " + name + " {}"); |
| 153 | + } |
| 154 | + var fifth = fixture.addClass("package p; public class E {}"); |
| 155 | + var file = (PsiJavaFile) fixture.configureByText("Main.java", """ |
| 156 | + import p.A; |
| 157 | + import p.B; |
| 158 | + import p.C; |
| 159 | + import p.D; |
| 160 | +
|
| 161 | + class Main {} |
| 162 | + """); |
| 163 | + |
| 164 | + WriteCommandAction.runWriteCommandAction(fixture.getProject(), () -> { |
| 165 | + JavaCodeStyleManager.getInstance(fixture.getProject()).addImport(file, fifth); |
| 166 | + }); |
| 167 | + return file.getText(); |
| 168 | + } |
| 169 | + |
| 170 | + private TransientCodeStyleSettings transientSettings(PsiFile file) { |
| 171 | + CodeStyleSettings projectSettings = CodeStyle.getSettings(fixture.getProject()); |
| 172 | + return new TransientCodeStyleSettings(file.getVirtualFile(), fixture.getProject(), projectSettings); |
| 173 | + } |
| 174 | +} |
0 commit comments