Calculate connection based on module name

git-svn-id: https://svn.apache.org/repos/asf/maven/shared/trunk@1540292 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/maven/shared/project/utils/ScmUtils.java b/src/main/java/org/apache/maven/shared/project/utils/ScmUtils.java
index 993a55c..7daab8e 100644
--- a/src/main/java/org/apache/maven/shared/project/utils/ScmUtils.java
+++ b/src/main/java/org/apache/maven/shared/project/utils/ScmUtils.java
@@ -1,5 +1,8 @@
 package org.apache.maven.shared.project.utils;

 

+import java.io.File;

+import java.util.Map;

+

 import org.apache.maven.model.Model;

 import org.apache.maven.project.MavenProject;

 

@@ -30,6 +33,7 @@
 

     /**

      * Resolve the scm connection, based on the type of project and inheritence.

+     * This method assumes that the connection ends with the path and can be extended.

      * 

      * @param project the Maven project

      * @return the resolved SCM connection, otherwise an empty String

@@ -43,10 +47,19 @@
         {

             // prevent null-value

             scmConnection = defaultString( getScmConnection( project ) );

-            

+

             if ( !ProjectUtils.isRootProject( project ) )

             {

-                // assuming that folder matches the moduleName

+                Map<String, String> modules = ProjectUtils.getAllModules( project.getParent() );

+

+                for ( String module : modules.keySet() )

+                {

+                    if ( new File( project.getParent().getBasedir(), module ).equals( project.getFile() ) )

+                    {

+                        return scmConnection + '/' + module;

+                    }

+                }

+                // project is not a module of its parent, so use project's directoryname

                 scmConnection += '/' + project.getFile().getParentFile().getName();

             }

         }

@@ -55,6 +68,7 @@
 

     /**

      * Resolve the scm developer connection, based on the type of project and inheritence.

+     * This method assumes that the developer connection ends with the path and can be extended.

      * 

      * @param project the Maven Project

      * @return the resolved SCM developer connection, otherwise an empty String

@@ -68,10 +82,19 @@
         {

             // prevent null-value

             scmDeveloperConnection = defaultString( getScmDeveloperConnection( project ) );

-            

+

             if ( !ProjectUtils.isRootProject( project ) )

             {

-                // assuming that folder matches the moduleName

+                Map<String, String> modules = ProjectUtils.getAllModules( project.getParent() );

+

+                for ( String module : modules.keySet() )

+                {

+                    if ( new File( project.getParent().getBasedir(), module ).equals( project.getFile() ) )

+                    {

+                        return scmDeveloperConnection + '/' + module;

+                    }

+                }

+                // project is not a module of its parent, so use project's directoryname

                 scmDeveloperConnection += '/' + project.getFile().getParentFile().getName();

             }

         }

@@ -118,5 +141,5 @@
     {

         return ( value == null ? "" : value );

     }

-    

+

 }

diff --git a/src/test/java/org/apache/maven/shared/project/utils/ScmUtilsTest.java b/src/test/java/org/apache/maven/shared/project/utils/ScmUtilsTest.java
index 6091427..97c39a9 100644
--- a/src/test/java/org/apache/maven/shared/project/utils/ScmUtilsTest.java
+++ b/src/test/java/org/apache/maven/shared/project/utils/ScmUtilsTest.java
@@ -1,12 +1,20 @@
 package org.apache.maven.shared.project.utils;

 

-import static org.junit.Assert.*;

+import static org.junit.Assert.assertEquals;

+import static org.junit.Assert.assertNull;

+import static org.mockito.Mockito.mock;

+import static org.mockito.Mockito.when;

+

+import java.io.File;

+import java.io.IOException;

 

 import org.apache.maven.model.Model;

 import org.apache.maven.model.Scm;

 import org.apache.maven.project.MavenProject;

 import org.junit.Test;

 

+import edu.emory.mathcs.backport.java.util.Collections;

+

 /*

  * Licensed to the Apache Software Foundation (ASF) under one

  * or more contributor license agreements.  See the NOTICE file

@@ -33,14 +41,64 @@
     public void resolveScmConnection()

     {

         MavenProject project = new MavenProject();

-        assertEquals( "",  ScmUtils.resolveScmConnection( project ) );

+        assertEquals( "", ScmUtils.resolveScmConnection( project ) );

     }

 

     @Test

     public void resolveScmDeveloperConnection()

     {

         MavenProject project = new MavenProject();

-        assertEquals( "",  ScmUtils.resolveScmDeveloperConnection( project ) );

+        assertEquals( "", ScmUtils.resolveScmDeveloperConnection( project ) );

+    }

+

+    @Test

+    public void resolveScmConnectionByParent() throws IOException

+    {

+        MavenProject parent = mock( MavenProject.class );

+        Model parentModel = mock( Model.class );

+        when( parentModel.getModules() ).thenReturn( Collections.singletonList( "module" ) );

+        when( parent.getModel() ).thenReturn( parentModel );

+        File parentBasedir = File.createTempFile( "tmpBasedir", null );

+        when( parent.getBasedir() ).thenReturn( parentBasedir );

+

+        MavenProject project = mock( MavenProject.class );

+        when( project.hasParent() ).thenReturn( true );

+        when( project.getParent() ).thenReturn( parent );

+        when( project.getFile() ).thenReturn( new File( parentBasedir, "module" ) );

+        Scm scm = mock( Scm.class );

+        when( scm.getConnection() ).thenReturn( "parent" );

+        when( project.getScm() ).thenReturn( scm );

+

+        Model projectModel = mock( Model.class );

+

+        when( project.getModel() ).thenReturn( projectModel );

+

+        assertEquals( "parent/module", ScmUtils.resolveScmConnection( project ) );

+    }

+

+    @Test

+    public void resolveScmDeveloperConnectionByParent() throws IOException

+    {

+        MavenProject parent = mock( MavenProject.class );

+        Model parentModel = mock( Model.class );

+        when( parentModel.getModules() ).thenReturn( Collections.singletonList( "module" ) );

+        when( parent.getModel() ).thenReturn( parentModel );

+        File parentBasedir = File.createTempFile( "tmpBasedir", null );

+        when( parent.getBasedir() ).thenReturn( parentBasedir );

+

+        MavenProject project = mock( MavenProject.class );

+        when( project.hasParent() ).thenReturn( true );

+        when( project.getParent() ).thenReturn( parent );

+        when( project.getFile() ).thenReturn( new File( parentBasedir, "module" ) );

+        Scm scm = mock( Scm.class );

+        when( scm.getDeveloperConnection() ).thenReturn( "parent" );

+        when( project.getScm() ).thenReturn( scm );

+

+        Model projectModel = mock( Model.class );

+

+        when( project.getModel() ).thenReturn( projectModel );

+

+        assertEquals( "parent/module", ScmUtils.resolveScmDeveloperConnection( project ) );

     }

 

     @Test