cleanup
[cdmlib.git] / 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.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.apache.http.HttpException;
12 import org.apache.http.NameValuePair;
13 import org.apache.http.client.ClientProtocolException;
14 import org.apache.http.message.BasicNameValuePair;
15 import org.apache.log4j.Logger;
16 import org.junit.Assert;
17 import org.junit.Test;
18
19 public class UriUtilsTest {
20
21 private static final Logger logger = Logger.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() throws ClientProtocolException, IOException, HttpException{
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 // TODO Auto-generated catch block
66 e.printStackTrace();
67 }
68
69 } else {
70 logger.warn("Test: testGetResourceLength() skipped, since internet is not available");
71 }
72 }
73
74 @Test
75 public void testGetResourceLengthMissingProtocol() throws ClientProtocolException, HttpException{
76 URI uri;
77 try {
78 uri = URI.create("www.abc.de");
79 UriUtils.getResourceLength(uri, null);
80 Assert.fail("getResourceLength works only on absolute URIs providing a protocol/scheme");
81 } catch (Exception e) {
82 Assert.assertEquals(IOException.class, e.getClass());
83 Assert.assertNotNull(e.getMessage().equals(UriUtils.URI_IS_NOT_ABSOLUTE));
84 }
85 }
86
87 @Test
88 public void testGetResourceLengthUnknownProtocol() throws ClientProtocolException, HttpException{
89 URI uri;
90 try {
91 uri = URI.create("xxx://www.abc.de");
92 UriUtils.getResourceLength(uri, null);
93 Assert.fail("getResourceLength works only on absolute URIs with supported protocols 'http(s):' and 'file:'");
94 } catch (Exception e) {
95 Assert.assertEquals(RuntimeException.class, e.getClass());
96 Assert.assertNotNull(e.getMessage().startsWith("Protocol not handled yet"));
97 }
98 }
99
100 @Test
101 public void testIsInternetAvailable() {
102 URI firstUri = URI.create("http://www.gmx.de/");
103 boolean isAvailable = UriUtils.isInternetAvailable(firstUri);
104 if (isAvailable == false){
105 logger.warn("Internet is not available!");
106 }
107 }
108
109 @Test
110 public void testIsRootServerAvailable() {
111 boolean isAvailable = UriUtils.isRootServerAvailable("www.gmx.de");
112 if (isAvailable == false){
113 logger.warn("RootServer is not available!");
114 }
115 }
116
117 }