| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | package net.sf.jomic.tools; |
| 17 | |
| 18 | import java.io.IOException; |
| 19 | |
| 20 | import javax.swing.JDialog; |
| 21 | import javax.swing.JOptionPane; |
| 22 | |
| 23 | import junit.framework.TestCase; |
| 24 | |
| 25 | /** |
| 26 | * @author Thomas Aglassinger |
| 27 | */ |
| 28 | public class ErrorToolsTest extends TestCase |
| 29 | { |
| 30 | private static final String TEST_TITLE = ErrorTools.class.getName(); |
| 31 | |
| 32 | private ErrorTools errorTools; |
| 33 | private TestTools testTools; |
| 34 | |
| 35 | protected void setUp() |
| 36 | throws Exception { |
| 37 | super.setUp(); |
| 38 | errorTools = ErrorTools.instance(); |
| 39 | testTools = TestTools.instance(); |
| 40 | } |
| 41 | |
| 42 | public void testCreateDialog() { |
| 43 | JDialog dialog; |
| 44 | |
| 45 | dialog = errorTools.createDialog(null, JOptionPane.ERROR_MESSAGE, "test error", null, false); |
| 46 | try { |
| 47 | assertNotNull(dialog); |
| 48 | dialog.setVisible(true); |
| 49 | testTools.waitSomeTime(); |
| 50 | } finally { |
| 51 | dialog.dispose(); |
| 52 | } |
| 53 | |
| 54 | dialog = errorTools.createDialog(null, JOptionPane.WARNING_MESSAGE, "test error with exception", |
| 55 | new Throwable("test stack"), false); |
| 56 | try { |
| 57 | assertNotNull(dialog); |
| 58 | dialog.setVisible(true); |
| 59 | testTools.waitSomeTime(); |
| 60 | } finally { |
| 61 | dialog.dispose(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | public void testGetDetailedErrorMessage() { |
| 66 | String errorText = "Test error message"; |
| 67 | |
| 68 | assertEquals(errorText, errorTools.getDetailedExceptionMessage(new RuntimeException(errorText))); |
| 69 | assertEquals("NullPointerException", errorTools.getDetailedExceptionMessage(new NullPointerException())); |
| 70 | } |
| 71 | |
| 72 | public void testGetStackTrace() { |
| 73 | Throwable error = new IOException("test"); |
| 74 | String errorText = errorTools.getStackTrace(error); |
| 75 | |
| 76 | assertNotNull(errorText); |
| 77 | assertTrue("errorText must contain \"testGetStackTrace\": " |
| 78 | + errorText, errorText.indexOf("testGetStackTrace") >= 0); |
| 79 | assertTrue("errorText must contain \"IOException\": " |
| 80 | + errorText, errorText.indexOf("IOException") >= 0); |
| 81 | } |
| 82 | |
| 83 | public void testTitle() { |
| 84 | assertNotNull(errorTools.getTitle(JOptionPane.WARNING_MESSAGE)); |
| 85 | assertNotNull(errorTools.getTitle(JOptionPane.ERROR_MESSAGE)); |
| 86 | errorTools.setTitle(JOptionPane.ERROR_MESSAGE, TEST_TITLE); |
| 87 | assertEquals("title", TEST_TITLE, errorTools.getTitle(JOptionPane.ERROR_MESSAGE)); |
| 88 | } |
| 89 | |
| 90 | protected void tearDown() |
| 91 | throws Exception { |
| 92 | testTools = null; |
| 93 | errorTools = null; |
| 94 | super.tearDown(); |
| 95 | } |
| 96 | } |