Skip to content

Commit

Permalink
environment variable implementation now compiles and runs against req…
Browse files Browse the repository at this point in the history
…uired classpath
  • Loading branch information
sigpwned committed Jan 7, 2025
1 parent a36dfa9 commit 550369e
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 56 deletions.
12 changes: 7 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,11 @@
jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED
</argLine>
<systemPropertyVariables>
<dagger.version>${dagger.version}</dagger.version>
<javax.inject.version>${javax.inject.version}</javax.inject.version>
<jakarta.inject-api.version>${jakarta.inject-api.version}</jakarta.inject-api.version>
<jsr305.version>${jsr305.version}</jsr305.version>
<maven.dagger.version>${dagger.version}</maven.dagger.version>
<maven.javax.inject.version>${javax.inject.version}</maven.javax.inject.version>
<maven.jakarta.inject-api.version>${jakarta.inject-api.version}</maven.jakarta.inject-api.version>
<maven.jsr305.version>${jsr305.version}</maven.jsr305.version>
<maven.project.basedir>${project.basedir}</maven.project.basedir>
</systemPropertyVariables>
</configuration>
</plugin>
Expand Down Expand Up @@ -250,7 +251,8 @@

<modules>
<module>rapier-core</module>
<module>rapier-processor-environment-variable</module>
<module>rapier-environment-variable</module>
<module>rapier-environment-variable-compiler</module>
<module>rapier-processor-system-property</module>
<module>rapier-processor-aws-ssm</module>
<module>rapier-processor-cli</module>
Expand Down
78 changes: 63 additions & 15 deletions rapier-core/src/test/java/rapier/core/DaggerTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
*/
package rapier.core;

import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.joining;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
Expand Down Expand Up @@ -96,13 +98,23 @@ protected String compileAndRunSourceCode(List<String> compilationUnitSourceCodes
return compileAndRunSourceCode(compilationUnitSourceCodes, DEFAULT_ANNOTATION_PROCESSORS);
}


