1 4 5 package org.springframework.web.servlet.view.xslt; 6 7 import javax.xml.transform.ErrorListener ; 8 import javax.xml.transform.TransformerException ; 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 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 } 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 } 77 } 78 79 public void testCustomErrorListener() { 80 if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) { 81 return; 82 } 83 84 view.setErrorListener(new ErrorListener () { 85 public void warning(TransformerException ex) { 86 incWarnings(); 87 } 88 public void error(TransformerException ex) { 89 incErrors(); 90 } 91 public void fatalError(TransformerException ex) { 92 incFatals(); 93 } 94 }); 95 96 view.setStylesheetLocation(new ClassPathResource("org/springframework/web/servlet/view/xslt/errors.xsl")); 98 try { 99 view.initApplicationContext(); 100 } 101 catch (ApplicationContextException ex) { 102 assertTrue(ex.getCause() instanceof TransformerException ); 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 |