001package org.eclipse.aether.internal.impl;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.eclipse.aether.DefaultRepositorySystemSession;
023import org.eclipse.aether.RepositorySystemSession;
024import org.eclipse.aether.repository.RemoteRepository;
025import org.eclipse.aether.transfer.RepositoryOfflineException;
026import org.junit.Before;
027import org.junit.Test;
028
029public class DefaultOfflineControllerTest
030{
031
032    private DefaultOfflineController controller;
033
034    private RepositorySystemSession newSession( boolean offline, String protocols, String hosts )
035    {
036        DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
037        session.setOffline( offline );
038        session.setConfigProperty( DefaultOfflineController.CONFIG_PROP_OFFLINE_PROTOCOLS, protocols );
039        session.setConfigProperty( DefaultOfflineController.CONFIG_PROP_OFFLINE_HOSTS, hosts );
040        return session;
041    }
042
043    private RemoteRepository newRepo( String url )
044    {
045        return new RemoteRepository.Builder( "central", "default", url ).build();
046    }
047
048    @Before
049    public void setup()
050    {
051        controller = new DefaultOfflineController();
052    }
053
054    @Test( expected = RepositoryOfflineException.class )
055    public void testCheckOffline_Online()
056        throws Exception
057    {
058        controller.checkOffline( newSession( false, null, null ), newRepo( "http://eclipse.org" ) );
059    }
060
061    @Test( expected = RepositoryOfflineException.class )
062    public void testCheckOffline_Offline()
063        throws Exception
064    {
065        controller.checkOffline( newSession( true, null, null ), newRepo( "http://eclipse.org" ) );
066    }
067
068    @Test
069    public void testCheckOffline_Offline_OfflineProtocol()
070        throws Exception
071    {
072        controller.checkOffline( newSession( true, "file", null ), newRepo( "file://repo" ) );
073        controller.checkOffline( newSession( true, "file", null ), newRepo( "FILE://repo" ) );
074        controller.checkOffline( newSession( true, "  file  ,  classpath  ", null ), newRepo( "file://repo" ) );
075        controller.checkOffline( newSession( true, "  file  ,  classpath  ", null ), newRepo( "classpath://repo" ) );
076    }
077
078    @Test( expected = RepositoryOfflineException.class )
079    public void testCheckOffline_Offline_OnlineProtocol()
080        throws Exception
081    {
082        controller.checkOffline( newSession( true, "file", null ), newRepo( "http://eclipse.org" ) );
083    }
084
085    @Test
086    public void testCheckOffline_Offline_OfflineHost()
087        throws Exception
088    {
089        controller.checkOffline( newSession( true, null, "localhost" ), newRepo( "http://localhost" ) );
090        controller.checkOffline( newSession( true, null, "localhost" ), newRepo( "http://LOCALHOST" ) );
091        controller.checkOffline( newSession( true, null, "  localhost  ,  127.0.0.1  " ), newRepo( "http://localhost" ) );
092        controller.checkOffline( newSession( true, null, "  localhost  ,  127.0.0.1  " ), newRepo( "http://127.0.0.1" ) );
093    }
094
095    @Test( expected = RepositoryOfflineException.class )
096    public void testCheckOffline_Offline_OnlineHost()
097        throws Exception
098    {
099        controller.checkOffline( newSession( true, null, "localhost" ), newRepo( "http://eclipse.org" ) );
100    }
101
102}