You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by te...@apache.org on 2020/09/30 08:18:55 UTC
[shardingsphere-elasticjob] branch master updated: Use SPI to
initialize JobErrorHandler in test cases #1515
This is an automated email from the ASF dual-hosted git repository.
technoboy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere-elasticjob.git
The following commit(s) were added to refs/heads/master by this push:
new 874e1c9 Use SPI to initialize JobErrorHandler in test cases #1515
874e1c9 is described below
commit 874e1c97f3821ba210701300c75be5ab6a4d9724
Author: jiang2015 <27...@qq.com>
AuthorDate: Wed Sep 30 16:18:40 2020 +0800
Use SPI to initialize JobErrorHandler in test cases #1515
Use mock & verify for EmailJobErrorHandlerTest #1517
---
.../dingtalk/DingtalkJobErrorHandlerTest.java | 19 +++++++++++---
...sphere.elasticjob.error.handler.JobErrorHandler | 18 ++++++++++++++
.../handler/email/EmailJobErrorHandlerTest.java | 29 +++++++++++++++++++++-
...sphere.elasticjob.error.handler.JobErrorHandler | 18 ++++++++++++++
.../handler/general/IgnoreJobErrorHandlerTest.java | 3 ++-
.../handler/general/LogJobErrorHandlerTest.java | 3 ++-
.../handler/general/ThrowJobErrorHandlerTest.java | 3 ++-
...sphere.elasticjob.error.handler.JobErrorHandler | 20 +++++++++++++++
.../handler/wechat/WechatJobErrorHandlerTest.java | 19 +++++++++++---
...sphere.elasticjob.error.handler.JobErrorHandler | 18 ++++++++++++++
10 files changed, 138 insertions(+), 12 deletions(-)
diff --git a/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-dingtalk/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/dingtalk/DingtalkJobErrorHandlerTest.java b/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-dingtalk/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/dingtalk/DingtalkJobErrorHandlerTest.java
index 03fea26..9e69711 100644
--- a/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-dingtalk/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/dingtalk/DingtalkJobErrorHandlerTest.java
+++ b/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-dingtalk/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/dingtalk/DingtalkJobErrorHandlerTest.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.elasticjob.error.handler.dingtalk;
import lombok.SneakyThrows;
+import org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandler;
import org.apache.shardingsphere.elasticjob.error.handler.dingtalk.fixture.DingtalkInternalController;
import org.apache.shardingsphere.elasticjob.restful.NettyRestfulService;
import org.apache.shardingsphere.elasticjob.restful.NettyRestfulServiceConfiguration;
@@ -32,6 +33,7 @@ import org.slf4j.Logger;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
+import java.util.ServiceLoader;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -60,7 +62,7 @@ public final class DingtalkJobErrorHandlerTest {
@Test
public void assertHandleExceptionWithNotifySuccessful() {
- DingtalkJobErrorHandler actual = new DingtalkJobErrorHandler();
+ DingtalkJobErrorHandler actual = getDingtalkJobErrorHandler();
setStaticFieldValue(actual);
Throwable cause = new RuntimeException("test");
actual.handleException("test_job", cause);
@@ -69,7 +71,7 @@ public final class DingtalkJobErrorHandlerTest {
@Test
public void assertHandleExceptionWithWrongToken() {
- DingtalkJobErrorHandler actual = new DingtalkJobErrorHandler();
+ DingtalkJobErrorHandler actual = getDingtalkJobErrorHandler();
actual.setDingtalkConfiguration(new DingtalkConfiguration("http://localhost:9875/send?access_token=wrongToken",
null, null, 3000, 500));
setStaticFieldValue(actual);
@@ -80,7 +82,7 @@ public final class DingtalkJobErrorHandlerTest {
@Test
public void assertHandleExceptionWithWrongUrl() {
- DingtalkJobErrorHandler actual = new DingtalkJobErrorHandler();
+ DingtalkJobErrorHandler actual = getDingtalkJobErrorHandler();
actual.setDingtalkConfiguration(new DingtalkConfiguration("http://localhost:9875/404?access_token=wrongToken",
null, null, 3000, 500));
setStaticFieldValue(actual);
@@ -91,7 +93,7 @@ public final class DingtalkJobErrorHandlerTest {
@Test
public void assertGetType() {
- DingtalkJobErrorHandler actual = new DingtalkJobErrorHandler();
+ DingtalkJobErrorHandler actual = getDingtalkJobErrorHandler();
assertThat(actual.getType(), is("DINGTALK"));
}
@@ -111,4 +113,13 @@ public final class DingtalkJobErrorHandlerTest {
restfulService.shutdown();
}
}
+
+ private DingtalkJobErrorHandler getDingtalkJobErrorHandler() {
+ for (JobErrorHandler each : ServiceLoader.load(JobErrorHandler.class)) {
+ if (null != each && each instanceof DingtalkJobErrorHandler) {
+ return (DingtalkJobErrorHandler) each;
+ }
+ }
+ return new DingtalkJobErrorHandler();
+ }
}
diff --git a/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-dingtalk/src/test/resources/META-INF.services/org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandler b/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-dingtalk/src/test/resources/META-INF.services/org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandler
new file mode 100644
index 0000000..cd9894e
--- /dev/null
+++ b/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-dingtalk/src/test/resources/META-INF.services/org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandler
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.elasticjob.error.handler.dingtalk.DingtalkJobErrorHandler
diff --git a/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-email/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/email/EmailJobErrorHandlerTest.java b/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-email/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/email/EmailJobErrorHandlerTest.java
index 100a12c..220f4ca 100644
--- a/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-email/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/email/EmailJobErrorHandlerTest.java
+++ b/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-email/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/email/EmailJobErrorHandlerTest.java
@@ -17,11 +17,14 @@
package org.apache.shardingsphere.elasticjob.error.handler.email;
+import org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandler;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
+import javax.mail.Session;
import java.lang.reflect.Field;
+import java.util.ServiceLoader;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertNotNull;
@@ -33,7 +36,7 @@ public final class EmailJobErrorHandlerTest {
@Test
public void assertHandleExceptionWithYAMLConfiguration() throws ReflectiveOperationException {
- EmailJobErrorHandler emailJobErrorHandler = new EmailJobErrorHandler();
+ EmailJobErrorHandler emailJobErrorHandler = getEmailJobErrorHandler();
emailJobErrorHandler.handleException("test job name", new RuntimeException("test exception"));
Field field = emailJobErrorHandler.getClass().getDeclaredField("config");
field.setAccessible(true);
@@ -50,4 +53,28 @@ public final class EmailJobErrorHandlerTest {
assertTrue(config.isUseSsl());
assertTrue(config.isDebug());
}
+
+ @Test
+ public void assertHandleExceptionWithSession() throws ReflectiveOperationException {
+ EmailJobErrorHandler emailJobErrorHandler = getEmailJobErrorHandler();
+ emailJobErrorHandler.handleException("test job name", new RuntimeException("test exception"));
+ Field field = emailJobErrorHandler.getClass().getDeclaredField("session");
+ field.setAccessible(true);
+ Session session = (Session) field.get(emailJobErrorHandler);
+ assertNotNull(session);
+ assertThat(session.getProperties().get("mail.smtp.host"), equalTo("yaml.email.com"));
+ assertThat(session.getProperties().get("mail.debug"), equalTo("true"));
+ assertThat(session.getProperties().get("mail.smtp.port"), equalTo(123));
+ assertThat(session.getProperties().get("mail.transport.protocol"), equalTo("yaml.smtp"));
+ assertThat(session.getProperties().get("mail.smtp.auth"), equalTo("true"));
+ }
+
+ private EmailJobErrorHandler getEmailJobErrorHandler() {
+ for (JobErrorHandler each : ServiceLoader.load(JobErrorHandler.class)) {
+ if (null != each && each instanceof EmailJobErrorHandler) {
+ return (EmailJobErrorHandler) each;
+ }
+ }
+ return new EmailJobErrorHandler();
+ }
}
diff --git a/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-email/src/test/resources/META-INF.services/org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandler b/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-email/src/test/resources/META-INF.services/org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandler
new file mode 100644
index 0000000..3096707
--- /dev/null
+++ b/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-email/src/test/resources/META-INF.services/org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandler
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.elasticjob.error.handler.email.EmailJobErrorHandler
diff --git a/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-general/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/general/IgnoreJobErrorHandlerTest.java b/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-general/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/general/IgnoreJobErrorHandlerTest.java
index e8f84d9..baf4e47 100644
--- a/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-general/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/general/IgnoreJobErrorHandlerTest.java
+++ b/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-general/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/general/IgnoreJobErrorHandlerTest.java
@@ -17,12 +17,13 @@
package org.apache.shardingsphere.elasticjob.error.handler.general;
+import org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandlerFactory;
import org.junit.Test;
public final class IgnoreJobErrorHandlerTest {
@Test
public void assertHandleException() {
- new IgnoreJobErrorHandler().handleException("test_job", new RuntimeException("test"));
+ JobErrorHandlerFactory.getHandler("IGNORE").handleException("test_job", new RuntimeException("test"));
}
}
diff --git a/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-general/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/general/LogJobErrorHandlerTest.java b/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-general/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/general/LogJobErrorHandlerTest.java
index 8575a49..4b6a134 100644
--- a/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-general/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/general/LogJobErrorHandlerTest.java
+++ b/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-general/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/general/LogJobErrorHandlerTest.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.elasticjob.error.handler.general;
import lombok.SneakyThrows;
+import org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandlerFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -37,7 +38,7 @@ public final class LogJobErrorHandlerTest {
@Test
public void assertHandleException() {
- LogJobErrorHandler actual = new LogJobErrorHandler();
+ LogJobErrorHandler actual = (LogJobErrorHandler) JobErrorHandlerFactory.getHandler("LOG");
setStaticFieldValue(actual);
Throwable cause = new RuntimeException("test");
actual.handleException("test_job", cause);
diff --git a/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-general/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/general/ThrowJobErrorHandlerTest.java b/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-general/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/general/ThrowJobErrorHandlerTest.java
index 51a5d23..a4c13f8 100644
--- a/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-general/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/general/ThrowJobErrorHandlerTest.java
+++ b/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-general/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/general/ThrowJobErrorHandlerTest.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.elasticjob.error.handler.general;
+import org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandlerFactory;
import org.apache.shardingsphere.elasticjob.infra.exception.JobSystemException;
import org.junit.Test;
@@ -24,6 +25,6 @@ public final class ThrowJobErrorHandlerTest {
@Test(expected = JobSystemException.class)
public void assertHandleException() {
- new ThrowJobErrorHandler().handleException("test_job", new RuntimeException("test"));
+ JobErrorHandlerFactory.getHandler("THROW").handleException("test_job", new RuntimeException("test"));
}
}
diff --git a/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-general/src/test/resources/META-INF/services/org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandler b/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-general/src/test/resources/META-INF/services/org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandler
new file mode 100644
index 0000000..02da0be
--- /dev/null
+++ b/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-general/src/test/resources/META-INF/services/org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandler
@@ -0,0 +1,20 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.elasticjob.error.handler.general.LogJobErrorHandler
+org.apache.shardingsphere.elasticjob.error.handler.general.IgnoreJobErrorHandler
+org.apache.shardingsphere.elasticjob.error.handler.general.ThrowJobErrorHandler
diff --git a/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-wechat/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/wechat/WechatJobErrorHandlerTest.java b/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-wechat/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/wechat/WechatJobErrorHandlerTest.java
index b4bdc9c..2d6d6d7 100644
--- a/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-wechat/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/wechat/WechatJobErrorHandlerTest.java
+++ b/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-wechat/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/wechat/WechatJobErrorHandlerTest.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.elasticjob.error.handler.wechat;
import lombok.SneakyThrows;
+import org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandler;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -28,6 +29,7 @@ import org.slf4j.Logger;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
+import java.util.ServiceLoader;
import org.apache.shardingsphere.elasticjob.error.handler.wechat.fixture.WechatInternalController;
import org.apache.shardingsphere.elasticjob.restful.NettyRestfulService;
@@ -61,7 +63,7 @@ public final class WechatJobErrorHandlerTest {
@Test
public void assertHandleExceptionWithNotifySuccessful() {
- WechatJobErrorHandler actual = new WechatJobErrorHandler();
+ WechatJobErrorHandler actual = getWechatJobErrorHandler();
setStaticFieldValue(actual);
Throwable cause = new RuntimeException("test");
actual.handleException("test_job", cause);
@@ -70,7 +72,7 @@ public final class WechatJobErrorHandlerTest {
@Test
public void assertHandleExceptionWithWrongToken() {
- WechatJobErrorHandler actual = new WechatJobErrorHandler();
+ WechatJobErrorHandler actual = getWechatJobErrorHandler();
actual.setWechatConfiguration(new WechatConfiguration(getHost() + "/send?key=wrongToken", 3000, 500));
setStaticFieldValue(actual);
Throwable cause = new RuntimeException("test");
@@ -80,7 +82,7 @@ public final class WechatJobErrorHandlerTest {
@Test
public void assertHandleExceptionWithWrongUrl() {
- WechatJobErrorHandler actual = new WechatJobErrorHandler();
+ WechatJobErrorHandler actual = getWechatJobErrorHandler();
actual.setWechatConfiguration(new WechatConfiguration(getHost() + "/404?access_token=wrongToken", 3000, 500));
setStaticFieldValue(actual);
Throwable cause = new RuntimeException("test");
@@ -90,7 +92,7 @@ public final class WechatJobErrorHandlerTest {
@Test
public void assertGetType() {
- WechatJobErrorHandler actual = new WechatJobErrorHandler();
+ WechatJobErrorHandler actual = getWechatJobErrorHandler();
assertThat(actual.getType(), is("WECHAT"));
}
@@ -108,6 +110,15 @@ public final class WechatJobErrorHandlerTest {
return String.format("http://%s:%s", HOST, PORT);
}
+ private WechatJobErrorHandler getWechatJobErrorHandler() {
+ for (JobErrorHandler each : ServiceLoader.load(JobErrorHandler.class)) {
+ if (null != each && each instanceof WechatJobErrorHandler) {
+ return (WechatJobErrorHandler) each;
+ }
+ }
+ return new WechatJobErrorHandler();
+ }
+
@AfterClass
public static void close() {
if (null != restfulService) {
diff --git a/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-wechat/src/test/resources/META-INF.services/org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandler b/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-wechat/src/test/resources/META-INF.services/org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandler
new file mode 100644
index 0000000..b0a5d66
--- /dev/null
+++ b/elasticjob-error-handler/elasticjob-error-handler-impl/elasticjob-error-handler-wechat/src/test/resources/META-INF.services/org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandler
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.elasticjob.error.handler.wechat.WechatJobErrorHandler