View Javadoc
1   package org.apache.maven.shared.release.exec;
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 static org.mockito.Matchers.isA;
23  import static org.mockito.Mockito.mock;
24  import static org.mockito.Mockito.spy;
25  import static org.mockito.Mockito.verify;
26  import static org.mockito.Mockito.when;
27  
28  import java.io.File;
29  import java.io.Writer;
30  
31  import org.apache.maven.settings.Proxy;
32  import org.apache.maven.settings.Server;
33  import org.apache.maven.settings.Settings;
34  import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer;
35  import org.apache.maven.shared.invoker.DefaultInvocationRequest;
36  import org.apache.maven.shared.invoker.InvocationRequest;
37  import org.apache.maven.shared.release.ReleaseResult;
38  import org.apache.maven.shared.release.env.DefaultReleaseEnvironment;
39  import org.apache.maven.shared.release.env.ReleaseEnvironment;
40  import org.codehaus.plexus.PlexusTestCase;
41  import org.codehaus.plexus.logging.Logger;
42  import org.junit.Test;
43  import org.mockito.ArgumentCaptor;
44  import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
45  
46  public class InvokerMavenExecutorTest
47      extends PlexusTestCase
48  {
49  
50      private InvokerMavenExecutor executor;
51  
52      private SecDispatcher secDispatcher;
53  
54      protected void setUp()
55          throws Exception
56      {
57          super.setUp();
58  
59          executor = (InvokerMavenExecutor) lookup( MavenExecutor.ROLE, "invoker" );
60  
61          secDispatcher = (SecDispatcher) lookup( SecDispatcher.ROLE, "mng-4384" );
62      }
63  
64      @Test
65      public void testThreads()
66          throws Exception
67      {
68          Logger logger = mock( Logger.class );
69          executor.enableLogging( logger );
70  
71          InvocationRequest req = new DefaultInvocationRequest();
72          executor.setupRequest( req, null, "-T 3" );
73          assertEquals( "3", req.getThreads() );
74  
75          req = new DefaultInvocationRequest();
76          executor.setupRequest( req, null, "-T4" );
77          assertEquals( "4", req.getThreads() );
78  
79          req = new DefaultInvocationRequest();
80          executor.setupRequest( req, null, "\"-T5\"" );
81          assertEquals( "5", req.getThreads() );
82      }
83  
84      @Test
85      public void testBatch()
86                    throws Exception
87      {
88          Logger logger = mock( Logger.class );
89          executor.enableLogging( logger );
90  
91          InvocationRequest req = new DefaultInvocationRequest();
92          // bug: assertEquals( true, req.isInteractive() );
93  
94          req = new DefaultInvocationRequest();
95          req.setInteractive( true );
96          executor.setupRequest( req, null, "-B" );
97          assertEquals( false, req.isInteractive() );
98  
99          req = new DefaultInvocationRequest();
100         req.setInteractive( true );
101         executor.setupRequest( req, null, "\"-B\"" );
102         assertEquals( false, req.isInteractive() );
103     }
104 
105     @Test
106     public void testUserToolchains()
107         throws Exception
108     {
109         Logger logger = mock( Logger.class );
110         executor.enableLogging( logger );
111 
112         InvocationRequest req = new DefaultInvocationRequest();
113         executor.setupRequest( req, null, "-t mytoolchains.xml" );
114         assertEquals( new File( "mytoolchains.xml" ), req.getToolchainsFile() );
115 
116         req = new DefaultInvocationRequest();
117         executor.setupRequest( req, null, "-tmytoolchains.xml" );
118         assertEquals( new File( "mytoolchains.xml" ), req.getToolchainsFile() );
119 
120         req = new DefaultInvocationRequest();
121         executor.setupRequest( req, null, "\"-tmytoolchains.xml\"" );
122         assertEquals( new File( "mytoolchains.xml" ), req.getToolchainsFile() );
123     }
124     
125     @Test
126     public void testGlobalSettings()
127         throws Exception
128     {
129         Logger logger = mock( Logger.class );
130         executor.enableLogging( logger );
131 
132         InvocationRequest req = new DefaultInvocationRequest();
133         executor.setupRequest( req, null, "-gs custom-settings.xml" );
134         assertEquals( "custom-settings.xml", req.getGlobalSettingsFile().getPath() );
135 
136         req = new DefaultInvocationRequest();
137         executor.setupRequest( req, null, "--global-settings other-settings.xml" );
138         assertEquals( "other-settings.xml", req.getGlobalSettingsFile().getPath() );
139     }
140 
141     public void testEncryptSettings()
142         throws Exception
143     {
144         // prepare
145         File workingDirectory = getTestFile( "target/working-directory" );
146         workingDirectory.mkdirs();
147         
148         
149         Settings settings = new Settings();
150         Server server = new Server();
151         server.setPassphrase( "server_passphrase" );
152         server.setPassword( "server_password" );
153         settings.addServer( server );
154         Proxy proxy = new Proxy();
155         proxy.setPassword( "proxy_password" );
156         settings.addProxy( proxy );
157 
158         ReleaseEnvironment releaseEnvironment = new DefaultReleaseEnvironment();
159         releaseEnvironment.setSettings( settings );
160         releaseEnvironment.setMavenHome( new File( System.getProperty( "injectedMavenHome" ) ) );
161 
162         InvokerMavenExecutor executorSpy = spy( executor );
163         SettingsXpp3Writer settingsWriter = mock( SettingsXpp3Writer.class );
164 
165         ArgumentCaptor<Settings> encryptedSettings = ArgumentCaptor.forClass( Settings.class );
166 
167         when( executorSpy.getSettingsWriter() ).thenReturn( settingsWriter );
168         when( executorSpy.getOutputHandler() ).thenReturn( null );
169         when( executorSpy.getInvokerLogger() ).thenReturn( null );
170 
171         try
172         {
173             executorSpy.executeGoals( workingDirectory, "validate", releaseEnvironment, false, null, new ReleaseResult() );
174         }
175         catch ( MavenExecutorException e )
176         {
177         }
178 
179         verify( settingsWriter ).write( isA( Writer.class ), encryptedSettings.capture() );
180 
181         assertNotSame( settings, encryptedSettings.getValue() );
182 
183         Server encryptedServer = encryptedSettings.getValue().getServers().get( 0 );
184         assertEquals( "server_passphrase", secDispatcher.decrypt( encryptedServer.getPassphrase() ) );
185         assertEquals( "server_password", secDispatcher.decrypt( encryptedServer.getPassword() ) );
186 
187         Proxy encryptedProxy = encryptedSettings.getValue().getProxies().get( 0 );
188         assertEquals( "proxy_password", secDispatcher.decrypt( encryptedProxy.getPassword() ) );
189 
190         File settingsSecurity = new File( System.getProperty( "user.home" ), ".m2/settings-security.xml" );
191         if ( settingsSecurity.exists() )
192         {
193             assertFalse( "server_passphrase".equals( encryptedServer.getPassphrase() ) );
194             assertFalse( "server_password".equals( encryptedServer.getPassword() ) );
195             assertFalse( "proxy_password".equals( encryptedProxy.getPassword() ) );
196         }
197     }
198 }