|
| 1 | +package org.scijava.command; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | +import org.scijava.Context; |
| 5 | +import org.scijava.ItemIO; |
| 6 | +import org.scijava.Priority; |
| 7 | +import org.scijava.convert.AbstractConverter; |
| 8 | +import org.scijava.plugin.Parameter; |
| 9 | +import org.scijava.plugin.Plugin; |
| 10 | + |
| 11 | +import java.util.concurrent.ExecutionException; |
| 12 | + |
| 13 | +import static org.junit.Assert.*; |
| 14 | + |
| 15 | +public class CommandArrayConverterTest { |
| 16 | + |
| 17 | + @Test |
| 18 | + public void testArrayCommandRaw() throws InterruptedException, |
| 19 | + ExecutionException |
| 20 | + { |
| 21 | + final Context context = new Context(CommandService.class); |
| 22 | + final CommandService commandService = context.service(CommandService.class); |
| 23 | + |
| 24 | + UserClass[] userObjects = new UserClass[2]; |
| 25 | + userObjects[0] = new UserClass("User Object 0", new Object()); |
| 26 | + userObjects[1] = new UserClass("User Object 1", new Object()); |
| 27 | + |
| 28 | + final CommandModule module = // |
| 29 | + commandService.run(CommandRawArrayInput.class, true, "userObjects", userObjects ).get(); // |
| 30 | + assertEquals("User Object 0;User Object 1;", module.getOutput("result")); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testArrayConvertFromStringCommandRaw() throws InterruptedException, |
| 35 | + ExecutionException |
| 36 | + { |
| 37 | + final Context context = new Context(CommandService.class); |
| 38 | + final CommandService commandService = context.service(CommandService.class); |
| 39 | + |
| 40 | + final CommandModule module = // |
| 41 | + commandService.run(CommandRawArrayInput.class, true, "userObjects", "User Object 0,User Object 1" ).get(); // |
| 42 | + |
| 43 | + assertEquals("User Object 0;User Object 1;", module.getOutput("result")); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testArrayCommandWildcardGenerics() throws InterruptedException, |
| 48 | + ExecutionException |
| 49 | + { |
| 50 | + final Context context = new Context(CommandService.class); |
| 51 | + final CommandService commandService = context.service(CommandService.class); |
| 52 | + |
| 53 | + UserClass[] userObjects = new UserClass[2]; |
| 54 | + userObjects[0] = new UserClass("User Object 0", new Object()); |
| 55 | + userObjects[1] = new UserClass("User Object 1", new Object()); |
| 56 | + |
| 57 | + final CommandModule module = // |
| 58 | + commandService.run(CommandGenericsWildcardArrayInput.class, true, "userObjects", userObjects ).get(); // |
| 59 | + assertEquals("User Object 0;User Object 1;", module.getOutput("result")); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testArrayConvertFromStringCommandWildcardGenerics() throws InterruptedException, |
| 64 | + ExecutionException |
| 65 | + { |
| 66 | + final Context context = new Context(CommandService.class); |
| 67 | + final CommandService commandService = context.service(CommandService.class); |
| 68 | + |
| 69 | + final CommandModule module = // |
| 70 | + commandService.run(CommandGenericsWildcardArrayInput.class, true, "userObjects", "User Object 0,User Object 1" ).get(); // |
| 71 | + |
| 72 | + assertEquals("User Object 0;User Object 1;", module.getOutput("result")); |
| 73 | + } |
| 74 | + |
| 75 | + /** A command which uses a UserClass Raw Array parameter. */ |
| 76 | + @Plugin(type = Command.class) |
| 77 | + public static class CommandRawArrayInput implements Command { |
| 78 | + |
| 79 | + @Parameter |
| 80 | + private UserClass[] userObjects; |
| 81 | + |
| 82 | + @Parameter(type = ItemIO.OUTPUT) |
| 83 | + private String result = "default"; |
| 84 | + |
| 85 | + @Override |
| 86 | + public void run() { |
| 87 | + final StringBuilder sb = new StringBuilder(); |
| 88 | + System.out.println("userObjects length : "+userObjects.length); |
| 89 | + System.out.println("class : "+userObjects.getClass()); |
| 90 | + for (UserClass obj : userObjects) { |
| 91 | + System.out.println("\t"+obj); |
| 92 | + sb.append(obj.toString()+";"); |
| 93 | + } |
| 94 | + result = sb.toString(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** A command which uses a UserClass Array with generics wildcard */ |
| 99 | + @Plugin(type = Command.class) |
| 100 | + public static class CommandGenericsWildcardArrayInput implements Command { |
| 101 | + |
| 102 | + @Parameter |
| 103 | + private UserClass<?>[] userObjects; |
| 104 | + |
| 105 | + @Parameter(type = ItemIO.OUTPUT) |
| 106 | + private String result = "default"; |
| 107 | + |
| 108 | + @Override |
| 109 | + public void run() { |
| 110 | + final StringBuilder sb = new StringBuilder(); |
| 111 | + System.out.println("userObjects length : "+userObjects.length); |
| 112 | + System.out.println("class : "+userObjects.getClass()); |
| 113 | + for (UserClass obj : userObjects) { |
| 114 | + System.out.println("\t"+obj); |
| 115 | + sb.append(obj.toString()+";"); |
| 116 | + } |
| 117 | + result = sb.toString(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Plugin(type = org.scijava.convert.Converter.class, priority = Priority.LOW) |
| 122 | + public static class StringToUserClassConverterNoGenerics extends AbstractConverter<String, UserClass[]> { |
| 123 | + |
| 124 | + @Override |
| 125 | + public <T> T convert(Object src, Class<T> dest) { |
| 126 | + String str = (String) src; |
| 127 | + String[] names = str.split(","); |
| 128 | + UserClass[] userObjects = new UserClass[names.length]; |
| 129 | + for (int index = 0; index < names.length ; index++) { |
| 130 | + userObjects[index] = new UserClass(names[index], new Object()); |
| 131 | + } |
| 132 | + return (T) userObjects; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public Class<UserClass[]> getOutputType() { |
| 137 | + return UserClass[].class; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public Class<String> getInputType() { |
| 142 | + return String.class; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Simple class to test input arrays in command |
| 148 | + */ |
| 149 | + public static class UserClass<T> { |
| 150 | + |
| 151 | + String name; |
| 152 | + |
| 153 | + public UserClass(String objectName, T generic_object) { |
| 154 | + name = objectName; |
| 155 | + } |
| 156 | + |
| 157 | + public String toString() { |
| 158 | + return name; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments