create changelog entry
[debian/openrocket] / core / test / net / sf / openrocket / l10n / TestExceptionSuppressingTranslator.java
1 package net.sf.openrocket.l10n;
2
3 import static org.junit.Assert.*;
4
5 import java.util.MissingResourceException;
6
7 import net.sf.openrocket.startup.Application;
8 import net.sf.openrocket.startup.ExceptionHandler;
9
10 import org.jmock.Expectations;
11 import org.jmock.Mockery;
12 import org.jmock.auto.Mock;
13 import org.jmock.integration.junit4.JMock;
14 import org.jmock.integration.junit4.JUnit4Mockery;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17
18 @RunWith(JMock.class)
19 public class TestExceptionSuppressingTranslator {
20         Mockery context = new JUnit4Mockery();
21         
22         @Mock
23         Translator translator;
24         
25         @Mock
26         ExceptionHandler exceptionHandler;
27         
28         @Test
29         public void testSuccessful() {
30                 Application.setExceptionHandler(exceptionHandler);
31                 ExceptionSuppressingTranslator est = new ExceptionSuppressingTranslator(translator);
32                 
33                 // @formatter:off
34                 context.checking(new Expectations() {{
35                                 oneOf(translator).get("fake.key4"); will(returnValue("foobar")); 
36                 }});
37                 // @formatter:on
38                 
39                 assertEquals("foobar", est.get("fake.key4"));
40         }
41         
42         
43         @Test
44         public void testFailure() {
45                 Application.setExceptionHandler(exceptionHandler);
46                 ExceptionSuppressingTranslator est = new ExceptionSuppressingTranslator(translator);
47                 
48                 assertFalse("Prerequisite failed", ExceptionSuppressingTranslator.errorReported);
49                 
50                 // @formatter:off
51                 context.checking(new Expectations() {{
52                         oneOf(exceptionHandler).handleErrorCondition(with(any(String.class)), with(any(MissingResourceException.class)));
53                         oneOf(translator).get("fake.key5"); will(throwException(new MissingResourceException("a", "b", "c"))); 
54                         oneOf(translator).get("fake.key5"); will(throwException(new MissingResourceException("a", "b", "c"))); 
55                         oneOf(translator).get("fake.key6"); will(throwException(new MissingResourceException("a", "b", "c"))); 
56                 }});
57                 // @formatter:on
58                 
59                 // Test first failure
60                 assertEquals("fake.key5", est.get("fake.key5"));
61                 assertTrue(ExceptionSuppressingTranslator.errorReported);
62                 
63                 // Test second failure
64                 assertEquals("fake.key5", est.get("fake.key5"));
65                 assertTrue(ExceptionSuppressingTranslator.errorReported);
66                 
67                 // Test failure with other key
68                 assertEquals("fake.key6", est.get("fake.key6"));
69                 assertTrue(ExceptionSuppressingTranslator.errorReported);
70         }
71         
72         
73 }