|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * Integration tests for the scijava-context library. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2021 - 2025 SciJava developers. |
| 6 | + * %% |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * #L% |
| 28 | + */ |
| 29 | + |
| 30 | +package org.scijava.context.test; |
| 31 | + |
| 32 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 35 | + |
| 36 | +import java.util.List; |
| 37 | +import java.util.stream.Collectors; |
| 38 | + |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | +import org.scijava.context.Context; |
| 41 | +import org.scijava.discovery.Discovery; |
| 42 | + |
| 43 | +/** |
| 44 | + * Tests plugin discovery: the {@code @Plugin} annotation, the index the |
| 45 | + * annotation processor writes at build time, and {@link Context#plugins}. |
| 46 | + * <p> |
| 47 | + * This lives in its own module so the whole path is real - the annotation is |
| 48 | + * processed by an actual build, and the implementations sit in a package that |
| 49 | + * is opened to the container but exported to nobody. |
| 50 | + * </p> |
| 51 | + * |
| 52 | + * @author Curtis Rueden |
| 53 | + */ |
| 54 | +public class PluginDiscoveryTest { |
| 55 | + |
| 56 | + private static final String LOUD = "org.scijava.context.test.impl.LoudShouter"; |
| 57 | + private static final String QUIET = |
| 58 | + "org.scijava.context.test.impl.QuietShouter"; |
| 59 | + |
| 60 | + /** Annotate a class, and it is found. No configuration file to write. */ |
| 61 | + @Test |
| 62 | + public void testPluginsAreDiscovered() { |
| 63 | + try (final Context context = Context.create()) { |
| 64 | + final List<String> names = context.plugins(Shouter.class).stream() // |
| 65 | + .map(Discovery::implClassName) // |
| 66 | + .collect(Collectors.toList()); |
| 67 | + // NB: highest priority first. |
| 68 | + assertEquals(List.of(LOUD, QUIET), names); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * The point of an index: metadata is readable without loading the plugin |
| 74 | + * class, which is what makes building a large menu cheap. |
| 75 | + */ |
| 76 | + @Test |
| 77 | + public void testMetadataWithoutLoadingTheClass() { |
| 78 | + try (final Context context = Context.create()) { |
| 79 | + final Discovery<Shouter> loud = context.plugins(Shouter.class).get(0); |
| 80 | + |
| 81 | + assertEquals(LOUD, loud.implClassName()); |
| 82 | + assertEquals("loud", loud.attr("name").orElse(null)); |
| 83 | + assertEquals("Loud shouter", loud.attr("label").orElse(null)); |
| 84 | + assertEquals("11", loud.attr("volume").orElse(null)); |
| 85 | + assertEquals(100.0, loud.priority()); |
| 86 | + |
| 87 | + // None of that constructed anything. |
| 88 | + assertFalse(constructedLoudShouter(), |
| 89 | + "reading metadata constructed the plugin"); |
| 90 | + |
| 91 | + // Asking for the object is what constructs it. |
| 92 | + assertEquals("HELLO", loud.get().shout()); |
| 93 | + assertTrue(constructedLoudShouter()); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** The implementations stay encapsulated: opened to the container only. */ |
| 98 | + @Test |
| 99 | + public void testImplementationsAreNotExported() { |
| 100 | + final Module module = Shouter.class.getModule(); |
| 101 | + assertTrue(module.isNamed(), "not running on the module path"); |
| 102 | + assertFalse(module.isExported("org.scijava.context.test.impl"), |
| 103 | + "the implementation package must not be exported"); |
| 104 | + assertTrue(module.isOpen("org.scijava.context.test.impl", |
| 105 | + Context.class.getModule()), "the container needs reflective access"); |
| 106 | + } |
| 107 | + |
| 108 | + /** An unindexed type yields nothing rather than failing. */ |
| 109 | + @Test |
| 110 | + public void testUnknownPluginType() { |
| 111 | + try (final Context context = Context.create()) { |
| 112 | + assertTrue(context.plugins(Runnable.class).isEmpty()); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * NB: read through the module system rather than touching the class, since |
| 118 | + * referring to the static field would itself load it. |
| 119 | + */ |
| 120 | + private static boolean constructedLoudShouter() { |
| 121 | + try { |
| 122 | + final Class<?> c = Class.forName(LOUD); |
| 123 | + return c.getField("constructed").getBoolean(null); |
| 124 | + } |
| 125 | + catch (final ReflectiveOperationException exc) { |
| 126 | + throw new AssertionError(exc); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments