View Javadoc

1   // Copyright (C) 2004 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 net.grinder.testutility.StubPrintWriter;
27  
28  
29  /**
30   *  Unit test case for {@link DisplayMessageConsoleException}.
31   *
32   * @author Philip Aston
33   */
34  public class TestDisplayMessageConsoleException extends TestCase {
35  
36    public void testDisplayMessageConsoleException() throws Exception {
37      final ResourcesImplementation resources = new ResourcesImplementation(getClass().getName());
38      resources.setErrorWriter(new StubPrintWriter());
39  
40      final DisplayMessageConsoleException e1 =
41        new DisplayMessageConsoleException(resources, "notthere");
42  
43      assertTrue(e1.getMessage().startsWith("No message"));
44      assertTrue(e1.getMessage().indexOf("notthere") >= 0);
45      assertNull(e1.getCause());
46  
47      final DisplayMessageConsoleException e2 =
48        new DisplayMessageConsoleException(resources, "helloworld");
49  
50      assertEquals("Hello world", e2.getMessage());
51      assertNull(e2.getCause());
52  
53      final DisplayMessageConsoleException e3 =
54        new DisplayMessageConsoleException(resources, "sum");
55  
56      assertEquals("{0} plus {1} is {2}", e3.getMessage());
57      assertNull(e2.getCause());
58  
59      final DisplayMessageConsoleException e4 =
60        new DisplayMessageConsoleException(resources, "sum", 
61                                           new Object[]{
62                                             new Integer(1),
63                                             new Integer(2),
64                                             "three"
65                                           });
66  
67      assertEquals("1 plus 2 is three", e4.getMessage());
68      assertNull(e2.getCause());
69  
70      final DisplayMessageConsoleException e5 =
71        new DisplayMessageConsoleException(resources, "notthere", e4);
72  
73      assertTrue(e5.getMessage().startsWith("No message"));
74      assertTrue(e5.getMessage().indexOf("notthere") >= 0);
75      assertSame(e4, e5.getCause());
76  
77      final DisplayMessageConsoleException e6 =
78        new DisplayMessageConsoleException(resources, "helloworld", e5);
79  
80      assertEquals("Hello world", e6.getMessage());
81      assertSame(e5, e6.getCause());
82  
83      final DisplayMessageConsoleException e7 =
84        new DisplayMessageConsoleException(resources,
85                                           "numberOfFiles",
86                                           new Object[] { new Integer(2) },
87                                           e6);
88  
89      assertEquals("There are 2 files", e7.getMessage());
90      assertSame(e6, e7.getCause());
91    }
92  }