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.ByteArrayInputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  
25  import java.net.URL;
26  import java.net.URLConnection;
27  import java.net.URLStreamHandler;
28  
29  import org.apache.myfaces.trinidad.resource.AggregatingResourceLoader;
30  import org.apache.myfaces.trinidad.resource.ResourceLoader;
31  
32  public class AggregatingResourceLoaderTest extends ResourceLoaderTestCase
33  {
34    public AggregatingResourceLoaderTest(
35      String testName)
36    {
37      super(testName);
38    }
39  
40    public void testContentLength() throws IOException
41    {
42      ResourceLoader loader = 
43        new AggregatingResourceLoader("test.xml", 
44                                      new String[]
45                                      {
46                                        "test-1.xml",
47                                        "test-2.xml"
48                                      },
49                                      new LocalResourceLoader());
50      doTestContentLength(loader.getResource("test.xml"));
51    }
52  
53    public void testContentLengthWithException() throws IOException
54    {
55      try
56      {
57        AggregatingResourceLoader loader = 
58          new AggregatingResourceLoader("test.xml", 
59                                        new String[]
60                                        {
61                                          "test-1.xml",
62                                          "test-exception.xml",
63                                          "test-2.xml"
64                                        },
65                                        new ThrowingResourceLoader());
66        loader.setSeparator("\n");
67        doTestContentLength(loader.getResource("test.xml"));
68        //always log an error here.
69        assertTrue("Expected IOException was not thrown.", false);
70      }
71      catch (IOException e)
72      {
73        if (!"This test exception is expected".equals(e.getMessage()))
74        {
75          throw new IOException(e.getMessage());
76        }
77      }
78    }
79  
80    public void testUnknownContentLength() throws IOException
81    {
82      AggregatingResourceLoader loader = 
83        new AggregatingResourceLoader("test.xml", 
84                                      new String[]
85                                      {
86                                        "test-1.xml",
87                                        "unknown-length.xml",
88                                        "test-2.xml"
89                                      },
90                                      new UnknownLengthResourceLoader());
91      loader.setSeparator("\n");
92      doTestUnknownContentLength(loader.getResource("test.xml"));
93    }
94  
95    private class ThrowingResourceLoader extends LocalResourceLoader
96    {
97      @Override
98      protected URL findResource(
99        String name
100       ) throws IOException
101     {
102       if ("test-exception.xml".equals(name))
103         throw new IOException("This test exception is expected");
104         
105       return super.findResource(name);
106     }
107   }
108 
109   private class UnknownLengthResourceLoader extends LocalResourceLoader
110   {
111     @Override
112     protected URL findResource(
113       String name
114       ) throws IOException
115     {
116       if ("unknown-length.xml".equals(name))
117         return new URL(super.findResource("test-1.xml"), name,
118                        new UnknownLengthStreamHandler());
119         
120       return super.findResource(name);
121     }
122   }
123   
124   private class UnknownLengthStreamHandler extends URLStreamHandler
125   {
126     @Override
127     protected URLConnection openConnection(
128       URL url
129       ) throws IOException
130     {
131       return new UnknownLengthURLConnection(url);
132     }
133     
134   }
135 
136   private class UnknownLengthURLConnection extends URLConnection
137   {
138     public UnknownLengthURLConnection(
139       URL url)
140     {
141       super(url);
142     }
143     
144     @Override
145     public int getContentLength()
146     {
147       return -1;
148     }
149 
150     @Override
151     public void connect() throws IOException
152     {
153       // no-op
154     }
155 
156     @Override
157     public InputStream getInputStream() throws IOException
158     {
159       return new ByteArrayInputStream(new byte[0]);
160     }
161   }
162 }