1
|
package eu.etaxonomy.cdm.common;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.net.MalformedURLException;
|
5
|
import java.net.URI;
|
6
|
import java.net.URISyntaxException;
|
7
|
import java.net.URL;
|
8
|
import java.util.ArrayList;
|
9
|
import java.util.List;
|
10
|
|
11
|
import org.junit.Assert;
|
12
|
|
13
|
import org.apache.http.HttpException;
|
14
|
import org.apache.http.NameValuePair;
|
15
|
import org.apache.http.client.ClientProtocolException;
|
16
|
import org.apache.http.message.BasicNameValuePair;
|
17
|
import org.apache.log4j.Logger;
|
18
|
import org.junit.Before;
|
19
|
import org.junit.BeforeClass;
|
20
|
import org.junit.Test;
|
21
|
|
22
|
public class UriUtilsTest {
|
23
|
private static final Logger logger = Logger.getLogger(UriUtilsTest.class);
|
24
|
|
25
|
@BeforeClass
|
26
|
public static void setUpBeforeClass() throws Exception {
|
27
|
}
|
28
|
|
29
|
@Before
|
30
|
public void setUp() throws Exception {
|
31
|
}
|
32
|
|
33
|
//********************* TESTS **********************************************/
|
34
|
|
35
|
@Test
|
36
|
public void testCreateUri() {
|
37
|
try {
|
38
|
URL baseUrl = new URL("http://www.abc.de");
|
39
|
String subPath = "fgh";
|
40
|
String fragment = "frag";
|
41
|
URI uri = UriUtils.createUri(baseUrl, subPath, null, fragment);
|
42
|
Assert.assertEquals("http://www.abc.de/fgh#frag", uri.toString());
|
43
|
List<NameValuePair> qparams = new ArrayList<NameValuePair>(0);
|
44
|
NameValuePair pair1 = new BasicNameValuePair("param1","value1");
|
45
|
qparams.add(pair1);
|
46
|
uri = UriUtils.createUri(baseUrl, subPath, qparams, fragment);
|
47
|
Assert.assertEquals("http://www.abc.de/fgh?param1=value1#frag", uri.toString());
|
48
|
|
49
|
} catch (MalformedURLException e) {
|
50
|
e.printStackTrace();
|
51
|
Assert.fail(e.getMessage());
|
52
|
} catch (URISyntaxException e) {
|
53
|
e.printStackTrace();
|
54
|
Assert.fail(e.getMessage());
|
55
|
}
|
56
|
}
|
57
|
|
58
|
@Test
|
59
|
public void testGetResourceLength() throws ClientProtocolException, IOException, HttpException{
|
60
|
if(UriUtils.isInternetAvailable(null)){
|
61
|
URI uri = URI.create("http://dev.e-taxonomy.eu/trac_htdocs/logo_edit.png");
|
62
|
Assert.assertEquals(9143, UriUtils.getResourceLength(uri, null));
|
63
|
} else {
|
64
|
logger.warn("Test: testGetResourceLength() skipped, since internet is not available");
|
65
|
}
|
66
|
}
|
67
|
|
68
|
@Test
|
69
|
public void testGetResourceLengthNull2() throws ClientProtocolException, IOException, HttpException{
|
70
|
if(UriUtils.isInternetAvailable(null)){
|
71
|
URI uri;
|
72
|
try {
|
73
|
uri = new URI("http:/www.abc.de:8080/xyz");
|
74
|
System.out.println(" sdf");
|
75
|
// Assert.assertEquals(9143, UriUtils.getResourceLength(uri, null));
|
76
|
} catch (URISyntaxException e) {
|
77
|
// TODO Auto-generated catch block
|
78
|
e.printStackTrace();
|
79
|
}
|
80
|
|
81
|
} else {
|
82
|
logger.warn("Test: testGetResourceLength() skipped, since internet is not available");
|
83
|
}
|
84
|
}
|
85
|
|
86
|
@Test
|
87
|
public void testGetResourceLengthMissingProtocol() throws ClientProtocolException, HttpException{
|
88
|
URI uri;
|
89
|
try {
|
90
|
uri = URI.create("www.abc.de");
|
91
|
UriUtils.getResourceLength(uri, null);
|
92
|
Assert.fail("getResourceLength works only on absolute URIs providing a protocol/scheme");
|
93
|
} catch (Exception e) {
|
94
|
Assert.assertEquals(IOException.class, e.getClass());
|
95
|
Assert.assertNotNull(e.getMessage().equals(UriUtils.URI_IS_NOT_ABSOLUTE));
|
96
|
}
|
97
|
}
|
98
|
|
99
|
@Test
|
100
|
public void testGetResourceLengthUnknownProtocol() throws ClientProtocolException, HttpException{
|
101
|
URI uri;
|
102
|
try {
|
103
|
uri = URI.create("xxx://www.abc.de");
|
104
|
UriUtils.getResourceLength(uri, null);
|
105
|
Assert.fail("getResourceLength works only on absolute URIs with supported protocols 'http(s):' and 'file:'");
|
106
|
} catch (Exception e) {
|
107
|
Assert.assertEquals(RuntimeException.class, e.getClass());
|
108
|
Assert.assertNotNull(e.getMessage().startsWith("Protocol not handled yet"));
|
109
|
}
|
110
|
}
|
111
|
|
112
|
@Test
|
113
|
public void testIsInternetAvailable() {
|
114
|
URI firstUri = URI.create("http://www.gmx.de/");
|
115
|
boolean isAvailable = UriUtils.isInternetAvailable(firstUri);
|
116
|
if (isAvailable == false){
|
117
|
logger.warn("Internet is not available!");
|
118
|
}
|
119
|
}
|
120
|
|
121
|
@Test
|
122
|
public void testIsRootServerAvailable() {
|
123
|
boolean isAvailable = UriUtils.isRootServerAvailable("www.gmx.de");
|
124
|
if (isAvailable == false){
|
125
|
logger.warn("RootServer is not available!");
|
126
|
}
|
127
|
}
|
128
|
|
129
|
}
|