Project

General

Profile

Download (4.13 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.common;
2

    
3
import java.io.IOException;
4
import java.net.MalformedURLException;
5
import java.net.URISyntaxException;
6
import java.net.URL;
7
import java.util.ArrayList;
8
import java.util.List;
9

    
10
import org.apache.http.HttpException;
11
import org.apache.http.NameValuePair;
12
import org.apache.http.client.ClientProtocolException;
13
import org.apache.http.message.BasicNameValuePair;
14
import org.apache.log4j.Logger;
15
import org.junit.Assert;
16
import org.junit.Test;
17

    
18
public class UriUtilsTest {
19

    
20
    private static final Logger logger = Logger.getLogger(UriUtilsTest.class);
21

    
22
//********************* TESTS **********************************************/
23

    
24
    @Test
25
    public void testCreateUri() {
26
        try {
27
            URL baseUrl = new URL("http://www.abc.de");
28
            String subPath = "fgh";
29
            String fragment = "frag";
30
            URI uri = UriUtils.createUri(baseUrl, subPath, null, fragment);
31
            Assert.assertEquals("http://www.abc.de/fgh#frag", uri.toString());
32
            List<NameValuePair> qparams = new ArrayList<>(0);
33
            NameValuePair pair1 = new BasicNameValuePair("param1","value1");
34
            qparams.add(pair1);
35
            uri = UriUtils.createUri(baseUrl, subPath, qparams, fragment);
36
            Assert.assertEquals("http://www.abc.de/fgh?param1=value1#frag", uri.toString());
37

    
38
        } catch (MalformedURLException e) {
39
            Assert.fail(e.getMessage());
40
        } catch (URISyntaxException e) {
41
            Assert.fail(e.getMessage());
42
        }
43
    }
44

    
45
    @Test
46
    public void testGetResourceLength() throws ClientProtocolException, IOException, HttpException{
47
        if(UriUtils.isInternetAvailable(null)){
48
            URI uri = URI.create("http://dev.e-taxonomy.eu/trac_htdocs/logo_edit.png");
49
            Assert.assertEquals(9143, UriUtils.getResourceLength(uri, null));
50
        } else {
51
            logger.warn("Test: testGetResourceLength() skipped, since internet is not available");
52
        }
53
    }
54

    
55
    @Test
56
    public void testGetResourceLengthNull2() throws ClientProtocolException, IOException, HttpException{
57
        if(UriUtils.isInternetAvailable(null)){
58
			try {
59
				@SuppressWarnings("unused")
60
                URI uri = new URI("http:/www.abc.de:8080/xyz");
61
				System.out.println("  sdf");
62
//				Assert.assertEquals(9143, UriUtils.getResourceLength(uri, null));
63
			} catch (URISyntaxException e) {
64
				// TODO Auto-generated catch block
65
				e.printStackTrace();
66
			}
67

    
68
        } else {
69
            logger.warn("Test: testGetResourceLength() skipped, since internet is not available");
70
        }
71
    }
72

    
73
    @Test
74
    public void testGetResourceLengthMissingProtocol() throws ClientProtocolException, HttpException{
75
    	URI uri;
76
		try {
77
			uri = URI.create("www.abc.de");
78
			UriUtils.getResourceLength(uri, null);
79
			Assert.fail("getResourceLength works only on absolute URIs providing a protocol/scheme");
80
		} catch (Exception e) {
81
			Assert.assertEquals(IOException.class, e.getClass());
82
			Assert.assertNotNull(e.getMessage().equals(UriUtils.URI_IS_NOT_ABSOLUTE));
83
		}
84
    }
85

    
86
    @Test
87
    public void testGetResourceLengthUnknownProtocol() throws ClientProtocolException, HttpException{
88
    	URI uri;
89
		try {
90
			uri = URI.create("xxx://www.abc.de");
91
			UriUtils.getResourceLength(uri, null);
92
			Assert.fail("getResourceLength works only on absolute URIs with supported protocols 'http(s):' and 'file:'");
93
		} catch (Exception e) {
94
			Assert.assertEquals(RuntimeException.class, e.getClass());
95
			Assert.assertNotNull(e.getMessage().startsWith("Protocol not handled yet"));
96
		}
97
    }
98

    
99
    @Test
100
    public void testIsInternetAvailable() {
101
        URI firstUri = URI.create("http://www.gmx.de/");
102
        boolean isAvailable = UriUtils.isInternetAvailable(firstUri);
103
        if (isAvailable == false){
104
            logger.warn("Internet is not available!");
105
        }
106
    }
107

    
108
    @Test
109
    public void testIsRootServerAvailable() {
110
        boolean isAvailable = UriUtils.isRootServerAvailable("www.gmx.de");
111
        if (isAvailable == false){
112
            logger.warn("RootServer is not available!");
113
        }
114
    }
115

    
116
}
(6-6/8)