1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional information regarding
4    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  package org.apache.maven.plugin.eclipse;
16  
17  import java.io.File;
18  import java.io.FileInputStream;
19  import java.util.Properties;
20  
21  import org.apache.maven.plugin.eclipse.writers.workspace.EclipseWorkspaceWriter;
22  import org.codehaus.plexus.PlexusTestCase;
23  import org.codehaus.plexus.util.FileUtils;
24  
25  /**
26   * @version $Id: $
27   */
28  public class EclipseWorkspaceTest
29      extends AbstractEclipsePluginTestCase
30  {
31  
32      private static final String ECLIPSE_JDT_CORE_PREFS_PATH =
33          EclipseWorkspaceWriter.ECLIPSE_CORE_RUNTIME_SETTINGS_DIR + "/" +
34              EclipseWorkspaceWriter.ECLIPSE_JDT_CORE_PREFS_FILE;
35  
36      private static final String ECLIPSE_JDT_UI_PREFS_PATH =
37          EclipseWorkspaceWriter.ECLIPSE_CORE_RUNTIME_SETTINGS_DIR + "/" +
38              EclipseWorkspaceWriter.ECLIPSE_JDT_UI_PREFS_FILE;
39  
40      protected void setUp()
41          throws Exception
42      {
43          super.setUp();
44      }
45  
46      public void testWorkspace01()
47          throws Exception
48      {
49          String projectName = "workspace-01";
50  
51          FileUtils.deleteDirectory( this.getTestWorkspaceWorkDirectory( "add-maven-repo" ) );
52          testWorkspace( projectName, "add-maven-repo" );
53  
54          this.validateM2REPOVar( projectName );
55  
56          File eclipseJDTUIPrefsFile = new File( this.getOutputDirectory( projectName ), ECLIPSE_JDT_UI_PREFS_PATH );
57  
58          assertFalse( eclipseJDTUIPrefsFile.exists() );
59      }
60  
61      public void testWorkspace02()
62          throws Exception
63      {
64          // In this test we purposely do not include
65          // expected/.metatdata/.plugins/org.eclipse.core.runtime.settings/org.eclipse.jdt.core.prefs
66          // The content of that file is heavily depended on the location of the test
67  
68          String projectName = "workspace-02";
69  
70          FileUtils.deleteDirectory( this.getTestWorkspaceWorkDirectory( projectName ) );
71          testWorkspace( projectName );
72  
73          this.validateM2REPOVar( projectName );
74  
75          File eclipseJDTUIPrefsFile = new File( this.getOutputDirectory( projectName ), ECLIPSE_JDT_UI_PREFS_PATH );
76  
77          assertTrue( eclipseJDTUIPrefsFile.exists() );
78  
79      }
80  
81      private void validateM2REPOVar( String projectName )
82          throws Exception
83      {
84          File eclipseJDTCorePrefsFile = new File( this.getOutputDirectory( projectName ), ECLIPSE_JDT_CORE_PREFS_PATH );
85  
86          assertTrue( "Test if workspace properties exists", eclipseJDTCorePrefsFile.exists() );
87  
88          Properties props = new Properties();
89          props.load( new FileInputStream( eclipseJDTCorePrefsFile ) );
90  
91          String M2_REPO = props.getProperty( EclipseWorkspaceWriter.CLASSPATH_VARIABLE_M2_REPO );
92  
93          assertNotNull( "Test M2_REPO has a value", M2_REPO );
94  
95          String localRepo = PlexusTestCase.getBasedir() + "/target/test-classes/m2repo";
96  
97          // comparing repo's all in lower case because windows is case insensitive and settings.xml may have
98          // a repository specified with different case
99          assertEquals( "Test M2_REPO value", localRepo.replace( '\\', '/' ).toLowerCase(),
100                       M2_REPO.replace( '\\', '/' ).toLowerCase() );
101 
102     }
103 
104 }