/*
 * The MIT License
 * 
 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package hudson.model;

import junit.framework.TestCase;
import net.sf.json.JSONObject;
import org.apache.commons.io.IOUtils;

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

/**
 * Quick test for {@link UpdateCenter}.
 * 
 * @author Kohsuke Kawaguchi
 */
public class UpdateCenterTest extends TestCase {
    public void testData() throws IOException {
        // check if we have the internet connectivity. See HUDSON-2095
        try {
            new URL("http://hudson-ci.org/").openStream();
        } catch (IOException e) {
            System.out.println("Skipping this test. No internet connectivity");
            return;
        }

        URL url = new URL("http://hudson-ci.org/update-center.json?version=build");
        String jsonp = IOUtils.toString(url.openStream());
        String json = jsonp.substring(jsonp.indexOf('(')+1,jsonp.lastIndexOf(')'));

        UpdateSite us = new UpdateSite("default", url.toExternalForm());
        UpdateSite.Data data = us.new Data(JSONObject.fromObject(json));
        assertTrue(data.core.url.startsWith("http://hudson-ci.org/"));
        assertTrue(data.plugins.containsKey("rake"));
        System.out.println(data.core.url);
    }
}

@@ -1206,31 +1206,82 @@
 rt j
-unit.framework.TestCase
+ava.io.IOException;%0Aimport java.net.HttpURLConnection;%0Aimport java.net.URL
 ;%0Aim
@@ -1351,56 +1351,79 @@
 ls;%0A
-%0A
 import 
-java.io.IOException;%0Aimport java.net.URL
+org.junit.Test;%0A%0Aimport static junit.framework.Assert.assertTrue
 ;%0A%0A/
@@ -1463,25 +1463,24 @@
 eCenter%7D.%0A *
- 
 %0A * @author 
@@ -1535,26 +1535,20 @@
 est 
-extends TestCase %7B
+%7B%0A%0A    @Test
 %0A   
@@ -1693,53 +1693,547 @@
     
-new URL(%22http://hudson-ci.org/%22).openStream()
+HttpURLConnection con = (HttpURLConnection) new URL(%22http://hudson-ci.org/%22).openConnection();%0A            con.setRequestMethod(%22HEAD%22);%0A            con.setConnectTimeout(10000); //set timeout to 10 seconds%0A            if (con.getResponseCode() != HttpURLConnection.HTTP_OK) %7B%0A                System.out.println(%22Skipping this test. Page doesn't exists%22);%0A                return;%0A            %7D%0A        %7D catch (java.net.SocketTimeoutException e) %7B%0A            System.out.println(%22Skipping this test. Timeout exception%22);%0A            return
 ;%0A  

Add connection timeout for UpdateCenter. Skip test if host is unreachable