Skip to content

Commit

Permalink
DUBBO-384 修改Validation测试
Browse files Browse the repository at this point in the history
git-svn-id: http://code.alibabatech.com/svn/dubbo/trunk@1837 1a56cb94-b969-4eaa-88fa-be21384802f2
  • Loading branch information
william.liangf committed May 22, 2012
1 parent af4fb4a commit 4a759d7
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ public static void main(String[] args) throws Exception {
}

// Delete OK
validationService.delete(2);
validationService.delete(2, "abc");
System.out.println("Validation Delete OK");

// Delete Error
try {
validationService.delete(0);
validationService.delete(0, "abc");
System.err.println("Validation Delete ERROR");
} catch (RpcException e) {
ConstraintViolationException ve = (ConstraintViolationException)e.getCause();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class ValidationParameter implements Serializable {
private static final long serialVersionUID = 7158911668568000392L;

@NotNull // 不允许为空
@Size(min = 1, max = 20) // 长度或大小范围
@Size(min = 2, max = 20) // 长度或大小范围
private String name;

@NotNull(groups = ValidationService.Save.class) // 保存时不允许为空,更新时允许为空 ,表示不更新该字段
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
package com.alibaba.dubbo.examples.validation.api;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;


/**
Expand All @@ -30,7 +33,7 @@ public interface ValidationService { // 缺省可按服务接口区分验证场

@interface Update{} // 与方法同名接口,首字母大写,用于区分验证场景,如:@NotNull(groups = ValidationService.Update.class),可选
void update(ValidationParameter parameter);
void delete(@Min(1) long id);

void delete(@Min(1) long id, @NotNull @Size(min = 2, max = 16) @Pattern(regexp = "^[a-zA-Z]+$") String operator);

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void save(ValidationParameter parameter) {
public void update(ValidationParameter parameter) {
}

public void delete(long id) {
public void delete(long id, String operator) {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright 1999-2012 Alibaba Group.
*
* 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 com.alibaba.dubbo.examples.validation;

import java.util.Date;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import junit.framework.Assert;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.dubbo.examples.validation.api.ValidationParameter;
import com.alibaba.dubbo.examples.validation.api.ValidationService;
import com.alibaba.dubbo.rpc.RpcException;

/**
* ValidationTest
*
* @author william.liangf
*/
public class ValidationTest {

@Test
public void testAnnotation() {
ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ValidationTest.class.getPackage().getName().replace('.', '/') + "/validation-provider.xml");
providerContext.start();
try {
ClassPathXmlApplicationContext consumerContext = new ClassPathXmlApplicationContext(ValidationTest.class.getPackage().getName().replace('.', '/') + "/validation-consumer.xml");
consumerContext.start();
try {
ValidationService validationService = (ValidationService) consumerContext.getBean("validationService");

// Save OK
ValidationParameter parameter = new ValidationParameter();
parameter.setName("liangfei");
parameter.setEmail("liangfei@liang.fei");
parameter.setAge(50);
parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
validationService.save(parameter);

try {
parameter = new ValidationParameter();
parameter.setName("l");
parameter.setEmail("liangfei@liang.fei");
parameter.setAge(50);
parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
validationService.save(parameter);
Assert.fail();
} catch (RpcException e) {
ConstraintViolationException ve = (ConstraintViolationException)e.getCause();
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
}

// Save Error
try {
parameter = new ValidationParameter();
validationService.save(parameter);
Assert.fail();
} catch (RpcException e) {
ConstraintViolationException ve = (ConstraintViolationException)e.getCause();
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
}

// Delete OK
validationService.delete(2, "abc");

// Delete Error
try {
validationService.delete(2, "a");
Assert.fail();
} catch (RpcException e) {
ConstraintViolationException ve = (ConstraintViolationException)e.getCause();
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
Assert.assertEquals(1, violations.size());
}

// Delete Error
try {
validationService.delete(0, "abc");
Assert.fail();
} catch (RpcException e) {
ConstraintViolationException ve = (ConstraintViolationException)e.getCause();
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
Assert.assertEquals(1, violations.size());
}
try {
validationService.delete(2, null);
Assert.fail();
} catch (RpcException e) {
ConstraintViolationException ve = (ConstraintViolationException)e.getCause();
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
Assert.assertEquals(1, violations.size());
}
try {
validationService.delete(0, null);
Assert.fail();
} catch (RpcException e) {
ConstraintViolationException ve = (ConstraintViolationException)e.getCause();
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
Assert.assertEquals(2, violations.size());
}
} finally {
consumerContext.stop();
consumerContext.close();
}
} finally {
providerContext.stop();
providerContext.close();
}
}

}

0 comments on commit 4a759d7

Please sign in to comment.