View Javadoc
1   package org.apache.maven.shared.transfer.artifact.install.internal;
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.when;
24  
25  import java.io.File;
26  import java.util.Collections;
27  import java.util.List;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.project.ProjectBuildingRequest;
31  import org.apache.maven.shared.transfer.artifact.install.ArtifactInstaller;
32  import org.apache.maven.shared.transfer.artifact.install.ArtifactInstallerException;
33  import org.apache.maven.shared.transfer.artifact.install.internal.DefaultArtifactInstaller;
34  import org.junit.Rule;
35  import org.junit.Test;
36  import org.junit.rules.ExpectedException;
37  
38  /**
39   * Check the parameter contracts which have been made based on the interface {@link ArtifactInstaller}.
40   * 
41   * @author Karl Heinz Marbaise <a href="mailto:khmarbaise@apache.org">khmabaise@apache.org</a>
42   */
43  public class DefaultArtifactInstallerTest
44  {
45      @Rule
46      public ExpectedException thrown = ExpectedException.none();
47  
48      @Test
49      public void installShouldReturnIllegalArgumentExceptionForFirstParameterWithNull() throws IllegalArgumentException, ArtifactInstallerException
50      {
51          DefaultArtifactInstaller dai = new DefaultArtifactInstaller();
52  
53          thrown.expect( IllegalArgumentException.class );
54          thrown.expectMessage( "The parameter request is not allowed to be null." );
55          dai.install( null, Collections.<Artifact>emptyList() );
56      }
57  
58      @Test
59      public void installShouldReturnIllegalArgumentExceptionForSecondParameterWithNull()
60          throws ArtifactInstallerException
61      {
62          DefaultArtifactInstaller dai = new DefaultArtifactInstaller();
63  
64          thrown.expect( IllegalArgumentException.class );
65          thrown.expectMessage( "The parameter mavenArtifacts is not allowed to be null." );
66          ProjectBuildingRequest pbr = mock( ProjectBuildingRequest.class );
67          dai.install( pbr, null );
68      }
69  
70      @Test
71      public void installShouldReturnIllegalArgumentExceptionForSecondParameterWithEmpty()
72          throws ArtifactInstallerException
73      {
74          DefaultArtifactInstaller dai = new DefaultArtifactInstaller();
75  
76          thrown.expect( IllegalArgumentException.class );
77          thrown.expectMessage( "The collection mavenArtifacts is not allowed to be empty." );
78          ProjectBuildingRequest pbr = mock( ProjectBuildingRequest.class );
79          dai.install( pbr, Collections.<Artifact>emptyList() );
80      }
81  
82      @Test
83      public void installWith3ParametersShouldReturnIllegalArgumentExceptionForFirstParameterWithNull()
84          throws ArtifactInstallerException
85      {
86          DefaultArtifactInstaller dai = new DefaultArtifactInstaller();
87  
88          thrown.expect( IllegalArgumentException.class );
89          thrown.expectMessage( "The parameter request is not allowed to be null." );
90          File localRepository = mock( File.class );
91          dai.install( null, localRepository, Collections.<Artifact>emptyList() );
92      }
93  
94      @Test
95      public void installWith3ParametersShouldReturnIllegalArgumentExceptionForSecondParameterWithNull()
96          throws ArtifactInstallerException
97      {
98          DefaultArtifactInstaller dai = new DefaultArtifactInstaller();
99  
100         thrown.expect( IllegalArgumentException.class );
101         thrown.expectMessage( "The parameter localRepository is not allowed to be null." );
102 
103         List<Artifact> singleEntryList = Collections.singletonList( mock( Artifact.class ) );
104         ProjectBuildingRequest pbr = mock( ProjectBuildingRequest.class );
105         dai.install( pbr, null, singleEntryList );
106     }
107 
108     @Test
109     public void installWith3ParametersShouldReturnIllegalArgumentExceptionForSecondParameterNotADirectory()
110         throws ArtifactInstallerException
111     {
112         DefaultArtifactInstaller dai = new DefaultArtifactInstaller();
113 
114         thrown.expect( IllegalArgumentException.class );
115         thrown.expectMessage( "The parameter localRepository must be a directory." );
116         ProjectBuildingRequest pbr = mock( ProjectBuildingRequest.class );
117         List<Artifact> singleEntryList = Collections.singletonList( mock( Artifact.class ) );
118 
119         File localRepository = mock( File.class );
120         when( localRepository.isDirectory() ).thenReturn( false );
121         dai.install( pbr, localRepository, singleEntryList );
122     }
123 
124     @Test
125     public void installWith3ParametersShouldReturnIllegalArgumentExceptionForThirdParameterWithNull()
126         throws ArtifactInstallerException
127     {
128         DefaultArtifactInstaller dai = new DefaultArtifactInstaller();
129 
130         thrown.expect( IllegalArgumentException.class );
131         thrown.expectMessage( "The parameter mavenArtifacts is not allowed to be null." );
132         ProjectBuildingRequest pbr = mock( ProjectBuildingRequest.class );
133         File localRepository = mock( File.class );
134         dai.install( pbr, localRepository, null );
135     }
136 
137 }