001package org.eclipse.aether.impl;
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.Arrays;
025import java.util.List;
026
027import org.eclipse.aether.RepositorySystem;
028import org.eclipse.aether.impl.ArtifactDescriptorReader;
029import org.eclipse.aether.impl.DefaultServiceLocator;
030import org.eclipse.aether.impl.VersionRangeResolver;
031import org.eclipse.aether.impl.VersionResolver;
032import org.eclipse.aether.spi.locator.Service;
033import org.eclipse.aether.spi.locator.ServiceLocator;
034import org.junit.Test;
035
036/**
037 */
038public class DefaultServiceLocatorTest
039{
040
041    @Test
042    public void testGetRepositorySystem()
043    {
044        DefaultServiceLocator locator = new DefaultServiceLocator();
045        locator.addService( ArtifactDescriptorReader.class, StubArtifactDescriptorReader.class );
046        locator.addService( VersionResolver.class, StubVersionResolver.class );
047        locator.addService( VersionRangeResolver.class, StubVersionRangeResolver.class );
048
049        RepositorySystem repoSys = locator.getService( RepositorySystem.class );
050        assertNotNull( repoSys );
051    }
052
053    @Test
054    public void testGetServicesUnmodifiable()
055    {
056        DefaultServiceLocator locator = new DefaultServiceLocator();
057        locator.setServices( String.class, "one", "two" );
058        List<String> services = locator.getServices( String.class );
059        assertNotNull( services );
060        try
061        {
062            services.set( 0, "fail" );
063            fail( "service list is modifable" );
064        }
065        catch ( UnsupportedOperationException e )
066        {
067            // expected
068        }
069    }
070
071    @Test
072    public void testSetInstancesAddClass()
073    {
074        DefaultServiceLocator locator = new DefaultServiceLocator();
075        locator.setServices( String.class, "one", "two" );
076        locator.addService( String.class, String.class );
077        assertEquals( Arrays.asList( "one", "two", "" ), locator.getServices( String.class ) );
078    }
079
080    @Test
081    public void testInitService()
082    {
083        DefaultServiceLocator locator = new DefaultServiceLocator();
084        locator.setService( DummyService.class, DummyService.class );
085        DummyService service = locator.getService( DummyService.class );
086        assertNotNull( service );
087        assertNotNull( service.locator );
088    }
089
090    private static class DummyService
091        implements Service
092    {
093
094        public ServiceLocator locator;
095
096        public void initService( ServiceLocator locator )
097        {
098            this.locator = locator;
099        }
100
101    }
102
103}