1   package org.apache.velocity.test;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import org.apache.velocity.runtime.RuntimeConstants;
23  import org.apache.velocity.runtime.resource.loader.URLResourceLoader;
24  import org.apache.velocity.test.misc.TestLogChute;
25  
26  /**
27   * This class tests support for custom timeouts in URLResourceLoader.
28   */
29  public class URLResourceLoaderTimeoutTestCase extends BaseTestCase
30  {
31      private static boolean isJava5plus;
32      static
33      {
34          try
35          {
36              Class.forName("java.lang.annotation.Annotation");
37              isJava5plus = true;
38          }
39          catch (ClassNotFoundException cnfe)
40          {
41              isJava5plus = false;
42          }
43      }
44      private TestLogChute logger = new TestLogChute();
45      private URLResourceLoader loader = new URLResourceLoader();
46      private int timeout = 2000;
47  
48      public URLResourceLoaderTimeoutTestCase(String name)
49      {
50         super(name);
51      }
52  
53      public void setUp() throws Exception
54      {
55          super.setUp();
56          engine.setProperty("resource.loader", "url");
57          engine.setProperty("url.resource.loader.instance", loader);
58          engine.setProperty("url.resource.loader.timeout", new Integer(timeout));
59  
60          // actual instance of logger
61          logger.on();
62          engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, logger);
63          engine.setProperty("runtime.log.logsystem.test.level", "debug");
64          engine.init();
65      }
66  
67      public void testTimeout()
68      {
69          if (isJava5plus)
70          {
71              System.out.println("Testing a 1.5+ JDK");
72              assertEquals(timeout, loader.getTimeout());
73          }
74          else
75          {
76              System.out.println("Testing a pre-1.5 JDK");
77              assertEquals(-1, loader.getTimeout());
78          }
79      }
80  
81  }