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
|
// Assert.assertEquals(9143, UriUtils.getResourceLength(uri, null));
|
71
|
} catch (URISyntaxException e) {
|
72
|
Assert.fail();
|
73
|
}
|
74
|
} else {
|
75
|
logger.warn("Test: testGetResourceLength() skipped, since internet is not available");
|
76
|
}
|
77
|
}
|
78
|
|
79
|
@Test
|
80
|
public void testGetResourceLengthMissingProtocol() throws ClientProtocolException, HttpException{
|
81
|
URI uri;
|
82
|
try {
|
83
|
uri = URI.create("www.abc.de");
|
84
|
UriUtils.getResourceLength(uri, null);
|
85
|
Assert.fail("getResourceLength works only on absolute URIs providing a protocol/scheme");
|
86
|
} catch (Exception e) {
|
87
|
Assert.assertEquals(IOException.class, e.getClass());
|
88
|
Assert.assertNotNull(e.getMessage().equals(UriUtils.URI_IS_NOT_ABSOLUTE));
|
89
|
}
|
90
|
}
|
91
|
|
92
|
@Test
|
93
|
public void testGetResourceLengthUnknownProtocol() throws ClientProtocolException, HttpException{
|
94
|
URI uri;
|
95
|
try {
|
96
|
uri = URI.create("xxx://www.abc.de");
|
97
|
UriUtils.getResourceLength(uri, null);
|
98
|
Assert.fail("getResourceLength works only on absolute URIs with supported protocols 'http(s):' and 'file:'");
|
99
|
} catch (Exception e) {
|
100
|
Assert.assertEquals(RuntimeException.class, e.getClass());
|
101
|
Assert.assertNotNull(e.getMessage().startsWith("Protocol not handled yet"));
|
102
|
}
|
103
|
}
|
104
|
|
105
|
@Test
|
106
|
public void testIsInternetAvailable() {
|
107
|
URI firstUri = URI.create("http://www.gmx.de/");
|
108
|
boolean isAvailable = UriUtils.isInternetAvailable(firstUri);
|
109
|
if (isAvailable == false){
|
110
|
logger.warn("Internet is not available!");
|
111
|
}
|
112
|
}
|
113
|
|
114
|
@Test
|
115
|
public void testIsRootServerAvailable() {
|
116
|
boolean isAvailable = UriUtils.isRootServerAvailable("www.gmx.de");
|
117
|
if (isAvailable == false){
|
118
|
logger.warn("RootServer is not available!");
|
119
|
}
|
120
|
}
|
121
|
|
122
|
}
|