This is an automated email from the ASF dual-hosted git repository.
iluo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git
The following commit(s) were added to refs/heads/master by this push:
new 2f0107c [Dubbo-1687]Add unit tests for dubbo-filter-validation module
(#1736)
2f0107c is described below
commit 2f0107c0549b79cb3abebafad04a5d371d7f1467
Author: qinnnyul <[email protected]>
AuthorDate: Mon May 14 15:52:25 2018 +0800
[Dubbo-1687]Add unit tests for dubbo-filter-validation module (#1736)
* Add unit tests for dubbo-filter-validation module
* add more jaxb api libs for testing with jdk9, because it does't contain
them by default any more
---
dubbo-filter/dubbo-filter-validation/pom.xml | 30 +++++
.../validation/filter/ValidationFilterTest.java | 129 +++++++++++++++++++++
.../support/jvalidation/JValidationTest.java | 45 +++++++
.../support/jvalidation/JValidatorTest.java | 53 +++++++++
.../jvalidation/mock/JValidatorTestTarget.java | 33 ++++++
.../jvalidation/mock/ValidationParameter.java | 31 +++++
pom.xml | 3 +
7 files changed, 324 insertions(+)
diff --git a/dubbo-filter/dubbo-filter-validation/pom.xml
b/dubbo-filter/dubbo-filter-validation/pom.xml
index d553750..4fb550f 100644
--- a/dubbo-filter/dubbo-filter-validation/pom.xml
+++ b/dubbo-filter/dubbo-filter-validation/pom.xml
@@ -39,5 +39,35 @@
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-validator</artifactId>
+ <scope>test</scope>
+ <version>${hibernate_validator_version}</version>
+ </dependency>
+ <dependency>
+ <groupId>javax.el</groupId>
+ <artifactId>javax.el-api</artifactId>
+ <scope>test</scope>
+ <version>${el_api_version}</version>
+ </dependency>
+ <dependency>
+ <groupId>javax.xml.bind</groupId>
+ <artifactId>jaxb-api</artifactId>
+ <scope>test</scope>
+ <version>${jaxb_api_version}</version>
+ </dependency>
+ <dependency>
+ <groupId>com.sun.xml.bind</groupId>
+ <artifactId>jaxb-impl</artifactId>
+ <scope>test</scope>
+ <version>${jaxb_api_version}</version>
+ </dependency>
+ <dependency>
+ <groupId>com.sun.xml.bind</groupId>
+ <artifactId>jaxb-core</artifactId>
+ <scope>test</scope>
+ <version>${jaxb_api_version}</version>
+ </dependency>
</dependencies>
</project>
\ No newline at end of file
diff --git
a/dubbo-filter/dubbo-filter-validation/src/test/java/com/alibaba/dubbo/validation/filter/ValidationFilterTest.java
b/dubbo-filter/dubbo-filter-validation/src/test/java/com/alibaba/dubbo/validation/filter/ValidationFilterTest.java
new file mode 100644
index 0000000..c1a0ae1
--- /dev/null
+++
b/dubbo-filter/dubbo-filter-validation/src/test/java/com/alibaba/dubbo/validation/filter/ValidationFilterTest.java
@@ -0,0 +1,129 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.validation.filter;
+
+import com.alibaba.dubbo.common.URL;
+import com.alibaba.dubbo.rpc.*;
+import com.alibaba.dubbo.validation.Validation;
+import com.alibaba.dubbo.validation.Validator;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import static org.mockito.BDDMockito.given;
+import static org.mockito.Mockito.mock;
+
+public class ValidationFilterTest {
+ private Invoker<?> invoker = mock(Invoker.class);
+ private Validation validation = mock(Validation.class);
+ private Validator validator = mock(Validator.class);
+ private RpcInvocation invocation = mock(RpcInvocation.class);
+
+ private ValidationFilter validationFilter;
+
+ @Before
+ public void setUp() throws Exception {
+ this.validationFilter = new ValidationFilter();
+ }
+
+ @Test
+ public void testItWithNotExistClass() throws Exception {
+ URL url = URL.valueOf("test://test:11/test?default.validation=true");
+
+ given(validation.getValidator(url)).willThrow(new
IllegalStateException("Not found class test, cause: test"));
+ given(invoker.invoke(invocation)).willReturn(new RpcResult("success"));
+ given(invoker.getUrl()).willReturn(url);
+ given(invocation.getMethodName()).willReturn("echo1");
+ given(invocation.getParameterTypes()).willReturn(new
Class<?>[]{String.class});
+ given(invocation.getArguments()).willReturn(new Object[]{"arg1"});
+
+ validationFilter.setValidation(validation);
+ Result result = validationFilter.invoke(invoker, invocation);
+
+ assertThat(result.getException().getMessage(), is("Not found class
test, cause: test"));
+
+ }
+
+ @Test
+ public void testItWithExistClass() throws Exception {
+ URL url = URL.valueOf("test://test:11/test?default.validation=true");
+
+ given(validation.getValidator(url)).willReturn(validator);
+ given(invoker.invoke(invocation)).willReturn(new RpcResult("success"));
+ given(invoker.getUrl()).willReturn(url);
+ given(invocation.getMethodName()).willReturn("echo1");
+ given(invocation.getParameterTypes()).willReturn(new
Class<?>[]{String.class});
+ given(invocation.getArguments()).willReturn(new Object[]{"arg1"});
+
+ validationFilter.setValidation(validation);
+ Result result = validationFilter.invoke(invoker, invocation);
+
+ assertThat(String.valueOf(result.getValue()), is("success"));
+ }
+
+ @Test
+ public void testItWithoutUrlParameters() throws Exception {
+ URL url = URL.valueOf("test://test:11/test");
+
+ given(validation.getValidator(url)).willReturn(validator);
+ given(invoker.invoke(invocation)).willReturn(new RpcResult("success"));
+ given(invoker.getUrl()).willReturn(url);
+ given(invocation.getMethodName()).willReturn("echo1");
+ given(invocation.getParameterTypes()).willReturn(new
Class<?>[]{String.class});
+ given(invocation.getArguments()).willReturn(new Object[]{"arg1"});
+
+ validationFilter.setValidation(validation);
+ Result result = validationFilter.invoke(invoker, invocation);
+
+ assertThat(String.valueOf(result.getValue()), is("success"));
+ }
+
+ @Test
+ public void testItWhileMethodNameStartWithDollar() throws Exception {
+ URL url = URL.valueOf("test://test:11/test");
+
+ given(validation.getValidator(url)).willReturn(validator);
+ given(invoker.invoke(invocation)).willReturn(new RpcResult("success"));
+ given(invoker.getUrl()).willReturn(url);
+ given(invocation.getMethodName()).willReturn("$echo1");
+ given(invocation.getParameterTypes()).willReturn(new
Class<?>[]{String.class});
+ given(invocation.getArguments()).willReturn(new Object[]{"arg1"});
+
+ validationFilter.setValidation(validation);
+ Result result = validationFilter.invoke(invoker, invocation);
+
+ assertThat(String.valueOf(result.getValue()), is("success"));
+
+ }
+
+
+ @Test(expected = RpcException.class)
+ public void testItWhileThrowoutRpcException() throws Exception {
+ URL url = URL.valueOf("test://test:11/test?default.validation=true");
+
+ given(validation.getValidator(url)).willThrow(new RpcException("rpc
exception"));
+ given(invoker.invoke(invocation)).willReturn(new RpcResult("success"));
+ given(invoker.getUrl()).willReturn(url);
+ given(invocation.getMethodName()).willReturn("echo1");
+ given(invocation.getParameterTypes()).willReturn(new
Class<?>[]{String.class});
+ given(invocation.getArguments()).willReturn(new Object[]{"arg1"});
+
+ validationFilter.setValidation(validation);
+ validationFilter.invoke(invoker, invocation);
+ }
+}
\ No newline at end of file
diff --git
a/dubbo-filter/dubbo-filter-validation/src/test/java/com/alibaba/dubbo/validation/support/jvalidation/JValidationTest.java
b/dubbo-filter/dubbo-filter-validation/src/test/java/com/alibaba/dubbo/validation/support/jvalidation/JValidationTest.java
new file mode 100644
index 0000000..8170804
--- /dev/null
+++
b/dubbo-filter/dubbo-filter-validation/src/test/java/com/alibaba/dubbo/validation/support/jvalidation/JValidationTest.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.validation.support.jvalidation;
+
+import com.alibaba.dubbo.common.URL;
+import com.alibaba.dubbo.validation.Validation;
+import com.alibaba.dubbo.validation.Validator;
+import org.junit.Test;
+
+import javax.validation.ValidationException;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+public class JValidationTest {
+ @Test(expected = ValidationException.class)
+ public void testReturnTypeWithInvalidValidationProvider() throws Exception
{
+ Validation jValidation = new JValidation();
+ URL url =
URL.valueOf("test://test:11/com.alibaba.dubbo.validation.support.jvalidation.JValidation?"
+
+ "jvalidation=com.alibaba.dubbo.validation.Validation");
+ jValidation.getValidator(url);
+ }
+
+ @Test
+ public void testReturnTypeWithDefaultValidatorProvider() throws Exception {
+ Validation jValidation = new JValidation();
+ URL url =
URL.valueOf("test://test:11/com.alibaba.dubbo.validation.support.jvalidation.JValidation");
+ Validator validator = jValidation.getValidator(url);
+ assertThat(validator instanceof JValidator, is(true));
+ }
+}
\ No newline at end of file
diff --git
a/dubbo-filter/dubbo-filter-validation/src/test/java/com/alibaba/dubbo/validation/support/jvalidation/JValidatorTest.java
b/dubbo-filter/dubbo-filter-validation/src/test/java/com/alibaba/dubbo/validation/support/jvalidation/JValidatorTest.java
new file mode 100644
index 0000000..6669a19
--- /dev/null
+++
b/dubbo-filter/dubbo-filter-validation/src/test/java/com/alibaba/dubbo/validation/support/jvalidation/JValidatorTest.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.validation.support.jvalidation;
+
+import com.alibaba.dubbo.common.URL;
+import
com.alibaba.dubbo.validation.support.jvalidation.mock.ValidationParameter;
+import org.junit.Test;
+
+import javax.validation.ConstraintViolationException;
+
+public class JValidatorTest {
+ @Test(expected = NoSuchMethodException.class)
+ public void testItWithNonExistMethod() throws Exception {
+ URL url =
URL.valueOf("test://test:11/com.alibaba.dubbo.validation.support.jvalidation.mock.JValidatorTestTarget");
+ JValidator jValidator = new JValidator(url);
+ jValidator.validate("nonExistingMethod", new Class<?>[]{String.class},
new Object[]{"arg1"});
+ }
+
+ @Test
+ public void testItWithExistMethod() throws Exception {
+ URL url =
URL.valueOf("test://test:11/com.alibaba.dubbo.validation.support.jvalidation.mock.JValidatorTestTarget");
+ JValidator jValidator = new JValidator(url);
+ jValidator.validate("someMethod1", new Class<?>[]{String.class}, new
Object[]{"anything"});
+ }
+
+ @Test(expected = ConstraintViolationException.class)
+ public void testItWhenItViolatedConstraint() throws Exception {
+ URL url =
URL.valueOf("test://test:11/com.alibaba.dubbo.validation.support.jvalidation.mock.JValidatorTestTarget");
+ JValidator jValidator = new JValidator(url);
+ jValidator.validate("someMethod2", new
Class<?>[]{ValidationParameter.class}, new Object[]{new ValidationParameter()});
+ }
+
+ @Test
+ public void testItWhenItMeetsConstraint() throws Exception {
+ URL url =
URL.valueOf("test://test:11/com.alibaba.dubbo.validation.support.jvalidation.mock.JValidatorTestTarget");
+ JValidator jValidator = new JValidator(url);
+ jValidator.validate("someMethod2", new
Class<?>[]{ValidationParameter.class}, new Object[]{new
ValidationParameter("NotBeNull")});
+ }
+}
\ No newline at end of file
diff --git
a/dubbo-filter/dubbo-filter-validation/src/test/java/com/alibaba/dubbo/validation/support/jvalidation/mock/JValidatorTestTarget.java
b/dubbo-filter/dubbo-filter-validation/src/test/java/com/alibaba/dubbo/validation/support/jvalidation/mock/JValidatorTestTarget.java
new file mode 100644
index 0000000..5939396
--- /dev/null
+++
b/dubbo-filter/dubbo-filter-validation/src/test/java/com/alibaba/dubbo/validation/support/jvalidation/mock/JValidatorTestTarget.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.validation.support.jvalidation.mock;
+
+import com.alibaba.dubbo.validation.MethodValidated;
+
+import javax.validation.constraints.NotNull;
+
+public interface JValidatorTestTarget {
+ @MethodValidated
+ public void someMethod1(String anything);
+
+ @MethodValidated(Test2.class)
+ public void someMethod2(@NotNull ValidationParameter validationParameter);
+
+ @interface Test2 {
+ }
+
+}
diff --git
a/dubbo-filter/dubbo-filter-validation/src/test/java/com/alibaba/dubbo/validation/support/jvalidation/mock/ValidationParameter.java
b/dubbo-filter/dubbo-filter-validation/src/test/java/com/alibaba/dubbo/validation/support/jvalidation/mock/ValidationParameter.java
new file mode 100644
index 0000000..f4c0619
--- /dev/null
+++
b/dubbo-filter/dubbo-filter-validation/src/test/java/com/alibaba/dubbo/validation/support/jvalidation/mock/ValidationParameter.java
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.validation.support.jvalidation.mock;
+
+import javax.validation.constraints.NotNull;
+
+public class ValidationParameter {
+ @NotNull
+ private String parameter;
+
+ public ValidationParameter() {
+ }
+
+ public ValidationParameter(String parameter) {
+ this.parameter = parameter;
+ }
+}
diff --git a/pom.xml b/pom.xml
index 856eca6..0c94439 100644
--- a/pom.xml
+++ b/pom.xml
@@ -91,6 +91,9 @@
<junit_version>4.12</junit_version>
<hazelcast_version>3.9-EA</hazelcast_version>
<hamcrest_version>1.3</hamcrest_version>
+ <hibernate_validator_version>5.2.4.Final</hibernate_validator_version>
+ <el_api_version>2.2.4</el_api_version>
+ <jaxb_api_version>2.2.7</jaxb_api_version>
<cglib_version>2.2</cglib_version>
<mockito_version>2.18.3</mockito_version>
<!-- Build args -->
--
To stop receiving notification emails like this one, please contact
[email protected].