Add SearchModule unit tests

git-svn-id: https://svn.apache.org/repos/asf/ant/easyant/core/trunk@1544430 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/easyant/tasks/SearchModule.java b/src/main/java/org/apache/easyant/tasks/SearchModule.java
index 3d1a0e2..5574a4d 100644
--- a/src/main/java/org/apache/easyant/tasks/SearchModule.java
+++ b/src/main/java/org/apache/easyant/tasks/SearchModule.java
@@ -103,7 +103,7 @@
                 log(sb.toString());
             }
         }
-        if (choices.size() == 0) {
+        if (choices.isEmpty()) {
             throw new BuildException("No matching module were found !");
         }
 
diff --git a/src/test/java/org/apache/easyant/tasks/GoOfflineTest.java b/src/test/java/org/apache/easyant/tasks/GoOfflineTest.java
index 3e6c7fe..20260fb 100644
--- a/src/test/java/org/apache/easyant/tasks/GoOfflineTest.java
+++ b/src/test/java/org/apache/easyant/tasks/GoOfflineTest.java
@@ -45,8 +45,6 @@
 
     private static final String EASYANT_BUILDSCOPE_REP = "easyant-buildscope-rep";
 
-    private File cache;
-
     private GoOffline goOffline;
 
     private File easyantBuildScopeRepoFolder;
@@ -59,7 +57,7 @@
 
     @Before
     public void setUp() throws URISyntaxException, IOException {
-        cache = folder.newFolder("build-cache");
+        File cache = folder.newFolder("build-cache");
 
         Project project = new Project();
         ProjectUtils.configureProjectHelper(project);
diff --git a/src/test/java/org/apache/easyant/tasks/SearchModuleTest.java b/src/test/java/org/apache/easyant/tasks/SearchModuleTest.java
new file mode 100644
index 0000000..2c4bdcb
--- /dev/null
+++ b/src/test/java/org/apache/easyant/tasks/SearchModuleTest.java
@@ -0,0 +1,180 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.easyant.tasks;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URISyntaxException;
+
+import org.apache.easyant.core.EasyAntMagicNames;
+import org.apache.easyant.core.ant.ProjectUtils;
+import org.apache.easyant.core.ivy.IvyInstanceHelper;
+import org.apache.ivy.ant.IvyConfigure;
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Location;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.input.InputHandler;
+import org.apache.tools.ant.input.InputRequest;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+
+public class SearchModuleTest {
+
+    private SearchModule searchModule = new SearchModule();
+
+    private FakeInputHandler fakeInputHandler = new FakeInputHandler();
+
+    @Rule
+    public TemporaryFolder folder = new TemporaryFolder();
+
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+
+    @Before
+    public void setUp() throws URISyntaxException, IOException {
+        File cache = folder.newFolder("build-cache");
+
+        Project project = new Project();
+        ProjectUtils.configureProjectHelper(project);
+        project.setProperty("ivy.cache.dir", cache.getAbsolutePath());
+
+        IvyConfigure configure = new IvyConfigure();
+        configure.setSettingsId(EasyAntMagicNames.EASYANT_IVY_INSTANCE);
+        configure.setProject(project);
+        configure.setFile(new File(this.getClass().getResource("/repositories/easyant-ivysettings-test.xml").toURI()));
+        configure.execute();
+
+        project.setBaseDir(new File(this.getClass().getResource("simple").toURI()));
+        project.setInputHandler(fakeInputHandler);
+
+        searchModule.setProject(project);
+        searchModule.setOwningTarget(ProjectUtils.createTopLevelTarget());
+        searchModule.setLocation(new Location(ProjectUtils.emulateMainScript(project).getAbsolutePath()));
+        searchModule.setSettingsRef(IvyInstanceHelper.buildEasyAntIvyReference(project));
+
+    }
+
+    @Test
+    public void shouldFailIfNoOrganisationIsGiven() {
+        expectedException.expectMessage("no organisation provided for SearchModule task");
+        searchModule.execute();
+    }
+
+    @Test
+    public void shouldFailIfNoModuleIsGiven() {
+        expectedException.expectMessage("no module name provided for SearchModule task");
+
+        searchModule.setOrganisation("foo");
+        searchModule.execute();
+    }
+
+    @Test
+    public void shouldFailIfNoRevisionIsGiven() {
+        expectedException.expectMessage("no revision provided for SearchModule task");
+
+        searchModule.setOrganisation("foo");
+        searchModule.setModule("bar");
+        searchModule.execute();
+    }
+
+    @Test
+    public void shouldFailIfNoPropertyPrefixIsGiven() {
+        expectedException.expectMessage("no property prefix provided provided for SearchModule task");
+
+        searchModule.setRevision("1.0");
+        searchModule.setOrganisation("foo");
+        searchModule.setModule("bar");
+        searchModule.execute();
+    }
+
+    @Test
+    public void shouldFailIfModuleNotFound() {
+        expectedException.expectMessage("No matching module were found !");
+
+        searchModule.setOrganisation("foo");
+        searchModule.setModule("bar");
+        searchModule.setRevision("1.0");
+        searchModule.setPropertyPrefix("mysearch");
+        searchModule.execute();
+    }
+
+    @Test
+    public void shouldFindExistingModuleWithExactParameter() {
+        searchModule.setOrganisation("mycompany");
+        searchModule.setModule("simpleplugin");
+        searchModule.setRevision("0.1");
+        searchModule.setPropertyPrefix("mysearch");
+        fakeInputHandler.setInput("0");
+        searchModule.execute();
+        assertThat(searchModule.getProject().getProperty("mysearch.org"), is("mycompany"));
+        assertThat(searchModule.getProject().getProperty("mysearch.module"), is("simpleplugin"));
+        assertThat(searchModule.getProject().getProperty("mysearch.rev"), is("0.1"));
+    }
+
+    @Test
+    public void shouldFindExistingModuleWithRegexpParameter() {
+        searchModule.setOrganisation("mycompany");
+        searchModule.setModule("*");
+        searchModule.setRevision("*");
+        searchModule.setPropertyPrefix("mysearch");
+        fakeInputHandler.setInput("0");
+        searchModule.execute();
+        assertThat(searchModule.getProject().getProperty("mysearch.org"), is("mycompany"));
+        assertThat(searchModule.getProject().getProperty("mysearch.module"), is("simpleplugin"));
+        assertThat(searchModule.getProject().getProperty("mysearch.rev"), is("0.1"));
+    }
+
+    @Test
+    public void shouldFindExistingModuleOnGivenResolver() {
+        searchModule.setOrganisation("mycompany");
+        searchModule.setModule("simpleplugin");
+        searchModule.setRevision("0.1");
+        searchModule.setPropertyPrefix("mysearch");
+        searchModule.setResolver("test-plugin");
+        fakeInputHandler.setInput("0");
+        searchModule.execute();
+        assertThat(searchModule.getProject().getProperty("mysearch.org"), is("mycompany"));
+        assertThat(searchModule.getProject().getProperty("mysearch.module"), is("simpleplugin"));
+        assertThat(searchModule.getProject().getProperty("mysearch.rev"), is("0.1"));
+    }
+
+    public static class FakeInputHandler implements InputHandler {
+
+        private String input = "";
+
+        public void handleInput(InputRequest request) throws BuildException {
+            request.setInput(input);
+        }
+
+        public String getInput() {
+            return input;
+        }
+
+        public void setInput(String input) {
+            this.input = input;
+        }
+
+    }
+
+}