-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Micronaut Guice feature (#2488)
* feat: add Micronaut Guice feature * Delete test-features/src/test/groovy/io/micronaut/starter/core/test/feature/guice/MicronautGuiceSpec.groovy
- Loading branch information
Showing
4 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
starter-core/src/main/java/io/micronaut/starter/feature/guice/MicronautGuice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
starter-core/src/test/groovy/io/micronaut/starter/feature/guice/MicronautGuiceSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |