View Javadoc
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.Map;
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 1677345 2015-05-02 20:02:31Z agudian $
31   */
32  public class ReadWorkspaceLocationsTest
33      extends TestCase
34  {
35  
36  //    private final File PROJECTS_DIRECTORY = new File( "target/tmp-workspace/eclipse" );
37  //
38  //    private final File DYNAMIC_WORKSPACE_DIRECTORY = TempEclipseWorkspace.getFixtureEclipseDynamicWorkspace().workspaceLocation;
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  private File workspaceLocation;
47  private File metaDataDirectory;
48  
49      /**
50       * {@inheritDoc}
51       */
52      protected void setUp()
53          throws Exception
54      {
55          super.setUp();
56          
57          workspaceLocation = TempEclipseWorkspace.getFixtureEclipseDynamicWorkspace().workspaceLocation;
58          metaDataDirectory = new File( workspaceLocation, ReadWorkspaceLocations.METADATA_PLUGINS_ORG_ECLIPSE_CORE_RESOURCES_PROJECTS );;
59  
60      }
61  
62      /**
63       * Project's at the workspace level do not have a .location file.
64       * <p>
65       * Therefore their project location is directly under the workspace.
66       * 
67       * @throws Exception
68       */
69      public void testGetProjectLocation_ForProjectAtWorkspaceLevel()
70          throws Exception
71      {
72          ReadWorkspaceLocations objectUnderTest = new ReadWorkspaceLocations();
73  
74          File metadataProjectDirectory = new File( metaDataDirectory , "project-A" );
75  
76          File projectLocation = objectUnderTest.getProjectLocation( workspaceLocation, metadataProjectDirectory );
77  
78          File expectedProjectDirectory = new File( workspaceLocation, "project-A" );
79          assertFileEquals( expectedProjectDirectory, projectLocation );
80      }
81  
82      /**
83       * Project's located other than at the workspace level have a .location file.
84       * <p>
85       * This URI specifies the fully qualified path to the project. Which may be located outside of the workspace as
86       * well.
87       * 
88       * @throws Exception
89       */
90      public void testGetProjectLocation_ForProjectsWithinProjects()
91          throws Exception
92      {
93          ReadWorkspaceLocations objectUnderTest = new ReadWorkspaceLocations();
94  
95          File metadataProjectDirectory = new File( metaDataDirectory, "module-A1" );
96          File expectedProjectDirectory =
97              new File( workspaceLocation, "project-A/module-A1" );
98  
99          File projectLocation = objectUnderTest.getProjectLocation( workspaceLocation, metadataProjectDirectory );
100 
101         assertFileEquals( expectedProjectDirectory, projectLocation );
102     }
103 
104     /**
105      * Project's located other than at the workspace level have a .location file.
106      * <p>
107      * This URI specifies the fully qualified path to the project. Which may be located outside of the workspace as
108      * well.
109      * 
110      * @throws Exception
111      */
112     public void testGetProjectLocation_ForProjectsOutsideWorkspace()
113         throws Exception
114     {
115         ReadWorkspaceLocations objectUnderTest = new ReadWorkspaceLocations();
116 
117         File metadataProjectDirectory = new File( metaDataDirectory, "project-O" );
118         File expectedProjectDirectory = new File( workspaceLocation, "../project-O" );
119 
120         File projectLocation = objectUnderTest.getProjectLocation( workspaceLocation, metadataProjectDirectory );
121 
122         assertFileEquals( expectedProjectDirectory, projectLocation );
123     }
124 
125     public void testReadDefinedServers_PrefsFileDoesNotExist()
126         throws Exception
127     {
128         MockControl logControl = MockControl.createControl( Log.class );
129         mm.add( logControl );
130 
131         Log logger = (Log) logControl.getMock();
132         WorkspaceConfiguration workspaceConfiguration = new WorkspaceConfiguration();
133         workspaceConfiguration.setWorkspaceDirectory( new File( "/does/not/exist" ) );
134 
135         mm.replayAll();
136 
137         ReadWorkspaceLocations objectUnderTest = new ReadWorkspaceLocations();
138         Map<String, String> servers = objectUnderTest.readDefinedServers( workspaceConfiguration, logger );
139 
140         mm.verifyAll();
141         assertTrue( servers.isEmpty() );
142     }
143 
144     public void testReadDefinedServers_PrefsFileExistsWithMissingRuntimes()
145         throws Exception
146     {
147         MockControl logControl = MockControl.createControl( Log.class );
148         mm.add( logControl );
149 
150         Log logger = (Log) logControl.getMock();
151         WorkspaceConfiguration workspaceConfiguration = new WorkspaceConfiguration();
152         File prefsFile =
153             new File(
154                       "target/test-classes/eclipse/dynamicWorkspace/workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.server.core.prefs" );
155         workspaceConfiguration.setWorkspaceDirectory( prefsFile );
156         mm.replayAll();
157 
158         ReadWorkspaceLocations objectUnderTest = new ReadWorkspaceLocations();
159         Map<String, String> servers = objectUnderTest.readDefinedServers( workspaceConfiguration, logger );
160 
161         mm.verifyAll();
162         assertTrue( servers.isEmpty() );
163     }
164 
165     /**
166      * Assert that two files represent the same absolute file.
167      * 
168      * @param expectedFile
169      * @param actualFile
170      * @throws IOException
171      */
172     private void assertFileEquals( File expectedFile, File actualFile )
173         throws IOException
174     {
175         assertEquals( expectedFile.getCanonicalFile(), actualFile.getCanonicalFile() );
176 
177     }
178 
179 }