/**
* Compiles a given source code, runs the main method and returns the output from stdout.
*/
protected String compileAndRunSourceCode(List<String> compilationUnitSourceCodes,
List<String> annotationProcessors) throws IOException {
try (TempDir tempDir = TempDir.createTempDirectory("dagger_test")) {
String errors = compileSourceCode(tempDir, compilationUnitSourceCodes, annotationProcessors);
return compileAndRunSourceCode(compilationUnitSourceCodes, annotationProcessors, emptyList());
}

/**
* Compiles a given source code, runs the main method and returns the output from stdout.
*/
protected String compileAndRunSourceCode(List<String> compilationUnitSourceCodes,
List<String> annotationProcessors, List<URL> additionalClasspathEntries) throws IOException {
try (TempDir tempDir = TempDir.createTempDirectory("rapier_test")) {
String errors = compileSourceCode(tempDir, compilationUnitSourceCodes, annotationProcessors,
additionalClasspathEntries);
if (!errors.isBlank()) {
throw new IllegalArgumentException(
"Compilation of invalid compilation units failed with errors: " + errors);
Expand All @@ -127,6 +139,8 @@ protected String compileAndRunSourceCode(List<String> compilationUnitSourceCodes
for (File simulationClasspathEntry : simulationClasspath())
classpath.add(simulationClasspathEntry.toURI().toURL());
classpath.add(tempDir.toURI().toURL());
if (additionalClasspathEntries != null)
classpath.addAll(additionalClasspathEntries);
try (URLClassLoader classLoader = new URLClassLoader(classpath.toArray(URL[]::new),
ClassLoader.getPlatformClassLoader())) {

Expand Down Expand Up @@ -198,6 +212,21 @@ private String compileSourceCode(File tempDir, List<String> compilationUnitSourc
*/
private String compileSourceCode(File tempDir, List<String> compilationUnitSourceCodes,
List<String> annotationProcessors) throws IOException {
return compileSourceCode(tempDir, compilationUnitSourceCodes, annotationProcessors,
emptyList());
}

/**
* Compiles a given source code string and returns any compilation errors.
*
* @param tempDir the temporary directory to store the generated source files
* @param compilationUnitSourceCodes the source code strings to compile
* @return any compilation errors
*
* @throws IOException if an I/O error occurs
*/
private String compileSourceCode(File tempDir, List<String> compilationUnitSourceCodes,
List<String> annotationProcessors, List<URL> additionalClasspathEntries) throws IOException {
// Get the system Java compiler
final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
Expand Down Expand Up @@ -226,8 +255,12 @@ private String compileSourceCode(File tempDir, List<String> compilationUnitSourc
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();

// Set up our classpath
final String classpath = simulationClasspath().stream().map(File::getAbsolutePath)
.collect(joining(File.pathSeparator));
final List<File> classpathEntries = new ArrayList<>();
classpathEntries.addAll(simulationClasspath());
for (URL additionalClasspathEntry : additionalClasspathEntries)
classpathEntries.add(new File(additionalClasspathEntry.getFile()));
final String classpath =
classpathEntries.stream().map(File::getAbsolutePath).collect(joining(File.pathSeparator));

// Configure annotation processors (include Dagger's processor)
List<String> options = List.of("-cp", classpath, "-processor",
Expand Down Expand Up @@ -255,47 +288,62 @@ private String compileSourceCode(File tempDir, List<String> compilationUnitSourc
}
}

/**
* The root directory of the current Maven module
*/
private static final File MAVEN_PROJECT_BASEDIR =
Optional.ofNullable(System.getProperty("maven.project.basedir")).map(File::new).orElseThrow(
() -> new IllegalStateException("maven.project.basedir system property not set"));

protected File resolveProjectFile(String path) throws FileNotFoundException {
final File result = new File(MAVEN_PROJECT_BASEDIR, path);
if (!result.exists())
throw new FileNotFoundException(result.toString());
return result;
}

/**
* The Dagger version to use for compiling test code. This should be passed by
* maven-surefire-plugin using the exact dagger version from the POM. See the root POM for the
* specific details of the setup.
*/
private static final String DAGGER_VERSION =
Optional.ofNullable(System.getProperty("dagger.version"))
.orElseThrow(() -> new IllegalStateException("dagger.version system property not set"));
Optional.ofNullable(System.getProperty("maven.dagger.version")).orElseThrow(
() -> new IllegalStateException("maven.dagger.version system property not set"));

/**
* The javax.inject version to use for compiling test code. This should be passed by
* maven-surefire-plugin using the exact javax.inject version from the POM. See the root POM for
* the specific details of the setup.
*/
private static final String JAVAX_INJECT_VERSION =
Optional.ofNullable(System.getProperty("javax.inject.version")).orElseThrow(
() -> new IllegalStateException("javax.inject.version system property not set"));
Optional.ofNullable(System.getProperty("maven.javax.inject.version")).orElseThrow(
() -> new IllegalStateException("maven.javax.inject.version system property not set"));

/**
* The Jakarta Inject API version to use for compiling test code. This should be passed by
* maven-surefire-plugin using the exact Jakarta Inject API version from the POM. See the root POM
* for the specific details
*/
private static final String JAKARTA_INJECT_API_VERSION =
Optional.ofNullable(System.getProperty("jakarta.inject-api.version")).orElseThrow(
() -> new IllegalStateException("jakarta.inject-api.version system property not set"));
Optional.ofNullable(System.getProperty("maven.jakarta.inject-api.version"))
.orElseThrow(() -> new IllegalStateException(
"maven.jakarta.inject-api.version system property not set"));

private static final String JSR_305_VERSION =
Optional.ofNullable(System.getProperty("jsr305.version")).orElseThrow(
() -> new IllegalStateException("jsr305.version system property not set"));
Optional.ofNullable(System.getProperty("maven.jsr305.version")).orElseThrow(
() -> new IllegalStateException("maven.jsr305.version system property not set"));


private List<File> simulationClasspath() {
private List<File> simulationClasspath() throws FileNotFoundException {
final File daggerJar =
Maven.findJarInLocalRepository("com.google.dagger", "dagger", DAGGER_VERSION);
final File javaxInjectJar =
Maven.findJarInLocalRepository("javax.inject", "javax.inject", JAVAX_INJECT_VERSION);
final File jakartaInjectApiJar = Maven.findJarInLocalRepository("jakarta.inject",
"jakarta.inject-api", JAKARTA_INJECT_API_VERSION);
final File jsr305Jar = Maven.findJarInLocalRepository("com.google.code.findbugs",
"jsr305", JSR_305_VERSION);
final File jsr305Jar =
Maven.findJarInLocalRepository("com.google.code.findbugs", "jsr305", JSR_305_VERSION);
return List.of(daggerJar, javaxInjectJar, jakartaInjectApiJar, jsr305Jar);
}
}
7 changes: 5 additions & 2 deletions rapier-core/src/test/java/rapier/core/util/Maven.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rapier.core.util;

import java.io.File;
import java.io.FileNotFoundException;

public final class Maven {
private Maven() {}
Expand All @@ -17,8 +18,10 @@ private Maven() {}
* @param artifactId The artifact ID of the artifact
* @param version The version of the artifact
* @return An Optional containing the JAR file if it exists, or an empty Optional otherwise
* @throws FileNotFoundException if the JAR file does not exist
*/
public static File findJarInLocalRepository(String groupId, String artifactId, String version) {
public static File findJarInLocalRepository(String groupId, String artifactId, String version)
throws FileNotFoundException {
// Convert groupId to directory path (e.g., org.apache.maven -> org/apache/maven)
final String groupPath = groupId.replace('.', '/');

Expand All @@ -30,7 +33,7 @@ public static File findJarInLocalRepository(String groupId, String artifactId, S
final File jarFile = new File(jarPath);

if (!jarFile.exists())
throw new IllegalArgumentException("JAR file does not exist: " + jarFile);
throw new FileNotFoundException(jarFile.getAbsolutePath());

return jarFile;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<version>0.0.0-b0-SNAPSHOT</version>
</parent>

<artifactId>rapier-processor-environment-variable</artifactId>
<name>rapier-processor-environment-variable</name>
<artifactId>rapier-environment-variable-compiler</artifactId>
<name>rapier-environment-variable-compiler</name>
<packaging>jar</packaging>

<dependencies>
Expand All @@ -17,6 +17,11 @@
<artifactId>rapier-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.sigpwned</groupId>
<artifactId>rapier-environment-variable</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.google.dagger</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* limitations under the License.
* ==================================LICENSE_END===================================
*/
package rapier.processor.envvar;
package rapier.envvar.compiler;

import static java.util.Collections.unmodifiableSet;
import static java.util.stream.Collectors.groupingBy;
Expand Down Expand Up @@ -64,10 +64,8 @@
import rapier.core.util.CaseFormat;
import rapier.core.util.Java;
import rapier.core.util.MoreSets;
import rapier.processor.envvar.model.ParameterKey;
import rapier.processor.envvar.model.ParameterMetadata;
import rapier.processor.envvar.model.RepresentationKey;
import rapier.processor.envvar.util.EnvironmentVariables;
import rapier.envvar.EnvironmentVariable;
import rapier.envvar.compiler.util.EnvironmentVariables;

@SupportedAnnotationTypes("dagger.Component")
@SupportedSourceVersion(SourceVersion.RELEASE_11)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
* limitations under the License.
* ==================================LICENSE_END===================================
*/
package rapier.processor.envvar.model;
package rapier.envvar.compiler;

import static java.util.Objects.requireNonNull;
import java.util.Objects;
import javax.lang.model.element.AnnotationMirror;
import rapier.core.model.DaggerInjectionSite;
import rapier.processor.envvar.EnvironmentVariable;
import rapier.processor.envvar.util.EnvironmentVariables;
import rapier.envvar.EnvironmentVariable;
import rapier.envvar.compiler.util.EnvironmentVariables;

/**
* A grouping key for the physical parameter provided by the user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* limitations under the License.
* ==================================LICENSE_END===================================
*/
package rapier.processor.envvar.model;
package rapier.envvar.compiler;

import java.util.Objects;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* limitations under the License.
* ==================================LICENSE_END===================================
*/
package rapier.processor.envvar.model;
package rapier.envvar.compiler;

import static java.util.Objects.requireNonNull;
import java.util.Comparator;
Expand All @@ -26,8 +26,8 @@
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.type.TypeMirror;
import rapier.core.model.DaggerInjectionSite;
import rapier.processor.envvar.EnvironmentVariable;
import rapier.processor.envvar.util.EnvironmentVariables;
import rapier.envvar.EnvironmentVariable;
import rapier.envvar.compiler.util.EnvironmentVariables;

/**
* A grouping key for the JSR 330 representation of an environment variable, namely type and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* limitations under the License.
* ==================================LICENSE_END===================================
*/
package rapier.processor.envvar.model;
package rapier.envvar.compiler;

import java.util.Objects;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
* limitations under the License.
* ==================================LICENSE_END===================================
*/
package rapier.processor.envvar.util;
package rapier.envvar.compiler.util;

import java.util.Map;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.util.SimpleAnnotationValueVisitor8;
import rapier.processor.envvar.EnvironmentVariable;
import rapier.envvar.EnvironmentVariable;

public final class EnvironmentVariables {
private EnvironmentVariables() {}
Expand Down
Loading

0 comments on commit 550369e

Please sign in to comment.