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