Remove test dependency on Swing

This commit is contained in:
Sampo Niskanen 2012-03-17 20:35:21 +00:00
parent db47952192
commit 8f6fa2121c
2 changed files with 26 additions and 27 deletions

View File

@ -35,11 +35,11 @@ public class TestClassBasedTranslator {
// @formatter:off // @formatter:off
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(translator).get("TestClassBasedTranslator.fake.key"); will(returnValue("foobar")); oneOf(translator).get("TestClassBasedTranslator.fake.key1"); will(returnValue("foobar"));
}}); }});
// @formatter:on // @formatter:on
assertEquals("foobar", cbt.get("fake.key")); assertEquals("foobar", cbt.get("fake.key1"));
} }
@ -49,12 +49,12 @@ public class TestClassBasedTranslator {
// @formatter:off // @formatter:off
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(translator).get("TestClassBasedTranslator.fake.key"); will(throwException(new MissingResourceException("a", "b", "c"))); oneOf(translator).get("TestClassBasedTranslator.fake.key2"); will(throwException(new MissingResourceException("a", "b", "c")));
oneOf(translator).get("fake.key"); will(returnValue("barbaz")); oneOf(translator).get("fake.key2"); will(returnValue("barbaz"));
}}); }});
// @formatter:on // @formatter:on
assertEquals("barbaz", cbt.get("fake.key")); assertEquals("barbaz", cbt.get("fake.key2"));
} }
@ -64,15 +64,15 @@ public class TestClassBasedTranslator {
// @formatter:off // @formatter:off
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(translator).get("TestClassBasedTranslator.fake.key"); will(throwException(new MissingResourceException("a", "b", "c"))); oneOf(translator).get("TestClassBasedTranslator.fake.key3"); will(throwException(new MissingResourceException("a", "b", "c")));
oneOf(translator).get("fake.key"); will(throwException(new MissingResourceException("a", "b", "c"))); oneOf(translator).get("fake.key3"); will(throwException(new MissingResourceException("a", "b", "c")));
}}); }});
// @formatter:on // @formatter:on
try { try {
fail("Returned: " + cbt.get("fake.key")); fail("Returned: " + cbt.get("fake.key3"));
} catch (MissingResourceException e) { } catch (MissingResourceException e) {
assertEquals("Neither key 'TestClassBasedTranslator.fake.key' nor 'fake.key' could be found", e.getMessage()); assertEquals("Neither key 'TestClassBasedTranslator.fake.key3' nor 'fake.key3' could be found", e.getMessage());
} }
} }

View File

@ -1,20 +1,17 @@
package net.sf.openrocket.l10n; package net.sf.openrocket.l10n;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.MissingResourceException; import java.util.MissingResourceException;
import net.sf.openrocket.gui.main.SwingExceptionHandler;
import net.sf.openrocket.startup.Application; import net.sf.openrocket.startup.Application;
import net.sf.openrocket.startup.ExceptionHandler;
import org.jmock.Expectations; import org.jmock.Expectations;
import org.jmock.Mockery; import org.jmock.Mockery;
import org.jmock.auto.Mock; import org.jmock.auto.Mock;
import org.jmock.integration.junit4.JMock; import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery; import org.jmock.integration.junit4.JUnit4Mockery;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -22,53 +19,55 @@ import org.junit.runner.RunWith;
public class TestExceptionSuppressingTranslator { public class TestExceptionSuppressingTranslator {
Mockery context = new JUnit4Mockery(); Mockery context = new JUnit4Mockery();
@Before
public void setupExceptionHandler() {
Application.setExceptionHandler( new SwingExceptionHandler() );
}
@Mock @Mock
Translator translator; Translator translator;
@Mock
ExceptionHandler exceptionHandler;
@Test @Test
public void testSuccessful() { public void testSuccessful() {
Application.setExceptionHandler(exceptionHandler);
ExceptionSuppressingTranslator est = new ExceptionSuppressingTranslator(translator); ExceptionSuppressingTranslator est = new ExceptionSuppressingTranslator(translator);
// @formatter:off // @formatter:off
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(translator).get("fake.key"); will(returnValue("foobar")); oneOf(translator).get("fake.key4"); will(returnValue("foobar"));
}}); }});
// @formatter:on // @formatter:on
assertEquals("foobar", est.get("fake.key")); assertEquals("foobar", est.get("fake.key4"));
} }
@Test @Test
public void testFailure() { public void testFailure() {
Application.setExceptionHandler(exceptionHandler);
ExceptionSuppressingTranslator est = new ExceptionSuppressingTranslator(translator); ExceptionSuppressingTranslator est = new ExceptionSuppressingTranslator(translator);
assertFalse("Prerequisite failed", ExceptionSuppressingTranslator.errorReported); assertFalse("Prerequisite failed", ExceptionSuppressingTranslator.errorReported);
// @formatter:off // @formatter:off
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(translator).get("fake.key"); will(throwException(new MissingResourceException("a", "b", "c"))); oneOf(exceptionHandler).handleErrorCondition(with(any(String.class)), with(any(MissingResourceException.class)));
oneOf(translator).get("fake.key"); will(throwException(new MissingResourceException("a", "b", "c"))); oneOf(translator).get("fake.key5"); will(throwException(new MissingResourceException("a", "b", "c")));
oneOf(translator).get("fake.key2"); will(throwException(new MissingResourceException("a", "b", "c"))); oneOf(translator).get("fake.key5"); will(throwException(new MissingResourceException("a", "b", "c")));
oneOf(translator).get("fake.key6"); will(throwException(new MissingResourceException("a", "b", "c")));
}}); }});
// @formatter:on // @formatter:on
// Test first failure // Test first failure
assertEquals("fake.key", est.get("fake.key")); assertEquals("fake.key5", est.get("fake.key5"));
assertTrue(ExceptionSuppressingTranslator.errorReported); assertTrue(ExceptionSuppressingTranslator.errorReported);
// Test second failure // Test second failure
assertEquals("fake.key", est.get("fake.key")); assertEquals("fake.key5", est.get("fake.key5"));
assertTrue(ExceptionSuppressingTranslator.errorReported); assertTrue(ExceptionSuppressingTranslator.errorReported);
// Test failure with other key // Test failure with other key
assertEquals("fake.key2", est.get("fake.key2")); assertEquals("fake.key6", est.get("fake.key6"));
assertTrue(ExceptionSuppressingTranslator.errorReported); assertTrue(ExceptionSuppressingTranslator.errorReported);
} }
} }