1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.console.common;
23
24 import junit.framework.TestCase;
25
26 import java.io.File;
27
28 import net.grinder.testutility.StubPrintWriter;
29 import net.grinder.testutility.FileUtilities;
30
31
32
33
34
35
36
37 public class TestResources extends TestCase {
38
39 private final StubPrintWriter m_errorWriter =
40 new StubPrintWriter();
41
42 public void testResources() throws Exception {
43 final ResourcesImplementation resources = new ResourcesImplementation(getClass().getName());
44 final ResourcesImplementation resources2 = new ResourcesImplementation("TestResources");
45
46 resources.setErrorWriter(m_errorWriter);
47 resources2.setErrorWriter(m_errorWriter);
48
49 assertEquals("file1", resources.getString("resourceFile"));
50 assertEquals("file2", resources2.getString("resourceFile"));
51
52 assertTrue(!(m_errorWriter.getOutputAndReset().length() > 0));
53 }
54
55 public void testGetString() throws Exception {
56 final ResourcesImplementation resources = new ResourcesImplementation(getClass().getName());
57 resources.setErrorWriter(m_errorWriter);
58
59 assertEquals("", resources.getString("notthere"));
60 assertTrue((m_errorWriter.getOutputAndReset().length() > 0));
61
62 assertNull(resources.getString("notthere", false));
63 assertTrue(!(m_errorWriter.getOutputAndReset().length() > 0));
64
65 assertEquals("", resources.getString("notthere", true));
66 assertTrue((m_errorWriter.getOutputAndReset().length() > 0));
67
68 assertEquals("A property value", resources.getString("key"));
69 assertTrue(!(m_errorWriter.getOutputAndReset().length() > 0));
70 }
71
72
73 public void testGetImageIcon() throws Exception {
74 final ResourcesImplementation resources = new ResourcesImplementation(getClass().getName());
75 resources.setErrorWriter(m_errorWriter);
76
77 assertNull(resources.getImageIcon("notthere"));
78 assertTrue(!(m_errorWriter.getOutputAndReset().length() > 0));
79
80 assertNull(resources.getImageIcon("notthere", true));
81 assertTrue((m_errorWriter.getOutputAndReset().length() > 0));
82
83 assertNull(resources.getImageIcon("notthere", false));
84 assertTrue(!(m_errorWriter.getOutputAndReset().length() > 0));
85
86 assertNull(resources.getImageIcon("resourceFile", false));
87 assertTrue((m_errorWriter.getOutputAndReset().length() > 0));
88
89 assertNotNull(resources.getImageIcon("image", false));
90 assertTrue(!(m_errorWriter.getOutputAndReset().length() > 0));
91 }
92
93 public void testGetStringFromFile() throws Exception {
94 final ResourcesImplementation resources = new ResourcesImplementation(getClass().getName());
95 resources.setErrorWriter(m_errorWriter);
96
97 assertNull(resources.getStringFromFile("notthere", false));
98 assertTrue(!(m_errorWriter.getOutputAndReset().length() > 0));
99
100 assertNull(resources.getStringFromFile("notthere", true));
101 assertTrue((m_errorWriter.getOutputAndReset().length() > 0));
102
103 assertNull(resources.getStringFromFile("resourceFile", false));
104 assertTrue((m_errorWriter.getOutputAndReset().length() > 0));
105
106 final String helloWorld = resources.getStringFromFile("aFile", true);
107 assertTrue(!(m_errorWriter.getOutputAndReset().length() > 0));
108 assertEquals("Hello world\n", helloWorld);
109
110 final File file =
111 new File(
112 ResourcesImplementation.class.getResource("resources/helloworld.txt").getFile());
113
114 FileUtilities.setCanAccess(file, false);
115
116 final String noResource = resources.getStringFromFile("aFile", false);
117 assertNull(noResource);
118 assertTrue((m_errorWriter.getOutputAndReset().length() > 0));
119
120 FileUtilities.setCanAccess(file, true);
121 }
122 }