1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.trinidad.resource;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.net.URL;
24 import java.net.URLConnection;
25
26 import org.apache.myfaces.trinidad.resource.ResourceLoader;
27
28 import junit.framework.TestCase;
29
30 abstract public class ResourceLoaderTestCase extends TestCase
31 {
32 public ResourceLoaderTestCase(
33 String testName)
34 {
35 super(testName);
36 }
37
38 protected void doTestUnknownContentLength(
39 URL url) throws IOException
40 {
41 URLConnection conn = url.openConnection();
42 long actualContentLength = conn.getContentLength();
43
44 assertEquals("Invalid explicit content length",
45 -1L, actualContentLength);
46 }
47
48 protected void doTestContentLength(
49 URL url) throws IOException
50 {
51 URLConnection conn = url.openConnection();
52 long expectedContentLength = conn.getContentLength();
53
54 if (expectedContentLength != -1)
55 {
56 byte[] buffer = new byte[2048];
57 InputStream in = conn.getInputStream();
58
59 try
60 {
61 long actualContentLength = 0;
62
63 int length;
64 while ((length = (in.read(buffer))) >= 0)
65 {
66 actualContentLength += length;
67 }
68
69 assertEquals("Inaccurate explicit content length",
70 expectedContentLength, actualContentLength);
71 }
72 finally
73 {
74 in.close();
75 }
76 }
77 }
78
79 public class LocalResourceLoader extends ResourceLoader
80 {
81 @Override
82 protected URL findResource(
83 String name
84 ) throws IOException
85 {
86 return getClass().getResource(name);
87 }
88 }
89 }