View Javadoc

1   // Copyright (C) 2004, 2005 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
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   *  Unit test case for {@link ResourcesImplementation}.
34   *
35   * @author Philip Aston
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 }