View Javadoc

1   package org.apache.continuum.profile;
2   
3   import org.apache.continuum.dao.DaoUtils;
4   import org.apache.maven.continuum.AbstractContinuumTest;
5   import org.apache.maven.continuum.installation.InstallationService;
6   import org.apache.maven.continuum.model.system.Installation;
7   import org.apache.maven.continuum.model.system.Profile;
8   import org.apache.maven.continuum.profile.AlreadyExistsProfileException;
9   import org.apache.maven.continuum.profile.ProfileService;
10  
11  import java.util.List;
12  
13  /*
14   * Licensed to the Apache Software Foundation (ASF) under one
15   * or more contributor license agreements.  See the NOTICE file
16   * distributed with this work for additional information
17   * regarding copyright ownership.  The ASF licenses this file
18   * to you under the Apache License, Version 2.0 (the
19   * "License"); you may not use this file except in compliance
20   * with the License.  You may obtain a copy of the License at
21   *
22   *   http://www.apache.org/licenses/LICENSE-2.0
23   *
24   * Unless required by applicable law or agreed to in writing,
25   * software distributed under the License is distributed on an
26   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
27   * KIND, either express or implied.  See the License for the
28   * specific language governing permissions and limitations
29   * under the License.
30   */
31  
32  /**
33   * @author <a href="mailto:olamy@codehaus.org">olamy</a>
34   * @version $Id: DefaultProfileServiceTest.java 1372260 2012-08-13 04:29:09Z brett $
35   * @since 15 juin 07
36   */
37  public class DefaultProfileServiceTest
38      extends AbstractContinuumTest
39  {
40  
41      Installation jdk1;
42  
43      private static final String jdk1Name = "jdk1";
44  
45      private Installation jdk2;
46  
47      private static final String jdk2Name = "jdk2";
48  
49      Installation mvn205;
50  
51      private static final String mvn205Name = "mvn 2.0.5";
52  
53      Installation mvn206;
54  
55      private static final String mvn206Name = "mvn 2.0.6";
56  
57      Profile jdk1mvn205;
58  
59      private static final String jdk1mvn205Name = "jdk1 mvn 2.0.5";
60  
61      Profile jdk2mvn206;
62  
63      private static final String jdk2mvn206Name = "jdk2 mvn 2.0.6";
64  
65      Installation mvnOpts1;
66  
67      private static final String mvnOpts1Name = "mvnOpts1";
68  
69      Installation mvnOpts2;
70  
71      private static final String mvnOpts2Name = "mvnOpts2";
72  
73      protected void setUp()
74          throws Exception
75      {
76          super.setUp();
77          DaoUtils daoUtils = (DaoUtils) lookup( DaoUtils.class.getName() );
78          daoUtils.eraseDatabase();
79  
80          jdk1 = new Installation();
81          jdk1.setType( InstallationService.JDK_TYPE );
82          jdk1.setVarValue( "/foo/bar" );
83          jdk1.setName( jdk1Name );
84          jdk1 = getInstallationService().add( jdk1 );
85  
86          jdk2 = new Installation();
87          jdk2.setType( InstallationService.JDK_TYPE );
88          jdk2.setVarValue( "/foo/bar/zloug" );
89          jdk2.setName( jdk2Name );
90          jdk2 = getInstallationService().add( jdk2 );
91  
92          mvn205 = new Installation();
93          mvn205.setType( InstallationService.MAVEN2_TYPE );
94          mvn205.setVarValue( "/users/maven-2.0.5" );
95          mvn205.setName( mvn205Name );
96          mvn205 = getInstallationService().add( mvn205 );
97  
98          mvn206 = new Installation();
99          mvn206.setType( InstallationService.MAVEN2_TYPE );
100         mvn206.setVarValue( "/users/maven-2.0.6" );
101         mvn206.setName( mvn206Name );
102         mvn206 = getInstallationService().add( mvn206 );
103 
104         jdk1mvn205 = new Profile();
105         jdk1mvn205.setJdk( jdk1 );
106         jdk1mvn205.setBuilder( mvn205 );
107         jdk1mvn205.setName( jdk1mvn205Name );
108         getProfileService().addProfile( jdk1mvn205 );
109 
110         jdk2mvn206 = new Profile();
111         jdk2mvn206.setJdk( jdk2 );
112         jdk2mvn206.setBuilder( mvn206 );
113         jdk2mvn206.setName( jdk2mvn206Name );
114         getProfileService().addProfile( jdk2mvn206 );
115 
116         mvnOpts1 = new Installation();
117         mvnOpts1.setType( InstallationService.ENVVAR_TYPE );
118         mvnOpts1.setVarName( "MAVEN_OPTS" );
119         mvnOpts1.setVarValue( "-Xmx256m -Djava.awt.headless=true" );
120         mvnOpts1.setName( mvnOpts1Name );
121         mvnOpts1 = getInstallationService().add( mvnOpts1 );
122 
123         mvnOpts2 = new Installation();
124         mvnOpts2.setType( InstallationService.ENVVAR_TYPE );
125         mvnOpts2.setVarName( "MAVEN_OPTS" );
126         mvnOpts2.setVarValue( "-Xmx1024m -Xms1024m" );
127         mvnOpts2.setName( mvnOpts2Name );
128         mvnOpts2 = getInstallationService().add( mvnOpts2 );
129 
130     }
131 
132     public InstallationService getInstallationService()
133         throws Exception
134     {
135         return (InstallationService) lookup( InstallationService.ROLE, "default" );
136     }
137 
138     public ProfileService getProfileService()
139         throws Exception
140     {
141         return (ProfileService) lookup( ProfileService.ROLE, "default" );
142     }
143 
144     public void testAddProfile()
145         throws Exception
146     {
147         Profile defaultProfile = new Profile();
148         String name = "default profile";
149         defaultProfile.setName( name );
150         Profile getted = getProfileService().addProfile( defaultProfile );
151         assertNotNull( getProfileService().getProfile( getted.getId() ) );
152         assertEquals( name, getProfileService().getProfile( getted.getId() ).getName() );
153         assertEquals( 3, getProfileService().getAllProfiles().size() );
154     }
155 
156     public void testAddDuplicateProfile()
157         throws Exception
158     {
159         Profile defaultProfile = new Profile();
160         String name = "default profile";
161         defaultProfile.setName( name );
162         Profile getted = getProfileService().addProfile( defaultProfile );
163         assertNotNull( getProfileService().getProfile( getted.getId() ) );
164         assertEquals( name, getProfileService().getProfile( getted.getId() ).getName() );
165         assertEquals( 3, getProfileService().getAllProfiles().size() );
166 
167         defaultProfile = new Profile();
168         defaultProfile.setName( name );
169         try
170         {
171             getProfileService().addProfile( defaultProfile );
172             fail( "no AlreadyExistsProfileException with an already exist name " );
173         }
174         catch ( AlreadyExistsProfileException e )
175         {
176             // we must be here
177         }
178         assertEquals( 3, getProfileService().getAllProfiles().size() );
179     }
180 
181     public void testDeleteProfile()
182         throws Exception
183     {
184         Profile defaultProfile = new Profile();
185         defaultProfile.setName( "default profile" );
186         Profile getted = getProfileService().addProfile( defaultProfile );
187         int id = getted.getId();
188         assertNotNull( getProfileService().getProfile( id ) );
189         getProfileService().deleteProfile( id );
190         assertNull( getProfileService().getProfile( id ) );
191     }
192 
193     public void testgetAllProfile()
194         throws Exception
195     {
196         List<Profile> all = getProfileService().getAllProfiles();
197         assertNotNull( all );
198         assertFalse( all.isEmpty() );
199         assertEquals( 2, all.size() );
200     }
201 
202     public void testupdateProfile()
203         throws Exception
204     {
205         Profile profile = getProfileService().getProfile( jdk1mvn205.getId() );
206         assertEquals( jdk1mvn205Name, profile.getName() );
207         String newName = "new name";
208         profile.setName( newName );
209         getProfileService().updateProfile( profile );
210 
211         Profile getted = getProfileService().getProfile( jdk1mvn205.getId() );
212         assertNotNull( getted );
213         assertEquals( newName, getted.getName() );
214     }
215 
216     public void testupdateProfileDuplicateName()
217         throws Exception
218     {
219         int profileId = jdk1mvn205.getId();
220         Profile profile = getProfileService().getProfile( profileId );
221         assertEquals( jdk1mvn205Name, profile.getName() );
222         profile.setName( jdk2mvn206Name );
223 
224         try
225         {
226             getProfileService().updateProfile( profile );
227 
228             fail( "no AlreadyExistsProfileException with duplicate name" );
229         }
230         catch ( AlreadyExistsProfileException e )
231         {
232             // we must be here
233         }
234         Profile getted = getProfileService().getProfile( profileId );
235         assertNotNull( getted );
236         assertEquals( jdk1mvn205Name, getted.getName() );
237     }
238 
239     public void testsetJdkInProfile()
240         throws Exception
241     {
242         Profile profile = getProfileService().getProfile( jdk1mvn205.getId() );
243         getProfileService().setJdkInProfile( profile, jdk2 );
244 
245         profile = getProfileService().getProfile( jdk1mvn205.getId() );
246         assertEquals( jdk2.getName(), profile.getJdk().getName() );
247         assertEquals( jdk2.getVarValue(), profile.getJdk().getVarValue() );
248     }
249 
250     public void testsetBuilderInProfile()
251         throws Exception
252     {
253         Profile profile = getProfileService().getProfile( jdk1mvn205.getId() );
254         getProfileService().setBuilderInProfile( profile, mvn206 );
255         profile = getProfileService().getProfile( jdk1mvn205.getId() );
256         assertEquals( mvn206.getName(), profile.getBuilder().getName() );
257         assertEquals( mvn206.getVarValue(), profile.getBuilder().getVarValue() );
258 
259     }
260 
261     public void testaddEnvVarInProfile()
262         throws Exception
263     {
264         Profile profile = getProfileService().getProfile( jdk1mvn205.getId() );
265         getProfileService().setBuilderInProfile( profile, mvn206 );
266         getProfileService().addEnvVarInProfile( profile, mvnOpts1 );
267         profile = getProfileService().getProfile( jdk1mvn205.getId() );
268         assertFalse( profile.getEnvironmentVariables().isEmpty() );
269         assertEquals( 1, profile.getEnvironmentVariables().size() );
270     }
271 
272     public void testRemoveInstallationLinkedToAProfile()
273         throws Exception
274     {
275         Profile profile = getProfileService().getProfile( jdk1mvn205.getId() );
276         getProfileService().setJdkInProfile( profile, jdk2 );
277 
278         getProfileService().getProfile( jdk1mvn205.getId() );
279         InstallationService installationService = (InstallationService) lookup( InstallationService.ROLE, "default" );
280         installationService.delete( jdk2 );
281     }
282 
283     public void testRemoveEnvVarFromProfile()
284         throws Exception
285     {
286         Profile profile = getProfileService().getProfile( jdk1mvn205.getId() );
287         getProfileService().setJdkInProfile( profile, jdk2 );
288         getProfileService().addEnvVarInProfile( profile, mvnOpts1 );
289         getProfileService().addEnvVarInProfile( profile, mvnOpts2 );
290 
291         profile = getProfileService().getProfile( jdk1mvn205.getId() );
292         assertNotNull( profile.getJdk() );
293         assertEquals( 2, profile.getEnvironmentVariables().size() );
294 
295         getProfileService().removeInstallationFromProfile( profile, mvnOpts1 );
296 
297         profile = getProfileService().getProfile( jdk1mvn205.getId() );
298         assertNotNull( profile.getJdk() );
299         assertEquals( 1, profile.getEnvironmentVariables().size() );
300 
301         getProfileService().removeInstallationFromProfile( profile, jdk2 );
302 
303         profile = getProfileService().getProfile( jdk1mvn205.getId() );
304         assertNull( profile.getJdk() );
305         assertEquals( 1, profile.getEnvironmentVariables().size() );
306 
307         getProfileService().removeInstallationFromProfile( profile, mvnOpts2 );
308         profile = getProfileService().getProfile( jdk1mvn205.getId() );
309         assertNull( profile.getJdk() );
310         assertEquals( 0, profile.getEnvironmentVariables().size() );
311     }
312 
313 
314 }