Author: baerrach
Date: Mon Sep 22 23:21:59 2008
New Revision: 698101

URL: http://svn.apache.org/viewvc?rev=698101&view=rev
Log:
Changed parameter order of detectWTPDefaultServer to be consistent with other 
methods.
Changed readDefinedServers to package scope for testing.
Added unit tests for missing wtp prefs file and missing runtimes element.
Fixed bug where missing prefs file or missing runtimes element caused exception.

Added:
    
maven/plugins/trunk/maven-eclipse-plugin/src/test/resources/eclipse/dynamicWorkspace/workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.server.core.prefs
Modified:
    
maven/plugins/trunk/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/reader/ReadWorkspaceLocations.java
    
maven/plugins/trunk/maven-eclipse-plugin/src/test/java/org/apache/maven/plugin/eclipse/reader/ReadWorkspaceLocationsTest.java

Modified: 
maven/plugins/trunk/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/reader/ReadWorkspaceLocations.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/reader/ReadWorkspaceLocations.java?rev=698101&r1=698100&r2=698101&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/reader/ReadWorkspaceLocations.java
 (original)
+++ 
maven/plugins/trunk/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/reader/ReadWorkspaceLocations.java
 Mon Sep 22 23:21:59 2008
@@ -91,12 +91,23 @@
     {
         detectDefaultJREContainer( workspaceConfiguration, project, log );
         readWorkspace( workspaceConfiguration, log );
-        detectWTPDefaultServer( log, workspaceConfiguration, wtpDefaultServer 
);
+        detectWTPDefaultServer( workspaceConfiguration, wtpDefaultServer, log 
);
     }
 
-    private void detectWTPDefaultServer( Log log, WorkspaceConfiguration 
workspaceConfiguration, String wtpDefaultServer )
+    /**
+     * Detect WTP Default Server. Do nothing if tehre are no defined servers 
in the settings.
+     * 
+     * @param workspaceConfiguration
+     * @param wtpDefaultServer
+     * @param log
+     */
+    private void detectWTPDefaultServer( WorkspaceConfiguration 
workspaceConfiguration, String wtpDefaultServer, Log log )
     {
         HashMap servers = readDefinedServers( workspaceConfiguration, log );
+        if ( servers == null || servers.isEmpty() )
+        {
+            return;
+        }
         if ( wtpDefaultServer != null )
         {
             Set ids = servers.keySet();
@@ -252,9 +263,9 @@
                 {
                     if ( !file.startsWith( "URI//" ) )
                     {
-                        throw new IOException( location.getAbsolutePath() + " 
contains unexpected data: " + file); 
+                        throw new IOException( location.getAbsolutePath() + " 
contains unexpected data: " + file );
                     }
-                    file = file.substring( "URI//".length() );                 
   
+                    file = file.substring( "URI//".length() );
                     return new File( new URI( file ) );
                 }
             }
@@ -349,7 +360,7 @@
         }
     }
 
-    private HashMap readDefinedServers( WorkspaceConfiguration 
workspaceConfiguration, Log logger )
+    /* package */ HashMap readDefinedServers( WorkspaceConfiguration 
workspaceConfiguration, Log logger )
     {
         HashMap detectedRuntimes = new HashMap();
         if ( workspaceConfiguration.getWorkspaceDirectory() != null )
@@ -360,11 +371,16 @@
                 File prefs =
                     new File( workspaceConfiguration.getWorkspaceDirectory(),
                               
ReadWorkspaceLocations.METADATA_PLUGINS_ORG_ECLIPSE_CORE_RUNTIME_SERVER_PREFS );
-                Properties properties = new Properties();
-                properties.load( new FileInputStream( prefs ) );
-                runtimesElement =
-                    Xpp3DomBuilder.build( new StringReader(
-                                                            
properties.getProperty( 
ReadWorkspaceLocations.METADATA_PLUGINS_ORG_ECLIPSE_CORE_RUNTIME_PREFS_RUNTIMES_KEY
 ) ) );
+                if ( prefs.exists() )
+                {
+                    Properties properties = new Properties();
+                    properties.load( new FileInputStream( prefs ) );
+                    String runtimes = properties.getProperty( 
ReadWorkspaceLocations.METADATA_PLUGINS_ORG_ECLIPSE_CORE_RUNTIME_PREFS_RUNTIMES_KEY
 );
+                    if ( runtimes != null )
+                    {
+                        runtimesElement = Xpp3DomBuilder.build( new 
StringReader( runtimes ) );
+                    }
+                }
             }
             catch ( Exception e )
             {
@@ -422,7 +438,7 @@
             logger.error( "Could not read workspace JRE preferences", e );
             return null;
         }
-        
+
         HashMap jreMap = new HashMap();
         jreMap.put( "1.2", CLASSPATHENTRY_STANDARD + "J2SE-1.2" );
         jreMap.put( "1.3", CLASSPATHENTRY_STANDARD + "J2SE-1.3" );
@@ -430,7 +446,7 @@
         jreMap.put( "1.5", CLASSPATHENTRY_STANDARD + "J2SE-1.5" );
         jreMap.put( "5", jreMap.get( "1.5" ) );
         jreMap.put( "1.6", CLASSPATHENTRY_STANDARD + "JavaSE-1.6" );
-        jreMap.put( "6", jreMap.get( "1.6" ) );        
+        jreMap.put( "6", jreMap.get( "1.6" ) );
         String defaultJRE = vms.getAttribute( "defaultVM" ).trim();
         Xpp3Dom[] vmTypes = vms.getChildren( "vmType" );
         for ( int vmTypeIndex = 0; vmTypeIndex < vmTypes.length; vmTypeIndex++ 
)

Modified: 
maven/plugins/trunk/maven-eclipse-plugin/src/test/java/org/apache/maven/plugin/eclipse/reader/ReadWorkspaceLocationsTest.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-eclipse-plugin/src/test/java/org/apache/maven/plugin/eclipse/reader/ReadWorkspaceLocationsTest.java?rev=698101&r1=698100&r2=698101&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-eclipse-plugin/src/test/java/org/apache/maven/plugin/eclipse/reader/ReadWorkspaceLocationsTest.java
 (original)
+++ 
maven/plugins/trunk/maven-eclipse-plugin/src/test/java/org/apache/maven/plugin/eclipse/reader/ReadWorkspaceLocationsTest.java
 Mon Sep 22 23:21:59 2008
@@ -15,10 +15,17 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.util.HashMap;
 
 import junit.framework.TestCase;
 
 import org.apache.maven.plugin.eclipse.TempEclipseWorkspace;
+import org.apache.maven.plugin.eclipse.WorkspaceConfiguration;
+import org.apache.maven.plugin.logging.Log;
+import org.apache.maven.plugin.logging.SystemStreamLog;
+import org.apache.maven.shared.tools.easymock.MockManager;
+import org.codehaus.plexus.archiver.manager.ArchiverManager;
+import org.easymock.MockControl;
 
 /**
  * @author <a href="mailto:[EMAIL PROTECTED]">Barrie Treloar</a>
@@ -37,6 +44,8 @@
     private static final File WORKSPACE_PROJECT_METADATA_DIRECTORY =
         new File( WORKSPACE_DIRECTORY, 
ReadWorkspaceLocations.METADATA_PLUGINS_ORG_ECLIPSE_CORE_RESOURCES_PROJECTS );
 
+    private MockManager mm = new MockManager();
+
     /**
      * [EMAIL PROTECTED]
      */
@@ -109,6 +118,46 @@
         assertFileEquals( expectedProjectDirectory, projectLocation );
     }
 
