|
| 1 | +/* |
| 2 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 | + * |
| 4 | + * Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved. |
| 5 | + * |
| 6 | + * The contents of this file are subject to the terms of either the GNU |
| 7 | + * General Public License Version 2 only ("GPL") or the Common Development |
| 8 | + * and Distribution License("CDDL") (collectively, the "License"). You |
| 9 | + * may not use this file except in compliance with the License. You can |
| 10 | + * obtain a copy of the License at |
| 11 | + * http://glassfish.java.net/public/CDDL+GPL_1_1.html |
| 12 | + * or packager/legal/LICENSE.txt. See the License for the specific |
| 13 | + * language governing permissions and limitations under the License. |
| 14 | + * |
| 15 | + * When distributing the software, include this License Header Notice in each |
| 16 | + * file and include the License file at packager/legal/LICENSE.txt. |
| 17 | + * |
| 18 | + * GPL Classpath Exception: |
| 19 | + * Oracle designates this particular file as subject to the "Classpath" |
| 20 | + * exception as provided by Oracle in the GPL Version 2 section of the License |
| 21 | + * file that accompanied this code. |
| 22 | + * |
| 23 | + * Modifications: |
| 24 | + * If applicable, add the following below the License Header, with the fields |
| 25 | + * enclosed by brackets [] replaced by your own identifying information: |
| 26 | + * "Portions Copyright [year] [name of copyright owner]" |
| 27 | + * |
| 28 | + * Contributor(s): |
| 29 | + * If you wish your version of this file to be governed by only the CDDL or |
| 30 | + * only the GPL Version 2, indicate your decision by adding "[Contributor] |
| 31 | + * elects to include this software in this distribution under the [CDDL or GPL |
| 32 | + * Version 2] license." If you don't indicate a single choice of license, a |
| 33 | + * recipient has the option to distribute your version of this file under |
| 34 | + * either the CDDL, the GPL Version 2 or to extend the choice of license to |
| 35 | + * its licensees as provided above. However, if you add GPL Version 2 code |
| 36 | + * and therefore, elected the GPL Version 2 license, then the option applies |
| 37 | + * only if the new code is made subject to such option by the copyright |
| 38 | + * holder. |
| 39 | + */ |
| 40 | + |
| 41 | +package org.glassfish.jersey.internal.inject; |
| 42 | + |
| 43 | +import static org.junit.Assert.assertTrue; |
| 44 | + |
| 45 | +import java.util.concurrent.CountDownLatch; |
| 46 | +import java.util.concurrent.TimeUnit; |
| 47 | + |
| 48 | +import javax.inject.Inject; |
| 49 | + |
| 50 | +import org.glassfish.hk2.api.Immediate; |
| 51 | +import org.glassfish.hk2.api.ServiceLocator; |
| 52 | +import org.glassfish.hk2.utilities.binding.AbstractBinder; |
| 53 | +import org.junit.Test; |
| 54 | + |
| 55 | +/** |
| 56 | + * Test for the {@link Injections} class. |
| 57 | + * |
| 58 | + * @author Jord Sonneveld ([email protected]) |
| 59 | + * |
| 60 | + */ |
| 61 | +public class InjectionsTest { |
| 62 | + |
| 63 | + /** |
| 64 | + * Verify that services marked with the HK2 Immediate annotation are indeed |
| 65 | + * created "immediately" (or at least "soon"). |
| 66 | + * |
| 67 | + * Because Immediate services are instantiated in a separate thread, we use |
| 68 | + * a {@link CountDownLatch} to wait for the service to be created. |
| 69 | + * |
| 70 | + * After the {@link ServiceLocator} is created, we specifically do not call |
| 71 | + * any more methods on it: the locator must instantiate the Immediate |
| 72 | + * service without any further prompting to the locator. |
| 73 | + * |
| 74 | + * @throws InterruptedException if awaiting on the latch is interrupted. |
| 75 | + */ |
| 76 | + @Test |
| 77 | + public void testHK2ImmediateAnnotation() throws InterruptedException { |
| 78 | + final CountDownLatch latch = new CountDownLatch(1); |
| 79 | + |
| 80 | + @SuppressWarnings("unused") // It is unused by design |
| 81 | + ServiceLocator sl = Injections.createLocator(new AbstractBinder() { |
| 82 | + @Override |
| 83 | + protected void configure() { |
| 84 | + bind(latch).to(CountDownLatch.class); |
| 85 | + bind(ImmediateMe.class).to(ImmediateMe.class).in( |
| 86 | + Immediate.class); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + // 10 seconds is a LONG time. It should be faster than that. However, 10 |
| 91 | + // seconds gives us a reasonable upper limit to wait in case the test |
| 92 | + // fails. |
| 93 | + assertTrue("Latch should be unlocked within 10 seconds.", |
| 94 | + latch.await(10, TimeUnit.SECONDS)); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Helper class for testing Immediate services. |
| 99 | + * |
| 100 | + */ |
| 101 | + public static final class ImmediateMe { |
| 102 | + @Inject |
| 103 | + public ImmediateMe(CountDownLatch latch) { |
| 104 | + latch.countDown(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments