View Javadoc
1   package org.eclipse.aether.impl;
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.util.Arrays;
25  import java.util.List;
26  
27  import org.eclipse.aether.RepositorySystem;
28  import org.eclipse.aether.spi.locator.Service;
29  import org.eclipse.aether.spi.locator.ServiceLocator;
30  import org.junit.Test;
31  
32  /**
33   */
34  public class DefaultServiceLocatorTest
35  {
36  
37      @Test
38      public void testGetRepositorySystem()
39      {
40          DefaultServiceLocator locator = new DefaultServiceLocator();
41          locator.addService( ArtifactDescriptorReader.class, StubArtifactDescriptorReader.class );
42          locator.addService( VersionResolver.class, StubVersionResolver.class );
43          locator.addService( VersionRangeResolver.class, StubVersionRangeResolver.class );
44  
45          RepositorySystem repoSys = locator.getService( RepositorySystem.class );
46          assertNotNull( repoSys );
47      }
48  
49      @Test
50      public void testGetServicesUnmodifiable()
51      {
52          DefaultServiceLocator locator = new DefaultServiceLocator();
53          locator.setServices( String.class, "one", "two" );
54          List<String> services = locator.getServices( String.class );
55          assertNotNull( services );
56          try
57          {
58              services.set( 0, "fail" );
59              fail( "service list is modifable" );
60          }
61          catch ( UnsupportedOperationException e )
62          {
63              // expected
64          }
65      }
66  
67      @Test
68      public void testSetInstancesAddClass()
69      {
70          DefaultServiceLocator locator = new DefaultServiceLocator();
71          locator.setServices( String.class, "one", "two" );
72          locator.addService( String.class, String.class );
73          assertEquals( Arrays.asList( "one", "two", "" ), locator.getServices( String.class ) );
74      }
75  
76      @Test
77      public void testInitService()
78      {
79          DefaultServiceLocator locator = new DefaultServiceLocator();
80          locator.setService( DummyService.class, DummyService.class );
81          DummyService service = locator.getService( DummyService.class );
82          assertNotNull( service );
83          assertNotNull( service.locator );
84      }
85  
86      private static class DummyService
87          implements Service
88      {
89  
90          public ServiceLocator locator;
91  
92          public void initService( ServiceLocator locator )
93          {
94              this.locator = locator;
95          }
96  
97      }
98  
99  }