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