1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to you under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
9    * or agreed to in writing, software distributed under the License is
10   * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11   * KIND, either express or implied. See the License for the specific language
12   * governing permissions and limitations under the License.
13   */
14  package org.apache.maven.plugin.eclipse.reader;
15  
16  import java.io.File;
17  import java.io.IOException;
18  import java.util.HashMap;
19  
20  import junit.framework.TestCase;
21  
22  import org.apache.maven.plugin.eclipse.TempEclipseWorkspace;
23  import org.apache.maven.plugin.eclipse.WorkspaceConfiguration;
24  import org.apache.maven.plugin.logging.Log;
25  import org.apache.maven.shared.tools.easymock.MockManager;
26  import org.easymock.MockControl;
27  
28  /**
29   * @author <a href="mailto:baerrach@apache.org">Barrie Treloar</a>
30   * @version $Id: ReadWorkspaceLocationsTest.java 751904 2009-03-09 23:13:33Z aheritier $
31   */
32  public class ReadWorkspaceLocationsTest
33      extends TestCase
34  {
35  
36      private static final File PROJECTS_DIRECTORY = new File( "target/test-classes/eclipse" );
37  
38      private static final File DYNAMIC_WORKSPACE_DIRECTORY = new File( PROJECTS_DIRECTORY, "dynamicWorkspace" );
39  
40      private static final File WORKSPACE_DIRECTORY = new File( DYNAMIC_WORKSPACE_DIRECTORY, "workspace" );
41  
42      private static final File WORKSPACE_PROJECT_METADATA_DIRECTORY =
43          new File( WORKSPACE_DIRECTORY, ReadWorkspaceLocations.METADATA_PLUGINS_ORG_ECLIPSE_CORE_RESOURCES_PROJECTS );
44  
45      private MockManager mm = new MockManager();
46  
47      /**
48       * {@inheritDoc}
49       */
50      protected void setUp()
51          throws Exception
52      {
53          super.setUp();
54      }
55  
56      /**
57       * Project's at the workspace level do not have a .location file.
58       * <p>
59       * Therefore their project location is directly under the workspace.
60       *
61       * @throws Exception
62       */
63      public void testGetProjectLocation_ForProjectAtWorkspaceLevel()
64          throws Exception
65      {
66          ReadWorkspaceLocations objectUnderTest = new ReadWorkspaceLocations();
67  
68          File metadataProjectDirectory = new File( WORKSPACE_PROJECT_METADATA_DIRECTORY, "project-A" );
69  
70          File projectLocation = objectUnderTest.getProjectLocation( WORKSPACE_DIRECTORY, metadataProjectDirectory );
71  
72          File expectedProjectDirectory = new File( WORKSPACE_DIRECTORY, "project-A" );
73          assertFileEquals( expectedProjectDirectory, projectLocation );
74      }
75  
76      /**
77       * Project's located other than at the workspace level have a .location file.
78       * <p>
79       * This URI specifies the fully qualified path to the project. Which may be located outside of the workspace as
80       * well.
81       *
82       * @throws Exception
83       */
84      public void testGetProjectLocation_ForProjectsWithinProjects()
85          throws Exception
86      {
87          ReadWorkspaceLocations objectUnderTest = new ReadWorkspaceLocations();
88  
89          File metadataProjectDirectory = new File( WORKSPACE_PROJECT_METADATA_DIRECTORY, "module-A1" );
90          File expectedProjectDirectory =
91              new File( TempEclipseWorkspace.getFixtureEclipseDynamicWorkspace().workspaceLocation, "project-A/module-A1" );
92  
93          File projectLocation = objectUnderTest.getProjectLocation( WORKSPACE_DIRECTORY, metadataProjectDirectory );
94  
95          assertFileEquals( expectedProjectDirectory, projectLocation );
96      }
97  
98      /**
99       * Project's located other than at the workspace level have a .location file.
100      * <p>
101      * This URI specifies the fully qualified path to the project. Which may be located outside of the workspace as
102      * well.
103      *
104      * @throws Exception
105      */
106     public void testGetProjectLocation_ForProjectsOutsideWorkspace()
107         throws Exception
108     {
109         ReadWorkspaceLocations objectUnderTest = new ReadWorkspaceLocations();
110 
111         File metadataProjectDirectory = new File( WORKSPACE_PROJECT_METADATA_DIRECTORY, "project-O" );
112         File expectedProjectDirectory = new File( DYNAMIC_WORKSPACE_DIRECTORY, "project-O" );
113 
114         File projectLocation = objectUnderTest.getProjectLocation( WORKSPACE_DIRECTORY, metadataProjectDirectory );
115 
116         assertFileEquals( expectedProjectDirectory, projectLocation );
117     }
118 
119     public void testReadDefinedServers_PrefsFileDoesNotExist()
120         throws Exception
121     {
122         MockControl logControl = MockControl.createControl( Log.class );
123         mm.add( logControl );
124 
125         Log logger = (Log) logControl.getMock();
126         WorkspaceConfiguration workspaceConfiguration = new WorkspaceConfiguration();
127         workspaceConfiguration.setWorkspaceDirectory( new File( "/does/not/exist" ) );
128 
129         mm.replayAll();
130 
131         ReadWorkspaceLocations objectUnderTest = new ReadWorkspaceLocations();
132         HashMap servers = objectUnderTest.readDefinedServers( workspaceConfiguration, logger );
133 
134         mm.verifyAll();
135         assertTrue( servers.isEmpty() );
136     }
137 
138     public void testReadDefinedServers_PrefsFileExistsWithMissingRuntimes()
139         throws Exception
140     {
141         MockControl logControl = MockControl.createControl( Log.class );
142         mm.add( logControl );
143 
144         Log logger = (Log) logControl.getMock();
145         WorkspaceConfiguration workspaceConfiguration = new WorkspaceConfiguration();
146         File prefsFile =
147             new File(
148                       "target/test-classes/eclipse/dynamicWorkspace/workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.server.core.prefs" );
149         workspaceConfiguration.setWorkspaceDirectory( prefsFile );
150         mm.replayAll();
151 
152         ReadWorkspaceLocations objectUnderTest = new ReadWorkspaceLocations();
153         HashMap servers = objectUnderTest.readDefinedServers( workspaceConfiguration, logger );
154 
155         mm.verifyAll();
156         assertTrue( servers.isEmpty() );
157     }
158 
159     /**
160      * Assert that two files represent the same absolute file.
161      *
162      * @param expectedFile
163      * @param actualFile
164      * @throws IOException
165      */
166     private void assertFileEquals( File expectedFile, File actualFile )
167         throws IOException
168     {
169         assertEquals( expectedFile.getCanonicalFile(), actualFile.getCanonicalFile() );
170 
171     }
172 
173 }