|
| 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.run; |
| 33 | + |
| 34 | +import static org.junit.Assert.assertEquals; |
| 35 | + |
| 36 | +import java.lang.reflect.InvocationTargetException; |
| 37 | +import java.util.LinkedHashMap; |
| 38 | +import java.util.Map; |
| 39 | + |
| 40 | +import org.junit.After; |
| 41 | +import org.junit.Before; |
| 42 | +import org.junit.Test; |
| 43 | +import org.scijava.Context; |
| 44 | +import org.scijava.plugin.Plugin; |
| 45 | + |
| 46 | +/** |
| 47 | + * Tests {@link RunService}. |
| 48 | + * |
| 49 | + * @author Curtis Rueden |
| 50 | + */ |
| 51 | +public class RunServiceTest { |
| 52 | + |
| 53 | + private RunService runService; |
| 54 | + |
| 55 | + @Before |
| 56 | + public void setUp() { |
| 57 | + runService = new Context().service(RunService.class); |
| 58 | + } |
| 59 | + |
| 60 | + @After |
| 61 | + public void tearDown() { |
| 62 | + runService.context().dispose(); |
| 63 | + } |
| 64 | + |
| 65 | + /** Tests {@link RunService#run(Object, Object...)}. */ |
| 66 | + @Test |
| 67 | + public void testRunList() throws InvocationTargetException { |
| 68 | + final StringBuilder sb = new StringBuilder(); |
| 69 | + runService.run(sb, "foo", "bar", "fu bar"); |
| 70 | + assertEquals("|foo|bar|fu bar|", sb.toString()); |
| 71 | + } |
| 72 | + |
| 73 | + /** Tests {@link RunService#run(Object, Object...)}. */ |
| 74 | + @Test |
| 75 | + public void testRunMap() throws InvocationTargetException { |
| 76 | + final StringBuilder sb = new StringBuilder(); |
| 77 | + final Map<String, Object> inputMap = new LinkedHashMap<String, Object>(); |
| 78 | + inputMap.put("foo", "bar"); |
| 79 | + inputMap.put("animal", "quick brown fox"); |
| 80 | + inputMap.put("number", 33); |
| 81 | + runService.run(sb, inputMap); |
| 82 | + assertEquals("|foo=bar|animal=quick brown fox|number=33|", sb.toString()); |
| 83 | + } |
| 84 | + |
| 85 | + // -- Helper classes -- |
| 86 | + |
| 87 | + /** A {@link CodeRunner} that stringifies its arguments. */ |
| 88 | + @Plugin(type = CodeRunner.class) |
| 89 | + public static class StringRunner extends AbstractCodeRunner { |
| 90 | + |
| 91 | + @Override |
| 92 | + public void run(final Object code, final Object... args) |
| 93 | + throws InvocationTargetException |
| 94 | + { |
| 95 | + final StringBuilder sb = getStringBuilder(code); |
| 96 | + sb.append("|"); |
| 97 | + for (final Object arg : args) { |
| 98 | + sb.append(arg); |
| 99 | + sb.append("|"); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void run(final Object code, final Map<String, Object> inputMap) |
| 105 | + throws InvocationTargetException |
| 106 | + { |
| 107 | + final StringBuilder sb = getStringBuilder(code); |
| 108 | + sb.append("|"); |
| 109 | + for (final String key : inputMap.keySet()) { |
| 110 | + sb.append(key); |
| 111 | + sb.append("="); |
| 112 | + sb.append(inputMap.get(key)); |
| 113 | + sb.append("|"); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public boolean supports(final Object code) { |
| 119 | + return getStringBuilder(code) != null; |
| 120 | + } |
| 121 | + |
| 122 | + private StringBuilder getStringBuilder(final Object code) { |
| 123 | + return code instanceof StringBuilder ? (StringBuilder) code : null; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments