/*
 * Copyright 2014 Paul Merlin.
 *
 * Licensed  under the  Apache License,  Version 2.0  (the "License");
 * you may not use  this file  except in  compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed  under the  License is distributed on an "AS IS" BASIS,
 * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
 * implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.qi4j.test.util;

import java.awt.GraphicsEnvironment;

import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.notNullValue;

/**
 * A set of methods useful for stating assumptions about the conditions in which a test is meaningful.
 */
public class Assume
    extends org.junit.Assume
{

    /**
     * If called on a IBM JDK, the test will halt and be ignored.
     */
    public static void assumeNoIbmJdk()
    {
        assumeFalse( System.getProperty( "java.vendor" ).contains( "IBM" ) );
    }

    /**
     * If called on a headless runtime, the test will halt and be ignored.
     */
    public static void assumeDisplayPresent()
    {
        assumeFalse( GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadlessInstance() );
        String display = System.getenv( "DISPLAY" );
        assumeThat( display, is( notNullValue() ) );
        assumeTrue( display.length() > 0 );
    }

}

@@ -671,16 +671,303 @@
 ronment;
+%0Aimport java.io.IOException;%0Aimport java.net.InetSocketAddress;%0Aimport java.net.NetworkInterface;%0Aimport java.net.Socket;%0Aimport java.net.SocketException;%0Aimport java.net.UnknownHostException;%0Aimport java.util.Enumeration;%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger;
 %0A%0Aimport
@@ -1824,10 +1824,1143 @@
 %0A    %7D%0A%0A
+    /**%0A     * If called on a networkless runtime, the test will halt and be ignored.%0A     */%0A    public static void assumeNetworking()%0A    %7B%0A        try%0A        %7B%0A            Enumeration%3CNetworkInterface%3E ifaces = NetworkInterface.getNetworkInterfaces();%0A            assumeThat( ifaces, is( notNullValue() ) );%0A            assumeTrue( ifaces.hasMoreElements() );%0A        %7D%0A        catch( SocketException ex )%0A        %7B%0A            assumeFalse( true );%0A        %7D%0A    %7D%0A%0A    /**%0A     * If called on a runtime with to access to qi4j.org on port 80, the test will halt and be ignored.%0A     */%0A    public static void assumeConnectivity()%0A    %7B%0A        assumeConnectivity( %22qi4j.org%22, 80 );%0A    %7D%0A%0A    /**%0A     * If called on a runtime with to access to given host and port, the test will halt and be ignored.%0A     *%0A     * @param host Host%0A     * @param port Port%0A     */%0A    public static void assumeConnectivity( String host, int port )%0A    %7B%0A        try( Socket socket = new Socket( host, port ) )%0A        %7B%0A            // Connected%0A        %7D%0A        catch( IOException ex )%0A        %7B%0A            assumeFalse( true );%0A        %7D%0A    %7D%0A
 %7D%0A

introduce Assume.assumeConnectivity