1 /* JeditorPaneBasedExceptionDialog.java
2 Copyright (C) 2012 Red Hat, Inc.
3 
4 This file is part of IcedTea.
5 
6 IcedTea is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 IcedTea is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with IcedTea; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20 
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25 
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37 package net.sourceforge.jnlp.splashscreen.parts;
38 
39 import java.io.PrintWriter;
40 import java.io.StringWriter;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.Date;
44 import java.util.List;
45 import net.sourceforge.jnlp.runtime.Translator;
46 import org.junit.Assert;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49 
50 public class JEditorPaneBasedExceptionDialogTest {
51 
52     private static RuntimeException eex = new RuntimeException("ex2");
53     private static Exception ex = new Exception("ex1", eex);
54     private static String ai = "Another info";
55     private static InformationElement ec = new InformationElement();
56     private static List<String> l = new ArrayList<String>(3);
57 
58     @BeforeClass
fillLists()59     public static void fillLists() {
60         ec.setHomepage("item 1");
61         ec.setTitle("item 2");
62         ec.setvendor("item 3");
63         ec.addDescription("item 4");
64         l = JEditorPaneBasedExceptionDialog.infoElementToList(ec);
65 
66     }
67 
assertHtml(String s)68     static void assertHtml(String s) {
69         Assert.assertTrue("result of getText must be marked html", s.contains("html"));
70         Assert.assertTrue("result of getText must be marked html", s.contains("body"));
71         assertMarkup(s);
72     }
73 
assertMarkup(String s)74     static void assertMarkup(String s) {
75         Assert.assertTrue("result of getText must be marked in by  html markup", s.contains("<") && s.contains(">"));
76         Assert.assertTrue("result of getText must be marked in by  html markup", s.contains("</") || s.contains("/>"));
77     }
78 
assertAI(String s, boolean b)79     private void assertAI(String s, boolean b) {
80         if (b) {
81             Assert.assertTrue("result of getText must contains annother info", s.contains(ai));
82         } else {
83             Assert.assertFalse("result of getText must NOT contains annother info", s.contains(ai));
84         }
85     }
86 
assertLL(String s, boolean b)87     private void assertLL(String s, boolean b) {
88         for (String i : l) {
89 
90             if (b) {
91                 Assert.assertTrue("result of getText must contains  info list", s.contains(i));
92             } else {
93                 Assert.assertFalse("result of getText must NOT contains info list", s.contains(i));
94             }
95         }
96     }
97 
assertFullException(String s, boolean b)98     private void assertFullException(String s, boolean b) {
99         if (b) {
100             Assert.assertTrue("result of getText must contains  complete exception", s.contains(ex.getMessage()));
101             Assert.assertTrue("result of getText must contains  complete exception", s.contains(eex.getMessage()));
102         } else {
103             Assert.assertFalse("result of getText must contains not  complete exception", s.contains(ex.getMessage()));
104             Assert.assertFalse("result of getText must contains  not complete exception", s.contains(eex.getMessage()));
105 
106         }
107     }
108 
109     @Test
getTextTest()110     public void getTextTest() {
111         String s1 = JEditorPaneBasedExceptionDialog.getText(ex, l, ai, new Date());
112         String s2 = JEditorPaneBasedExceptionDialog.getText(ex, l, null, new Date());
113         String s3 = JEditorPaneBasedExceptionDialog.getText(ex, null, ai, new Date());
114         String s4 = JEditorPaneBasedExceptionDialog.getText(null, l, ai, new Date());
115         assertHtml(s1);
116         assertHtml(s2);
117         assertHtml(s3);
118         assertHtml(s4);
119         assertAI(s1, true);
120         assertAI(s2, false);
121         assertAI(s3, true);
122         assertAI(s4, true);
123         assertLL(s1, true);
124         assertLL(s2, true);
125         assertLL(s3, false);
126         assertLL(s4, true);
127         assertFullException(s1, true);
128         assertFullException(s2, true);
129         assertFullException(s3, true);
130         assertFullException(s4, false);
131         JEditorPaneBasedExceptionDialog d1 = new JEditorPaneBasedExceptionDialog(null, false, ex, ec, ai);
132         JEditorPaneBasedExceptionDialog d2 = new JEditorPaneBasedExceptionDialog(null, false, ex, ec, null);
133         JEditorPaneBasedExceptionDialog d3 = new JEditorPaneBasedExceptionDialog(null, false, ex, null, ai);
134         JEditorPaneBasedExceptionDialog d4 = new JEditorPaneBasedExceptionDialog(null, false, null, ec, ai);
135         Assert.assertTrue("message from dialog must be same as pattern", d1.getMessage().equals(s1));
136         Assert.assertTrue("message from dialog must be same as pattern", d2.getMessage().equals(s2));
137         Assert.assertTrue("message from dialog must be same as pattern", d3.getMessage().equals(s3));
138         Assert.assertTrue("message from dialog must be same as pattern", d4.getMessage().equals(s4));
139 
140     }
141 
142     @Test
getExceptionStackTraceAsString()143     public void getExceptionStackTraceAsString() {
144         String t1 = JEditorPaneBasedExceptionDialog.getExceptionStackTraceAsString(ex);
145         assertFullException(t1, true);
146         String t2 = JEditorPaneBasedExceptionDialog.getExceptionStackTraceAsString(null);
147         Assert.assertNotNull("For null empty result must not be null", t2);
148         Assert.assertEquals("null input must result to empty string", "", t2);
149     }
150 
151     @Test
getExceptionStackTraceAsStrings()152     public void getExceptionStackTraceAsStrings() {
153         String[] t1 = JEditorPaneBasedExceptionDialog.getExceptionStackTraceAsStrings(ex);
154         assertFullException(Arrays.toString(t1), true);
155         String[] t2 = JEditorPaneBasedExceptionDialog.getExceptionStackTraceAsStrings(null);
156         Assert.assertNotNull("For null empty result must not be null", t2);
157         Assert.assertArrayEquals("null input must result to empty array", new String[0], t2);
158     }
159 
160     @Test
formatListInfoList()161     public void formatListInfoList() {
162         String t1 = JEditorPaneBasedExceptionDialog.formatListInfoList(l);
163         assertMarkup(t1);
164         assertLL(t1, true);
165         String t2 = JEditorPaneBasedExceptionDialog.formatInfo(null);
166         Assert.assertNotNull("For null empty result must not be null", t2);
167         Assert.assertEquals("null input must result to empty string", "", t2);
168     }
169 
170     @Test
formatInfo()171     public void formatInfo() {
172         String s = "SOME STRING";
173         String t1 = JEditorPaneBasedExceptionDialog.formatInfo(s);
174         assertMarkup(t1);
175         Assert.assertTrue("Marked text must contains source", t1.contains(s));
176         String t2 = JEditorPaneBasedExceptionDialog.formatInfo(null);
177         Assert.assertNotNull("For null empty result must not be null", t2);
178         Assert.assertEquals("null input must result to empty string", "", t2);
179 
180     }
181 
182     @Test
infoElementToListTets()183     public void infoElementToListTets() {
184 
185         List<String> tl = JEditorPaneBasedExceptionDialog.infoElementToList(ec);
186         Assert.assertTrue("Transformed elemetn must contains all items ", tl.contains(l.get(0)));
187         Assert.assertTrue("Transformed elemetn must contains all items ", tl.contains(l.get(1)));
188         Assert.assertTrue("Transformed elemetn must contains all items ", tl.contains(l.get(2)));
189         Assert.assertTrue("Transformed elemetn must contains all items ", tl.contains(l.get(3)));
190     }
191 }
192