View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.impl;
20  
21  import java.util.Arrays;
22  import java.util.List;
23  
24  import org.eclipse.aether.RepositorySystem;
25  import org.eclipse.aether.spi.locator.Service;
26  import org.eclipse.aether.spi.locator.ServiceLocator;
27  import org.junit.Test;
28  
29  import static org.junit.Assert.*;
30  
31  /**
32   */
33  public class DefaultServiceLocatorTest {
34  
35      @Test
36      public void testGetRepositorySystem() {
37          DefaultServiceLocator locator = new DefaultServiceLocator();
38          locator.addService(ArtifactDescriptorReader.class, StubArtifactDescriptorReader.class);
39          locator.addService(VersionResolver.class, StubVersionResolver.class);
40          locator.addService(VersionRangeResolver.class, StubVersionRangeResolver.class);
41  
42          RepositorySystem repoSys = locator.getService(RepositorySystem.class);
43          assertNotNull(repoSys);
44      }
45  
46      @Test
47      public void testGetServicesUnmodifiable() {
48          DefaultServiceLocator locator = new DefaultServiceLocator();
49          locator.setServices(String.class, "one", "two");
50          List<String> services = locator.getServices(String.class);
51          assertNotNull(services);
52          try {
53              services.set(0, "fail");
54              fail("service list is modifable");
55          } catch (UnsupportedOperationException e) {
56              // expected
57          }
58      }
59  
60      @Test
61      public void testSetInstancesAddClass() {
62          DefaultServiceLocator locator = new DefaultServiceLocator();
63          locator.setServices(String.class, "one", "two");
64          locator.addService(String.class, String.class);
65          assertEquals(Arrays.asList("one", "two", ""), locator.getServices(String.class));
66      }
67  
68      @Test
69      public void testInitService() {
70          DefaultServiceLocator locator = new DefaultServiceLocator();
71          locator.setService(DummyService.class, DummyService.class);
72          DummyService service = locator.getService(DummyService.class);
73          assertNotNull(service);
74          assertNotNull(service.locator);
75      }
76  
77      private static class DummyService implements Service {
78  
79          public ServiceLocator locator;
80  
81          public void initService(ServiceLocator locator) {
82              this.locator = locator;
83          }
84      }
85  }