View Javadoc
1   package org.eclipse.aether.util.artifact;
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.junit.Assert.*;
23  
24  import java.io.File;
25  import java.util.Collections;
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import org.eclipse.aether.artifact.Artifact;
30  import org.eclipse.aether.artifact.DefaultArtifact;
31  import org.eclipse.aether.util.artifact.SubArtifact;
32  import org.junit.Test;
33  
34  /**
35   */
36  public class SubArtifactTest
37  {
38  
39      private Artifact newMainArtifact( String coords )
40      {
41          return new DefaultArtifact( coords );
42      }
43  
44      @Test
45      public void testMainArtifactFileNotRetained()
46      {
47          Artifact a = newMainArtifact( "gid:aid:ver" ).setFile( new File( "" ) );
48          assertNotNull( a.getFile() );
49          a = new SubArtifact( a, "", "pom" );
50          assertNull( a.getFile() );
51      }
52  
53      @Test
54      public void testMainArtifactPropertiesNotRetained()
55      {
56          Artifact a = newMainArtifact( "gid:aid:ver" ).setProperties( Collections.singletonMap( "key", "value" ) );
57          assertEquals( 1, a.getProperties().size() );
58          a = new SubArtifact( a, "", "pom" );
59          assertEquals( 0, a.getProperties().size() );
60          assertSame( null, a.getProperty( "key", null ) );
61      }
62  
63      @Test( expected = NullPointerException.class )
64      public void testMainArtifactMissing()
65      {
66          new SubArtifact( null, "", "pom" );
67      }
68  
69      @Test
70      public void testEmptyClassifier()
71      {
72          Artifact main = newMainArtifact( "gid:aid:ext:cls:ver" );
73          Artifact sub = new SubArtifact( main, "", "pom" );
74          assertEquals( "", sub.getClassifier() );
75          sub = new SubArtifact( main, null, "pom" );
76          assertEquals( "", sub.getClassifier() );
77      }
78  
79      @Test
80      public void testEmptyExtension()
81      {
82          Artifact main = newMainArtifact( "gid:aid:ext:cls:ver" );
83          Artifact sub = new SubArtifact( main, "tests", "" );
84          assertEquals( "", sub.getExtension() );
85          sub = new SubArtifact( main, "tests", null );
86          assertEquals( "", sub.getExtension() );
87      }
88  
89      @Test
90      public void testSameClassifier()
91      {
92          Artifact main = newMainArtifact( "gid:aid:ext:cls:ver" );
93          Artifact sub = new SubArtifact( main, "*", "pom" );
94          assertEquals( "cls", sub.getClassifier() );
95      }
96  
97      @Test
98      public void testSameExtension()
99      {
100         Artifact main = newMainArtifact( "gid:aid:ext:cls:ver" );
101         Artifact sub = new SubArtifact( main, "tests", "*" );
102         assertEquals( "ext", sub.getExtension() );
103     }
104 
105     @Test
106     public void testDerivedClassifier()
107     {
108         Artifact main = newMainArtifact( "gid:aid:ext:cls:ver" );
109         Artifact sub = new SubArtifact( main, "*-tests", "pom" );
110         assertEquals( "cls-tests", sub.getClassifier() );
111         sub = new SubArtifact( main, "tests-*", "pom" );
112         assertEquals( "tests-cls", sub.getClassifier() );
113 
114         main = newMainArtifact( "gid:aid:ext:ver" );
115         sub = new SubArtifact( main, "*-tests", "pom" );
116         assertEquals( "tests", sub.getClassifier() );
117         sub = new SubArtifact( main, "tests-*", "pom" );
118         assertEquals( "tests", sub.getClassifier() );
119     }
120 
121     @Test
122     public void testDerivedExtension()
123     {
124         Artifact main = newMainArtifact( "gid:aid:ext:cls:ver" );
125         Artifact sub = new SubArtifact( main, "", "*.asc" );
126         assertEquals( "ext.asc", sub.getExtension() );
127         sub = new SubArtifact( main, "", "asc.*" );
128         assertEquals( "asc.ext", sub.getExtension() );
129     }
130 
131     @Test
132     public void testImmutability()
133     {
134         Artifact a = new SubArtifact( newMainArtifact( "gid:aid:ver" ), "", "pom" );
135         assertNotSame( a, a.setFile( new File( "file" ) ) );
136         assertNotSame( a, a.setVersion( "otherVersion" ) );
137         assertNotSame( a, a.setProperties( Collections.singletonMap( "key", "value" ) ) );
138     }
139 
140     @Test
141     public void testPropertiesCopied()
142     {
143         Map<String, String> props = new HashMap<>();
144         props.put( "key", "value1" );
145 
146         Artifact a = new SubArtifact( newMainArtifact( "gid:aid:ver" ), "", "pom", props, null );
147         assertEquals( "value1", a.getProperty( "key", null ) );
148         props.clear();
149         assertEquals( "value1", a.getProperty( "key", null ) );
150 
151         props.put( "key", "value2" );
152         a = a.setProperties( props );
153         assertEquals( "value2", a.getProperty( "key", null ) );
154         props.clear();
155         assertEquals( "value2", a.getProperty( "key", null ) );
156     }
157 
158 }