|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * SciJava Common shared library for SciJava software. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2014 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck |
| 7 | + * Institute of Molecular Cell Biology and Genetics. |
| 8 | + * %% |
| 9 | + * Redistribution and use in source and binary forms, with or without |
| 10 | + * modification, are permitted provided that the following conditions are met: |
| 11 | + * |
| 12 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer. |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + * this list of conditions and the following disclaimer in the documentation |
| 16 | + * and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 22 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + * POSSIBILITY OF SUCH DAMAGE. |
| 29 | + * #L% |
| 30 | + */ |
| 31 | + |
| 32 | +package org.scijava.module; |
| 33 | + |
| 34 | +import static org.junit.Assert.assertNull; |
| 35 | +import static org.junit.Assert.assertSame; |
| 36 | + |
| 37 | +import org.junit.Test; |
| 38 | +import org.scijava.Context; |
| 39 | + |
| 40 | +/** |
| 41 | + * Tests {@link ModuleService}. |
| 42 | + * |
| 43 | + * @author Curtis Rueden |
| 44 | + */ |
| 45 | +public class ModuleServiceTest { |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testGetSingleInput() throws ModuleException { |
| 49 | + final Context context = new Context(ModuleService.class); |
| 50 | + final ModuleService moduleService = context.getService(ModuleService.class); |
| 51 | + |
| 52 | + final ModuleInfo info = new FooModuleInfo(); |
| 53 | + final Module module = info.createModule(); |
| 54 | + |
| 55 | + // verify single string input is detected |
| 56 | + final ModuleItem<String> singleString = |
| 57 | + moduleService.getSingleInput(module, String.class); |
| 58 | + assertSame(info.getInput("string"), singleString); |
| 59 | + |
| 60 | + // check that non-autofilled inputs are not detected |
| 61 | + final ModuleItem<Float> singleFloat = |
| 62 | + moduleService.getSingleInput(module, Float.class); |
| 63 | + assertNull(singleFloat); |
| 64 | + |
| 65 | + // verify that multiple inputs of the same type are not detected |
| 66 | + final ModuleItem<Integer> singleInteger = |
| 67 | + moduleService.getSingleInput(module, Integer.class); |
| 68 | + assertNull(singleInteger); |
| 69 | + |
| 70 | + // verify that single input is detected if there are |
| 71 | + // non-autofilled inputs of the same kind too |
| 72 | + final ModuleItem<Double> singleDouble = |
| 73 | + moduleService.getSingleInput(module, Double.class); |
| 74 | + assertSame(info.getInput("double2"), singleDouble); |
| 75 | + } |
| 76 | + |
| 77 | + /** A sample module for testing the module service. */ |
| 78 | + public static class FooModule extends AbstractModule { |
| 79 | + |
| 80 | + private final FooModuleInfo info; |
| 81 | + |
| 82 | + public FooModule(final FooModuleInfo info) { |
| 83 | + this.info = info; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public ModuleInfo getInfo() { |
| 88 | + return info; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void run() { |
| 93 | + // TODO Auto-generated method stub |
| 94 | + } |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + /** {@link ModuleInfo} implementation for the {@link FooModule}. */ |
| 99 | + public static class FooModuleInfo extends AbstractModuleInfo { |
| 100 | + |
| 101 | + @Override |
| 102 | + public String getDelegateClassName() { |
| 103 | + return FooModule.class.getName(); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public Class<?> loadDelegateClass() throws ClassNotFoundException { |
| 108 | + return FooModule.class; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public Module createModule() throws ModuleException { |
| 113 | + return new FooModule(this); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + protected void parseParameters() { |
| 118 | + addInput("string", String.class, true); |
| 119 | + addInput("float", Float.class, false); |
| 120 | + addInput("integer1", Integer.class, true); |
| 121 | + addInput("integer2", Integer.class, true); |
| 122 | + addInput("double1", Double.class, false); |
| 123 | + addInput("double2", Double.class, true); |
| 124 | + } |
| 125 | + |
| 126 | + private <T> void addInput(final String name, final Class<T> type, |
| 127 | + final boolean autoFill) |
| 128 | + { |
| 129 | + registerInput(new AbstractModuleItem<T>(this) { |
| 130 | + |
| 131 | + @Override |
| 132 | + public String getName() { |
| 133 | + return name; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public Class<T> getType() { |
| 138 | + return type; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public boolean isAutoFill() { |
| 143 | + return autoFill; |
| 144 | + } |
| 145 | + |
| 146 | + }); |
| 147 | + } |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments