KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > xslt > XsltViewTests


1 /*
2  * XsltViewTests.java
3  */

4
5 package org.springframework.web.servlet.view.xslt;
6
7 import javax.xml.transform.ErrorListener JavaDoc;
8 import javax.xml.transform.TransformerException JavaDoc;
9
10 import junit.framework.TestCase;
11
12 import org.springframework.context.ApplicationContextException;
13 import org.springframework.core.JdkVersion;
14 import org.springframework.core.io.ClassPathResource;
15 import org.springframework.core.io.FileSystemResource;
16
17 /**
18  * @author Darren Davison
19  * @since 11.03.2005
20  */

21 public class XsltViewTests extends TestCase {
22
23     private TestXsltView view;
24
25     private int warnings = 0;
26
27     private int errors = 0;
28
29     private int fatal = 0;
30
31     public void setUp() {
32         view = new TestXsltView();
33     }
34
35     private void incWarnings() {
36         warnings++;
37     }
38
39     private void incErrors() {
40         errors++;
41     }
42
43     private void incFatals() {
44         fatal++;
45     }
46
47     public void testNoSuchStylesheet() {
48         if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) {
49             return;
50         }
51
52         view.setStylesheetLocation(new FileSystemResource("/does/not/exist.xsl"));
53         try {
54             view.initApplicationContext();
55             fail("Should have thrown ApplicationContextException");
56         }
57         catch (ApplicationContextException e) {
58             // OK
59
}
60     }
61
62     public void testChangeStylesheetReCachesTemplate() {
63         if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) {
64             return;
65         }
66
67         view.setStylesheetLocation(new ClassPathResource("org/springframework/web/servlet/view/xslt/valid.xsl"));
68         view.initApplicationContext();
69
70         try {
71             view.setStylesheetLocation(new FileSystemResource("/does/not/exist.xsl"));
72             fail("Should throw ApplicationContextException on re-caching template");
73         }
74         catch (ApplicationContextException ex) {
75             // OK
76
}
77     }
78
79     public void testCustomErrorListener() {
80         if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) {
81             return;
82         }
83
84         view.setErrorListener(new ErrorListener JavaDoc() {
85             public void warning(TransformerException JavaDoc ex) {
86                 incWarnings();
87             }
88             public void error(TransformerException JavaDoc ex) {
89                 incErrors();
90             }
91             public void fatalError(TransformerException JavaDoc ex) {
92                 incFatals();
93             }
94         });
95
96         // loaded stylesheet is not well formed
97
view.setStylesheetLocation(new ClassPathResource("org/springframework/web/servlet/view/xslt/errors.xsl"));
98         try {
99             view.initApplicationContext();
100         }
101         catch (ApplicationContextException ex) {
102             // shouldn't really happen, but can be let through by XSLT engine
103
assertTrue(ex.getCause() instanceof TransformerException JavaDoc);
104         }
105         assertEquals(1, fatal);
106         assertEquals(1, errors);
107         assertEquals(0, warnings);
108     }
109
110
111     private static class TestXsltView extends AbstractXsltView {
112
113     }
114
115 }
116
Popular Tags