View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  }