|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * SciJava Common shared library for SciJava software. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2018 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck |
| 7 | + * Institute of Molecular Cell Biology and Genetics, University of |
| 8 | + * Konstanz, and KNIME GmbH. |
| 9 | + * %% |
| 10 | + * Redistribution and use in source and binary forms, with or without |
| 11 | + * modification, are permitted provided that the following conditions are met: |
| 12 | + * |
| 13 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 14 | + * this list of conditions and the following disclaimer. |
| 15 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 16 | + * this list of conditions and the following disclaimer in the documentation |
| 17 | + * and/or other materials provided with the distribution. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 23 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | + * POSSIBILITY OF SUCH DAMAGE. |
| 30 | + * #L% |
| 31 | + */ |
| 32 | + |
| 33 | +package org.scijava.plugin; |
| 34 | + |
| 35 | +import static org.junit.Assert.assertEquals; |
| 36 | +import static org.junit.Assert.assertFalse; |
| 37 | +import static org.junit.Assert.assertTrue; |
| 38 | + |
| 39 | +import java.util.List; |
| 40 | + |
| 41 | +import org.junit.Test; |
| 42 | +import org.scijava.Context; |
| 43 | + |
| 44 | +/** |
| 45 | + * Tests for the {@link SingletonService} |
| 46 | + * |
| 47 | + * @author Gabriel Einsdorf KNIME GmbH |
| 48 | + */ |
| 49 | +public class SingletonServiceTest { |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testListenToRemove() { |
| 53 | + |
| 54 | + final Context ctx = new Context(PluginService.class, DummySingletonService.class, |
| 55 | + DummySingletonService2.class); |
| 56 | + |
| 57 | + final DummySingletonService dss = ctx.getService( |
| 58 | + DummySingletonService.class); |
| 59 | + |
| 60 | + final DummySingletonService2 dss2 = ctx.getService( |
| 61 | + DummySingletonService2.class); |
| 62 | + |
| 63 | + final List<DummyPlugin> instances = dss.getInstances(); |
| 64 | + final DummyPlugin dummy = instances.get(0); |
| 65 | + |
| 66 | + assertFalse("Service not correctly initialized", dss2.getInstances() |
| 67 | + .isEmpty()); |
| 68 | + |
| 69 | + // test successful removal |
| 70 | + final PluginService ps = ctx.getService(PluginService.class); |
| 71 | + ps.removePlugin(dummy.getInfo()); |
| 72 | + |
| 73 | + assertFalse("Plugin was removed from wrong service!", dss2.getInstances() |
| 74 | + .isEmpty()); |
| 75 | + assertTrue("Plugin was not removed!", dss.getInstances().isEmpty()); |
| 76 | + |
| 77 | + // test successful add |
| 78 | + ps.addPlugin(dummy.getInfo()); |
| 79 | + assertEquals("Wrong number of plugins in service:", 1, dss.getInstances() |
| 80 | + .size()); |
| 81 | + assertEquals("Wrong number of plugins in independent service:", 1, dss2 |
| 82 | + .getInstances().size()); |
| 83 | + } |
| 84 | + |
| 85 | + @Plugin(type = DummyPlugin.class) |
| 86 | + public static class DummyPlugin extends AbstractRichPlugin implements |
| 87 | + SingletonPlugin |
| 88 | + { |
| 89 | + // NB: No implementation needed. |
| 90 | + } |
| 91 | + |
| 92 | + public static class DummySingletonService extends |
| 93 | + AbstractSingletonService<DummyPlugin> |
| 94 | + { |
| 95 | + |
| 96 | + @Override |
| 97 | + public Class<DummyPlugin> getPluginType() { |
| 98 | + return DummyPlugin.class; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Plugin(type = DummyPlugin2.class) |
| 103 | + public static class DummyPlugin2 extends AbstractRichPlugin implements |
| 104 | + SingletonPlugin |
| 105 | + { |
| 106 | + // NB: No implementation needed. |
| 107 | + } |
| 108 | + |
| 109 | + public static class DummySingletonService2 extends |
| 110 | + AbstractSingletonService<DummyPlugin2> |
| 111 | + { |
| 112 | + |
| 113 | + @Override |
| 114 | + public Class<DummyPlugin2> getPluginType() { |
| 115 | + return DummyPlugin2.class; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments