1 package eu
.etaxonomy
.cdm
.common
;
3 import java
.io
.IOException
;
4 import java
.net
.MalformedURLException
;
6 import java
.net
.URISyntaxException
;
8 import java
.util
.ArrayList
;
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
;
19 public class UriUtilsTest
{
21 private static final Logger logger
= Logger
.getLogger(UriUtilsTest
.class);
23 //********************* TESTS **********************************************/
26 public void testCreateUri() {
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");
36 uri
= UriUtils
.createUri(baseUrl
, subPath
, qparams
, fragment
);
37 Assert
.assertEquals("http://www.abc.de/fgh?param1=value1#frag", uri
.toString());
39 } catch (MalformedURLException e
) {
40 Assert
.fail(e
.getMessage());
41 } catch (URISyntaxException e
) {
42 Assert
.fail(e
.getMessage());
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));
52 logger
.warn("Test: testGetResourceLength() skipped, since internet is not available");
57 public void testGetResourceLengthNull2() throws ClientProtocolException
, IOException
, HttpException
{
58 if(UriUtils
.isInternetAvailable(null)){
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
70 logger
.warn("Test: testGetResourceLength() skipped, since internet is not available");
75 public void testGetResourceLengthMissingProtocol() throws ClientProtocolException
, HttpException
{
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
));
88 public void testGetResourceLengthUnknownProtocol() throws ClientProtocolException
, HttpException
{
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"));
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!");
110 public void testIsRootServerAvailable() {
111 boolean isAvailable
= UriUtils
.isRootServerAvailable("www.gmx.de");
112 if (isAvailable
== false){
113 logger
.warn("RootServer is not available!");