Project

General

Profile

« Previous | Next » 

Revision 794be65e

Added by Andreas Müller almost 2 years ago

cleanup

View differences:

cdmlib-commons/src/test/java/eu/etaxonomy/cdm/common/UriUtilsTest.java
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.logging.log4j.LogManager;
15
import org.apache.logging.log4j.Logger;
16
import org.junit.Assert;
17
import org.junit.Test;
18

  
19
public class UriUtilsTest {
20

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

  
23
//********************* TESTS **********************************************/
24

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

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

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

  
56
    @Test
57
    public void testGetResourceLengthNull2(){
58
        if(UriUtils.isInternetAvailable(null)){
59
			try {
60
				@SuppressWarnings("unused")
61
                URI uri = new URI("http:/www.abc.de:8080/xyz");
62
				System.out.println("  sdf");
63
//				Assert.assertEquals(9143, UriUtils.getResourceLength(uri, null));
64
			} catch (URISyntaxException e) {
65
				Assert.fail();
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
}
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
}

Also available in: Unified diff