Project

General

Profile

Download (4.21 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9
package eu.etaxonomy.cdm.common;
10

    
11
import java.io.IOException;
12
import java.net.MalformedURLException;
13
import java.net.URISyntaxException;
14
import java.net.URL;
15
import java.util.ArrayList;
16
import java.util.List;
17

    
18
import org.apache.http.HttpException;
19
import org.apache.http.NameValuePair;
20
import org.apache.http.client.ClientProtocolException;
21
import org.apache.http.message.BasicNameValuePair;
22
import org.apache.logging.log4j.LogManager;
23
import org.apache.logging.log4j.Logger;
24
import org.junit.Assert;
25
import org.junit.Test;
26

    
27
public class UriUtilsTest {
28

    
29
    private static final Logger logger = LogManager.getLogger();
30

    
31
//********************* TESTS **********************************************/
32

    
33
    @Test
34
    public void testCreateUri() {
35
        try {
36
            URL baseUrl = new URL("http://www.abc.de");
37
            String subPath = "fgh";
38
            String fragment = "frag";
39
            URI uri = UriUtils.createUri(baseUrl, subPath, null, fragment);
40
            Assert.assertEquals("http://www.abc.de/fgh#frag", uri.toString());
41
            List<NameValuePair> qparams = new ArrayList<>(0);
42
            NameValuePair pair1 = new BasicNameValuePair("param1","value1");
43
            qparams.add(pair1);
44
            uri = UriUtils.createUri(baseUrl, subPath, qparams, fragment);
45
            Assert.assertEquals("http://www.abc.de/fgh?param1=value1#frag", uri.toString());
46

    
47
        } catch (MalformedURLException e) {
48
            Assert.fail(e.getMessage());
49
        } catch (URISyntaxException e) {
50
            Assert.fail(e.getMessage());
51
        }
52
    }
53

    
54
    @Test
55
    public void testGetResourceLength() throws ClientProtocolException, IOException, HttpException{
56
        if(UriUtils.isInternetAvailable(null)){
57
            URI uri = URI.create("http://dev.e-taxonomy.eu/trac_htdocs/logo_edit.png");
58
            Assert.assertEquals(9143, UriUtils.getResourceLength(uri, null));
59
        } else {
60
            logger.warn("Test: testGetResourceLength() skipped, since internet is not available");
61
        }
62
    }
63

    
64
    @Test
65
    public void testGetResourceLengthNull2(){
66
        if(UriUtils.isInternetAvailable(null)){
67
			try {
68
				@SuppressWarnings("unused")
69
                URI uri = new URI("http:/www.abc.de:8080/xyz");
70
				System.out.println("  sdf");
71
//				Assert.assertEquals(9143, UriUtils.getResourceLength(uri, null));
72
			} catch (URISyntaxException e) {
73
				Assert.fail();
74
			}
75

    
76
        } else {
77
            logger.warn("Test: testGetResourceLength() skipped, since internet is not available");
78
        }
79
    }
80

    
81
    @Test
82
    public void testGetResourceLengthMissingProtocol() throws ClientProtocolException, HttpException{
83
    	URI uri;
84
		try {
85
			uri = URI.create("www.abc.de");
86
			UriUtils.getResourceLength(uri, null);
87
			Assert.fail("getResourceLength works only on absolute URIs providing a protocol/scheme");
88
		} catch (Exception e) {
89
			Assert.assertEquals(IOException.class, e.getClass());
90
			Assert.assertNotNull(e.getMessage().equals(UriUtils.URI_IS_NOT_ABSOLUTE));
91
		}
92
    }
93

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

    
107
    @Test
108
    public void testIsInternetAvailable() {
109
        URI firstUri = URI.create("http://www.gmx.de/");
110
        boolean isAvailable = UriUtils.isInternetAvailable(firstUri);
111
        if (isAvailable == false){
112
            logger.warn("Internet is not available!");
113
        }
114
    }
115

    
116
    @Test
117
    public void testIsRootServerAvailable() {
118
        boolean isAvailable = UriUtils.isRootServerAvailable("www.gmx.de");
119
        if (isAvailable == false){
120
            logger.warn("RootServer is not available!");
121
        }
122
    }
123

    
124
}
(6-6/8)