X-Git-Url: http://jaekl.net/gitweb/?p=frank.git;a=blobdiff_plain;f=test%2Fnet%2Fjaekl%2Ffrank%2FErrorHandlerTest.java;fp=test%2Fnet%2Fjaekl%2Ffrank%2FErrorHandlerTest.java;h=4db0602c8cf428aa4deb7bef90f500ba4fc2da57;hp=0000000000000000000000000000000000000000;hb=60dc53edca9ae2bcd6703e02b26acd47ef3d61a8;hpb=418e4d229a8b607b022cfb867bb702bec1765d13 diff --git a/test/net/jaekl/frank/ErrorHandlerTest.java b/test/net/jaekl/frank/ErrorHandlerTest.java new file mode 100644 index 0000000..4db0602 --- /dev/null +++ b/test/net/jaekl/frank/ErrorHandlerTest.java @@ -0,0 +1,102 @@ +package net.jaekl.frank; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintWriter; +import java.sql.SQLException; +import java.util.Locale; + +import net.jaekl.qd.QDException; +import net.jaekl.qd.http.InvalidResponseException; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ErrorHandlerTest { + static final String GATEWAY = "http://www.jaekl.net/api"; + static final String METHOD = "SampleApiMethod"; + static final String RESPONSE = "Required parameter not specified."; + + ByteArrayOutputStream m_baos; + PrintWriter m_pw; + + @Before + public void setUp() { + m_baos = new ByteArrayOutputStream(); + m_pw = new PrintWriter(m_baos); + } + + @After + public void tearDown() throws IOException { + m_pw.close(); + } + + @Test + public void testWriteErrorPage_basicBehaviourWithVariousExceptions() { + ErrorHandler eh = new ErrorHandler(); + + Throwable[] throwables = { + new NullPointerException(), + new QDException(), + new SQLException(), + new InvalidResponseException(new NullPointerException(), GATEWAY, METHOD, RESPONSE) + }; + + for (Throwable t : throwables) { + m_baos.reset(); + eh.writeErrorPage(m_pw, t, Locale.CANADA); + m_pw.flush(); + + String actual = m_baos.toString(); + Assert.assertTrue(actual.contains("Frank: Error Page")); + Assert.assertTrue(actual.contains(t.toString())); + } + } + + @Test + public void testExplain_unexpectedException() { + Locale[] locales = { Locale.CANADA, Locale.FRANCE, Locale.JAPAN, Locale.CHINA}; + + ErrorHandler eh = new ErrorHandler(); + + for (Locale locale : locales) { + FrankBundle bundle = FrankBundle.getInst(locale); + + m_baos.reset(); + eh.writeErrorPage(m_pw, new NullPointerException(), Locale.CANADA); + m_pw.flush(); + + String actual = m_baos.toString(); + Assert.assertTrue(actual.contains(bundle.get(FrankBundle.UNEXPECTED_EXCEPTION))); + } + } + + @Test + public void testExplain_wrappedInvalidResponseException() { + Locale[] locales = { Locale.CANADA, Locale.FRANCE, Locale.JAPAN, Locale.CHINA}; + + ErrorHandler eh = new ErrorHandler(); + InvalidResponseException ire = new InvalidResponseException(null, GATEWAY, METHOD, RESPONSE); + FrankException fe = new FrankException(ire); + + for (Locale locale : locales) { + FrankBundle bundle = FrankBundle.getInst(locale); + + m_baos.reset(); + eh.writeErrorPage(m_pw, fe, Locale.CANADA); + m_pw.flush(); + + String actual = m_baos.toString(); + + Assert.assertTrue(actual.contains(bundle.get(FrankBundle.INVALID_RESPONSE))); + + Assert.assertTrue(actual.contains(bundle.get(FrankBundle.URL_CONTACTED))); + Assert.assertTrue(actual.contains(bundle.get(FrankBundle.REQUEST_MADE))); + Assert.assertTrue(actual.contains(bundle.get(FrankBundle.ANSWER_RECEIVED))); + + Assert.assertTrue(actual.contains(bundle.get(FrankBundle.MAYBE_SERVER_PROBLEM))); + } + } +}