Author: jlboudart
Date: Tue Sep 24 07:13:21 2013
New Revision: 1525806

URL: http://svn.apache.org/r1525806
Log:
Add some tests on easyant ivy settings file configuration

Modified:
    
ant/easyant/core/trunk/src/main/java/org/apache/easyant/core/EasyAntEngine.java
    
ant/easyant/core/trunk/src/test/java/org/apache/easyant/core/EasyAntEngineTest.java
    
ant/easyant/core/trunk/src/test/resources/repositories/easyant-ivysettings-test.xml

Modified: 
ant/easyant/core/trunk/src/main/java/org/apache/easyant/core/EasyAntEngine.java
URL: 
http://svn.apache.org/viewvc/ant/easyant/core/trunk/src/main/java/org/apache/easyant/core/EasyAntEngine.java?rev=1525806&r1=1525805&r2=1525806&view=diff
==============================================================================
--- 
ant/easyant/core/trunk/src/main/java/org/apache/easyant/core/EasyAntEngine.java 
(original)
+++ 
ant/easyant/core/trunk/src/main/java/org/apache/easyant/core/EasyAntEngine.java 
Tue Sep 24 07:13:21 2013
@@ -196,7 +196,7 @@ public class EasyAntEngine {
      * @param project
      * @return the configured user easyant-ivysettings.file
      */
-    private File getUserEasyAntIvySettings(Project project) {
+    protected File getUserEasyAntIvySettings(Project project) {
         // path can be specified through a property
         String path = 
project.getProperty(EasyAntMagicNames.USER_EASYANT_IVYSETTINGS);
         // if no property is set check the default location
@@ -215,7 +215,7 @@ public class EasyAntEngine {
      * @return the configured global easyant-ivysettings.file
      * @throws MalformedURLException
      */
-    private URL getGlobalEasyAntIvySettings(Project project) throws 
MalformedURLException {
+    protected URL getGlobalEasyAntIvySettings(Project project) throws 
MalformedURLException {
         PropertyHelper helper = PropertyHelper.getPropertyHelper(project);
         URL path = null;
         if (configuration.getEasyantIvySettingsFile() != null) {

Modified: 
ant/easyant/core/trunk/src/test/java/org/apache/easyant/core/EasyAntEngineTest.java
URL: 
http://svn.apache.org/viewvc/ant/easyant/core/trunk/src/test/java/org/apache/easyant/core/EasyAntEngineTest.java?rev=1525806&r1=1525805&r2=1525806&view=diff
==============================================================================
--- 
ant/easyant/core/trunk/src/test/java/org/apache/easyant/core/EasyAntEngineTest.java
 (original)
+++ 
ant/easyant/core/trunk/src/test/java/org/apache/easyant/core/EasyAntEngineTest.java
 Tue Sep 24 07:13:21 2013
@@ -1,19 +1,31 @@
 package org.apache.easyant.core;
 
+import static org.hamcrest.CoreMatchers.endsWith;
 import static org.hamcrest.CoreMatchers.hasItem;
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.isA;
+import static org.hamcrest.CoreMatchers.notNullValue;
 import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.CoreMatchers.sameInstance;
 import static org.junit.Assert.assertThat;
 
 import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.net.MalformedURLException;
 import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.Arrays;
 
 import org.apache.easyant.core.ant.listerners.MultiModuleLogger;
 import org.apache.easyant.core.configuration.EasyAntConfiguration;
+import org.apache.easyant.core.ivy.IvyInstanceHelper;
 import org.apache.easyant.core.services.impl.DefaultPluginServiceImpl;
+import org.apache.ivy.Ivy;
 import org.apache.ivy.ant.IvyAntSettings;
+import org.apache.ivy.core.cache.EasyAntRepositoryCacheManager;
+import org.apache.ivy.core.cache.EasyantResolutionCacheManager;
 import org.apache.tools.ant.BuildLogger;
 import org.apache.tools.ant.Project;
 import org.apache.tools.ant.input.DefaultInputHandler;
@@ -128,7 +140,7 @@ public class EasyAntEngineTest {
                 instanceOf(DefaultPluginServiceImpl.class));
         assertThat(easyantEngine.getPluginService(), 
instanceOf(DefaultPluginServiceImpl.class));
         assertThat(easyantEngine.getPluginService(),
-                
is(project.getReference(EasyAntMagicNames.PLUGIN_SERVICE_INSTANCE)));
+                
sameInstance(project.getReference(EasyAntMagicNames.PLUGIN_SERVICE_INSTANCE)));
     }
 
     @Test
@@ -188,4 +200,95 @@ public class EasyAntEngineTest {
         assertThat(project.getName(), is("EasyAnt"));
     }
 
+    @Test
+    public void 
shouldConfigureEasyAntIvyInstanceWithSettingsFileFromConfiguration() throws 
URISyntaxException {
+        File f = new 
File(this.getClass().getResource("/repositories/easyant-ivysettings-test.xml").toURI());
+        easyAntConfiguration.setEasyantIvySettingsFile(f.getAbsolutePath());
+        IvyAntSettings configuredEasyAntIvyInstance = 
easyantEngine.configureEasyAntIvyInstance(project);
+
+        
assertThat(project.getReference(EasyAntMagicNames.EASYANT_IVY_INSTANCE), 
instanceOf(IvyAntSettings.class));
+        
assertThat(project.getProperty(EasyAntMagicNames.EASYANT_DEFAULT_IVYSETTINGS),
+                
is(this.getClass().getResource("/org/apache/easyant/core/default-easyant-ivysettings.xml")
+                        .toExternalForm()));
+        
assertThat(Boolean.valueOf(project.getProperty(EasyAntMagicNames.IGNORE_USER_IVYSETTINGS)),
 is(false));
+
+        
assertThat(project.getProperty(EasyAntMagicNames.OFFLINE_EASYANT_RESOLVER),
+                is(EasyAntConstants.DEFAULT_OFFLINE_EASYANT_RESOLVER));
+        
assertThat(project.getProperty(EasyAntMagicNames.OFFLINE_BASE_DIRECTORY), 
is(project.getBaseDir()
+                .getAbsolutePath() + "/offline/"));
+
+        IvyAntSettings easyantIvySettings = 
IvyInstanceHelper.getEasyAntIvyAntSettings(project);
+        Ivy ivyInstance = easyantIvySettings.getConfiguredIvyInstance(null);
+        assertThat(ivyInstance.getResolutionCacheManager(), 
instanceOf(EasyantResolutionCacheManager.class));
+        
assertThat(Arrays.asList(ivyInstance.getSettings().getRepositoryCacheManagers()),
+                hasItem(isA(EasyAntRepositoryCacheManager.class)));
+
+        assertThat(configuredEasyAntIvyInstance, 
sameInstance(easyantIvySettings));
+    }
+
+    @Test
+    public void shouldReturnDefaultUserEasyAntIvySettingsLocation() {
+        File userEasyAntIvySettings = 
easyantEngine.getUserEasyAntIvySettings(project);
+        assertThat(userEasyAntIvySettings.getAbsolutePath(),
+                endsWith(EasyAntConstants.DEFAULT_USER_EASYANT_IVYSETTINGS));
+    }
+
+    @Test
+    public void 
shouldReturnUserEasyAntIvySettingsLocationSpecifiedByProperty() {
+        String fakeLocation = "/path/to/userSettings.xml";
+        project.setNewProperty(EasyAntMagicNames.USER_EASYANT_IVYSETTINGS, 
fakeLocation);
+        File userEasyAntIvySettings = 
easyantEngine.getUserEasyAntIvySettings(project);
+        assertThat(userEasyAntIvySettings.getAbsolutePath(), is(fakeLocation));
+    }
+
+    @Test
+    public void 
shouldReturnNullGlobalEasyAntIvySettingsLocationIfNoDefaultGlobalExists() 
throws MalformedURLException {
+        // configure default global to missing directory
+        project.setNewProperty(EasyAntMagicNames.EASYANT_HOME, "/fake/path");
+        URL globalEasyAntIvySettings = 
easyantEngine.getGlobalEasyAntIvySettings(project);
+        assertThat(globalEasyAntIvySettings, nullValue());
+    }
+
+    @Test
+    public void shouldReturnDefaultGlobalEasyAntIvySettingsLocationIfExists() 
throws IOException {
+        File f = new File(System.getProperty("java.io.tmpdir"), 
"easyant-ivysettings.xml");
+        f.deleteOnExit();
+        FileOutputStream fos = null;
+        try {
+            // write file
+            fos = new FileOutputStream(f);
+            project.setNewProperty(EasyAntMagicNames.EASYANT_HOME, 
f.getParent());
+            URL globalEasyAntIvySettings = 
easyantEngine.getGlobalEasyAntIvySettings(project);
+            assertThat(globalEasyAntIvySettings, notNullValue());
+            assertThat(globalEasyAntIvySettings, is(f.toURI().toURL()));
+        } finally {
+            if (fos != null) {
+                fos.close();
+            }
+        }
+    }
+
+    @Test
+    public void 
shouldReturnGlobalEasyAntIvySettingsLocationSpecifiedByConfigurationFile() 
throws MalformedURLException {
+        
easyAntConfiguration.setEasyantIvySettingsFile("/path/to/fake/easyantIvySettingsFile.xml");
+        URL globalEasyAntIvySettings = 
easyantEngine.getGlobalEasyAntIvySettings(project);
+        assertThat(globalEasyAntIvySettings.toString(), 
endsWith(easyAntConfiguration.getEasyantIvySettingsFile()));
+    }
+
+    @Test
+    public void 
shouldReturnGlobalEasyAntIvySettingsLocationSpecifiedByConfigurationURLAsString()
+            throws MalformedURLException {
+        
easyAntConfiguration.setEasyantIvySettingsUrl("file:/path/to/fake/easyantIvySettingsFile.xml");
+        URL globalEasyAntIvySettings = 
easyantEngine.getGlobalEasyAntIvySettings(project);
+        assertThat(globalEasyAntIvySettings.toString(), 
is(easyAntConfiguration.getEasyantIvySettingsUrl()));
+    }
+
+    @Test
+    public void 
shouldReturnGlobalEasyAntIvySettingsLocationSpecifiedByConfigurationURL() 
throws MalformedURLException {
+        
easyAntConfiguration.setEasyantIvySettingsUrl(this.getClass().getResource(
+                "/repositories/easyant-ivysettings-test.xml"));
+        URL globalEasyAntIvySettings = 
easyantEngine.getGlobalEasyAntIvySettings(project);
+        assertThat(globalEasyAntIvySettings.toString(), 
is(easyAntConfiguration.getEasyantIvySettingsUrl()));
+    }
+
 }

Modified: 
ant/easyant/core/trunk/src/test/resources/repositories/easyant-ivysettings-test.xml
URL: 
http://svn.apache.org/viewvc/ant/easyant/core/trunk/src/test/resources/repositories/easyant-ivysettings-test.xml?rev=1525806&r1=1525805&r2=1525806&view=diff
==============================================================================
--- 
ant/easyant/core/trunk/src/test/resources/repositories/easyant-ivysettings-test.xml
 (original)
+++ 
ant/easyant/core/trunk/src/test/resources/repositories/easyant-ivysettings-test.xml
 Tue Sep 24 07:13:21 2013
@@ -21,8 +21,8 @@
     <caches defaultCacheDir="${cache.dir}" />
     <resolvers>
             <filesystem name="test-plugin">
-                <ivy 
pattern="${ivy.settings.dir}/plugins/[organisation]/[module]/ivys/ivy-[revision].xml"/>
-                <artifact 
pattern="${ivy.settings.dir}/plugins/[organisation]/[module]/[type]s/[artifact]-[revision].[ext]"/>
+                <ivy 
pattern="${ivy.basedir}/src/test/resources/repositories/plugins/[organisation]/[module]/ivys/ivy-[revision].xml"/>
+                <artifact 
pattern="${ivy.basedir}/src/test/resources/repositories/plugins/[organisation]/[module]/[type]s/[artifact]-[revision].[ext]"/>
             </filesystem>
     </resolvers>
 </ivysettings>


Reply via email to