View Javadoc
1   package org.eclipse.aether.internal.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 org.eclipse.aether.DefaultRepositorySystemSession;
23  import org.eclipse.aether.RepositorySystemSession;
24  import org.eclipse.aether.repository.RemoteRepository;
25  import org.eclipse.aether.transfer.RepositoryOfflineException;
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  public class DefaultOfflineControllerTest
30  {
31  
32      private DefaultOfflineController controller;
33  
34      private RepositorySystemSession newSession( boolean offline, String protocols, String hosts )
35      {
36          DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
37          session.setOffline( offline );
38          session.setConfigProperty( DefaultOfflineController.CONFIG_PROP_OFFLINE_PROTOCOLS, protocols );
39          session.setConfigProperty( DefaultOfflineController.CONFIG_PROP_OFFLINE_HOSTS, hosts );
40          return session;
41      }
42  
43      private RemoteRepository newRepo( String url )
44      {
45          return new RemoteRepository.Builder( "central", "default", url ).build();
46      }
47  
48      @Before
49      public void setup()
50      {
51          controller = new DefaultOfflineController();
52      }
53  
54      @Test( expected = RepositoryOfflineException.class )
55      public void testCheckOffline_Online()
56          throws Exception
57      {
58          controller.checkOffline( newSession( false, null, null ), newRepo( "http://eclipse.org" ) );
59      }
60  
61      @Test( expected = RepositoryOfflineException.class )
62      public void testCheckOffline_Offline()
63          throws Exception
64      {
65          controller.checkOffline( newSession( true, null, null ), newRepo( "http://eclipse.org" ) );
66      }
67  
68      @Test
69      public void testCheckOffline_Offline_OfflineProtocol()
70          throws Exception
71      {
72          controller.checkOffline( newSession( true, "file", null ), newRepo( "file://repo" ) );
73          controller.checkOffline( newSession( true, "file", null ), newRepo( "FILE://repo" ) );
74          controller.checkOffline( newSession( true, "  file  ,  classpath  ", null ), newRepo( "file://repo" ) );
75          controller.checkOffline( newSession( true, "  file  ,  classpath  ", null ), newRepo( "classpath://repo" ) );
76      }
77  
78      @Test( expected = RepositoryOfflineException.class )
79      public void testCheckOffline_Offline_OnlineProtocol()
80          throws Exception
81      {
82          controller.checkOffline( newSession( true, "file", null ), newRepo( "http://eclipse.org" ) );
83      }
84  
85      @Test
86      public void testCheckOffline_Offline_OfflineHost()
87          throws Exception
88      {
89          controller.checkOffline( newSession( true, null, "localhost" ), newRepo( "http://localhost" ) );
90          controller.checkOffline( newSession( true, null, "localhost" ), newRepo( "http://LOCALHOST" ) );
91          controller.checkOffline( newSession( true, null, "  localhost  ,  127.0.0.1  " ), newRepo( "http://localhost" ) );
92          controller.checkOffline( newSession( true, null, "  localhost  ,  127.0.0.1  " ), newRepo( "http://127.0.0.1" ) );
93      }
94  
95      @Test( expected = RepositoryOfflineException.class )
96      public void testCheckOffline_Offline_OnlineHost()
97          throws Exception
98      {
99          controller.checkOffline( newSession( true, null, "localhost" ), newRepo( "http://eclipse.org" ) );
100     }
101 
102 }