Skip to content

Commit

Permalink
feat: add Micronaut Guice feature (#2488)
Browse files Browse the repository at this point in the history
* feat: add Micronaut Guice feature

* Delete test-features/src/test/groovy/io/micronaut/starter/core/test/feature/guice/MicronautGuiceSpec.groovy
  • Loading branch information
sdelamo authored May 31, 2024
1 parent c5f3c05 commit c87fe02
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public final class MicronautDependencyUtils {
.compile()
.build();

public static final String GROUP_ID_MICRONAUT_GUICE = "io.micronaut.guice";
public static final String GROUP_ID_MICRONAUT = "io.micronaut";
public static final String GROUP_ID_MICRONAUT_TESTRESOURCES = "io.micronaut.testresources";
public static final String GROUP_ID_MICRONAUT_JAXRS = "io.micronaut.jaxrs";
Expand Down Expand Up @@ -82,6 +83,11 @@ private MicronautDependencyUtils() {

}

@NonNull
public static Dependency.Builder guiceDependency() {
return micronautDependency(GROUP_ID_MICRONAUT_GUICE);
}

@NonNull
public static Dependency.Builder coreDependency() {
return micronautDependency(GROUP_ID_MICRONAUT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Category {
public static final String CONFIGURATION = "Configuration";
public static final String DATABASE = "Database";
public static final String DEV_TOOLS = "Development Tools";
public static final String DI = "Dependency Injection";
public static final String DISTRIBUTED_CONFIG = "Distributed Configuration";
public static final String DOCUMENTATION = "Documentation";
public static final String GROOVY_MODULE = "Groovy Optional Modules";
Expand All @@ -58,4 +59,5 @@ public class Category {
public static final String TRACING = "Distributed Tracing";
public static final String VALIDATION = "Validation";
public static final String VIEW = "View Rendering";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.starter.feature.guice;

import io.micronaut.starter.application.ApplicationType;
import io.micronaut.starter.application.generator.GeneratorContext;
import io.micronaut.starter.build.dependencies.Dependency;
import io.micronaut.starter.build.dependencies.MicronautDependencyUtils;
import io.micronaut.starter.feature.Category;
import io.micronaut.starter.feature.Feature;
import jakarta.inject.Singleton;

@Singleton
public class MicronautGuice implements Feature {
public static final String NAME = "guice";
private static final String ARTIFACT_ID_MICRONAUT_GUICE = "micronaut-guice";
private static final String ARTIFACT_ID_MICRONAUT_GUICE_PROCESSOR = "micronaut-guice-processor";
private static final Dependency MICRONAUT_GUICE_ANNOTATION_PROCESSOR = MicronautDependencyUtils.guiceDependency()
.artifactId(ARTIFACT_ID_MICRONAUT_GUICE_PROCESSOR)
.annotationProcessor()
.build();
private static final Dependency MICRONAUT_GUICE = MicronautDependencyUtils.guiceDependency()
.artifactId(ARTIFACT_ID_MICRONAUT_GUICE)
.compile()
.build();

@Override
public String getName() {
return NAME;
}

@Override
public String getTitle() {
return "Micronaut Guice";
}

@Override
public String getDescription() {
return "Micronaut Guice allows importing Guice modules into a Micronaut application, making the Guice bindings available for Dependency Injection.";
}

@Override
public boolean supports(ApplicationType applicationType) {
return true;
}

@Override
public void apply(GeneratorContext generatorContext) {
addDependencies(generatorContext);
}

private void addDependencies(GeneratorContext generatorContext) {
generatorContext.addDependency(MICRONAUT_GUICE);
generatorContext.addDependency(MICRONAUT_GUICE_ANNOTATION_PROCESSOR);
}

@Override
public String getMicronautDocumentation() {
return "https://micronaut-projects.github.io/micronaut-guice/latest/guide/index.html";
}

@Override
public String getCategory() {
return Category.DI;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package io.micronaut.starter.feature.guice

import io.micronaut.starter.ApplicationContextSpec
import io.micronaut.starter.BuildBuilder
import io.micronaut.starter.application.ApplicationType
import io.micronaut.starter.build.BuildTestUtil
import io.micronaut.starter.build.BuildTestVerifier
import io.micronaut.starter.build.dependencies.Scope
import io.micronaut.starter.feature.Feature
import io.micronaut.starter.fixture.CommandOutputFixture
import io.micronaut.starter.options.BuildTool
import io.micronaut.starter.options.Language

class MicronautGuiceSpec extends ApplicationContextSpec implements CommandOutputFixture {
void 'test readme.md with feature guice contains links to micronaut docs'() {
when:
Map<String, String> output = generate([MicronautGuice.NAME])
String readme = output["README.md"]

then:
readme
readme.contains("https://micronaut-projects.github.io/micronaut-guice/latest/guide/index.html")
}

void 'test #buildTool guice feature for language=#language'(Language language, BuildTool buildTool) {
when:
String template = new BuildBuilder(beanContext, buildTool)
.language(language)
.features([MicronautGuice.NAME])
.render()
BuildTestVerifier verifier = BuildTestUtil.verifier(buildTool, language, template)

then:
verifier.hasDependency("io.micronaut.guice", "micronaut-guice", Scope.COMPILE)
verifier.hasAnnotationProcessor("io.micronaut.guice", "micronaut-guice-processor")

where:
[language, buildTool] << [Language.values().toList(), BuildTool.values()].combinations()
}

void "guice feature is in the dependency injection category"() {
given:
String feature = MicronautGuice.NAME

when:
Optional<Feature> featureOptional = findFeatureByName(feature)

then:
featureOptional.isPresent()

when:
Feature f = featureOptional.get()

then:
f.category == "Dependency Injection"

and: 'supports every application type'
for (ApplicationType applicationType : ApplicationType.values()) {
assert f.supports(applicationType)
}
}
}

0 comments on commit c87fe02

Please sign in to comment.