View Javadoc

1   package org.apache.maven.continuum.utils;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.continuum.configuration.ConfigurationService;
23  import org.apache.maven.continuum.model.project.Project;
24  import org.codehaus.plexus.spring.PlexusInSpringTestCase;
25  import org.jmock.Expectations;
26  import org.jmock.Mockery;
27  import org.jmock.integration.junit3.JUnit3Mockery;
28  
29  import java.io.File;
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  /**
34   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
35   */
36  public class DefaultWorkingDirectoryServiceTest
37      extends PlexusInSpringTestCase
38  {
39      private DefaultWorkingDirectoryService workingDirectoryService;
40  
41      private Mockery context;
42  
43      private ConfigurationService configurationService;
44  
45      public void setUp()
46          throws Exception
47      {
48          super.setUp();
49  
50          context = new JUnit3Mockery();
51  
52          configurationService = context.mock( ConfigurationService.class );
53  
54          workingDirectoryService = (DefaultWorkingDirectoryService) lookup( WorkingDirectoryService.class );
55  
56          workingDirectoryService.setConfigurationService( configurationService );
57      }
58  
59      private Project createProject( int id, String groupId, String artifactId, String version, String scmUrl,
60                                     boolean checkedOutInSingleDirectory )
61      {
62          Project project = new Project();
63          project.setId( id );
64          project.setGroupId( groupId );
65          project.setArtifactId( artifactId );
66          project.setVersion( version );
67          project.setScmUrl( scmUrl );
68          project.setCheckedOutInSingleDirectory( checkedOutInSingleDirectory );
69  
70          return project;
71      }
72  
73      public void testGetWorkingDirectoryOfSingleCheckoutFlatMultiModules()
74          throws Exception
75      {
76          List<Project> projects = new ArrayList<Project>();
77  
78          Project project = createProject( 7, "org.apache.continuum", "module-a", "1.0-SNAPSHOT",
79                                           "scm:local:src/test-projects:flat-multi-module/module-a", true );
80  
81          projects.add( project );
82  
83          projects.add( createProject( 8, "org.apache.continuum", "module-b", "1.0-SNAPSHOT",
84                                       "scm:local:src/test-projects:flat-multi-module/module-b", true ) );
85  
86          projects.add( createProject( 6, "org.apache.continuum", "parent-project", "1.0-SNAPSHOT",
87                                       "scm:local:src/test-projects:flat-multi-module/parent-project", true ) );
88  
89          final File baseWorkingDirectory = new File( getBasedir(), "target" + File.separatorChar + "working-directory" );
90  
91          context.checking( new Expectations()
92          {
93              {
94                  exactly( 2 ).of( configurationService ).getWorkingDirectory();
95                  will( returnValue( baseWorkingDirectory ) );
96              }
97          } );
98  
99          File projectWorkingDirectory = workingDirectoryService.getWorkingDirectory( project,
100                                                                                     "scm:local:src/test-projects:flat-multi-module",
101                                                                                     projects );
102 
103         assertEquals( "Incorrect working directory for flat multi-module project", baseWorkingDirectory +
104             File.separator + "6" + File.separator + "module-a", projectWorkingDirectory.getPath() );
105 
106         // test if separator is appended at the end of the scm root url
107         projectWorkingDirectory = workingDirectoryService.getWorkingDirectory( project,
108                                                                                "scm:local:src/test-projects:flat-multi-module/",
109                                                                                projects );
110 
111         assertEquals( "Incorrect working directory for flat multi-module project", baseWorkingDirectory +
112             File.separator + "6" + File.separator + "module-a", projectWorkingDirectory.getPath() );
113     }
114 
115     public void testGetWorkingDirectoryOfSingleCheckoutRegularMultiModules()
116         throws Exception
117     {
118         List<Project> projects = new ArrayList<Project>();
119 
120         Project project = createProject( 10, "org.apache.continuum", "module-a", "1.0-SNAPSHOT",
121                                          "scm:local:src/test-projects:regular-multi-module/module-a", true );
122 
123         projects.add( project );
124 
125         projects.add( createProject( 11, "org.apache.continuum", "module-b", "1.0-SNAPSHOT",
126                                      "scm:local:src/test-projects:regular-multi-module/module-b", true ) );
127 
128         projects.add( createProject( 9, "org.apache.continuum", "parent-project", "1.0-SNAPSHOT",
129                                      "scm:local:src/test-projects:regular-multi-module/", true ) );
130 
131         final File baseWorkingDirectory = new File( getBasedir(), "target" + File.separator + "working-directory" );
132 
133         context.checking( new Expectations()
134         {
135             {
136                 exactly( 2 ).of( configurationService ).getWorkingDirectory();
137                 will( returnValue( baseWorkingDirectory ) );
138             }
139         } );
140 
141         File projectWorkingDirectory = workingDirectoryService.getWorkingDirectory( project,
142                                                                                     "scm:local:src/test-projects:regular-multi-module",
143                                                                                     projects );
144 
145         assertEquals( "Incorrect working directory for regular multi-module project",
146                       baseWorkingDirectory + File.separator +
147                           "9" + File.separator + "module-a", projectWorkingDirectory.getPath() );
148 
149         // test if separator is appended at the end of the scm root url
150         projectWorkingDirectory = workingDirectoryService.getWorkingDirectory( project,
151                                                                                "scm:local:src/test-projects:regular-multi-module/",
152                                                                                projects );
153 
154         assertEquals( "Incorrect working directory for regular multi-module project",
155                       baseWorkingDirectory + File.separator +
156                           "9" + File.separator + "module-a", projectWorkingDirectory.getPath() );
157 
158         // test generated path of parent project
159         project = createProject( 9, "org.apache.continuum", "parent-project", "1.0-SNAPSHOT",
160                                  "scm:local:src/test-projects:regular-multi-module", true );
161 
162         context.checking( new Expectations()
163         {
164             {
165                 one( configurationService ).getWorkingDirectory();
166                 will( returnValue( baseWorkingDirectory ) );
167             }
168         } );
169 
170         projectWorkingDirectory = workingDirectoryService.getWorkingDirectory( project,
171                                                                                "scm:local:src/test-projects:regular-multi-module",
172                                                                                projects );
173 
174         assertEquals( "Incorrect working directory for regular multi-module project", baseWorkingDirectory +
175             File.separator + "9", projectWorkingDirectory.getPath() );
176     }
177 }