diff --git a/build.gradle b/build.gradle index cf34d9b..94be48f 100644 --- a/build.gradle +++ b/build.gradle @@ -35,7 +35,7 @@ compileTestKotlin { kotlinOptions.jvmTarget = "1.8" } intellij { - version '2019.2.4' // Upgrading to 2019.3.x breaks build/tests due to dependency with assertj-core 13.3.2 + version '2019.3.2' // pluginName 'Concise AssertJ Optimizing Nitpicker (Cajon)' updateSinceUntilBuild false plugins = ['java'] diff --git a/src/test/java/de/platon42/intellij/jupiter/AddMavenDependencyToModule.java b/src/test/java/de/platon42/intellij/jupiter/AddMavenDependencyToModule.java new file mode 100644 index 0000000..d8428cb --- /dev/null +++ b/src/test/java/de/platon42/intellij/jupiter/AddMavenDependencyToModule.java @@ -0,0 +1,24 @@ +package de.platon42.intellij.jupiter; + +import com.intellij.openapi.roots.DependencyScope; + +import java.lang.annotation.*; + +@Target({ElementType.TYPE, ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +@Inherited +@Repeatable(AddMavenDependencyToModule.List.class) +public @interface AddMavenDependencyToModule { + String value(); + + boolean includeTransitiveDependencies = false; + + DependencyScope scope = DependencyScope.COMPILE; + + @Target({ElementType.TYPE, ElementType.METHOD}) + @Retention(RetentionPolicy.RUNTIME) + @Inherited + @interface List { + AddMavenDependencyToModule[] value(); + } +} \ No newline at end of file diff --git a/src/test/java/de/platon42/intellij/jupiter/LightCodeInsightExtension.java b/src/test/java/de/platon42/intellij/jupiter/LightCodeInsightExtension.java index 3b1d614..cba34c9 100644 --- a/src/test/java/de/platon42/intellij/jupiter/LightCodeInsightExtension.java +++ b/src/test/java/de/platon42/intellij/jupiter/LightCodeInsightExtension.java @@ -1,10 +1,17 @@ package de.platon42.intellij.jupiter; +import com.intellij.jarRepository.JarRepositoryManager; +import com.intellij.jarRepository.RemoteRepositoryDescription; import com.intellij.openapi.module.Module; import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.projectRoots.impl.JavaAwareProjectJdkTableImpl; import com.intellij.openapi.roots.ContentEntry; +import com.intellij.openapi.roots.DependencyScope; +import com.intellij.openapi.roots.LibraryOrderEntry; import com.intellij.openapi.roots.ModifiableRootModel; +import com.intellij.openapi.roots.libraries.Library; +import com.intellij.openapi.roots.libraries.LibraryTable; +import com.intellij.openapi.roots.libraries.ui.OrderRoot; import com.intellij.openapi.util.Disposer; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; @@ -13,6 +20,7 @@ import com.intellij.testFramework.fixtures.IdeaTestExecutionPolicy; import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture; import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase; import org.jetbrains.annotations.NotNull; +import org.jetbrains.idea.maven.utils.library.RepositoryLibraryProperties; import org.junit.jupiter.api.extension.*; import org.junit.jupiter.api.extension.ExtensionContext.Namespace; import org.junit.jupiter.api.extension.ExtensionContext.Store; @@ -23,7 +31,11 @@ import java.lang.reflect.Parameter; import java.net.URISyntaxException; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; import java.util.logging.Logger; +import java.util.stream.Collectors; import java.util.stream.Stream; public class LightCodeInsightExtension implements ParameterResolver, AfterTestExecutionCallback, InvocationInterceptor { @@ -150,10 +162,12 @@ public class LightCodeInsightExtension implements ParameterResolver, AfterTestEx @Override public void configureModule(@NotNull Module module, @NotNull ModifiableRootModel model, @NotNull ContentEntry contentEntry) { super.configureModule(module, model, contentEntry); - AddLocalJarToModule methodOrClassAnnotation = getMethodOrClassAnnotation(AddLocalJarToModule.class); - if (methodOrClassAnnotation != null) { - Stream.of(methodOrClassAnnotation.value()).forEach(it -> addJarContaining(model, it)); + AddLocalJarToModule localJars = getMethodOrClassAnnotation(AddLocalJarToModule.class); + if (localJars != null) { + Stream.of(localJars.value()).forEach(it -> addJarContaining(model, it)); } + List mavenDependencies = getMethodOrClassAnnotations(AddMavenDependencyToModule.class); + mavenDependencies.forEach(it -> addFromMaven(model, it.value(), it.includeTransitiveDependencies, it.scope)); } }; } @@ -175,6 +189,33 @@ public class LightCodeInsightExtension implements ParameterResolver, AfterTestEx } } + void addFromMaven(ModifiableRootModel model, String mavenCoordinates, + boolean includeTransitiveDependencies, DependencyScope dependencyScope) { + List remoteRepositoryDescriptions = RemoteRepositoryDescription.DEFAULT_REPOSITORIES; + RepositoryLibraryProperties libraryProperties = new RepositoryLibraryProperties(mavenCoordinates, includeTransitiveDependencies); + Collection roots = + JarRepositoryManager.loadDependenciesModal(model.getProject(), libraryProperties, false, false, null, remoteRepositoryDescriptions); + LibraryTable.ModifiableModel tableModel = model.getModuleLibraryTable().getModifiableModel(); + Library library = tableModel.createLibrary(mavenCoordinates); + Library.ModifiableModel libraryModel = library.getModifiableModel(); + if (roots.isEmpty()) { + throw new IllegalStateException(String.format("No roots for '%s'", mavenCoordinates)); + } + + for (OrderRoot root : roots) { + libraryModel.addRoot(root.getFile(), root.getType()); + } + + LibraryOrderEntry libraryOrderEntry = model.findLibraryOrderEntry(library); + if (libraryOrderEntry == null) { + throw new IllegalStateException("Unable to find registered library " + mavenCoordinates); + } + libraryOrderEntry.setScope(dependencyScope); + + libraryModel.commit(); + tableModel.commit(); + } + @Override protected String getTestDataPath() { TestDataPath testDataPath = getMethodOrClassAnnotation(TestDataPath.class); @@ -199,5 +240,12 @@ public class LightCodeInsightExtension implements ParameterResolver, AfterTestEx } return annotation; } + + private List getMethodOrClassAnnotations(Class clazz) { + return Stream.of(extensionContext.getRequiredTestMethod().getAnnotationsByType(clazz), + extensionContext.getRequiredTestClass().getAnnotationsByType(clazz)) + .flatMap(Arrays::stream) + .collect(Collectors.toList()); + } } } diff --git a/src/test/java/de/platon42/intellij/plugins/cajon/AbstractCajonTest.kt b/src/test/java/de/platon42/intellij/plugins/cajon/AbstractCajonTest.kt index 5401d82..d25561b 100644 --- a/src/test/java/de/platon42/intellij/plugins/cajon/AbstractCajonTest.kt +++ b/src/test/java/de/platon42/intellij/plugins/cajon/AbstractCajonTest.kt @@ -3,11 +3,10 @@ package de.platon42.intellij.plugins.cajon import com.intellij.codeInsight.intention.IntentionAction import com.intellij.pom.java.LanguageLevel import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture -import de.platon42.intellij.jupiter.AddLocalJarToModule +import de.platon42.intellij.jupiter.AddMavenDependencyToModule import de.platon42.intellij.jupiter.LightCodeInsightExtension import de.platon42.intellij.jupiter.TestDataPath import de.platon42.intellij.jupiter.TestJdk -import org.assertj.core.api.Assertions import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.DisplayNameGeneration import org.junit.jupiter.api.DisplayNameGenerator @@ -17,7 +16,8 @@ import java.lang.reflect.Method @ExtendWith(LightCodeInsightExtension::class) @TestDataPath("src/test/resources") @TestJdk(LanguageLevel.JDK_1_8, annotations = true, useInternal = true) -@AddLocalJarToModule(Assertions::class) +//@AddLocalJarToModule(Assertions::class) +@AddMavenDependencyToModule("org.assertj:assertj-core:3.15.0") @DisplayNameGeneration(AbstractCajonTest.CutOffFixtureDisplayNameGenerator::class) abstract class AbstractCajonTest {