001package org.eclipse.aether.repository;
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.util.ArrayList;
025import java.util.Arrays;
026import java.util.List;
027import java.util.Map;
028
029import org.eclipse.aether.repository.RemoteRepository.Builder;
030import org.junit.Before;
031import org.junit.Test;
032
033public class RemoteRepositoryBuilderTest
034{
035
036    private RemoteRepository prototype;
037
038    @Before
039    public void init()
040    {
041        prototype = new Builder( "id", "type", "file:void" ).build();
042    }
043
044    @Test
045    public void testReusePrototype()
046    {
047        Builder builder = new Builder( prototype );
048        assertSame( prototype, builder.build() );
049    }
050
051    @Test( expected = NullPointerException.class )
052    public void testPrototypeMandatory()
053    {
054        new Builder( null );
055    }
056
057    @Test
058    public void testSetId()
059    {
060        Builder builder = new Builder( prototype );
061        RemoteRepository repo = builder.setId( prototype.getId() ).build();
062        assertSame( prototype, repo );
063        repo = builder.setId( "new-id" ).build();
064        assertEquals( "new-id", repo.getId() );
065    }
066
067    @Test
068    public void testSetContentType()
069    {
070        Builder builder = new Builder( prototype );
071        RemoteRepository repo = builder.setContentType( prototype.getContentType() ).build();
072        assertSame( prototype, repo );
073        repo = builder.setContentType( "new-type" ).build();
074        assertEquals( "new-type", repo.getContentType() );
075    }
076
077    @Test
078    public void testSetUrl()
079    {
080        Builder builder = new Builder( prototype );
081        RemoteRepository repo = builder.setUrl( prototype.getUrl() ).build();
082        assertSame( prototype, repo );
083        repo = builder.setUrl( "file:new" ).build();
084        assertEquals( "file:new", repo.getUrl() );
085    }
086
087    @Test
088    public void testSetPolicy()
089    {
090        Builder builder = new Builder( prototype );
091        RemoteRepository repo = builder.setPolicy( prototype.getPolicy( false ) ).build();
092        assertSame( prototype, repo );
093        RepositoryPolicy policy = new RepositoryPolicy( true, "never", "fail" );
094        repo = builder.setPolicy( policy ).build();
095        assertEquals( policy, repo.getPolicy( true ) );
096        assertEquals( policy, repo.getPolicy( false ) );
097    }
098
099    @Test
100    public void testSetReleasePolicy()
101    {
102        Builder builder = new Builder( prototype );
103        RemoteRepository repo = builder.setReleasePolicy( prototype.getPolicy( false ) ).build();
104        assertSame( prototype, repo );
105        RepositoryPolicy policy = new RepositoryPolicy( true, "never", "fail" );
106        repo = builder.setReleasePolicy( policy ).build();
107        assertEquals( policy, repo.getPolicy( false ) );
108        assertEquals( prototype.getPolicy( true ), repo.getPolicy( true ) );
109    }
110
111    @Test
112    public void testSetSnapshotPolicy()
113    {
114        Builder builder = new Builder( prototype );
115        RemoteRepository repo = builder.setSnapshotPolicy( prototype.getPolicy( true ) ).build();
116        assertSame( prototype, repo );
117        RepositoryPolicy policy = new RepositoryPolicy( true, "never", "fail" );
118        repo = builder.setSnapshotPolicy( policy ).build();
119        assertEquals( policy, repo.getPolicy( true ) );
120        assertEquals( prototype.getPolicy( false ), repo.getPolicy( false ) );
121    }
122
123    @Test
124    public void testSetProxy()
125    {
126        Builder builder = new Builder( prototype );
127        RemoteRepository repo = builder.setProxy( prototype.getProxy() ).build();
128        assertSame( prototype, repo );
129        Proxy proxy = new Proxy( "http", "localhost", 8080 );
130        repo = builder.setProxy( proxy ).build();
131        assertEquals( proxy, repo.getProxy() );
132    }
133
134    @Test
135    public void testSetAuthentication()
136    {
137        Builder builder = new Builder( prototype );
138        RemoteRepository repo = builder.setAuthentication( prototype.getAuthentication() ).build();
139        assertSame( prototype, repo );
140        Authentication auth = new Authentication()
141        {
142            public void fill( AuthenticationContext context, String key, Map<String, String> data )
143            {
144            }
145
146            public void digest( AuthenticationDigest digest )
147            {
148            }
149        };
150        repo = builder.setAuthentication( auth ).build();
151        assertEquals( auth, repo.getAuthentication() );
152    }
153
154    @Test
155    public void testSetMirroredRepositories()
156    {
157        Builder builder = new Builder( prototype );
158        RemoteRepository repo = builder.setMirroredRepositories( prototype.getMirroredRepositories() ).build();
159        assertSame( prototype, repo );
160        List<RemoteRepository> mirrored = new ArrayList<>( Arrays.asList( repo ) );
161        repo = builder.setMirroredRepositories( mirrored ).build();
162        assertEquals( mirrored, repo.getMirroredRepositories() );
163    }
164
165    @Test
166    public void testAddMirroredRepository()
167    {
168        Builder builder = new Builder( prototype );
169        RemoteRepository repo = builder.addMirroredRepository( null ).build();
170        assertSame( prototype, repo );
171        repo = builder.addMirroredRepository( prototype ).build();
172        assertEquals( Arrays.asList( prototype ), repo.getMirroredRepositories() );
173    }
174
175    @Test
176    public void testSetRepositoryManager()
177    {
178        Builder builder = new Builder( prototype );
179        RemoteRepository repo = builder.setRepositoryManager( prototype.isRepositoryManager() ).build();
180        assertSame( prototype, repo );
181        repo = builder.setRepositoryManager( !prototype.isRepositoryManager() ).build();
182        assertEquals( !prototype.isRepositoryManager(), repo.isRepositoryManager() );
183    }
184
185}