View Javadoc
1   package org.apache.maven.wrapper;
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.Mockito.mock;
23  import static org.mockito.Mockito.verify;
24  import static org.mockito.Mockito.when;
25  
26  import java.io.OutputStream;
27  import java.net.URI;
28  import java.nio.file.Files;
29  import java.nio.file.Path;
30  import java.nio.file.Paths;
31  import java.util.Properties;
32  
33  import org.junit.Assert;
34  import org.junit.Test;
35  import org.mockito.Mockito;
36  
37  public class WrapperExecutorTest
38  {
39    private final Installer install;
40  
41    private final BootstrapMainStarter start;
42  
43    private Path propertiesFile;
44  
45    private Properties properties = new Properties();
46  
47    private Path testDir = Paths.get( "target/test-files/SystemPropertiesHandlerTest-" + System.currentTimeMillis() );
48  
49    private Path mockInstallDir = testDir.resolve( "mock-dir" );
50  
51    public WrapperExecutorTest()
52      throws Exception
53    {
54      install = mock( Installer.class );
55      when( install.createDist( Mockito.any( WrapperConfiguration.class ) ) ).thenReturn( mockInstallDir );
56      start = mock( BootstrapMainStarter.class );
57  
58      Files.createDirectories( testDir );
59      propertiesFile = testDir.resolve( "maven/wrapper/maven-wrapper.properties" );
60  
61      properties.put( "distributionUrl", "http://server/test/maven.zip" );
62      properties.put( "distributionBase", "testDistBase" );
63      properties.put( "distributionPath", "testDistPath" );
64      properties.put( "zipStoreBase", "testZipBase" );
65      properties.put( "zipStorePath", "testZipPath" );
66  
67      writePropertiesFile( properties, propertiesFile, "header" );
68  
69    }
70  
71    @Test
72    public void loadWrapperMetadataFromFile()
73      throws Exception
74    {
75      WrapperExecutor wrapper = WrapperExecutor.forWrapperPropertiesFile( propertiesFile );
76  
77      Assert.assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getDistribution() );
78      Assert.assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getConfiguration().getDistribution() );
79      Assert.assertEquals( "testDistBase", wrapper.getConfiguration().getDistributionBase() );
80      Assert.assertEquals( "testDistPath", wrapper.getConfiguration().getDistributionPath().toString() );
81      Assert.assertEquals( "testZipBase", wrapper.getConfiguration().getZipBase() );
82      Assert.assertEquals( "testZipPath", wrapper.getConfiguration().getZipPath().toString() );
83    }
84  
85    @Test
86    public void loadWrapperMetadataFromDirectory()
87      throws Exception
88    {
89      WrapperExecutor wrapper = WrapperExecutor.forProjectDirectory( testDir );
90  
91      Assert.assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getDistribution() );
92      Assert.assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getConfiguration().getDistribution() );
93      Assert.assertEquals( "testDistBase", wrapper.getConfiguration().getDistributionBase() );
94      Assert.assertEquals( "testDistPath", wrapper.getConfiguration().getDistributionPath().toString() );
95      Assert.assertEquals( "testZipBase", wrapper.getConfiguration().getZipBase() );
96      Assert.assertEquals( "testZipPath", wrapper.getConfiguration().getZipPath().toString() );
97    }
98  
99    @Test
100   public void useDefaultMetadataNoProeprtiesFile()
101     throws Exception
102   {
103     WrapperExecutor wrapper = WrapperExecutor.forProjectDirectory( testDir.resolve( "unknown" ) );
104 
105     Assert.assertNull( wrapper.getDistribution() );
106     Assert.assertNull( wrapper.getConfiguration().getDistribution() );
107     Assert.assertEquals( PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getDistributionBase() );
108     Assert.assertEquals( Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getDistributionPath() );
109     Assert.assertEquals( PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getZipBase() );
110     Assert.assertEquals( Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getZipPath() );
111   }
112 
113   @Test
114   public void propertiesFileOnlyContainsDistURL()
115     throws Exception
116   {
117 
118     properties = new Properties();
119     properties.put( "distributionUrl", "http://server/test/maven.zip" );
120     writePropertiesFile( properties, propertiesFile, "header" );
121 
122     WrapperExecutor wrapper = WrapperExecutor.forWrapperPropertiesFile( propertiesFile );
123 
124     Assert.assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getDistribution() );
125     Assert.assertEquals( new URI( "http://server/test/maven.zip" ), wrapper.getConfiguration().getDistribution() );
126     Assert.assertEquals( PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getDistributionBase() );
127     Assert.assertEquals( Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getDistributionPath() );
128     Assert.assertEquals( PathAssembler.MAVEN_USER_HOME_STRING, wrapper.getConfiguration().getZipBase() );
129     Assert.assertEquals( Installer.DEFAULT_DISTRIBUTION_PATH, wrapper.getConfiguration().getZipPath() );
130   }
131 
132   @Test
133   public void executeInstallAndLaunch()
134     throws Exception
135   {
136     WrapperExecutor wrapper = WrapperExecutor.forProjectDirectory( propertiesFile );
137 
138     wrapper.execute( new String[] { "arg" }, install, start );
139     verify( install ).createDist( Mockito.any( WrapperConfiguration.class ) );
140     verify( start ).start( new String[] { "arg" }, mockInstallDir );
141   }
142 
143   @Test( )
144   public void failWhenDistNotSetInProperties()
145     throws Exception
146   {
147     properties = new Properties();
148     writePropertiesFile( properties, propertiesFile, "header" );
149 
150     try
151     {
152       WrapperExecutor.forWrapperPropertiesFile( propertiesFile );
153       Assert.fail( "Expected RuntimeException" );
154     }
155     catch ( RuntimeException e )
156     {
157       Assert.assertEquals( "Could not load wrapper properties from '" + propertiesFile + "'.", e.getMessage() );
158       Assert.assertEquals( "No value with key 'distributionUrl' specified in wrapper properties file '" + propertiesFile
159         + "'.", e.getCause().getMessage() );
160     }
161 
162   }
163 
164   @Test
165   public void failWhenPropertiesFileDoesNotExist()
166   {
167     propertiesFile = testDir.resolve( "unknown.properties" );
168 
169     try
170     {
171       WrapperExecutor.forWrapperPropertiesFile( propertiesFile );
172       Assert.fail( "Expected RuntimeException" );
173     }
174     catch ( RuntimeException e )
175     {
176       Assert.assertEquals( "Wrapper properties file '" + propertiesFile + "' does not exist.", e.getMessage() );
177     }
178   }
179 
180   @Test
181   public void testRelativeDistUrl()
182     throws Exception
183   {
184 
185     properties = new Properties();
186     properties.put( "distributionUrl", "some/relative/url/to/bin.zip" );
187     writePropertiesFile( properties, propertiesFile, "header" );
188 
189     WrapperExecutor wrapper = WrapperExecutor.forWrapperPropertiesFile( propertiesFile );
190     Assert.assertNotEquals( "some/relative/url/to/bin.zip", wrapper.getDistribution().getSchemeSpecificPart() );
191     Assert.assertTrue( wrapper.getDistribution().getSchemeSpecificPart().endsWith( "some/relative/url/to/bin.zip" ) );
192   }
193 
194   private void writePropertiesFile( Properties properties, Path propertiesFile, String message )
195     throws Exception
196   {
197     Files.createDirectories( propertiesFile.getParent() );
198     try ( OutputStream outStream = Files.newOutputStream( propertiesFile ) )
199     {
200       properties.store( outStream, message );
201     }
202   }
203 }