1 21 22 package org.apache.commons.validator; 23 24 import java.io.IOException ; 25 26 import org.xml.sax.SAXException ; 27 28 31 public class ExceptionTest extends TestCommon { 32 33 37 protected static String FORM_KEY = "exceptionForm"; 38 39 42 protected static String ACTION = "raiseException"; 43 44 public ExceptionTest(String name) { 45 super(name); 46 } 47 48 52 protected void setUp() throws IOException , SAXException { 53 loadResources("validator-exception.xml"); 54 } 55 56 60 public void testValidatorException() { 61 ValueBean info = new ValueBean(); 63 info.setValue("VALIDATOR"); 64 65 Validator validator = new Validator(resources, FORM_KEY); 68 validator.setParameter(Validator.BEAN_PARAM, info); 71 72 try { 74 validator.validate(); 75 fail("ValidatorException should occur here!"); 76 } catch (ValidatorException expected) { 77 assertTrue("VALIDATOR-EXCEPTION".equals(expected.getMessage())); 78 } 79 } 80 81 84 public void testRuntimeException() throws ValidatorException { 85 ValueBean info = new ValueBean(); 87 info.setValue("RUNTIME"); 88 89 Validator validator = new Validator(resources, FORM_KEY); 92 validator.setParameter(Validator.BEAN_PARAM, info); 95 96 try { 98 validator.validate(); 99 } catch (RuntimeException expected) { 101 fail("RuntimeExceptions should be treated as validation failures in Validator 1.x."); 102 } 105 } 106 107 111 public void testCheckedException() { 112 ValueBean info = new ValueBean(); 114 info.setValue("CHECKED"); 115 116 Validator validator = new Validator(resources, FORM_KEY); 119 validator.setParameter(Validator.BEAN_PARAM, info); 122 123 125 try { 127 validator.validate(); 128 } catch (ValidatorException expected) { 129 fail("Checked exceptions are not wrapped in ValidatorException in Validator 1.x."); 130 } catch (Exception e) { 131 assertTrue("CHECKED-EXCEPTION".equals(e.getMessage())); 132 } 133 134 } 142 } 143 | Popular Tags |