001package org.eclipse.aether.util.artifact;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import static org.junit.Assert.*;
023
024import java.io.File;
025import java.util.Collections;
026import java.util.HashMap;
027import java.util.Map;
028
029import org.eclipse.aether.artifact.Artifact;
030import org.eclipse.aether.artifact.DefaultArtifact;
031import org.eclipse.aether.util.artifact.SubArtifact;
032import org.junit.Test;
033
034/**
035 */
036public class SubArtifactTest
037{
038
039    private Artifact newMainArtifact( String coords )
040    {
041        return new DefaultArtifact( coords );
042    }
043
044    @Test
045    public void testMainArtifactFileNotRetained()
046    {
047        Artifact a = newMainArtifact( "gid:aid:ver" ).setFile( new File( "" ) );
048        assertNotNull( a.getFile() );
049        a = new SubArtifact( a, "", "pom" );
050        assertNull( a.getFile() );
051    }
052
053    @Test
054    public void testMainArtifactPropertiesNotRetained()
055    {
056        Artifact a = newMainArtifact( "gid:aid:ver" ).setProperties( Collections.singletonMap( "key", "value" ) );
057        assertEquals( 1, a.getProperties().size() );
058        a = new SubArtifact( a, "", "pom" );
059        assertEquals( 0, a.getProperties().size() );
060        assertSame( null, a.getProperty( "key", null ) );
061    }
062
063    @Test( expected = NullPointerException.class )
064    public void testMainArtifactMissing()
065    {
066        new SubArtifact( null, "", "pom" );
067    }
068
069    @Test
070    public void testEmptyClassifier()
071    {
072        Artifact main = newMainArtifact( "gid:aid:ext:cls:ver" );
073        Artifact sub = new SubArtifact( main, "", "pom" );
074        assertEquals( "", sub.getClassifier() );
075        sub = new SubArtifact( main, null, "pom" );
076        assertEquals( "", sub.getClassifier() );
077    }
078
079    @Test
080    public void testEmptyExtension()
081    {
082        Artifact main = newMainArtifact( "gid:aid:ext:cls:ver" );
083        Artifact sub = new SubArtifact( main, "tests", "" );
084        assertEquals( "", sub.getExtension() );
085        sub = new SubArtifact( main, "tests", null );
086        assertEquals( "", sub.getExtension() );
087    }
088
089    @Test
090    public void testSameClassifier()
091    {
092        Artifact main = newMainArtifact( "gid:aid:ext:cls:ver" );
093        Artifact sub = new SubArtifact( main, "*", "pom" );
094        assertEquals( "cls", sub.getClassifier() );
095    }
096
097    @Test
098    public void testSameExtension()
099    {
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}