1 |
|
package jalview.util; |
2 |
|
|
3 |
|
import java.io.BufferedReader; |
4 |
|
import java.io.IOException; |
5 |
|
import java.io.InputStream; |
6 |
|
import java.io.InputStreamReader; |
7 |
|
import java.net.HttpURLConnection; |
8 |
|
import java.net.URL; |
9 |
|
import java.util.Locale; |
10 |
|
|
11 |
|
import org.testng.Assert; |
12 |
|
import org.testng.annotations.DataProvider; |
13 |
|
import org.testng.annotations.Test; |
14 |
|
|
|
|
| 0% |
Uncovered Elements: 33 (33) |
Complexity: 6 |
Complexity Density: 0.24 |
|
15 |
|
public class HttpUtilsTest |
16 |
|
{ |
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
4-
|
|
17 |
0 |
@Test(groups = { "Network" }, dataProvider = "urlTargetsAndDestinations")... |
18 |
|
public void testFollowConnection(String targetUrl, String finalUrl, |
19 |
|
String notUsed0, String notUsed1, boolean followAnyway) |
20 |
|
throws IOException |
21 |
|
{ |
22 |
0 |
URL tUrl = new URL(targetUrl); |
23 |
0 |
URL fUrl = new URL(finalUrl); |
24 |
0 |
HttpURLConnection conn1 = HttpUtils.followConnection( |
25 |
|
(HttpURLConnection) tUrl.openConnection(), followAnyway); |
26 |
0 |
URL url1 = conn1.getURL(); |
27 |
0 |
Assert.assertEquals(url1, fUrl, "Final URL is not the same."); |
28 |
|
} |
29 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
4-
|
|
30 |
0 |
@Test(groups = { "Network" }, dataProvider = "urlTargetsAndDestinations")... |
31 |
|
public void testOpenConnection(String targetUrl, String finalUrl, |
32 |
|
String notUsed0, String notUsed1, boolean followAnyway) |
33 |
|
throws IOException |
34 |
|
{ |
35 |
0 |
URL tUrl = new URL(targetUrl); |
36 |
0 |
URL fUrl = new URL(finalUrl); |
37 |
0 |
HttpURLConnection conn1 = (HttpURLConnection) HttpUtils |
38 |
|
.openConnection(tUrl, followAnyway); |
39 |
0 |
URL url1 = conn1.getURL(); |
40 |
0 |
Assert.assertEquals(url1, fUrl, "Final URL is not the same."); |
41 |
|
} |
42 |
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 3 |
Complexity Density: 0.21 |
4-
|
|
43 |
0 |
@Test(groups = { "Network" }, dataProvider = "urlTargetsAndDestinations")... |
44 |
|
public void testOpenStream(String targetUrl, String finalUrl, |
45 |
|
String inFirstLine, String inDocument, boolean followAnyway) |
46 |
|
throws IOException |
47 |
|
{ |
48 |
0 |
URL tUrl = new URL(targetUrl); |
49 |
0 |
URL fUrl = new URL(finalUrl); |
50 |
0 |
InputStream is1 = HttpUtils.openStream(tUrl, followAnyway); |
51 |
0 |
BufferedReader br1 = new BufferedReader(new InputStreamReader(is1)); |
52 |
0 |
String firstLine = br1.readLine().toLowerCase(Locale.ROOT); |
53 |
0 |
Assert.assertTrue( |
54 |
|
firstLine.contains(inFirstLine.toLowerCase(Locale.ROOT)), |
55 |
|
"First line of text '" + firstLine + "' does not contain '" |
56 |
|
+ inFirstLine + "'"); |
57 |
0 |
String inDocumentLC = inDocument.toLowerCase(Locale.ROOT); |
58 |
0 |
boolean found = false; |
59 |
0 |
String line = null; |
60 |
0 |
while ((line = br1.readLine()) != null) |
61 |
|
{ |
62 |
0 |
if (line.toLowerCase(Locale.ROOT).contains(inDocumentLC)) |
63 |
|
{ |
64 |
0 |
found = true; |
65 |
0 |
break; |
66 |
|
} |
67 |
|
} |
68 |
0 |
Assert.assertTrue(found, |
69 |
|
"Text '" + inDocument + "' not found in '" + finalUrl + "'"); |
70 |
|
} |
71 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
72 |
0 |
@DataProvider(name = "urlTargetsAndDestinations")... |
73 |
|
public Object[][] urlTargetsAndDestinations() |
74 |
|
{ |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
0 |
return new Object[][] { |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
{ "http://jalview.org/", "https://www.jalview.org/", "<!doctype", |
87 |
|
"Jalview is a", false }, |
88 |
|
{ "http://www.jalview.org/", "https://www.jalview.org/", |
89 |
|
"<!doctype", "Jalview is a", false }, |
90 |
|
{ "https://jalview.org/", "https://jalview.org/", "<!doctype", |
91 |
|
"Jalview is a", false }, |
92 |
|
{ "http://jalview.org/", "https://www.jalview.org/", "<!doctype", |
93 |
|
"Jalview is a", true }, |
94 |
|
{ "http://www.jalview.org/", "https://www.jalview.org/", |
95 |
|
"<!doctype", "Jalview is a", true }, |
96 |
|
{ "https://jalview.org", "https://www.jalview.org/", "<!doctype", |
97 |
|
"Jalview is a", true }, |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
}; |
102 |
|
} |
103 |
|
|
104 |
|
} |