-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for injection into @QuarkusTest
- Loading branch information
1 parent
93aaed1
commit 8680597
Showing
6 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
core/deployment/src/main/java/io/quarkus/deployment/test/TestResourceProvider.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,7 @@ | ||
package io.quarkus.deployment.test; | ||
|
||
public interface TestResourceProvider { | ||
|
||
void inject(Object test); | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
...sions/arc/deployment/src/main/java/io/quarkus/arc/deployment/ArcTestResourceProvider.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,49 @@ | ||
package io.quarkus.arc.deployment; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Field; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import javax.enterprise.context.spi.CreationalContext; | ||
import javax.enterprise.inject.spi.Bean; | ||
import javax.enterprise.inject.spi.BeanManager; | ||
import javax.inject.Inject; | ||
import javax.inject.Qualifier; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.deployment.test.TestResourceProvider; | ||
|
||
public class ArcTestResourceProvider implements TestResourceProvider { | ||
|
||
@Override | ||
public void inject(Object test) { | ||
Class<?> c = test.getClass(); | ||
while (c != Object.class) { | ||
for (Field f : c.getDeclaredFields()) { | ||
if (f.isAnnotationPresent(Inject.class)) { | ||
BeanManager beanManager = Arc.container().beanManager(); | ||
List<Annotation> qualifiers = new ArrayList<>(); | ||
for (Annotation a : f.getAnnotations()) { | ||
if (a.annotationType().isAnnotationPresent(Qualifier.class)) { | ||
qualifiers.add(a); | ||
} | ||
} | ||
Set<Bean<?>> beans = beanManager.getBeans(f.getType(), | ||
qualifiers.toArray(new Annotation[qualifiers.size()])); | ||
Bean<?> bean = beanManager.resolve(beans); | ||
CreationalContext<?> ctx = beanManager.createCreationalContext(bean); | ||
Object instance = beanManager.getReference(bean, f.getType(), ctx); | ||
f.setAccessible(true); | ||
try { | ||
f.set(test, instance); | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
c = c.getSuperclass(); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ment/src/main/resources/META-INF/services/io.quarkus.deployment.test.TestResourceProvider
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 @@ | ||
io.quarkus.arc.deployment.ArcTestResourceProvider |
43 changes: 43 additions & 0 deletions
43
integration-tests/main/src/test/java/io/quarkus/example/test/HealthCheckTestCase.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,43 @@ | ||
/* | ||
* Copyright 2018 Red Hat, Inc. | ||
* | ||
* 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 | ||
* | ||
* 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 io.quarkus.example.test; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.eclipse.microprofile.health.Health; | ||
import org.eclipse.microprofile.health.HealthCheckResponse; | ||
import org.junit.jupiter.api.Assumptions; | ||
import org.junit.jupiter.api.Test; | ||
import org.wildfly.common.Assert; | ||
|
||
import io.quarkus.example.health.SimpleHealthCheck; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.test.junit.SubstrateTest; | ||
|
||
@QuarkusTest | ||
public class HealthCheckTestCase { | ||
|
||
@Inject | ||
@Health | ||
SimpleHealthCheck checkks; | ||
|
||
@Test | ||
public void testInjection() { | ||
Assumptions.assumeFalse(getClass().isAnnotationPresent(SubstrateTest.class)); | ||
Assert.assertTrue(checkks.call().getState() == HealthCheckResponse.State.UP); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
test-framework/common/src/main/java/io/quarkus/test/common/TestInjectionManager.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,25 @@ | ||
package io.quarkus.test.common; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.ServiceLoader; | ||
|
||
import io.quarkus.deployment.test.TestResourceProvider; | ||
|
||
public class TestInjectionManager { | ||
|
||
private static final List<TestResourceProvider> resourceProviders = new ArrayList<>(); | ||
|
||
static { | ||
for (TestResourceProvider i : ServiceLoader.load(TestResourceProvider.class)) { | ||
resourceProviders.add(i); | ||
} | ||
} | ||
|
||
public static void inject(Object test) { | ||
for (TestResourceProvider i : resourceProviders) { | ||
i.inject(test); | ||
} | ||
} | ||
|
||
} |
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