+    public void testReadDefinedServers_PrefsFileDoesNotExist()
+        throws Exception
+    {
+        MockControl logControl = MockControl.createControl( Log.class );
+        mm.add( logControl );
+
+        Log logger = (Log) logControl.getMock();
+        WorkspaceConfiguration workspaceConfiguration = new 
WorkspaceConfiguration();
+        workspaceConfiguration.setWorkspaceDirectory( new File( 
"/does/not/exist" ) );
+
+        mm.replayAll();
+
+        ReadWorkspaceLocations objectUnderTest = new ReadWorkspaceLocations();
+        HashMap servers = objectUnderTest.readDefinedServers( 
workspaceConfiguration, logger );
+
+        mm.verifyAll();
+        assertTrue( servers.isEmpty() );
+    }
+
+    public void testReadDefinedServers_PrefsFileExistsWithMissingRuntimes()
+        throws Exception
+    {
+        MockControl logControl = MockControl.createControl( Log.class );
+        mm.add( logControl );
+
+        Log logger = (Log) logControl.getMock();
+        WorkspaceConfiguration workspaceConfiguration = new 
WorkspaceConfiguration();
+        File prefsFile =
+            new File(
+                      
"target/test-classes/eclipse/dynamicWorkspace/workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.server.core.prefs"
 );
+        workspaceConfiguration.setWorkspaceDirectory( prefsFile );
+        mm.replayAll();
+
+        ReadWorkspaceLocations objectUnderTest = new ReadWorkspaceLocations();
+        HashMap servers = objectUnderTest.readDefinedServers( 
workspaceConfiguration, logger );
+
+        mm.verifyAll();
+        assertTrue( servers.isEmpty() );
+    }
+
     /**
      * Assert that two files represent the same absolute file.
      * 

Added: 
maven/plugins/trunk/maven-eclipse-plugin/src/test/resources/eclipse/dynamicWorkspace/workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.server.core.prefs
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-eclipse-plugin/src/test/resources/eclipse/dynamicWorkspace/workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.server.core.prefs?rev=698101&view=auto
==============================================================================
--- 
maven/plugins/trunk/maven-eclipse-plugin/src/test/resources/eclipse/dynamicWorkspace/workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.server.core.prefs
 (added)
+++ 
maven/plugins/trunk/maven-eclipse-plugin/src/test/resources/eclipse/dynamicWorkspace/workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.server.core.prefs
 Mon Sep 22 23:21:59 2008
@@ -0,0 +1 @@
+# File exists but is empty on purpose


Reply via email to