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 org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import java.lang.reflect.Method; import org.apache.commons.validator.Field; import org.apache.commons.validator.ValidatorAction; import org.junit.Before; import org.junit.Test; /** * {@link jp.terasoluna.fw.validation.FieldChecks} * ? * * <p> * <h4>???</h4> * TERASOLUNA????? * <p> * * @see jp.terasoluna.fw.validation.FieldChecks */ public class FieldChecksTest01 { /** * */ private ValidatorAction va = null; /** * */ private Field field = null; /** * ??? */ @Before public void setUp() { va = new ValidatorAction(); field = new Field(); } /** * testGetParamClass01() * <br><br> * * () * <br> * F * <br><br> * () va:not null<br> * () va.methodParams:""<br> * * <br> * () Class[]:Class[]{}<br> * ?0<br> * * <br> * va?methodParams?????? * ?0?Class?????????? * <br> * * @throws Exception ????? */ @Test public void testGetParamClass01() throws Exception { // ?? va.setMethodParams(""); // @SuppressWarnings("rawtypes") Class[] result = new FieldChecks().getParamClass(va); // assertEquals(0, result.length); } /** * testGetParamClass02() * <br><br> * * () * <br> * F * <br><br> * () va:not null<br> * () va.methodParams: * "java.lang.String,java.lang.Integer,java.lang.Boolean"<br> * * <br> * () Class[]:{String.class,<br> * Integer.class,<br> * Boolean.class}<br> * * <br> * va?methodParams????? * ????????????? * <br> * * @throws Exception ????? */ @Test public void testGetParamClass02() throws Exception { // ?? va.setMethodParams("java.lang.String,java.lang.Integer,java.lang.Boolean"); // @SuppressWarnings("rawtypes") Class[] result = new FieldChecks().getParamClass(va); // assertEquals(3, result.length); assertSame(String.class, result[0]); assertSame(Integer.class, result[1]); assertSame(Boolean.class, result[2]); } /** * testGetParamClass03() * <br><br> * * () * <br> * F * <br><br> * () va:not null<br> * () va.methodParams:"java.lang.String,bbb"<br> * * <br> * () Class[]:null<br> * * <br> * va?methodParams?????????????? * null????????? * <br> * * @throws Exception ????? */ @Test public void testGetParamClass03() throws Exception { // ?? va.setMethodParams("java.lang.String,bbb"); // // assertNull(new FieldChecks().getParamClass(va)); } /** * testGetMethod01() * <br><br> * * () * <br> * C,F * <br><br> * () va:not null<br> * () va.getName:null<br> * * <br> * () Method:null<br> * * <br> * va???????null???null??????? * <br> * * @throws Exception ????? */ @Test public void testGetMethod01() throws Exception { // ?? va.setName(null); // // assertNull(new FieldChecks().getMethod(va, new Class[] {})); } /** * testGetMethod02() * <br><br> * * () * <br> * C,F * <br><br> * () va:not null<br> * () va.getName:<br> * * <br> * () Method:null<br> * * <br> * va??????????null??????? * <br> * * @throws Exception ????? */ @Test public void testGetMethod02() throws Exception { // ?? va.setName(""); // // assertNull(new FieldChecks().getMethod(va, new Class[] {})); } /** * testGetMethod03() * <br><br> * * () * <br> * F * <br><br> * () va:not null<br> * () paramClass:{Object.class,<br> * ValidatorAction.class,<br> * Field.class,<br> * ValidationErrors.class}<br> * () va.getName:"requiredArray"<br> * * <br> * () Method:new FieldChecks()#validateRequired<br> * * <br> * va??????5??? * ?validate?????? * ????????????? * <br> * * @throws Exception ????? */ @Test public void testGetMethod03() throws Exception { // ?? va.setName("requiredArray"); @SuppressWarnings("rawtypes") Class[] paramClass = { Object.class, ValidatorAction.class, Field.class, ValidationErrors.class }; // Method result = new FieldChecks().getMethod(va, paramClass); // assertEquals(FieldChecks.class, result.getDeclaringClass()); assertEquals("validateRequired", result.getName()); } /** * testGetMethod04() * <br><br> * * () * <br> * F * <br><br> * () va:not null<br> * () paramClass:{Object.class,<br> * ValidatorAction.class,<br> * Field.class}<br> * () va.getName:"requiredArray"<br> * * <br> * () Method:null<br> * * <br> * va??????5??? * ?validate?????? * ??paramClass?????? * null??????? * <br> * * @throws Exception ????? */ @Test public void testGetMethod04() throws Exception { // ?? va.setName("requiredArray"); @SuppressWarnings("rawtypes") Class[] paramClass = { Object.class, ValidatorAction.class, Field.class }; // // assertNull(new FieldChecks().getMethod(va, paramClass)); } /** * testGetMethod05() * <br><br> * * () * <br> * F * <br><br> * () va:not null<br> * () paramClass:{Object.class,<br> * ValidatorAction.class,<br> * Field.class,<br> * ValidationErrors.class}<br> * () va.getName:"requiredXXXXX"<br> * * <br> * () Method:new FieldChecks()#validateRequired<br> * * <br> * va??????5??? * ?validate?????? * ????????????? * <br> * * @throws Exception ????? */ @Test public void testGetMethod05() throws Exception { // ?? va.setName("requiredXXXXX"); @SuppressWarnings("rawtypes") Class[] paramClass = { Object.class, ValidatorAction.class, Field.class, ValidationErrors.class }; // Method result = new FieldChecks().getMethod(va, paramClass); // assertEquals(FieldChecks.class, result.getDeclaringClass()); assertEquals("validateRequired", result.getName()); } /** * testExtractValue01() * <br><br> * * () * <br> * C,F * <br><br> * () bean:null<br> * * <br> * () String:null<br> * * <br> * ?bean?null???null??????? * <br> * * @throws Exception ????? */ @Test public void testExtractValue01() throws Exception { // // assertNull(new FieldChecks().extractValue(null, field)); } /** * testExtractValue02() * <br><br> * * () * <br> * F * <br><br> * () bean:"test"<br> * * <br> * () String:"test"<br> * * <br> * ?bean?String???bean???????? * <br> * * @throws Exception ????? */ @Test public void testExtractValue02() throws Exception { // // assertEquals("test", new FieldChecks().extractValue("test", field)); } /** * testExtractValue03() * <br><br> * * () * <br> * F * <br><br> * () bean:JavaBean {<br> * field="testProperty"<br> * }<br> * () field:property="field"<br> * * <br> * () String:"testProperty"<br> * * <br> * bean??field?property????? * ???????????? * <br> * * @throws Exception ????? */ @Test public void testExtractValue03() throws Exception { // ?? FieldChecks_JavaBeanStub01 bean = new FieldChecks_JavaBeanStub01(); field.setProperty("field"); // // assertEquals("testProperty", new FieldChecks().extractValue(bean, field)); } /** * testExtractValue04() * <br><br> * * () * <br> * F * <br><br> * () bean:JavaBean {<br> * field="testProperty"<br> * }<br> * () field:property="field2"<br> * * <br> * () String:null<br> * * <br> * bean??field?property??????? * null????????? * <br> * * @throws Exception ????? */ @Test public void testExtractValue04() throws Exception { // ?? FieldChecks_JavaBeanStub01 bean = new FieldChecks_JavaBeanStub01(); field.setProperty("field2"); // // assertNull(new FieldChecks().extractValue(bean, field)); } /** * testExtractValue05() * <br><br> * * () * <br> * F * <br><br> * () bean:new Integer(12345)<br> * * <br> * () String:"12345"<br> * * <br> * ?bean?Number???bean???????? * <br> * * @throws Exception ????? */ @Test public void testExtractValue05() throws Exception { // // assertEquals("12345", new FieldChecks().extractValue(new Integer(12345), field)); } /** * testExtractValue06() * <br><br> * * () * <br> * F * <br><br> * () bean:new Boolean(true)<br> * * <br> * () String:"true"<br> * * <br> * ?bean?Boolean???bean???????? * <br> * * @throws Exception ????? */ @Test public void testExtractValue06() throws Exception { // // assertEquals("true", new FieldChecks().extractValue(new Boolean(true), field)); } /** * testExtractValue07() * <br><br> * * () * <br> * F * <br><br> * () bean:new Character('@')<br> * * <br> * () String:"@"<br> * * <br> * ?bean?Character???bean???????? * <br> * * @throws Exception ????? */ @Test public void testExtractValue07() throws Exception { // // assertEquals("@", new FieldChecks().extractValue(new Character('@'), field)); } /** * testRejectValue01() * <br><br> * * () * <br> * F * <br><br> * () errors:not null<br> * () field:not null<br> * () va:not null<br> * () bean:not null<br> * * <br> * () errors:bean,field,va??? * addErrors???<br> * * <br> * bean,field,va???addErrors??????? * <br> * * @throws Exception ????? */ @Test public void testRejectValue01() throws Exception { // ?? FieldChecks_JavaBeanStub01 bean = new FieldChecks_JavaBeanStub01(); FieldChecks_ValidationErrorsImpl01 errors = new FieldChecks_ValidationErrorsImpl01(); // new FieldChecks().rejectValue(errors, field, va, bean); // assertEquals(1, errors.addErrorCount); assertSame(field, errors.fieldList.get(0)); assertSame(va, errors.vaList.get(0)); assertSame(bean, errors.beanList.get(0)); } }