Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename PropertiesPreconditions #2324

Merged
merged 4 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.shardingsphere.elasticjob.error.handler.dingtalk;

import org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandlerPropertiesValidator;
import org.apache.shardingsphere.elasticjob.infra.validate.JobPropertiesValidateRule;
import org.apache.shardingsphere.elasticjob.infra.exception.PropertiesPreconditions;

import java.util.Properties;

Expand All @@ -29,9 +29,9 @@ public final class DingtalkJobErrorHandlerPropertiesValidator implements JobErro

@Override
public void validate(final Properties props) {
JobPropertiesValidateRule.validateIsRequired(props, DingtalkPropertiesConstants.WEBHOOK);
JobPropertiesValidateRule.validateIsPositiveInteger(props, DingtalkPropertiesConstants.CONNECT_TIMEOUT_MILLISECONDS);
JobPropertiesValidateRule.validateIsPositiveInteger(props, DingtalkPropertiesConstants.READ_TIMEOUT_MILLISECONDS);
PropertiesPreconditions.checkRequired(props, DingtalkPropertiesConstants.WEBHOOK);
PropertiesPreconditions.checkPositiveInteger(props, DingtalkPropertiesConstants.CONNECT_TIMEOUT_MILLISECONDS);
PropertiesPreconditions.checkPositiveInteger(props, DingtalkPropertiesConstants.READ_TIMEOUT_MILLISECONDS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void assertValidateWithPropsIsNull() {
void assertValidateWithWebhookIsNull() {
try {
TypedSPILoader.getService(JobErrorHandlerPropertiesValidator.class, "DINGTALK").validate(new Properties());
} catch (final NullPointerException ex) {
} catch (final IllegalArgumentException ex) {
assertThat(ex.getMessage(), is(String.format("The property `%s` is required.", DingtalkPropertiesConstants.WEBHOOK)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.shardingsphere.elasticjob.error.handler.email;

import org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandlerPropertiesValidator;
import org.apache.shardingsphere.elasticjob.infra.validate.JobPropertiesValidateRule;
import org.apache.shardingsphere.elasticjob.infra.exception.PropertiesPreconditions;

import java.util.Properties;

Expand All @@ -29,13 +29,13 @@ public final class EmailJobErrorHandlerPropertiesValidator implements JobErrorHa

@Override
public void validate(final Properties props) {
JobPropertiesValidateRule.validateIsRequired(props, EmailPropertiesConstants.HOST);
JobPropertiesValidateRule.validateIsRequired(props, EmailPropertiesConstants.PORT);
JobPropertiesValidateRule.validateIsPositiveInteger(props, EmailPropertiesConstants.PORT);
JobPropertiesValidateRule.validateIsRequired(props, EmailPropertiesConstants.USERNAME);
JobPropertiesValidateRule.validateIsRequired(props, EmailPropertiesConstants.PASSWORD);
JobPropertiesValidateRule.validateIsRequired(props, EmailPropertiesConstants.FROM);
JobPropertiesValidateRule.validateIsRequired(props, EmailPropertiesConstants.TO);
PropertiesPreconditions.checkRequired(props, EmailPropertiesConstants.HOST);
PropertiesPreconditions.checkRequired(props, EmailPropertiesConstants.PORT);
PropertiesPreconditions.checkPositiveInteger(props, EmailPropertiesConstants.PORT);
PropertiesPreconditions.checkRequired(props, EmailPropertiesConstants.USERNAME);
PropertiesPreconditions.checkRequired(props, EmailPropertiesConstants.PASSWORD);
PropertiesPreconditions.checkRequired(props, EmailPropertiesConstants.FROM);
PropertiesPreconditions.checkRequired(props, EmailPropertiesConstants.TO);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void assertValidateWithPropsIsNull() {
void assertValidateWithHostIsNull() {
try {
TypedSPILoader.getService(JobErrorHandlerPropertiesValidator.class, "EMAIL").validate(new Properties());
} catch (final NullPointerException ex) {
} catch (final IllegalArgumentException ex) {
assertThat(ex.getMessage(), is(String.format("The property `%s` is required.", EmailPropertiesConstants.HOST)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.shardingsphere.elasticjob.error.handler.wechat;

import org.apache.shardingsphere.elasticjob.error.handler.JobErrorHandlerPropertiesValidator;
import org.apache.shardingsphere.elasticjob.infra.validate.JobPropertiesValidateRule;
import org.apache.shardingsphere.elasticjob.infra.exception.PropertiesPreconditions;

import java.util.Properties;

Expand All @@ -29,9 +29,9 @@ public final class WechatJobErrorHandlerPropertiesValidator implements JobErrorH

@Override
public void validate(final Properties props) {
JobPropertiesValidateRule.validateIsRequired(props, WechatPropertiesConstants.WEBHOOK);
JobPropertiesValidateRule.validateIsPositiveInteger(props, WechatPropertiesConstants.CONNECT_TIMEOUT_MILLISECONDS);
JobPropertiesValidateRule.validateIsPositiveInteger(props, WechatPropertiesConstants.READ_TIMEOUT_MILLISECONDS);
PropertiesPreconditions.checkRequired(props, WechatPropertiesConstants.WEBHOOK);
PropertiesPreconditions.checkPositiveInteger(props, WechatPropertiesConstants.CONNECT_TIMEOUT_MILLISECONDS);
PropertiesPreconditions.checkPositiveInteger(props, WechatPropertiesConstants.READ_TIMEOUT_MILLISECONDS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void assertValidateWithPropsIsNull() {
void assertValidateWithWebhookIsNull() {
try {
TypedSPILoader.getService(JobErrorHandlerPropertiesValidator.class, "WECHAT").validate(new Properties());
} catch (final NullPointerException ex) {
} catch (final IllegalArgumentException ex) {
assertThat(ex.getMessage(), is(String.format("The property `%s` is required.", WechatPropertiesConstants.WEBHOOK)));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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 org.apache.shardingsphere.elasticjob.infra.exception;

import com.google.common.base.Preconditions;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Properties;

/**
* Properties preconditions.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class PropertiesPreconditions {

/**
* Check property value is required.
*
* @param props properties to be checked
* @param key property key to be checked
*/
public static void checkRequired(final Properties props, final String key) {
Preconditions.checkArgument(props.containsKey(key), "The property `%s` is required.", key);
}

/**
* Check property value is positive integer.
*
* @param props properties to be checked
* @param key property key to be checked
*/
public static void checkPositiveInteger(final Properties props, final String key) {
String propertyValue = props.getProperty(key);
if (null == propertyValue) {
return;
}
int integerValue;
try {
integerValue = Integer.parseInt(propertyValue);
} catch (final NumberFormatException ignored) {
throw new IllegalArgumentException(String.format("The property `%s` should be integer.", key));
}
Preconditions.checkArgument(integerValue > 0, "The property `%s` should be positive.", key);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.elasticjob.infra.validate;
package org.apache.shardingsphere.elasticjob.infra.exception;

import org.junit.jupiter.api.Test;

Expand All @@ -24,13 +24,13 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

class JobPropertiesValidateRuleTest {
class PropertiesPreconditionsTest {

@Test
void assertValidateIsRequiredWithValidateError() {
try {
JobPropertiesValidateRule.validateIsRequired(new Properties(), "key");
} catch (NullPointerException ex) {
PropertiesPreconditions.checkRequired(new Properties(), "key");
} catch (IllegalArgumentException ex) {
assertThat(ex.getMessage(), is(String.format("The property `%s` is required.", "key")));
}
}
Expand All @@ -39,27 +39,27 @@ void assertValidateIsRequiredWithValidateError() {
void assertValidateIsRequiredWithNormal() {
Properties props = new Properties();
props.setProperty("key", "value");
JobPropertiesValidateRule.validateIsRequired(props, "key");
PropertiesPreconditions.checkRequired(props, "key");
}

@Test
void assertValidateIsPositiveIntegerWithValueNoExist() {
JobPropertiesValidateRule.validateIsPositiveInteger(new Properties(), "key");
PropertiesPreconditions.checkPositiveInteger(new Properties(), "key");
}

@Test
void assertValidateIsPositiveIntegerWithNormal() {
Properties props = new Properties();
props.setProperty("key", "1");
JobPropertiesValidateRule.validateIsPositiveInteger(props, "key");
PropertiesPreconditions.checkPositiveInteger(props, "key");
}

@Test
void assertValidateIsPositiveIntegerWithWrongString() {
Properties props = new Properties();
props.setProperty("key", "wrong_value");
try {
JobPropertiesValidateRule.validateIsPositiveInteger(props, "key");
PropertiesPreconditions.checkPositiveInteger(props, "key");
} catch (IllegalArgumentException ex) {
assertThat(ex.getMessage(), is(String.format("The property `%s` should be integer.", "key")));
}
Expand All @@ -70,7 +70,7 @@ void assertValidateIsPositiveIntegerWithNegativeNumber() {
Properties props = new Properties();
props.setProperty("key", "-1");
try {
JobPropertiesValidateRule.validateIsPositiveInteger(props, "key");
PropertiesPreconditions.checkPositiveInteger(props, "key");
} catch (IllegalArgumentException ex) {
assertThat(ex.getMessage(), is(String.format("The property `%s` should be positive.", "key")));
}
Expand Down