Skip to content

Commit

Permalink
chore: Support aspectj-maven-plugin using AspectJ idea plugin #7
Browse files Browse the repository at this point in the history
-tests
  • Loading branch information
gmyasoedov committed Apr 9, 2024
1 parent 0d3d498 commit 7dc2fc1
Show file tree
Hide file tree
Showing 3 changed files with 378 additions and 126 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ru.rzn.gmyasoedov.gmaven

import com.intellij.externalSystem.JavaModuleData
import com.intellij.openapi.application.ReadAction
import com.intellij.openapi.externalSystem.service.project.ProjectDataManager
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil
import com.intellij.openapi.module.Module
import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.roots.ContentEntry
Expand All @@ -9,22 +12,50 @@ import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.util.text.StringUtil
import com.intellij.openapi.vfs.VfsUtilCore
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.pom.java.LanguageLevel
import org.intellij.lang.annotations.Language
import org.jetbrains.jps.model.java.JavaResourceRootType
import org.jetbrains.jps.model.java.JavaSourceRootType
import ru.rzn.gmyasoedov.gmaven.project.externalSystem.model.MainJavaCompilerData
import ru.rzn.gmyasoedov.gmaven.settings.MavenProjectSettings
import ru.rzn.gmyasoedov.gmaven.settings.MavenSettings
import ru.rzn.gmyasoedov.gmaven.wizard.GOpenProjectProvider
import ru.rzn.gmyasoedov.gmaven.wizard.createMavenProjectSettings
import java.util.*

abstract class MavenImportingTestCase : MavenTestCase() {
protected var mavenSettings: MavenSettings? = null
protected var mavenProjectSettings: MavenProjectSettings? = null

protected fun getProjectSettings() = mavenProjectSettings!!

protected fun getExternalProjectPath() = mavenProjectSettings!!.externalProjectPath

protected fun getLanguageLevel(): LanguageLevel {
return ProjectDataManager.getInstance().getExternalProjectData(project,
GMavenConstants.SYSTEM_ID, getExternalProjectPath())
?.externalProjectStructure
?.let { ExternalSystemApiUtil.findAllRecursively(it, JavaModuleData.KEY) }
?.map { it.data }
?.firstOrNull()
?.languageLevel!!
}

protected fun getMainJavaCompilerData(): MainJavaCompilerData {
return ProjectDataManager.getInstance().getExternalProjectData(project,
GMavenConstants.SYSTEM_ID, getExternalProjectPath())
?.externalProjectStructure
?.let { ExternalSystemApiUtil.findAllRecursively(it, MainJavaCompilerData.KEY) }
?.map { it.data }
?.first()!!
}

protected fun import(projectFile: VirtualFile) {
val mavenSettings = MavenSettings.getInstance(project)
mavenSettings.storeProjectFilesExternally = true
val mavenProjectSettings = createMavenProjectSettings(projectFile, project)
mavenSettings = MavenSettings.getInstance(project)
mavenSettings!!.storeProjectFilesExternally = true
mavenProjectSettings = createMavenProjectSettings(projectFile, project)

GOpenProjectProvider().attachProjectAndRefresh(mavenProjectSettings, project)
GOpenProjectProvider().attachProjectAndRefresh(mavenProjectSettings!!, project)
}

protected fun import(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package ru.rzn.gmyasoedov.gmaven.project

import com.intellij.compiler.CompilerConfiguration
import com.intellij.compiler.CompilerConfigurationImpl
import com.intellij.pom.java.LanguageLevel
import junit.framework.TestCase
import ru.rzn.gmyasoedov.gmaven.MavenImportingTestCase

class AspectJMavenPluginTest : MavenImportingTestCase() {

fun testBaseArgs() {
val languageLevel = LanguageLevel.HIGHEST.previewLevel ?: return
val languageString = languageLevel.toJavaVersion().toFeatureString()
val aspectJVersion = "1.14"
import(
"""
<groupId>org.example</groupId>
<artifactId>project</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>dev.aspectj</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>${aspectJVersion}</version>
<executions>
<execution>
<phase>process-sources</phase>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>1.8</source>
<target>1.8</target>
<complianceLevel>${languageString}</complianceLevel>
<aspectDirectory>src</aspectDirectory>
<XnotReweavable>true</XnotReweavable>
<Xlint>ignore</Xlint>
<deprecation>true</deprecation>
<enablePreview>true</enablePreview>
</configuration>
</plugin>
</plugins>
</build>
"""
)

assertModules("project")
val compilerConfiguration = CompilerConfiguration.getInstance(project) as CompilerConfigurationImpl
assertUnorderedElementsAreEqual(
compilerConfiguration.getAdditionalOptions(getModule("project")),
"--enable-preview"
)
val mainJavaCompilerData = getMainJavaCompilerData()
TestCase.assertTrue(mainJavaCompilerData.dependenciesPath.isNotEmpty())
TestCase.assertTrue(mainJavaCompilerData.dependenciesPath.first().contains("aspectjtools-1.9.7.jar"))
assertUnorderedElementsAreEqual(
listOf("-Xlint:ignore", "-XnotReweavable", "-deprecation", "-enablePreview"),
mainJavaCompilerData.arguments
)
TestCase.assertTrue(getLanguageLevel().isPreview)
}

fun testLanguageLevelFromSource() {
val aspectJVersion = "1.14"
import(
"""
<groupId>org.example</groupId>
<artifactId>project</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>dev.aspectj</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>${aspectJVersion}</version>
<executions>
<execution>
<phase>process-sources</phase>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
"""
)

assertModules("project")
val level = getLanguageLevel()
TestCase.assertEquals(LanguageLevel.JDK_1_8, level)
}

fun testLanguageLevelFromReleasePriority() {
val aspectJVersion = "1.14"
import(
"""
<groupId>org.example</groupId>
<artifactId>project</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>dev.aspectj</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>${aspectJVersion}</version>
<executions>
<execution>
<phase>process-sources</phase>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>1.8</source>
<target>1.8</target>
<release>11</release>
</configuration>
</plugin>
</plugins>
</build>
"""
)

assertModules("project")
val level = getLanguageLevel()
TestCase.assertEquals(LanguageLevel.JDK_11, level)
}

fun testCustomAspectJar() {
val aspectJVersion = "1.14"
import(
"""
<groupId>org.example</groupId>
<artifactId>project</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>dev.aspectj</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>${aspectJVersion}</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.9.22</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>process-sources</phase>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<complianceLevel>17</complianceLevel>
</configuration>
</plugin>
</plugins>
</build>
"""
)

assertModules("project")
val level = getLanguageLevel()
TestCase.assertEquals(LanguageLevel.JDK_17, level)
val mainJavaCompilerData = getMainJavaCompilerData()
TestCase.assertTrue(mainJavaCompilerData.dependenciesPath.isNotEmpty())
TestCase.assertTrue(mainJavaCompilerData.dependenciesPath.first().contains("aspectjtools-1.9.22.jar"))
}
}
Loading

0 comments on commit 7dc2fc1

Please sign in to comment.