Created
February 23, 2020 20:10
-
-
Save jvz/c71701a318dc225456bbb8a92627708a to your computer and use it in GitHub Desktop.
Work in progress test suite showing off various ways dependency injection fits together
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
/* | |
* Licensed to the Apache Software Foundation (ASF) under one or more | |
* contributor license agreements. See the NOTICE file distributed with | |
* this work for additional information regarding copyright ownership. | |
* The ASF licenses this file to You 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 | |
* | |
* http://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 org.apache.logging.log4j.plugins.defaults.bean; | |
import org.apache.logging.log4j.plugins.api.Default; | |
import org.apache.logging.log4j.plugins.api.Inject; | |
import org.apache.logging.log4j.plugins.api.Produces; | |
import org.apache.logging.log4j.plugins.api.Provider; | |
import org.apache.logging.log4j.plugins.api.QualifierType; | |
import org.apache.logging.log4j.plugins.test.InjectionJUnitRunner; | |
import org.apache.logging.log4j.plugins.test.WithBeans; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.util.Optional; | |
import static org.junit.Assert.*; | |
@RunWith(InjectionJUnitRunner.class) | |
public class DefaultInjectionManagerTest { | |
@Retention(RetentionPolicy.RUNTIME) | |
@QualifierType | |
public @interface Run { | |
} | |
@Produces | |
public String globalString = "global string value"; | |
@Produces | |
@Run | |
public String testString() { | |
return "test string value"; | |
} | |
@Test | |
public void testParameterInjection(final String unqualified, @Run final String qualified) { | |
assertEquals(globalString, unqualified); | |
assertEquals(testString(), qualified); | |
} | |
public static class FieldInjection { | |
@Inject | |
private String unqualified; | |
@Run | |
private String implicitQualified; | |
@Default | |
private String implicitDefault; | |
} | |
@WithBeans(FieldInjection.class) | |
@Test | |
public void testFieldInjection(final FieldInjection instance) { | |
assertEquals(globalString, instance.unqualified); | |
assertEquals(testString(), instance.implicitQualified); | |
assertEquals(globalString, instance.implicitDefault); | |
} | |
public static class ExplicitConstructorInjection { | |
private final String first; | |
private final String second; | |
@Inject | |
public ExplicitConstructorInjection(@Default final String first, @Run final String second) { | |
this.first = first; | |
this.second = second; | |
} | |
} | |
@WithBeans(ExplicitConstructorInjection.class) | |
@Test | |
public void testExplicitConstructorInjection(final ExplicitConstructorInjection instance) { | |
assertEquals(globalString, instance.first); | |
assertEquals(testString(), instance.second); | |
} | |
public static class ImplicitConstructorInjection { | |
private final String first; | |
private final String second; | |
public ImplicitConstructorInjection(final String first, @Run final String second) { | |
this.first = first; | |
this.second = second; | |
} | |
} | |
@WithBeans(ImplicitConstructorInjection.class) | |
@Test | |
public void testImplicitConstructorInjection(final ImplicitConstructorInjection instance) { | |
assertEquals(globalString, instance.first); | |
assertEquals(testString(), instance.second); | |
} | |
public static class DefaultConstructorInjection { | |
} | |
@WithBeans(DefaultConstructorInjection.class) | |
@Test | |
public void testNoArgsConstructorInjection(final DefaultConstructorInjection instance) { | |
assertNotNull(instance); | |
} | |
public static class StaticMethodProduction { | |
private final String first; | |
private final String second; | |
private StaticMethodProduction(final String first, final String second) { | |
this.first = first; | |
this.second = second; | |
} | |
@Produces | |
public static StaticMethodProduction produce(final String first, @Run final String second) { | |
return new StaticMethodProduction(first, second); | |
} | |
} | |
@WithBeans(StaticMethodProduction.class) | |
@Test | |
public void testStaticMethodProduction(final StaticMethodProduction instance) { | |
assertEquals(globalString, instance.first); | |
assertEquals(testString(), instance.second); | |
} | |
public static class ProviderProvidedProduction { | |
private final String first; | |
private final String second; | |
private ProviderProvidedProduction(final String first, final String second) { | |
this.first = first; | |
this.second = second; | |
} | |
public static class Builder implements Provider<ProviderProvidedProduction> { | |
private String first; | |
private String second; | |
@Inject | |
public Builder withFirst(final String first) { | |
this.first = first; | |
return this; | |
} | |
@Inject | |
public Builder withSecond(@Run final String second) { | |
this.second = second; | |
return this; | |
} | |
@Override | |
public ProviderProvidedProduction get() { | |
return new ProviderProvidedProduction(first, second); | |
} | |
} | |
} | |
@WithBeans(ProviderProvidedProduction.Builder.class) | |
@Test | |
public void testProviderProvidedProduction(final ProviderProvidedProduction instance) { | |
assertEquals(globalString, instance.first); | |
assertEquals(testString(), instance.second); | |
} | |
@Test | |
public void testProviderInjection(final Provider<String> stringProvider) { | |
assertEquals(globalString, stringProvider.get()); | |
} | |
@Test | |
public void testQualifiedProviderInjection(@Run final Provider<String> stringProvider) { | |
assertEquals(testString(), stringProvider.get()); | |
} | |
@Test | |
public void testOptionalInjectionWhenNoBeanProvided(final Optional<DefaultConstructorInjection> instance) { | |
assertFalse(instance.isPresent()); | |
} | |
@WithBeans(DefaultConstructorInjection.class) | |
@Test | |
public void testOptionalInjectionWhenBeanProvided(final Optional<DefaultConstructorInjection> instance) { | |
assertTrue(instance.isPresent()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment