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.app.VelocityEngine;
23  import org.apache.velocity.runtime.RuntimeConstants;
24  import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
25  import org.apache.velocity.test.misc.TestLogChute;
26  
27  /**
28   * Test the resource exists method
29   *
30   * @version $Id: ResourceExistsTestCase.java 687191 2008-08-19 23:02:41Z nbubna $
31   */
32  public class ResourceExistsTestCase extends BaseTestCase
33  {
34      private VelocityEngine velocity;
35      private String path = TEST_COMPARE_DIR + "/resourceexists";
36      private TestLogChute logger = new TestLogChute();
37  
38      public ResourceExistsTestCase(String name)
39      {
40          super(name);
41      }
42  
43      public void setUp() throws Exception
44      {
45  try {
46          velocity = new VelocityEngine();
47          velocity.setProperty("resource.loader", "file,string");
48          velocity.setProperty("file.resource.loader.path", path);
49          velocity.setProperty("string.resource.loader.class", StringResourceLoader.class.getName());
50  
51          // actual instance of logger
52          logger.on();
53          velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, logger);
54          velocity.setProperty("runtime.log.logsystem.test.level", "debug");
55  } catch (Exception e) {
56      System.out.println("exception via gump: "+e);
57      e.printStackTrace();
58      System.out.println("log: "+logger.getLog());
59  }
60      }
61  
62      public void testFileResourceExists() throws Exception
63      {
64  try {
65          if (!velocity.resourceExists("testfile.vm"))
66          {
67              String msg = "testfile.vm was not found in path "+path;
68              System.out.println(msg);
69              System.out.println("Log was: "+logger.getLog());
70              path = path+"/testfile.vm";
71              java.io.File file = new java.io.File(path);
72              if (file.exists()) {
73                  System.out.println("file system found "+path);
74              } else {
75                  System.out.println(file+" could not be found as a file");
76              }
77              fail(msg);
78          }
79          if (velocity.resourceExists("nosuchfile.vm"))
80          {
81              String msg = "nosuchfile.vm should not have been found in path "+path;
82              System.out.println(msg);
83              fail(msg);
84          }
85  } catch (Exception e) {
86      System.out.println("exception via gump: "+e);
87      e.printStackTrace();
88      System.out.println("log: "+logger.getLog());
89  }
90      }
91  
92      public void testStringResourceExists() throws Exception
93      {
94  try {
95          assertFalse(velocity.resourceExists("foo.vm"));
96          StringResourceLoader.getRepository().putStringResource("foo.vm", "Make it so!");
97          assertTrue(velocity.resourceExists("foo.vm"));
98  } catch (Exception e) {
99      System.out.println("exception via gump: "+e);
100     e.printStackTrace();
101     System.out.println("log: "+logger.getLog());
102 }
103     }
104 }