Java tutorial
/* * Copyright (c) 2007 NTT DATA Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jp.terasoluna.fw.validation; import static java.util.Arrays.asList; import static org.hamcrest.core.Is.is; import static org.hamcrest.core.IsEqual.equalTo; import static org.hamcrest.core.IsInstanceOf.instanceOf; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static uk.org.lidalia.slf4jtest.LoggingEvent.error; import java.lang.reflect.InvocationTargetException; import jp.terasoluna.fw.util.ClassLoadException; import org.apache.commons.validator.Field; import org.apache.commons.validator.ValidatorAction; import org.apache.commons.validator.Var; import org.junit.After; import org.junit.Before; import org.junit.Test; import uk.org.lidalia.slf4jtest.TestLogger; import uk.org.lidalia.slf4jtest.TestLoggerFactory; /** * {@link FieldChecks} ? * <p> * <h4>???</h4> TERASOLUNA????? * <p> * @see jp.terasoluna.fw.validation.FieldChecks */ public class FieldChecksTest11 { private TestLogger logger = TestLoggerFactory.getTestLogger(FieldChecks.class); /** * ??? */ @Before public void setUp() { FieldChecks_MultiFieldValidatorImpl01.result = false; FieldChecks_MultiFieldValidatorImpl01.validateCalledCount = 0; FieldChecks_MultiFieldValidatorImpl01.value = null; FieldChecks_MultiFieldValidatorImpl01.fields = null; } /** * ?? */ @After public void tearDown() throws Exception { logger.clear(); } /** * testValidateMultiField01() <br> * <br> * () <br> * C, F, I <br> * <br> * () bean:null<br> * () va:ValidatorActionn?<br> * () field:Field?<br> * <br> * Msg("message, "message")<br> * () errors:MockValidationErrors?<br> * <br> * () boolean:true<br> * () errors:errorMessage?null??????<br> * () :ERROR<br> * "bean is null."<br> * <br> * bean?null??????TRUE??????? <br> * @throws Exception ????? */ @Test public void testValidateMultiField01() throws Exception { // ?? Object bean = null; ValidatorAction va = new ValidatorAction(); Field field = new Field(); FieldChecks_ValidationErrorsImpl03 errors = new FieldChecks_ValidationErrorsImpl03(); // FieldChecks fieldChecks = new FieldChecks(); boolean result = fieldChecks.validateMultiField(bean, va, field, errors); // assertTrue(result); assertNull(errors.errorMessage); assertThat(logger.getLoggingEvents(), is(asList(error("bean is null.")))); } /** * testValidateMultiField02() <br> * <br> * () <br> * C, F, G, I <br> * <br> * () bean:""<br> * () va:ValidatorActionn?<br> * () field:???Field?<br> * <br> * varmultiFieldValidator=null<br> * () errors:MockValidationErrors?<br> * <br> * () errors:errorMessage?null??????<br> * () :IllegalArgumentException<br> * "var value[multiFieldValidator] is required."<br> * () :ERROR<br> * "var value[multiFieldValidator] is required."<br> * <br> * field??var-namemultiFieldValidator??var-value?null???? ???IllegalArgumentException??????<br> * <br> * bean????????????? <br> * @throws Exception ????? */ @Test public void testValidateMultiField02() throws Exception { // ?? Object bean = ""; ValidatorAction va = new ValidatorAction(); Field field = new Field(); Var var = new Var("multiFieldValidator", null, null); field.addVar(var); FieldChecks_ValidationErrorsImpl03 errors = new FieldChecks_ValidationErrorsImpl03(); // FieldChecks fieldChecks = new FieldChecks(); try { fieldChecks.validateMultiField(bean, va, field, errors); fail("IllegalArgumentException??????"); } catch (IllegalArgumentException e) { // assertNull(errors.errorMessage); assertEquals(IllegalArgumentException.class.getName(), e.getClass().getName()); assertEquals("var value[multiFieldValidator] is required.", e.getMessage()); assertThat(logger.getLoggingEvents(), is(asList(error("var value[multiFieldValidator] is required.")))); } } /** * testValidateMultiField03() <br> * <br> * () <br> * C, F, G, I <br> * <br> * () bean:"bean"<br> * () va:ValidatorActionn?<br> * () field:???Field?<br> * <br> * varmultiFieldValidator=""<br> * () errors:MockValidationErrors?<br> * <br> * () errors:errorMessage?null??????<br> * () :IllegalArgumentException<br> * "var value[multiFieldValidator] is required."<br> * () :ERROR<br> * "var value[multiFieldValidator] is required."<br> * <br> * field??var-namemultiFieldValidator??var-value????? ???IllegalArgumentException?????? <br> * @throws Exception ????? */ @Test public void testValidateMultiField03() throws Exception { // ?? Object bean = "bean"; ValidatorAction va = new ValidatorAction(); Field field = new Field(); Var var = new Var("multiFieldValidator", "", null); field.addVar(var); FieldChecks_ValidationErrorsImpl03 errors = new FieldChecks_ValidationErrorsImpl03(); // FieldChecks fieldChecks = new FieldChecks(); try { fieldChecks.validateMultiField(bean, va, field, errors); fail("IllegalArgumentException??????"); } catch (IllegalArgumentException e) { // assertNull(errors.errorMessage); assertEquals(IllegalArgumentException.class.getName(), e.getClass().getName()); assertEquals("var value[multiFieldValidator] is required.", e.getMessage()); assertThat(logger.getLoggingEvents(), is(asList(error("var value[multiFieldValidator] is required.")))); } } /** * testValidateMultiField04() <br> * <br> * () <br> * F, G, I <br> * <br> * () bean:"bean"<br> * () va:ValidatorActionn?<br> * () field:???Field?<br> * <br> * varmultiFieldValidator="not.Exist"<br> * () errors:MockValidationErrors?<br> * <br> * () errors:errorMessage?null??????<br> * () :IllegalArgumentException<br> * "var value[multiFieldValidator] is invalid."<br> * ??ClassLoadException<br> * () :ERROR<br> * "var value[multiFieldValidator] is invalid."<br> * ??ClassLoadException<br> * <br> * field??var-namemultiFieldValidator??var-value?? ????????????IllegalArgumentException? * ????? <br> * @throws Exception ????? */ @Test public void testValidateMultiField04() throws Exception { // ?? Object bean = "bean"; ValidatorAction va = new ValidatorAction(); Field field = new Field(); Var var = new Var("multiFieldValidator", "not.Exist", null); field.addVar(var); FieldChecks_ValidationErrorsImpl03 errors = new FieldChecks_ValidationErrorsImpl03(); // FieldChecks fieldChecks = new FieldChecks(); try { fieldChecks.validateMultiField(bean, va, field, errors); fail("IllegalArgumentException??????"); } catch (IllegalArgumentException e) { // assertNull(errors.errorMessage); assertEquals(IllegalArgumentException.class.getName(), e.getClass().getName()); assertEquals("var value[multiFieldValidator] is invalid.", e.getMessage()); assertTrue(e.getCause() instanceof ClassLoadException); assertThat(logger.getLoggingEvents().get(0).getMessage(), is(equalTo("var value[multiFieldValidator] is invalid."))); // assertTrue(LogUTUtil.checkError( // "var value[multiFieldValidator] is invalid.", // new ClassLoadException(new RuntimeException()))); assertThat(logger.getLoggingEvents().get(0).getThrowable().get(), instanceOf(ClassLoadException.class)); assertThat(logger.getLoggingEvents().get(0).getThrowable().get().getCause(), instanceOf(ClassNotFoundException.class)); } } /** * testValidateMultiField05() <br> * <br> * () <br> * F, G, I <br> * <br> * () bean:"bean"<br> * () va:ValidatorActionn?<br> * () field:???Field?<br> * <br> * varmultiFieldValidator="java.lang.String"<br> * () errors:MockValidationErrors?<br> * <br> * () errors:errorMessage?null??????<br> * () :IllegalArgumentException<br> * "var value[multiFieldValidator] is invalid."<br> * ??ClassCastException<br> * () :ERROR<br> * "var value[multiFieldValidator] is invalid."<br> * ??ClassCastException<br> * <br> * field??var-namemultiFieldValidator??var-value? MultiFieldValidator?????????????? * IllegalArgumentException?????? <br> * @throws Exception ????? */ @Test public void testValidateMultiField05() throws Exception { // ?? Object bean = "bean"; ValidatorAction va = new ValidatorAction(); Field field = new Field(); Var var = new Var("multiFieldValidator", "java.lang.String", null); field.addVar(var); FieldChecks_ValidationErrorsImpl03 errors = new FieldChecks_ValidationErrorsImpl03(); // FieldChecks fieldChecks = new FieldChecks(); try { fieldChecks.validateMultiField(bean, va, field, errors); fail("IllegalArgumentException??????"); } catch (IllegalArgumentException e) { // assertNull(errors.errorMessage); assertEquals(IllegalArgumentException.class.getName(), e.getClass().getName()); assertEquals("var value[multiFieldValidator] is invalid.", e.getMessage()); assertTrue(e.getCause() instanceof ClassCastException); assertThat(logger.getLoggingEvents().get(0).getMessage(), is(equalTo("var value[multiFieldValidator] is invalid."))); assertThat(logger.getLoggingEvents().get(0).getThrowable().get(), instanceOf(ClassCastException.class)); } } /** * testValidateMultiField06() <br> * <br> * () <br> * F <br> * <br> * () bean:"bean"<br> * () va:ValidatorActionn?<br> * () field:???Field?<br> * <br> * varmultiFieldValidator= "jp.terasoluna.fw.validation. FieldChecks_MultiFieldValidatorImpl01"<br> * varfields=null<br> * () errors:MockValidationErrors?<br> * () MultiFieldValidator#validate?:TRUE?<br> * <br> * () boolean:true<br> * () errors:errorMessage?null??????<br> * () MultiFieldValidator:validateCalledCount? 1??????<br> * <br> * value?"bean"??????<br> * <br> * fields???0??????<br> * <br> * field?var-namefields??var-value?null??? MultiFieldValidator#validate??????????<br> * <br> * bean?????MultiFieldValidator#validate? ???????????? <br> * @throws Exception ????? */ @Test public void testValidateMultiField06() throws Exception { // ?? Object bean = "bean"; ValidatorAction va = new ValidatorAction(); Field field = new Field(); Var var1 = new Var("multiFieldValidator", "jp.terasoluna.fw.validation." + "FieldChecks_MultiFieldValidatorImpl01", null); Var var2 = new Var("fields", null, null); field.addVar(var1); field.addVar(var2); FieldChecks_ValidationErrorsImpl03 errors = new FieldChecks_ValidationErrorsImpl03(); FieldChecks_MultiFieldValidatorImpl01.result = true; // FieldChecks fieldChecks = new FieldChecks(); boolean result = fieldChecks.validateMultiField(bean, va, field, errors); // assertTrue(result); assertNull(errors.errorMessage); assertEquals(1, FieldChecks_MultiFieldValidatorImpl01.validateCalledCount); assertEquals("bean", FieldChecks_MultiFieldValidatorImpl01.value); assertNotNull(FieldChecks_MultiFieldValidatorImpl01.fields); assertEquals(0, FieldChecks_MultiFieldValidatorImpl01.fields.length); } /** * testValidateMultiField07() <br> * <br> * () <br> * F <br> * <br> * () bean:"bean"<br> * () va:ValidatorActionn?<br> * () field:???Field?<br> * <br> * varmultiFieldValidator= "jp.terasoluna.fw.validation. FieldChecks_MultiFieldValidatorImpl01"<br> * varfields=""<br> * () errors:MockValidationErrors?<br> * () MultiFieldValidator#validate?:TRUE?<br> * <br> * () boolean:true<br> * () errors:errorMessage?null??????<br> * () MultiFieldValidator:validateCalledCount?1? ?????<br> * <br> * value?"bean"??????<br> * <br> * fields???0??????<br> * <br> * field?var-namefields??var-value???? MultiFieldValidator#validate?????????? <br> * @throws Exception ????? */ @Test public void testValidateMultiField07() throws Exception { // ?? Object bean = "bean"; ValidatorAction va = new ValidatorAction(); Field field = new Field(); Var var1 = new Var("multiFieldValidator", "jp.terasoluna.fw.validation." + "FieldChecks_MultiFieldValidatorImpl01", null); Var var2 = new Var("fields", "", null); field.addVar(var1); field.addVar(var2); FieldChecks_ValidationErrorsImpl03 errors = new FieldChecks_ValidationErrorsImpl03(); FieldChecks_MultiFieldValidatorImpl01.result = true; // FieldChecks fieldChecks = new FieldChecks(); boolean result = fieldChecks.validateMultiField(bean, va, field, errors); // assertTrue(result); assertNull(errors.errorMessage); assertEquals(1, FieldChecks_MultiFieldValidatorImpl01.validateCalledCount); assertEquals("bean", FieldChecks_MultiFieldValidatorImpl01.value); assertNotNull(FieldChecks_MultiFieldValidatorImpl01.fields); assertEquals(0, FieldChecks_MultiFieldValidatorImpl01.fields.length); } /** * testValidateMultiField08() <br> * <br> * () <br> * F <br> * <br> * () bean:???FieldChecksExtend_BeanStub01?<br> * <br> * field1=Object?1<br> * field2=Object?2<br> * () va:ValidatorActionn?<br> * () field:???Field?<br> * <br> * property="field1"<br> * varmultiFieldValidator= "jp.terasoluna.fw.validation. FieldChecks_MultiFieldValidatorImpl01"<br> * varfields="field2"<br> * () errors:MockValidationErrors?<br> * () MultiFieldValidator#validate?:TRUE?<br> * <br> * () boolean:true<br> * () errors:errorMessage?null??????<br> * () MultiFieldValidator:validateCalledCount? 1??????<br> * <br> * value????Object?1? ????????<br> * <br> * fields???1??????<br> * <br> * fields??1???????<br> * fields[0]=Object?2???<br> * <br> * field?var-namefields??var-value????? ?????????????bean???? * ?1?????MultiFieldValidator???????<br> * <br> * bean?JavaBean????field????? ?bean???????MultiFieldValidator#validate??? ?????? * <br> * @throws Exception ????? */ @Test public void testValidateMultiField08() throws Exception { // ?? FieldChecks_JavaBeanStub02 bean = new FieldChecks_JavaBeanStub02(); Object testValue1 = new Object(); Object testValue2 = new Object(); bean.field1 = testValue1; bean.field2 = testValue2; ValidatorAction va = new ValidatorAction(); Field field = new Field(); field.setProperty("field1"); Var var1 = new Var("multiFieldValidator", "jp.terasoluna.fw.validation." + "FieldChecks_MultiFieldValidatorImpl01", null); Var var2 = new Var("fields", "field2", null); field.addVar(var1); field.addVar(var2); FieldChecks_ValidationErrorsImpl03 errors = new FieldChecks_ValidationErrorsImpl03(); FieldChecks_MultiFieldValidatorImpl01.result = true; // FieldChecks fieldChecks = new FieldChecks(); boolean result = fieldChecks.validateMultiField(bean, va, field, errors); // assertTrue(result); assertNull(errors.errorMessage); assertEquals(1, FieldChecks_MultiFieldValidatorImpl01.validateCalledCount); assertSame(testValue1, FieldChecks_MultiFieldValidatorImpl01.value); assertNotNull(FieldChecks_MultiFieldValidatorImpl01.fields); assertEquals(1, FieldChecks_MultiFieldValidatorImpl01.fields.length); assertSame(testValue2, FieldChecks_MultiFieldValidatorImpl01.fields[0]); } /** * testValidateMultiField09() <br> * <br> * () <br> * A, D, F, I <br> * <br> * () bean:???FieldChecksExtend_BeanStub01?<br> * <br> * field1=Object?1<br> * field2=Object?2<br> * field3=Object?3<br> * () va:ValidatorActionn?<br> * () field:???Field?<br> * <br> * property="invalidProperty"<br> * varmultiFieldValidator= "jp.terasoluna.fw.validation. FieldChecks_MultiFieldValidatorImpl01"<br> * varfields="field1 ,,invalidProperty,field2,field3"<br> * () errors:MockValidationErrors?<br> * () MultiFieldValidator#validate?:FALSE?<br> * <br> * () boolean:false<br> * () errors:errorMessage?"errorMessage"??????<br> * () MultiFieldValidator:validateCalledCount? 1??????<br> * <br> * value??null??????<br> * <br> * fields???3??????<br> * <br> * fields??1???????<br> * fields[0]=Object?1???<br> * fields[1]=Object?2???<br> * fields[2]=Object?3???<br> * () :ERROR<br> * "Unknown property 'invalidProperty'"<br> * ??NoSuchMethodException<br> * <br> * ERROR<br> * "Unknown property 'invalidProperty'"<br> * ??NoSuchMethodException<br> * <br> * field?var-namefields??var-value???? ?????????????? * ?bean?????3?????MultiFieldValidator?? ?????<br> * <br> * MultiFieldValidator#validate???false??? ??false?????????<br> * <br> * ??bean????? PropertyUtils#getProperty??????NoSuchMethodException?? * ???????????<br> * <br> * ???bean????? PropertyUtils#getProperty??????NoSuchMethodException?? * ??????????? <br> * @throws Exception ????? */ @Test public void testValidateMultiField09() throws Exception { // ?? FieldChecks_JavaBeanStub02 bean = new FieldChecks_JavaBeanStub02(); Object testValue1 = new Object(); Object testValue2 = new Object(); Object testValue3 = new Object(); bean.field1 = testValue1; bean.field2 = testValue2; bean.field3 = testValue3; ValidatorAction va = new ValidatorAction(); Field field = new Field(); field.setProperty("invalidProperty"); Var var1 = new Var("multiFieldValidator", "jp.terasoluna.fw.validation." + "FieldChecks_MultiFieldValidatorImpl01", null); Var var2 = new Var("fields", "field1 ,,invalidProperty,field2,field3", null); field.addVar(var1); field.addVar(var2); FieldChecks_ValidationErrorsImpl03 errors = new FieldChecks_ValidationErrorsImpl03(); FieldChecks_MultiFieldValidatorImpl01.result = false; // FieldChecks fieldChecks = new FieldChecks(); boolean result = fieldChecks.validateMultiField(bean, va, field, errors); // assertFalse(result); assertEquals("errorMessage", errors.errorMessage); assertEquals(1, FieldChecks_MultiFieldValidatorImpl01.validateCalledCount); assertNull(FieldChecks_MultiFieldValidatorImpl01.value); assertNotNull(FieldChecks_MultiFieldValidatorImpl01.fields); assertEquals(3, FieldChecks_MultiFieldValidatorImpl01.fields.length); assertSame(testValue1, FieldChecks_MultiFieldValidatorImpl01.fields[0]); assertSame(testValue2, FieldChecks_MultiFieldValidatorImpl01.fields[1]); assertSame(testValue3, FieldChecks_MultiFieldValidatorImpl01.fields[2]); assertThat(logger.getLoggingEvents().get(0).getMessage(), is(equalTo( "Unknown property 'invalidProperty' on class 'class jp.terasoluna.fw.validation.FieldChecks_JavaBeanStub02'"))); assertThat(logger.getLoggingEvents().get(0).getThrowable().get(), instanceOf(NoSuchMethodException.class)); assertThat(logger.getLoggingEvents().get(0).getMessage(), is(equalTo( "Unknown property 'invalidProperty' on class 'class jp.terasoluna.fw.validation.FieldChecks_JavaBeanStub02'"))); assertThat(logger.getLoggingEvents().get(0).getThrowable().get(), instanceOf(NoSuchMethodException.class)); } /** * testValidateMultiField10() <br> * <br> * () <br> * A, F, I <br> * <br> * () bean:??FieldChecksExtend_BeanStub03?<br> * <br> * field1?getter?RuntimeException?? InvocationTargetException?<br> * field2?getter?RuntimeException?? InvocationTargetException?<br> * () va:ValidatorActionn?<br> * () field:???Field?<br> * <br> * property="field1"<br> * varmultiFieldValidator= "jp.terasoluna.fw.validation. FieldChecks_MultiFieldValidatorImpl01"<br> * varfields="field2"<br> * () errors:MockValidationErrors?<br> * () MultiFieldValidator#validate?:TRUE?<br> * <br> * () boolean:true<br> * () errors:errorMessage?null??????<br> * () MultiFieldValidator:validateCalledCount? 1??????<br> * <br> * value?null??????<br> * <br> * fields???0??????<br> * () :ERROR<br> * ??InvocationTargetException<br> * <br> * ERROR<br> * ??InvocationTargetException<br> * <br> * ??bean????? PropertyUtils#getProperty??????InvocationTargetException?? * ?????????<br> * <br> * ???bean????? PropertyUtils#getProperty??????InvocationTargetException?? * ??????????? <br> * @throws Exception ????? */ @Test public void testValidateMultiField10() throws Exception { // ?? FieldChecks_JavaBeanStub03 bean = new FieldChecks_JavaBeanStub03(); ValidatorAction va = new ValidatorAction(); Field field = new Field(); field.setProperty("field1"); Var var1 = new Var("multiFieldValidator", "jp.terasoluna.fw.validation." + "FieldChecks_MultiFieldValidatorImpl01", null); Var var2 = new Var("fields", "field2", null); field.addVar(var1); field.addVar(var2); FieldChecks_ValidationErrorsImpl03 errors = new FieldChecks_ValidationErrorsImpl03(); FieldChecks_MultiFieldValidatorImpl01.result = true; // FieldChecks fieldChecks = new FieldChecks(); boolean result = fieldChecks.validateMultiField(bean, va, field, errors); // assertTrue(result); assertNull(errors.errorMessage); assertEquals(1, FieldChecks_MultiFieldValidatorImpl01.validateCalledCount); assertNull(FieldChecks_MultiFieldValidatorImpl01.value); assertNotNull(FieldChecks_MultiFieldValidatorImpl01.fields); assertEquals(0, FieldChecks_MultiFieldValidatorImpl01.fields.length); assertThat(logger.getLoggingEvents().get(0).getMessage(), is(equalTo("null"))); // assertTrue(LogUTUtil.checkError(null, // new InvocationTargetException(new RuntimeException()))); // assertTrue(LogUTUtil.checkError(null, // new InvocationTargetException(new RuntimeException()))); assertThat(logger.getLoggingEvents().get(0).getThrowable().get(), instanceOf(InvocationTargetException.class)); assertThat(logger.getLoggingEvents().get(0).getThrowable().get().getCause(), instanceOf(InvocationTargetException.class)); } }