All Downloads are FREE. Search and download functionalities are using the official Maven repository.

patterntesting.sample.WebDog Maven / Gradle / Ivy

Go to download

PatternTesting Samples (patterntesting-samples) is a collection of samples how to use the PatternTesting framework. It uses patterntesting-rt, patterntesting-check, patterntesting-concurrent and patterntesting-exception to give you some ideas how to use it.

The newest version!
/*
 * $Id: WebDog.java,v 1.6 2016/12/21 22:09:02 oboehm Exp $
 *
 * Copyright (c) 2009 by Oliver Boehm
 *
 * 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 orimplied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * (c)reated 09.03.2009 by oliver ([email protected])
 */
package patterntesting.sample;

import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import patterntesting.annotation.check.ct.OnlyForTesting;
import patterntesting.annotation.check.runtime.PublicForTesting;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * The WebDog can watch a web server if he is working correct.
 * It is an example how you can use the @OnlyForTesting annotation to
 * support the testability of this class.
 *
 * @author oliver
 * @version $Revision: 1.6 $
 * @since 09.03.2009
 */
public final class WebDog {

    private static final Logger log = LoggerFactory.getLogger(WebDog.class);
    private final URL url;
    private int responseCode;
    private boolean running = true;

    /**
     * Instantiates a new web dog.
     *
     * @param url the url
     */
    public WebDog(final URL url) {
        this.url = url;
    }

    /**
     * We are a very busy WebDog - we watch the given URL without any pause.
     */
    public void watch() {
        while (running) {
            this.ping();
        }
    }

    /**
     * With this method you can stop the watch method.
     */
    public void stop() {
        running = false;
    }

    /**
     * Ping.
     */
    protected void ping() {
        try {
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.getContentType();
            this.responseCode = getResponseCodeFrom(connection);
        } catch (IOException ioe) {
            log.error("can't connect to " + url, ioe);
            this.responseCode = -1;
        }
        logMessage(log);
    }

    /**
     * It's a little bit tricky to get the field "responseCode" of
     * HttpURLConnection because it is protected. We use reflection to
     * do it.
     *
     * @param connection HTTP connection
     */
    private static int getResponseCodeFrom(final HttpURLConnection connection) {
        try {
            return connection.getResponseCode();
        } catch (IOException ex) {
            throw new IllegalStateException("can't get responseCode of " + connection, ex);
        }
    }

    /**
     * This method should only be called for testing.
     *
     * @param rc the rc
     */
    @OnlyForTesting
    protected void setResponseCode(final int rc) {
        this.responseCode = rc;
    }

    /**
     * Gets the response code.
     *
     * @return the response code
     */
    public int getResponseCode() {
        return this.responseCode;
    }

    /**
     * One of the requirement for our WebDog is to log the following
     * messages (remember, it is only an example):
     * 
*
responseCode=303
*
too much traffic to url
*
responsCode=404
*
url has Alzheimer
*
* For testing and for the use of @PublicForTesting it is now public. * * @param lg the log */ @PublicForTesting public void logMessage(Logger lg) { switch (responseCode) { case 200: lg.info(url + " is ok"); break; case 303: lg.info("too much traffic to " + url); break; case 404: lg.info(url + " has Alzheimer"); break; default: lg.info(url + " responded with " + this.responseCode); break; } } /** * The main method. * * @param args the arguments */ public static void main(String... args) { String host = (args.length == 0) ? "http://localhost" : args[0]; try { WebDog dog = new WebDog(new URL(host)); dog.watch(); } catch (MalformedURLException e) { //e.printStackTrace(); /* * If you uncomment the line above you'll get a warning. * You should use better a log statement to print the exception! */ log.info("oops", e); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy