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