|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * SciJava Common shared library for SciJava software. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2016 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.command; |
| 33 | + |
| 34 | +import static org.junit.Assert.assertEquals; |
| 35 | +import static org.junit.Assert.assertFalse; |
| 36 | +import static org.junit.Assert.assertNotNull; |
| 37 | +import static org.junit.Assert.assertNull; |
| 38 | +import static org.junit.Assert.assertSame; |
| 39 | +import static org.junit.Assert.assertTrue; |
| 40 | + |
| 41 | +import java.util.Arrays; |
| 42 | +import java.util.Iterator; |
| 43 | + |
| 44 | +import org.junit.Before; |
| 45 | +import org.junit.Test; |
| 46 | +import org.scijava.Context; |
| 47 | +import org.scijava.command.CommandInfoTest.CommandWithEnumParam.Choice; |
| 48 | +import org.scijava.module.ModuleItem; |
| 49 | +import org.scijava.plugin.Parameter; |
| 50 | +import org.scijava.plugin.Plugin; |
| 51 | + |
| 52 | +/** |
| 53 | + * Tests {@link CommandInfo}. |
| 54 | + * |
| 55 | + * @author Curtis Rueden |
| 56 | + */ |
| 57 | +public class CommandInfoTest { |
| 58 | + |
| 59 | + private CommandService commandService; |
| 60 | + |
| 61 | + @Before |
| 62 | + public void setUp() { |
| 63 | + final Context ctx = new Context(CommandService.class); |
| 64 | + commandService = ctx.getService(CommandService.class); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testEnumParam() { |
| 69 | + final CommandInfo info = commandService.getCommand( |
| 70 | + CommandWithEnumParam.class); |
| 71 | + assertNotNull(info); |
| 72 | + |
| 73 | + final Iterator<ModuleItem<?>> iter = info.inputs().iterator(); |
| 74 | + assertTrue(iter.hasNext()); |
| 75 | + ModuleItem<?> freeform = iter.next(); |
| 76 | + assertTrue(iter.hasNext()); |
| 77 | + ModuleItem<?> constrained = iter.next(); |
| 78 | + assertTrue(iter.hasNext()); |
| 79 | + ModuleItem<?> choice = iter.next(); |
| 80 | + assertFalse(iter.hasNext()); |
| 81 | + |
| 82 | + assertSame(String.class, freeform.getType()); |
| 83 | + assertNull(freeform.getChoices()); |
| 84 | + |
| 85 | + assertSame(String.class, constrained.getType()); |
| 86 | + assertEquals(Arrays.asList("foo", "bar", "fubar"), // |
| 87 | + constrained.getChoices()); |
| 88 | + |
| 89 | + assertSame(Choice.class, choice.getType()); |
| 90 | + assertEquals(Arrays.asList(Choice.YES, Choice.NO, Choice.MAYBE_SO), // |
| 91 | + choice.getChoices()); |
| 92 | + } |
| 93 | + |
| 94 | + // -- Helper classes -- |
| 95 | + |
| 96 | + /** A command with an enum parameter. */ |
| 97 | + @Plugin(type = Command.class) |
| 98 | + public static class CommandWithEnumParam implements Command { |
| 99 | + |
| 100 | + public enum Choice { |
| 101 | + YES, NO, MAYBE_SO |
| 102 | + } |
| 103 | + |
| 104 | + @Parameter |
| 105 | + private String freeform; |
| 106 | + |
| 107 | + @Parameter(choices = {"foo", "bar", "fubar"}) |
| 108 | + private String constrained; |
| 109 | + |
| 110 | + @Parameter |
| 111 | + private Choice choice; |
| 112 | + |
| 113 | + @Override |
| 114 | + public void run() { |
| 115 | + // NB: No implementation needed. |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments