View Javadoc

1   package org.apache.continuum.installation;
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.continuum.dao.DaoUtils;
23  import org.apache.maven.continuum.AbstractContinuumTest;
24  import org.apache.maven.continuum.execution.ExecutorConfigurator;
25  import org.apache.maven.continuum.installation.AlreadyExistsInstallationException;
26  import org.apache.maven.continuum.installation.InstallationService;
27  import org.apache.maven.continuum.model.system.Installation;
28  import org.apache.maven.continuum.model.system.Profile;
29  import org.apache.maven.continuum.profile.ProfileService;
30  import org.codehaus.plexus.util.StringUtils;
31  
32  import java.util.List;
33  
34  /**
35   * @author <a href="mailto:olamy@codehaus.org">olamy</a>
36   * @version $Id: DefaultInstallationServiceTest.java 1160736 2011-08-23 15:11:43Z ctan $
37   * @since 13 juin 07
38   */
39  public class DefaultInstallationServiceTest
40      extends AbstractContinuumTest
41  {
42      private static final String NEW_INSTALLATION_NAME = "newInstallation";
43  
44      protected void setUp()
45          throws Exception
46      {
47          super.setUp();
48          DaoUtils daoUtils = (DaoUtils) lookup( DaoUtils.class.getName() );
49          daoUtils.eraseDatabase();
50      }
51  
52      private InstallationService getInstallationService()
53          throws Exception
54      {
55          //Continuum continuum = (Continuum) lookup( Continuum.ROLE );
56          //return continuum.getInstallationService();
57          return (InstallationService) lookup( InstallationService.ROLE );
58      }
59  
60      private Installation addInstallation( String name, String varName, String varValue, String type )
61          throws Exception
62      {
63  
64          Installation installation = new Installation();
65          installation.setType( type );
66          installation.setName( name );
67          installation.setVarName( varName );
68          installation.setVarValue( varValue );
69          return getInstallationService().add( installation );
70      }
71  
72      public void testAddInstallation()
73          throws Exception
74      {
75          Installation added = this.addInstallation( NEW_INSTALLATION_NAME, null, "bar", InstallationService.JDK_TYPE );
76          Installation getted = getInstallationService().getInstallation( added.getInstallationId() );
77          assertNotNull( getted );
78          assertEquals( getInstallationService().getEnvVar( InstallationService.JDK_TYPE ), getted.getVarName() );
79          assertEquals( "bar", getted.getVarValue() );
80          assertEquals( 1, getInstallationService().getAllInstallations().size() );
81          assertNotNull( getInstallationService().getInstallation( NEW_INSTALLATION_NAME ) );
82      }
83  
84      public void testAddDuplicateInstallation()
85          throws Exception
86      {
87          Installation added = this.addInstallation( NEW_INSTALLATION_NAME, null, "bar", InstallationService.JDK_TYPE );
88          Installation getted = getInstallationService().getInstallation( added.getInstallationId() );
89          assertNotNull( getted );
90          assertEquals( getInstallationService().getEnvVar( InstallationService.JDK_TYPE ), getted.getVarName() );
91          assertEquals( "bar", getted.getVarValue() );
92          try
93          {
94              this.addInstallation( NEW_INSTALLATION_NAME, null, "bar", InstallationService.JDK_TYPE );
95              fail( "not in AlreadyExistsInstallationException" );
96          }
97          catch ( AlreadyExistsInstallationException e )
98          {
99              // we must be here
100         }
101         assertEquals( 1, getInstallationService().getAllInstallations().size() );
102     }
103 
104     public void testRemove()
105         throws Exception
106     {
107         String name = "toremove";
108         Installation added = this.addInstallation( name, "foo", "bar", InstallationService.JDK_TYPE );
109         Installation getted = getInstallationService().getInstallation( added.getInstallationId() );
110         assertNotNull( getted );
111         getInstallationService().delete( getted );
112         getted = getInstallationService().getInstallation( added.getInstallationId() );
113         assertNull( getted );
114 
115     }
116 
117     public void testUpdate()
118         throws Exception
119     {
120         String name = "toupdate";
121         Installation added = this.addInstallation( name, "foo", "bar", InstallationService.JDK_TYPE );
122         Installation getted = getInstallationService().getInstallation( added.getInstallationId() );
123         assertNotNull( getted );
124         assertEquals( getInstallationService().getEnvVar( InstallationService.JDK_TYPE ), getted.getVarName() );
125         assertEquals( "bar", getted.getVarValue() );
126         getted.setVarName( "updatefoo" );
127         getted.setVarValue( "updatedbar" );
128         getInstallationService().update( getted );
129         getted = getInstallationService().getInstallation( added.getInstallationId() );
130         assertNotNull( getted );
131         assertEquals( getInstallationService().getEnvVar( InstallationService.JDK_TYPE ), getted.getVarName() );
132         assertEquals( "updatedbar", getted.getVarValue() );
133     }
134 
135     public void testgetDefaultJdkInformations()
136         throws Exception
137     {
138         InstallationService installationService = (InstallationService) lookup( InstallationService.ROLE, "default" );
139         List<String> infos = installationService.getDefaultJdkInformations();
140         assertNotNull( infos );
141     }
142 
143     public void testgetJdkInformations()
144         throws Exception
145     {
146         InstallationService installationService = (InstallationService) lookup( InstallationService.ROLE, "default" );
147         String javaHome = System.getenv( "JAVA_HOME" );
148         if ( StringUtils.isEmpty( javaHome ) )
149         {
150             javaHome = System.getProperty( "java.home" );
151         }
152         Installation installation = new Installation();
153         installation.setName( "test" );
154         installation.setType( InstallationService.JDK_TYPE );
155         installation.setVarValue( javaHome );
156 
157         List<String> infos = installationService.getJdkInformations( installation );
158         assertNotNull( infos );
159     }
160 
161     public void testgetJdkInformationsWithCommonMethod()
162         throws Exception
163     {
164         InstallationService installationService = (InstallationService) lookup( InstallationService.ROLE, "default" );
165         ExecutorConfigurator java = installationService.getExecutorConfigurator( InstallationService.JDK_TYPE );
166         String javaHome = System.getenv( "JAVA_HOME" );
167         if ( StringUtils.isEmpty( javaHome ) )
168         {
169             javaHome = System.getProperty( "java.home" );
170         }
171         List<String> infos = installationService.getExecutorConfiguratorVersion( javaHome, java, null );
172         System.out.println( infos );
173         assertNotNull( infos );
174     }
175 
176 /* CONTINUUM-2559 - test may fail even in a valid environment
177     public void testgetMvnVersionWithCommonMethod()
178         throws Exception
179     {
180         InstallationService installationService = (InstallationService) lookup( InstallationService.ROLE, "default" );
181         ExecutorConfigurator java = installationService.getExecutorConfigurator( InstallationService.MAVEN2_TYPE );
182         List<String> infos = installationService.getExecutorConfiguratorVersion( null, java, null );
183         assertNotNull( infos );
184     }
185 */
186 
187     public void testAddInstallationAutomaticProfile()
188         throws Exception
189     {
190 
191         Installation installation = new Installation();
192         installation.setType( InstallationService.JDK_TYPE );
193         installation.setName( "automaticJdk" );
194         installation.setVarName( "automaticvarName" );
195         installation.setVarValue( "automaticvarValue" );
196         getInstallationService().add( installation, true );
197         ProfileService profileService = (ProfileService) lookup( ProfileService.ROLE, "default" );
198         List<Profile> profiles = profileService.getAllProfiles();
199         assertEquals( 1, profiles.size() );
200         Profile profile = profiles.get( 0 );
201         assertEquals( "automaticJdk", profile.getName() );
202         Installation jdk = profile.getJdk();
203         assertNotNull( jdk );
204         assertEquals( "automaticJdk", jdk.getName() );
205     }
206 
207     public void testUpdateName()
208         throws Exception
209     {
210         Installation installation = new Installation();
211         installation.setType( InstallationService.JDK_TYPE );
212         installation.setName( "automatic" );
213         installation.setVarName( "automaticvarName" );
214         installation.setVarValue( "automaticvarValue" );
215         installation = getInstallationService().add( installation, true );
216 
217         installation.setName( "new name here" );
218         getInstallationService().update( installation );
219 
220         Installation getted = getInstallationService().getInstallation( installation.getInstallationId() );
221         assertEquals( "new name here", getted.getName() );
222 
223 
224     }
225 }