1 package org.apache.velocity.test;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
29
30
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
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 }