|
| 1 | +/* |
| 2 | + * Copyright MapStruct Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | +package org.mapstruct.intellij.expression; |
| 7 | + |
| 8 | +import com.intellij.codeInsight.completion.CompletionType; |
| 9 | +import com.intellij.codeInsight.lookup.LookupElementPresentation; |
| 10 | +import com.intellij.ide.highlighter.JavaFileType; |
| 11 | +import com.intellij.psi.PsiElement; |
| 12 | +import com.intellij.psi.PsiFile; |
| 13 | +import com.intellij.psi.PsiIdentifier; |
| 14 | +import com.intellij.psi.PsiJavaToken; |
| 15 | +import com.intellij.psi.PsiReference; |
| 16 | +import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry; |
| 17 | +import org.intellij.lang.annotations.Language; |
| 18 | +import org.mapstruct.intellij.MapstructBaseCompletionTestCase; |
| 19 | + |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author Filip Hrisafov |
| 24 | + */ |
| 25 | +public class JavaExpressionInjectionTest extends MapstructBaseCompletionTestCase { |
| 26 | + |
| 27 | + private static final String CAR_MAPPER = "import java.util.List;\n" + |
| 28 | + "\n" + |
| 29 | + "import org.mapstruct.Mapper;\n" + |
| 30 | + "import org.mapstruct.Mapping;\n" + |
| 31 | + "import org.example.dto.CarDto;\n" + |
| 32 | + "import org.example.dto.Car;\n" + |
| 33 | + "\n" + |
| 34 | + "@Mapper(%s)\n" + |
| 35 | + "public interface CarMapper {\n" + |
| 36 | + "\n" + |
| 37 | + " %s" + |
| 38 | + " CarDto carToCarDto(Car car);\n" + |
| 39 | + "}"; |
| 40 | + |
| 41 | + @Override |
| 42 | + protected String getTestDataPath() { |
| 43 | + return "testData/expression"; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + protected void setUp() throws Exception { |
| 48 | + super.setUp(); |
| 49 | + |
| 50 | + addDirectoryToProject( "dto" ); |
| 51 | + } |
| 52 | + |
| 53 | + public void testExpressionWithNoTargetDefinedMapper() { |
| 54 | + noTargetDefinedMapper( "expression" ); |
| 55 | + noTargetDefinedMapper( "defaultExpression" ); |
| 56 | + } |
| 57 | + |
| 58 | + protected void noTargetDefinedMapper(String attribute) { |
| 59 | + String mapping = "@Mapping(target = \"\", " + attribute + " = \"java(car.<caret>)\")\n"; |
| 60 | + @Language("java") |
| 61 | + String mapper = String.format( CAR_MAPPER, "", mapping ); |
| 62 | + PsiFile file = configureMapperByText( mapper ); |
| 63 | + |
| 64 | + assertThat( myFixture.completeBasic() ) |
| 65 | + .extracting( LookupElementPresentation::renderElement ) |
| 66 | + .extracting( LookupElementPresentation::getItemText ) |
| 67 | + .isEmpty(); |
| 68 | + |
| 69 | + PsiElement elementAt = file.findElementAt( myFixture.getCaretOffset() ); |
| 70 | + assertThat( elementAt ) |
| 71 | + .isNotNull() |
| 72 | + .isInstanceOf( PsiJavaToken.class ); |
| 73 | + assertThat( elementAt.getText() ).isEqualTo( "\"java(car.)\"" ); |
| 74 | + } |
| 75 | + |
| 76 | + public void testExpressionWithoutJavaExpression() { |
| 77 | + withoutJavaExpresion( "expression" ); |
| 78 | + withoutJavaExpresion( "defaultExpression" ); |
| 79 | + } |
| 80 | + |
| 81 | + protected void withoutJavaExpresion(String attribute) { |
| 82 | + String mapping = "@Mapping(target = \"manufacturingYear\", " + attribute + " = \"car<caret>\")\n"; |
| 83 | + @Language("java") |
| 84 | + String mapper = String.format( CAR_MAPPER, "", mapping ); |
| 85 | + PsiFile file = configureMapperByText( mapper ); |
| 86 | + |
| 87 | + PsiElement elementAt = file.findElementAt( myFixture.getCaretOffset() ); |
| 88 | + assertThat( elementAt ) |
| 89 | + .isNotNull() |
| 90 | + .isInstanceOf( PsiJavaToken.class ); |
| 91 | + assertThat( elementAt.getText() ).isEqualTo( "\"car\"" ); |
| 92 | + } |
| 93 | + |
| 94 | + public void testExpressionWithTargetDefinedMapper() { |
| 95 | + withTargetDefinedMapper( "expression" ); |
| 96 | + withTargetDefinedMapper( "defaultExpression" ); |
| 97 | + } |
| 98 | + |
| 99 | + protected void withTargetDefinedMapper(String attribute) { |
| 100 | + String mapping = "@Mapping(target = \"manufacturingYear\", " + attribute + " = \"java(car.<caret>)\")\n"; |
| 101 | + @Language("java") |
| 102 | + String mapper = String.format( CAR_MAPPER, "", mapping ); |
| 103 | + PsiFile file = configureMapperByText( mapper ); |
| 104 | + |
| 105 | + assertThat( myFixture.completeBasic() ) |
| 106 | + .extracting( LookupElementPresentation::renderElement ) |
| 107 | + .extracting( LookupElementPresentation::getItemText ) |
| 108 | + .contains( |
| 109 | + "getMake", |
| 110 | + "setMake", |
| 111 | + "getManufacturingDate", |
| 112 | + "setManufacturingDate", |
| 113 | + "getNumberOfSeats", |
| 114 | + "setNumberOfSeats" |
| 115 | + ); |
| 116 | + |
| 117 | + assertThat( myFixture.complete( CompletionType.SMART ) ) |
| 118 | + .extracting( LookupElementPresentation::renderElement ) |
| 119 | + .extracting( LookupElementPresentation::getItemText ) |
| 120 | + .containsExactlyInAnyOrder( "getMake", "toString" ); |
| 121 | + |
| 122 | + PsiElement elementAt = file.findElementAt( myFixture.getCaretOffset() ); |
| 123 | + assertThat( elementAt ) |
| 124 | + .isNotNull() |
| 125 | + .isInstanceOf( PsiJavaToken.class ); |
| 126 | + assertThat( elementAt.getText() ).isEqualTo( ";" ); |
| 127 | + } |
| 128 | + |
| 129 | + public void testExpressionWithMapperWithImports() { |
| 130 | + withMapperWithImports( "expression" ); |
| 131 | + withMapperWithImports( "defaultExpression" ); |
| 132 | + } |
| 133 | + |
| 134 | + protected void withMapperWithImports(String attribute) { |
| 135 | + String mapping = "@Mapping(target = \"manufacturingYear\", " + attribute + " = \"java(Collections<caret>)\")\n"; |
| 136 | + @Language("java") |
| 137 | + String mapper = String.format( CAR_MAPPER, "imports = Collections.class", mapping ); |
| 138 | + PsiFile file = configureMapperByText( mapper ); |
| 139 | + |
| 140 | + assertThat( myFixture.completeBasic() ) |
| 141 | + .extracting( LookupElementPresentation::renderElement ) |
| 142 | + .extracting( LookupElementPresentation::getItemText ) |
| 143 | + .contains( |
| 144 | + "Collections" |
| 145 | + ); |
| 146 | + |
| 147 | + PsiElement elementAt = file.findElementAt( myFixture.getCaretOffset() - 1 ); |
| 148 | + assertThat( elementAt ) |
| 149 | + .isNotNull() |
| 150 | + .isInstanceOf( PsiIdentifier.class ); |
| 151 | + assertThat( elementAt.getText() ).isEqualTo( "Collections" ); |
| 152 | + |
| 153 | + PsiReference[] references = ReferenceProvidersRegistry.getReferencesFromProviders( elementAt ); |
| 154 | + assertThat( references ).isEmpty(); |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + public void testExpressionWithMapperWithoutImports() { |
| 159 | + withMapperWithoutImports( "expression" ); |
| 160 | + withMapperWithoutImports( "defaultExpression" ); |
| 161 | + } |
| 162 | + |
| 163 | + protected void withMapperWithoutImports(String attribute) { |
| 164 | + String mapping = "@Mapping(target = \"manufacturingYear\", " + attribute + " = \"java(Collections<caret>)\")\n"; |
| 165 | + @Language("java") |
| 166 | + String mapper = String.format( CAR_MAPPER, "", mapping ); |
| 167 | + PsiFile file = configureMapperByText( mapper ); |
| 168 | + |
| 169 | + assertThat( myFixture.completeBasic() ) |
| 170 | + .extracting( LookupElementPresentation::renderElement ) |
| 171 | + .extracting( LookupElementPresentation::getItemText ) |
| 172 | + .contains( |
| 173 | + "Collections" |
| 174 | + ); |
| 175 | + |
| 176 | + PsiElement elementAt = file.findElementAt( myFixture.getCaretOffset() - 1 ); |
| 177 | + assertThat( elementAt ) |
| 178 | + .isNotNull() |
| 179 | + .isInstanceOf( PsiIdentifier.class ); |
| 180 | + assertThat( elementAt.getText() ).isEqualTo( "Collections" ); |
| 181 | + |
| 182 | + PsiReference[] references = ReferenceProvidersRegistry.getReferencesFromProviders( elementAt ); |
| 183 | + assertThat( references ).isEmpty(); |
| 184 | + |
| 185 | + } |
| 186 | + |
| 187 | + private PsiFile configureMapperByText(@Language("java") String text) { |
| 188 | + return myFixture.configureByText( JavaFileType.INSTANCE, text ); |
| 189 | + } |
| 190 | +} |
0 commit comments