|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the |
| 10 | + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | + * version 2 with the GNU Classpath Exception, which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + */ |
| 16 | + |
| 17 | +package org.glassfish.jersey.tests.e2e.inject.hk2; |
| 18 | + |
| 19 | +import org.glassfish.jersey.internal.inject.AbstractBinder; |
| 20 | +import org.glassfish.jersey.server.ResourceConfig; |
| 21 | +import org.glassfish.jersey.test.JerseyTest; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import javax.inject.Inject; |
| 25 | +import javax.inject.Singleton; |
| 26 | +import javax.ws.rs.core.Application; |
| 27 | +import javax.ws.rs.core.Feature; |
| 28 | +import javax.ws.rs.core.FeatureContext; |
| 29 | +import java.util.concurrent.atomic.AtomicInteger; |
| 30 | + |
| 31 | +import static org.hamcrest.CoreMatchers.is; |
| 32 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 33 | + |
| 34 | +/** |
| 35 | + * Test that HK2 binder allows for injection into a feature |
| 36 | + */ |
| 37 | +public class HK2AbstractBinderInFeaturesTest extends JerseyTest { |
| 38 | + |
| 39 | + private static final AtomicInteger binderCounter = new AtomicInteger(); |
| 40 | + private static final AtomicInteger feature1Counter = new AtomicInteger(); |
| 41 | + private static final AtomicInteger feature2Counter = new AtomicInteger(); |
| 42 | + private static final String VALUE = "CONFIGURED_VALUE"; |
| 43 | + |
| 44 | + public static class InjectableHK2Binder extends org.glassfish.hk2.utilities.binding.AbstractBinder { |
| 45 | + @Override |
| 46 | + protected void configure() { |
| 47 | + binderCounter.incrementAndGet(); |
| 48 | + bindAsContract(ConfigurableInjectable.class).to(Injectable.class).in(Singleton.class); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public static class JerseyInjectableHK2Binder extends AbstractBinder { |
| 53 | + @Override |
| 54 | + protected void configure() { |
| 55 | + bindAsContract(ConfigurableInjectable.class).to(ExtendedInjectable.class).in(Singleton.class); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public static final class InjectableHK2BindingFeature implements Feature { |
| 60 | + private final Injectable service; |
| 61 | + private final ExtendedInjectable extendedService; |
| 62 | + |
| 63 | + @Inject |
| 64 | + public InjectableHK2BindingFeature(Injectable service, ExtendedInjectable extendedService) { |
| 65 | + feature1Counter.incrementAndGet(); |
| 66 | + this.service = service; |
| 67 | + this.extendedService = extendedService; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public boolean configure(FeatureContext context) { |
| 72 | + if (service != null) { |
| 73 | + ((ConfigurableInjectable) service).set(VALUE); |
| 74 | + } |
| 75 | + if (extendedService != null) { |
| 76 | + feature2Counter.incrementAndGet(); |
| 77 | + } |
| 78 | + return true; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public static class ConfigurableInjectable implements ExtendedInjectable { |
| 83 | + private String value; |
| 84 | + public void set(String value) { |
| 85 | + this.value = value; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public String toString() { |
| 90 | + return value; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public static interface ExtendedInjectable extends Injectable { |
| 95 | + }; |
| 96 | + |
| 97 | + @Override |
| 98 | + protected Application configure() { |
| 99 | + return new ResourceConfig(InjectableHK2BindingFeature.class, AbstractBinderTestResource.class, |
| 100 | + InjectableTestFilter.class, InjectableHK2Binder.class).register(new JerseyInjectableHK2Binder()); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testInjectableInjection() { |
| 105 | + String response = target().request().get(String.class); |
| 106 | + assertThat(response, is(VALUE)); |
| 107 | + assertThat(1, is(binderCounter.get())); |
| 108 | + assertThat(1, is(feature1Counter.get())); |
| 109 | + assertThat(1, is(feature2Counter.get())); |
| 110 | + } |
| 111 | +} |
0 commit comments