Improve error reporting. New error page that gives a bit more of an explanation...
[frank.git] / test / net / jaekl / frank / ErrorHandlerTest.java
diff --git a/test/net/jaekl/frank/ErrorHandlerTest.java b/test/net/jaekl/frank/ErrorHandlerTest.java
new file mode 100644 (file)
index 0000000..4db0602
--- /dev/null
@@ -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("<TITLE>Frank: Error Page</TITLE>"));
+                       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)));
+               }
+       }
+}