|
| 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.script; |
| 33 | + |
| 34 | +import static org.junit.Assert.assertEquals; |
| 35 | + |
| 36 | +import java.io.File; |
| 37 | +import java.io.IOException; |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.Arrays; |
| 40 | +import java.util.List; |
| 41 | + |
| 42 | +import javax.script.ScriptEngine; |
| 43 | + |
| 44 | +import org.junit.AfterClass; |
| 45 | +import org.junit.BeforeClass; |
| 46 | +import org.junit.Test; |
| 47 | +import org.scijava.Context; |
| 48 | +import org.scijava.plugin.Plugin; |
| 49 | +import org.scijava.test.TestUtils; |
| 50 | +import org.scijava.util.FileUtils; |
| 51 | + |
| 52 | +/** |
| 53 | + * Tests the {@link ScriptFinder}. |
| 54 | + * |
| 55 | + * @author Curtis Rueden |
| 56 | + */ |
| 57 | +public class ScriptFinderTest { |
| 58 | + |
| 59 | + private static File scriptsDir; |
| 60 | + |
| 61 | + // -- Test setup -- |
| 62 | + |
| 63 | + @BeforeClass |
| 64 | + public static void setUp() throws IOException { |
| 65 | + scriptsDir = TestUtils.createTemporaryDirectory("script-finder-"); |
| 66 | + TestUtils.createPath(scriptsDir, "ignored.foo"); |
| 67 | + TestUtils.createPath(scriptsDir, "Plugins/quick.foo"); |
| 68 | + TestUtils.createPath(scriptsDir, "Plugins/brown.foo"); |
| 69 | + TestUtils.createPath(scriptsDir, "Plugins/fox.foo"); |
| 70 | + TestUtils.createPath(scriptsDir, "Plugins/The_Lazy_Dog.foo"); |
| 71 | + TestUtils.createPath(scriptsDir, "Math/add.foo"); |
| 72 | + TestUtils.createPath(scriptsDir, "Math/subtract.foo"); |
| 73 | + TestUtils.createPath(scriptsDir, "Math/multiply.foo"); |
| 74 | + TestUtils.createPath(scriptsDir, "Math/divide.foo"); |
| 75 | + TestUtils.createPath(scriptsDir, "Math/Trig/cos.foo"); |
| 76 | + TestUtils.createPath(scriptsDir, "Math/Trig/sin.foo"); |
| 77 | + TestUtils.createPath(scriptsDir, "Math/Trig/tan.foo"); |
| 78 | + } |
| 79 | + |
| 80 | + @AfterClass |
| 81 | + public static void tearDown() { |
| 82 | + FileUtils.deleteRecursively(scriptsDir); |
| 83 | + } |
| 84 | + |
| 85 | + // -- Unit tests -- |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testFindScripts() { |
| 89 | + final Context context = new Context(ScriptService.class); |
| 90 | + final ScriptService scriptService = context.service(ScriptService.class); |
| 91 | + scriptService.addScriptDirectory(scriptsDir); |
| 92 | + |
| 93 | + final ScriptFinder scriptFinder = new ScriptFinder(scriptService); |
| 94 | + |
| 95 | + final ArrayList<ScriptInfo> scripts = new ArrayList<ScriptInfo>(); |
| 96 | + scriptFinder.findScripts(scripts); |
| 97 | + assertEquals(11, scripts.size()); |
| 98 | + assertMenuPath("Math > add", scripts, 0); |
| 99 | + assertMenuPath("Math > divide", scripts, 1); |
| 100 | + assertMenuPath("Math > multiply", scripts, 2); |
| 101 | + assertMenuPath("Math > subtract", scripts, 3); |
| 102 | + assertMenuPath("Math > Trig > cos", scripts, 4); |
| 103 | + assertMenuPath("Math > Trig > sin", scripts, 5); |
| 104 | + assertMenuPath("Math > Trig > tan", scripts, 6); |
| 105 | + assertMenuPath("Plugins > brown", scripts, 7); |
| 106 | + assertMenuPath("Plugins > fox", scripts, 8); |
| 107 | + assertMenuPath("Plugins > quick", scripts, 9); |
| 108 | + assertMenuPath("Plugins > The Lazy Dog", scripts, 10); |
| 109 | + } |
| 110 | + |
| 111 | + // -- Helper methods -- |
| 112 | + |
| 113 | + private void assertMenuPath(final String menuString, |
| 114 | + final ArrayList<ScriptInfo> scripts, final int i) |
| 115 | + { |
| 116 | + assertEquals(menuString, scripts.get(i).getMenuPath().getMenuString()); |
| 117 | + } |
| 118 | + |
| 119 | + // -- Helper classes -- |
| 120 | + |
| 121 | + /** "Handles" scripts with .foo extension. */ |
| 122 | + @Plugin(type = ScriptLanguage.class) |
| 123 | + public static class FooScriptLanguage extends AbstractScriptLanguage { |
| 124 | + |
| 125 | + @Override |
| 126 | + public List<String> getExtensions() { |
| 127 | + return Arrays.asList("foo"); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public ScriptEngine getScriptEngine() { |
| 132 | + // NB: Should never be called by the unit tests. |
| 133 | + throw new IllegalStateException(); |
| 134 | + } |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments