Project

General

Profile

Download (4.03 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(){
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
				Assert.fail();
65
			}
66

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

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

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

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

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

    
115
}
(6-6/8)