From 5f4b14fee2421486716425229cd69ab270408958 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Tue, 11 Jul 2023 21:53:10 +0900 Subject: [PATCH 01/14] Add new module --- .../converters/ByteUnitIntegerConverter.java | 41 ++++++++++++ .../converters/ByteUnitToLongConverter.java | 39 ++++++++++++ .../pulsar/cli/converters/ByteUnitUtil.java | 62 +++++++++++++++++++ .../converters/TimeUnitToMillisConverter.java | 43 +++++++++++++ .../TimeUnitToSecondsConverter.java | 43 +++++++++++++ .../pulsar/cli/converters/package-info.java | 19 ++++++ .../org/apache/pulsar/cli/package-info.java | 19 ++++++ .../IntegerMaxValueLongValidator.java | 30 +++++++++ .../validators/MinNegativeOneValidator.java | 30 +++++++++ .../validators/NonNegativeValueValidator.java | 30 +++++++++ .../PositiveIntegerValueValidator.java | 31 ++++++++++ .../PositiveLongValueValidator.java | 31 ++++++++++ .../pulsar/cli/validators/package-info.java | 19 ++++++ 13 files changed, 437 insertions(+) create mode 100644 pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitIntegerConverter.java create mode 100644 pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitToLongConverter.java create mode 100644 pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitUtil.java create mode 100644 pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToMillisConverter.java create mode 100644 pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToSecondsConverter.java create mode 100644 pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/package-info.java create mode 100644 pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/package-info.java create mode 100644 pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/IntegerMaxValueLongValidator.java create mode 100644 pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/MinNegativeOneValidator.java create mode 100644 pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/NonNegativeValueValidator.java create mode 100644 pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveIntegerValueValidator.java create mode 100644 pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveLongValueValidator.java create mode 100644 pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/package-info.java diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitIntegerConverter.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitIntegerConverter.java new file mode 100644 index 0000000000000..319d5f7e650c8 --- /dev/null +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitIntegerConverter.java @@ -0,0 +1,41 @@ +/* + * 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.pulsar.cli.converters; + +import static org.apache.pulsar.admin.cli.utils.ValueValidationUtils.emptyCheck; +import static org.apache.pulsar.admin.cli.utils.converters.ByteUnitUtil.validateSizeString; +import com.beust.jcommander.converters.BaseConverter; + +public class ByteUnitIntegerConverter extends BaseConverter { + + public ByteUnitIntegerConverter(String optionName) { + super(optionName); + } + + @Override + public Integer convert(String argStr) { + return parseBytes(argStr).intValue(); + } + + Long parseBytes(String argStr) { + emptyCheck(getOptionName(), argStr); + long valueInBytes = validateSizeString(argStr); + return valueInBytes; + } +} diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitToLongConverter.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitToLongConverter.java new file mode 100644 index 0000000000000..ffb03be60f55a --- /dev/null +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitToLongConverter.java @@ -0,0 +1,39 @@ +/* + * 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.pulsar.cli.converters; + +import static org.apache.pulsar.admin.cli.utils.ValueValidationUtils.emptyCheck; +import com.beust.jcommander.converters.BaseConverter; + +public class ByteUnitToLongConverter extends BaseConverter { + + public ByteUnitToLongConverter(String optionName) { + super(optionName); + } + + @Override + public Long convert(String argStr) { + return parseBytes(argStr); + } + + Long parseBytes(String argStr) { + emptyCheck(getOptionName(), argStr); + return ByteUnitUtil.validateSizeString(argStr); + } +} diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitUtil.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitUtil.java new file mode 100644 index 0000000000000..e2d9adcdfb23e --- /dev/null +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitUtil.java @@ -0,0 +1,62 @@ +/* + * 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.pulsar.cli.converters; + +import com.beust.jcommander.ParameterException; +import com.google.common.collect.Sets; +import java.util.Set; + +class ByteUnitUtil { + + private static Set sizeUnit = Sets.newHashSet('k', 'K', 'm', 'M', 'g', 'G', 't', 'T'); + + static long validateSizeString(String s) { + char last = s.charAt(s.length() - 1); + String subStr = s.substring(0, s.length() - 1); + long size; + try { + size = sizeUnit.contains(last) + ? Long.parseLong(subStr) + : Long.parseLong(s); + } catch (IllegalArgumentException e) { + throw new ParameterException(String.format("Invalid size '%s'. Valid formats are: %s", + s, "(4096, 100K, 10M, 16G, 2T)")); + } + switch (last) { + case 'k': + case 'K': + return size * 1024; + + case 'm': + case 'M': + return size * 1024 * 1024; + + case 'g': + case 'G': + return size * 1024 * 1024 * 1024; + + case 't': + case 'T': + return size * 1024 * 1024 * 1024 * 1024; + + default: + return size; + } + } +} diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToMillisConverter.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToMillisConverter.java new file mode 100644 index 0000000000000..8381b2f2a8c4a --- /dev/null +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToMillisConverter.java @@ -0,0 +1,43 @@ +/* + * 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.pulsar.cli.converters; + +import static org.apache.pulsar.admin.cli.utils.ValueValidationUtils.emptyCheck; +import com.beust.jcommander.ParameterException; +import com.beust.jcommander.converters.BaseConverter; +import java.util.concurrent.TimeUnit; +import org.apache.pulsar.common.util.RelativeTimeUtil; + +public class TimeUnitToMillisConverter extends BaseConverter { + + public TimeUnitToMillisConverter(String optionName) { + super(optionName); + } + + @Override + public Long convert(String str) { + emptyCheck(getOptionName(), str); + try { + return TimeUnit.SECONDS.toMillis( + RelativeTimeUtil.parseRelativeTimeInSeconds(str.trim())); + } catch (IllegalArgumentException exception) { + throw new ParameterException("For input " + getOptionName() + ": " + exception.getMessage()); + } + } +} diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToSecondsConverter.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToSecondsConverter.java new file mode 100644 index 0000000000000..1716cc71af753 --- /dev/null +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToSecondsConverter.java @@ -0,0 +1,43 @@ +/* + * 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.pulsar.cli.converters; + +import static org.apache.pulsar.admin.cli.utils.ValueValidationUtils.emptyCheck; +import com.beust.jcommander.ParameterException; +import com.beust.jcommander.converters.BaseConverter; +import java.util.concurrent.TimeUnit; +import org.apache.pulsar.common.util.RelativeTimeUtil; + +public class TimeUnitToSecondsConverter extends BaseConverter { + + public TimeUnitToSecondsConverter(String optionName) { + super(optionName); + } + + @Override + public Long convert(String str) { + emptyCheck(getOptionName(), str); + try { + return TimeUnit.SECONDS.toSeconds( + RelativeTimeUtil.parseRelativeTimeInSeconds(str.trim())); + } catch (IllegalArgumentException exception) { + throw new ParameterException("For input " + getOptionName() + ": " + exception.getMessage()); + } + } +} diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/package-info.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/package-info.java new file mode 100644 index 0000000000000..4204abdef3b31 --- /dev/null +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/package-info.java @@ -0,0 +1,19 @@ +/* + * 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.pulsar.cli.converters; diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/package-info.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/package-info.java new file mode 100644 index 0000000000000..2b2198c265c64 --- /dev/null +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/package-info.java @@ -0,0 +1,19 @@ +/* + * 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.pulsar.cli; diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/IntegerMaxValueLongValidator.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/IntegerMaxValueLongValidator.java new file mode 100644 index 0000000000000..38a1d4e13d2ed --- /dev/null +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/IntegerMaxValueLongValidator.java @@ -0,0 +1,30 @@ +/* + * 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.pulsar.cli.validators; + +import com.beust.jcommander.IValueValidator; +import com.beust.jcommander.ParameterException; +import org.apache.pulsar.admin.cli.utils.ValueValidationUtils; + +public class IntegerMaxValueLongValidator implements IValueValidator { + @Override + public void validate(String name, Long value) throws ParameterException { + ValueValidationUtils.maxValueCheck(name, value, Integer.MAX_VALUE); + } +} diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/MinNegativeOneValidator.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/MinNegativeOneValidator.java new file mode 100644 index 0000000000000..e642e890a75fe --- /dev/null +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/MinNegativeOneValidator.java @@ -0,0 +1,30 @@ +/* + * 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.pulsar.cli.validators; + +import com.beust.jcommander.IValueValidator; +import com.beust.jcommander.ParameterException; +import org.apache.pulsar.admin.cli.utils.ValueValidationUtils; + +public class MinNegativeOneValidator implements IValueValidator { + @Override + public void validate(String name, Long value) throws ParameterException { + ValueValidationUtils.minValueCheck(name, value, -1L); + } +} diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/NonNegativeValueValidator.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/NonNegativeValueValidator.java new file mode 100644 index 0000000000000..c7cb5fa64d05a --- /dev/null +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/NonNegativeValueValidator.java @@ -0,0 +1,30 @@ +/* + * 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.pulsar.cli.validators; + +import com.beust.jcommander.IValueValidator; +import com.beust.jcommander.ParameterException; +import org.apache.pulsar.admin.cli.utils.ValueValidationUtils; + +public class NonNegativeValueValidator implements IValueValidator { + @Override + public void validate(String name, Long value) throws ParameterException { + ValueValidationUtils.minValueCheck(name, value, 0L); + } +} diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveIntegerValueValidator.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveIntegerValueValidator.java new file mode 100644 index 0000000000000..834a8fcc849e1 --- /dev/null +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveIntegerValueValidator.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 org.apache.pulsar.cli.validators; + +import com.beust.jcommander.IValueValidator; +import com.beust.jcommander.ParameterException; +import org.apache.pulsar.admin.cli.utils.ValueValidationUtils; + +public class PositiveIntegerValueValidator implements IValueValidator { + + @Override + public void validate(String name, Integer value) throws ParameterException { + ValueValidationUtils.positiveCheck(name, value); + } +} diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveLongValueValidator.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveLongValueValidator.java new file mode 100644 index 0000000000000..031309ca1113e --- /dev/null +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveLongValueValidator.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 org.apache.pulsar.cli.validators; + +import com.beust.jcommander.IValueValidator; +import com.beust.jcommander.ParameterException; +import org.apache.pulsar.admin.cli.utils.ValueValidationUtils; + +public class PositiveLongValueValidator implements IValueValidator { + + @Override + public void validate(String name, Long value) throws ParameterException { + ValueValidationUtils.positiveCheck(name, value); + } +} diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/package-info.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/package-info.java new file mode 100644 index 0000000000000..4d132b984c244 --- /dev/null +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/package-info.java @@ -0,0 +1,19 @@ +/* + * 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.pulsar.cli.validators; From 4945b0e58712f6251c2dbffddaf9f1e4b123ef6d Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Tue, 11 Jul 2023 22:20:38 +0900 Subject: [PATCH 02/14] Fix maven --- pom.xml | 1 + pulsar-cli-utils/pom.xml | 178 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 179 insertions(+) create mode 100644 pulsar-cli-utils/pom.xml diff --git a/pom.xml b/pom.xml index 7e3538dc6a154..1f08a8c081f2f 100644 --- a/pom.xml +++ b/pom.xml @@ -2207,6 +2207,7 @@ flexible messaging model and an intuitive client API. pulsar-common pulsar-broker-common pulsar-broker + pulsar-cli-utils pulsar-client-api pulsar-client pulsar-client-shaded diff --git a/pulsar-cli-utils/pom.xml b/pulsar-cli-utils/pom.xml new file mode 100644 index 0000000000000..222526ab3ef7c --- /dev/null +++ b/pulsar-cli-utils/pom.xml @@ -0,0 +1,178 @@ + + + + 4.0.0 + + org.apache.pulsar + pulsar + 3.1.0-SNAPSHOT + .. + + + pulsar-cli-utils + Pulsar CLI Utils + Isolated CLI utility module + + + + com.beust + jcommander + compile + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${pulsar.client.compiler.release} + + + + + org.gaul + modernizer-maven-plugin + + true + 8 + + + + modernizer + verify + + modernizer + + + + + + + com.github.splunk.lightproto + lightproto-maven-plugin + ${lightproto-maven-plugin.version} + + + + generate + + + + + + + pl.project13.maven + git-commit-id-plugin + + + git-info + + revision + + + + + false + true + git + false + false + false + properties + + true + + + + + + org.codehaus.mojo + templating-maven-plugin + 1.0.0 + + + filtering-java-templates + + filter-sources + + + + + + com.github.spotbugs + spotbugs-maven-plugin + ${spotbugs-maven-plugin.version} + + + spotbugs + verify + + check + + + + + ${basedir}/src/main/resources/findbugsExclude.xml + + + + org.apache.maven.plugins + maven-checkstyle-plugin + + + checkstyle + verify + + check + + + + + + maven-resources-plugin + + + copy-resources + test-compile + + copy-resources + + + ${project.build.testOutputDirectory}/certificate-authority + true + + + ${project.parent.basedir}/tests/certificate-authority + false + + + + + + + + + From ceb3fc4af5df86482ca616b7cff20f9f605d8f61 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Tue, 11 Jul 2023 22:50:41 +0900 Subject: [PATCH 03/14] Fix compile error wrt validation utils --- .../pulsar/cli/ValueValidationUtils.java | 57 +++++++++++++++++++ .../converters/ByteUnitIntegerConverter.java | 4 +- .../converters/ByteUnitToLongConverter.java | 2 +- .../converters/TimeUnitToMillisConverter.java | 2 +- .../TimeUnitToSecondsConverter.java | 2 +- .../IntegerMaxValueLongValidator.java | 2 +- .../validators/MinNegativeOneValidator.java | 2 +- .../validators/NonNegativeValueValidator.java | 2 +- .../PositiveIntegerValueValidator.java | 2 +- .../PositiveLongValueValidator.java | 2 +- 10 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/ValueValidationUtils.java diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/ValueValidationUtils.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/ValueValidationUtils.java new file mode 100644 index 0000000000000..021c52169023e --- /dev/null +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/ValueValidationUtils.java @@ -0,0 +1,57 @@ +/* + * 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.pulsar.cli; + +import com.beust.jcommander.ParameterException; +import org.apache.commons.lang3.StringUtils; + +public class ValueValidationUtils { + private ValueValidationUtils() { + } + + public static void maxValueCheck(String paramName, long value, long maxValue) { + if (value > maxValue) { + throw new ParameterException(paramName + " cannot be bigger than <" + maxValue + ">!"); + } + } + + public static void positiveCheck(String paramName, long value) { + if (value <= 0) { + throw new ParameterException(paramName + " cannot be less than or equal to <0>!"); + } + } + + public static void positiveCheck(String paramName, int value) { + if (value <= 0) { + throw new ParameterException(paramName + " cannot be less than or equal to <0>!"); + } + } + + public static void emptyCheck(String paramName, String value) { + if (StringUtils.isEmpty(value)) { + throw new ParameterException("The value of " + paramName + " can't be empty"); + } + } + + public static void minValueCheck(String name, Long value, long min) { + if (value < min) { + throw new ParameterException(name + " cannot be less than <" + min + ">!"); + } + } +} diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitIntegerConverter.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitIntegerConverter.java index 319d5f7e650c8..f0089a185f3d2 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitIntegerConverter.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitIntegerConverter.java @@ -18,8 +18,8 @@ */ package org.apache.pulsar.cli.converters; -import static org.apache.pulsar.admin.cli.utils.ValueValidationUtils.emptyCheck; -import static org.apache.pulsar.admin.cli.utils.converters.ByteUnitUtil.validateSizeString; +import static org.apache.pulsar.cli.ValueValidationUtils.emptyCheck; +import static org.apache.pulsar.cli.converters.ByteUnitUtil.validateSizeString; import com.beust.jcommander.converters.BaseConverter; public class ByteUnitIntegerConverter extends BaseConverter { diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitToLongConverter.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitToLongConverter.java index ffb03be60f55a..4f2b06631c747 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitToLongConverter.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitToLongConverter.java @@ -18,7 +18,7 @@ */ package org.apache.pulsar.cli.converters; -import static org.apache.pulsar.admin.cli.utils.ValueValidationUtils.emptyCheck; +import static org.apache.pulsar.cli.ValueValidationUtils.emptyCheck; import com.beust.jcommander.converters.BaseConverter; public class ByteUnitToLongConverter extends BaseConverter { diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToMillisConverter.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToMillisConverter.java index 8381b2f2a8c4a..56e5dd136c074 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToMillisConverter.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToMillisConverter.java @@ -18,7 +18,7 @@ */ package org.apache.pulsar.cli.converters; -import static org.apache.pulsar.admin.cli.utils.ValueValidationUtils.emptyCheck; +import static org.apache.pulsar.cli.ValueValidationUtils.emptyCheck; import com.beust.jcommander.ParameterException; import com.beust.jcommander.converters.BaseConverter; import java.util.concurrent.TimeUnit; diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToSecondsConverter.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToSecondsConverter.java index 1716cc71af753..e62b3dd81549a 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToSecondsConverter.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToSecondsConverter.java @@ -18,7 +18,7 @@ */ package org.apache.pulsar.cli.converters; -import static org.apache.pulsar.admin.cli.utils.ValueValidationUtils.emptyCheck; +import static org.apache.pulsar.cli.ValueValidationUtils.emptyCheck; import com.beust.jcommander.ParameterException; import com.beust.jcommander.converters.BaseConverter; import java.util.concurrent.TimeUnit; diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/IntegerMaxValueLongValidator.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/IntegerMaxValueLongValidator.java index 38a1d4e13d2ed..9670c17a25dbd 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/IntegerMaxValueLongValidator.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/IntegerMaxValueLongValidator.java @@ -20,7 +20,7 @@ import com.beust.jcommander.IValueValidator; import com.beust.jcommander.ParameterException; -import org.apache.pulsar.admin.cli.utils.ValueValidationUtils; +import org.apache.pulsar.cli.ValueValidationUtils; public class IntegerMaxValueLongValidator implements IValueValidator { @Override diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/MinNegativeOneValidator.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/MinNegativeOneValidator.java index e642e890a75fe..03c51d1bd8cf5 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/MinNegativeOneValidator.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/MinNegativeOneValidator.java @@ -20,7 +20,7 @@ import com.beust.jcommander.IValueValidator; import com.beust.jcommander.ParameterException; -import org.apache.pulsar.admin.cli.utils.ValueValidationUtils; +import org.apache.pulsar.cli.ValueValidationUtils; public class MinNegativeOneValidator implements IValueValidator { @Override diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/NonNegativeValueValidator.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/NonNegativeValueValidator.java index c7cb5fa64d05a..7f52f5f892bb1 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/NonNegativeValueValidator.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/NonNegativeValueValidator.java @@ -20,7 +20,7 @@ import com.beust.jcommander.IValueValidator; import com.beust.jcommander.ParameterException; -import org.apache.pulsar.admin.cli.utils.ValueValidationUtils; +import org.apache.pulsar.cli.ValueValidationUtils; public class NonNegativeValueValidator implements IValueValidator { @Override diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveIntegerValueValidator.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveIntegerValueValidator.java index 834a8fcc849e1..218541a8762e6 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveIntegerValueValidator.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveIntegerValueValidator.java @@ -20,7 +20,7 @@ import com.beust.jcommander.IValueValidator; import com.beust.jcommander.ParameterException; -import org.apache.pulsar.admin.cli.utils.ValueValidationUtils; +import org.apache.pulsar.cli.ValueValidationUtils; public class PositiveIntegerValueValidator implements IValueValidator { diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveLongValueValidator.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveLongValueValidator.java index 031309ca1113e..1390a21a5e0a5 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveLongValueValidator.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveLongValueValidator.java @@ -20,7 +20,7 @@ import com.beust.jcommander.IValueValidator; import com.beust.jcommander.ParameterException; -import org.apache.pulsar.admin.cli.utils.ValueValidationUtils; +import org.apache.pulsar.cli.ValueValidationUtils; public class PositiveLongValueValidator implements IValueValidator { From 45b2120605121b21aa83a77ef9211c6f9bb6e617 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Tue, 11 Jul 2023 22:56:04 +0900 Subject: [PATCH 04/14] Add apache commons dep --- pulsar-cli-utils/pom.xml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pulsar-cli-utils/pom.xml b/pulsar-cli-utils/pom.xml index 222526ab3ef7c..e86e0b3b19bf1 100644 --- a/pulsar-cli-utils/pom.xml +++ b/pulsar-cli-utils/pom.xml @@ -35,11 +35,21 @@ Isolated CLI utility module + + ${project.groupId} + pulsar-common + ${project.version} + provided + com.beust jcommander compile + + org.apache.commons + commons-lang3 + From 963a7e3bf24e6a65deb9b74d84e6d9a46c76bed6 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Tue, 11 Jul 2023 22:56:19 +0900 Subject: [PATCH 05/14] Add test for ValueValidationUtilsTest --- .../pulsar/cli/ValueValidationUtilsTest.java | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/ValueValidationUtilsTest.java diff --git a/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/ValueValidationUtilsTest.java b/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/ValueValidationUtilsTest.java new file mode 100644 index 0000000000000..4ea081f692fc2 --- /dev/null +++ b/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/ValueValidationUtilsTest.java @@ -0,0 +1,102 @@ +package org.apache.pulsar.cli; + +import static org.testng.Assert.assertThrows; +import com.beust.jcommander.ParameterException; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +public class ValueValidationUtilsTest { + + @DataProvider(name = "maxValueCheckData") + public static Object[][] maxValueCheckData() { + return new Object[][]{ + {"param1", 11L, 10L}, // value > max + {"param2", 10L, 10L}, // value = max + {"param3", 9L, 10L} // value < max + }; + } + + @Test(dataProvider = "maxValueCheckData") + public void maxValueCheckTest(String param, long value, long max) { + if (value > max) { + assertThrows(ParameterException.class, () -> ValueValidationUtils.maxValueCheck(param, value, max)); + } else { + ValueValidationUtils.maxValueCheck(param, value, max); // should not throw exception + } + } + + @DataProvider(name = "positiveCheckData") + public static Object[][] positiveCheckData() { + return new Object[][]{ + {"param1", 0L}, // value = 0 + {"param2", -1L}, // value < 0 + {"param3", 1L}, // value > 0 + {"param4", 0}, // value = 0 + {"param5", -1}, // value < 0 + {"param6", 1} // value > 0 + }; + } + + @Test(dataProvider = "positiveCheckData") + public void positiveCheckTest(String param, long value) { + if (value <= 0) { + assertThrows(ParameterException.class, () -> ValueValidationUtils.positiveCheck(param, value)); + } else { + ValueValidationUtils.positiveCheck(param, value); // should not throw exception + } + } + + @DataProvider(name = "emptyCheckData") + public static Object[][] emptyCheckData() { + return new Object[][]{ + {"param1", ""}, // value is empty string + {"param2", null}, // value is null + {"param3", "nonEmpty"} // value is not empty + }; + } + + @Test(dataProvider = "emptyCheckData") + public void emptyCheckTest(String param, String value) { + if (value == null || value.isEmpty()) { + assertThrows(ParameterException.class, () -> ValueValidationUtils.emptyCheck(param, value)); + } else { + ValueValidationUtils.emptyCheck(param, value); // should not throw exception + } + } + + @DataProvider(name = "minValueCheckData") + public static Object[][] minValueCheckData() { + return new Object[][]{ + {"param1", 9L, 10L}, // value < min + {"param2", 10L, 10L}, // value = min + {"param3", 11L, 10L} // value > min + }; + } + + @Test(dataProvider = "minValueCheckData") + public void minValueCheckTest(String param, long value, long min) { + if (value < min) { + assertThrows(ParameterException.class, () -> ValueValidationUtils.minValueCheck(param, value, min)); + } else { + ValueValidationUtils.minValueCheck(param, value, min); // should not throw exception + } + } + + @DataProvider(name = "positiveCheckIntData") + public static Object[][] positiveCheckIntData() { + return new Object[][]{ + {"param1", 0}, // value = 0 + {"param2", -1}, // value < 0 + {"param3", 1} // value > 0 + }; + } + + @Test(dataProvider = "positiveCheckIntData") + public void positiveCheckIntTest(String param, int value) { + if (value <= 0) { + assertThrows(ParameterException.class, () -> ValueValidationUtils.positiveCheck(param, value)); + } else { + ValueValidationUtils.positiveCheck(param, value); // should not throw exception + } + } +} From cbdda802982282a9c791f445caf78c23e60efbc1 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Tue, 11 Jul 2023 23:08:25 +0900 Subject: [PATCH 06/14] Refactor test methods --- .../pulsar/cli/ValueValidationUtilsTest.java | 113 +++++------------- 1 file changed, 30 insertions(+), 83 deletions(-) diff --git a/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/ValueValidationUtilsTest.java b/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/ValueValidationUtilsTest.java index 4ea081f692fc2..4f44dd3246491 100644 --- a/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/ValueValidationUtilsTest.java +++ b/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/ValueValidationUtilsTest.java @@ -2,101 +2,48 @@ import static org.testng.Assert.assertThrows; import com.beust.jcommander.ParameterException; -import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class ValueValidationUtilsTest { - @DataProvider(name = "maxValueCheckData") - public static Object[][] maxValueCheckData() { - return new Object[][]{ - {"param1", 11L, 10L}, // value > max - {"param2", 10L, 10L}, // value = max - {"param3", 9L, 10L} // value < max - }; + @Test + public void testMaxValueCheck() { + assertThrows(ParameterException.class, () -> ValueValidationUtils.maxValueCheck("param1", 11L, 10L)); + ValueValidationUtils.maxValueCheck("param2", 10L, 10L); + ValueValidationUtils.maxValueCheck("param3", 9L, 10L); } - @Test(dataProvider = "maxValueCheckData") - public void maxValueCheckTest(String param, long value, long max) { - if (value > max) { - assertThrows(ParameterException.class, () -> ValueValidationUtils.maxValueCheck(param, value, max)); - } else { - ValueValidationUtils.maxValueCheck(param, value, max); // should not throw exception - } - } - - @DataProvider(name = "positiveCheckData") - public static Object[][] positiveCheckData() { - return new Object[][]{ - {"param1", 0L}, // value = 0 - {"param2", -1L}, // value < 0 - {"param3", 1L}, // value > 0 - {"param4", 0}, // value = 0 - {"param5", -1}, // value < 0 - {"param6", 1} // value > 0 - }; - } - - @Test(dataProvider = "positiveCheckData") - public void positiveCheckTest(String param, long value) { - if (value <= 0) { - assertThrows(ParameterException.class, () -> ValueValidationUtils.positiveCheck(param, value)); - } else { - ValueValidationUtils.positiveCheck(param, value); // should not throw exception - } - } - - @DataProvider(name = "emptyCheckData") - public static Object[][] emptyCheckData() { - return new Object[][]{ - {"param1", ""}, // value is empty string - {"param2", null}, // value is null - {"param3", "nonEmpty"} // value is not empty - }; - } - - @Test(dataProvider = "emptyCheckData") - public void emptyCheckTest(String param, String value) { - if (value == null || value.isEmpty()) { - assertThrows(ParameterException.class, () -> ValueValidationUtils.emptyCheck(param, value)); - } else { - ValueValidationUtils.emptyCheck(param, value); // should not throw exception - } - } + @Test + public void testPositiveCheck() { + // Long + assertThrows(ParameterException.class, () -> ValueValidationUtils.positiveCheck("param1", 0L)); + assertThrows(ParameterException.class, () -> ValueValidationUtils.positiveCheck("param2", -1L)); + ValueValidationUtils.positiveCheck("param3", 1L); - @DataProvider(name = "minValueCheckData") - public static Object[][] minValueCheckData() { - return new Object[][]{ - {"param1", 9L, 10L}, // value < min - {"param2", 10L, 10L}, // value = min - {"param3", 11L, 10L} // value > min - }; + // Integer + assertThrows(ParameterException.class, () -> ValueValidationUtils.positiveCheck("param4", 0)); + assertThrows(ParameterException.class, () -> ValueValidationUtils.positiveCheck("param5", -1)); + ValueValidationUtils.positiveCheck("param6", 1); } - @Test(dataProvider = "minValueCheckData") - public void minValueCheckTest(String param, long value, long min) { - if (value < min) { - assertThrows(ParameterException.class, () -> ValueValidationUtils.minValueCheck(param, value, min)); - } else { - ValueValidationUtils.minValueCheck(param, value, min); // should not throw exception - } + @Test + public void testEmptyCheck() { + assertThrows(ParameterException.class, () -> ValueValidationUtils.emptyCheck("param1", "")); + assertThrows(ParameterException.class, () -> ValueValidationUtils.emptyCheck("param2", null)); + ValueValidationUtils.emptyCheck("param3", "nonEmpty"); } - @DataProvider(name = "positiveCheckIntData") - public static Object[][] positiveCheckIntData() { - return new Object[][]{ - {"param1", 0}, // value = 0 - {"param2", -1}, // value < 0 - {"param3", 1} // value > 0 - }; + @Test + public void testMinValueCheck() { + assertThrows(ParameterException.class, () -> ValueValidationUtils.minValueCheck("param1", 9L, 10L)); + ValueValidationUtils.minValueCheck("param2", 10L, 10L); + ValueValidationUtils.minValueCheck("param3", 11L, 10L); } - @Test(dataProvider = "positiveCheckIntData") - public void positiveCheckIntTest(String param, int value) { - if (value <= 0) { - assertThrows(ParameterException.class, () -> ValueValidationUtils.positiveCheck(param, value)); - } else { - ValueValidationUtils.positiveCheck(param, value); // should not throw exception - } + @Test + public void testPositiveCheckInt() { + assertThrows(ParameterException.class, () -> ValueValidationUtils.positiveCheck("param1", 0)); + assertThrows(ParameterException.class, () -> ValueValidationUtils.positiveCheck("param2", -1)); + ValueValidationUtils.positiveCheck("param3", 1); } } From 346a361bc7d443f0774ea55aab5aedf6a01a8ccb Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Tue, 11 Jul 2023 23:33:04 +0900 Subject: [PATCH 07/14] Fix tests and compile errors --- pulsar-cli-utils/pom.xml | 6 -- .../pulsar/cli/ValueValidationUtils.java | 6 +- .../pulsar/cli/converters/ByteUnitUtil.java | 9 ++- .../cli/converters/RelativeTimeUtil.java | 79 +++++++++++++++++++ .../converters/TimeUnitToMillisConverter.java | 1 - .../TimeUnitToSecondsConverter.java | 1 - .../pulsar/cli/ValueValidationUtilsTest.java | 18 +++++ .../cli/converters/CliUtilConverterTest.java | 63 +++++++++++++++ .../cli/validators/CliUtilValidatorsTest.java | 65 +++++++++++++++ 9 files changed, 235 insertions(+), 13 deletions(-) create mode 100644 pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/RelativeTimeUtil.java create mode 100644 pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/converters/CliUtilConverterTest.java create mode 100644 pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/validators/CliUtilValidatorsTest.java diff --git a/pulsar-cli-utils/pom.xml b/pulsar-cli-utils/pom.xml index e86e0b3b19bf1..917b3a3177f42 100644 --- a/pulsar-cli-utils/pom.xml +++ b/pulsar-cli-utils/pom.xml @@ -35,12 +35,6 @@ Isolated CLI utility module - - ${project.groupId} - pulsar-common - ${project.version} - provided - com.beust jcommander diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/ValueValidationUtils.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/ValueValidationUtils.java index 021c52169023e..307cb0ada087a 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/ValueValidationUtils.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/ValueValidationUtils.java @@ -19,12 +19,12 @@ package org.apache.pulsar.cli; import com.beust.jcommander.ParameterException; +import lombok.experimental.UtilityClass; import org.apache.commons.lang3.StringUtils; +@UtilityClass public class ValueValidationUtils { - private ValueValidationUtils() { - } - + public static void maxValueCheck(String paramName, long value, long maxValue) { if (value > maxValue) { throw new ParameterException(paramName + " cannot be bigger than <" + maxValue + ">!"); diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitUtil.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitUtil.java index e2d9adcdfb23e..2d0cd56a3039e 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitUtil.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitUtil.java @@ -19,12 +19,17 @@ package org.apache.pulsar.cli.converters; import com.beust.jcommander.ParameterException; -import com.google.common.collect.Sets; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; import java.util.Set; +import lombok.experimental.UtilityClass; +@UtilityClass class ByteUnitUtil { - private static Set sizeUnit = Sets.newHashSet('k', 'K', 'm', 'M', 'g', 'G', 't', 'T'); + private static Set sizeUnit = Collections.unmodifiableSet( + new HashSet<>(Arrays.asList('k', 'K', 'm', 'M', 'g', 'G', 't', 'T'))); static long validateSizeString(String s) { char last = s.charAt(s.length() - 1); diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/RelativeTimeUtil.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/RelativeTimeUtil.java new file mode 100644 index 0000000000000..5418210caf3c9 --- /dev/null +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/RelativeTimeUtil.java @@ -0,0 +1,79 @@ +/* + * 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.pulsar.cli.converters; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.concurrent.TimeUnit; +import lombok.experimental.UtilityClass; + +/** + * Mirror of {@link org.apache.pulsar.common.util.RelativeTimeUtil}. + */ +@UtilityClass +public class RelativeTimeUtil { + public static long parseRelativeTimeInSeconds(String relativeTime) { + if (relativeTime.isEmpty()) { + throw new IllegalArgumentException("expiry time cannot be empty"); + } + + int lastIndex = relativeTime.length() - 1; + char lastChar = relativeTime.charAt(lastIndex); + final char timeUnit; + + if (!Character.isAlphabetic(lastChar)) { + // No unit specified, assume seconds + timeUnit = 's'; + lastIndex = relativeTime.length(); + } else { + timeUnit = Character.toLowerCase(lastChar); + } + + long duration = Long.parseLong(relativeTime.substring(0, lastIndex)); + + switch (timeUnit) { + case 's': + return duration; + case 'm': + return TimeUnit.MINUTES.toSeconds(duration); + case 'h': + return TimeUnit.HOURS.toSeconds(duration); + case 'd': + return TimeUnit.DAYS.toSeconds(duration); + case 'w': + return 7 * TimeUnit.DAYS.toSeconds(duration); + // No unit for months + case 'y': + return 365 * TimeUnit.DAYS.toSeconds(duration); + default: + throw new IllegalArgumentException("Invalid time unit '" + lastChar + "'"); + } + } + + /** + * Convert nanoseconds to seconds and keep three decimal places. + * @param ns + * @return seconds + */ + public static double nsToSeconds(long ns) { + double seconds = (double) ns / 1_000_000_000; + BigDecimal bd = new BigDecimal(seconds); + return bd.setScale(3, RoundingMode.HALF_UP).doubleValue(); + } +} diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToMillisConverter.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToMillisConverter.java index 56e5dd136c074..be8fe00707aba 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToMillisConverter.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToMillisConverter.java @@ -22,7 +22,6 @@ import com.beust.jcommander.ParameterException; import com.beust.jcommander.converters.BaseConverter; import java.util.concurrent.TimeUnit; -import org.apache.pulsar.common.util.RelativeTimeUtil; public class TimeUnitToMillisConverter extends BaseConverter { diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToSecondsConverter.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToSecondsConverter.java index e62b3dd81549a..41a34bb33cf0e 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToSecondsConverter.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToSecondsConverter.java @@ -22,7 +22,6 @@ import com.beust.jcommander.ParameterException; import com.beust.jcommander.converters.BaseConverter; import java.util.concurrent.TimeUnit; -import org.apache.pulsar.common.util.RelativeTimeUtil; public class TimeUnitToSecondsConverter extends BaseConverter { diff --git a/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/ValueValidationUtilsTest.java b/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/ValueValidationUtilsTest.java index 4f44dd3246491..65c1c1d2cd12d 100644 --- a/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/ValueValidationUtilsTest.java +++ b/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/ValueValidationUtilsTest.java @@ -1,3 +1,21 @@ +/* + * 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.pulsar.cli; import static org.testng.Assert.assertThrows; diff --git a/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/converters/CliUtilConverterTest.java b/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/converters/CliUtilConverterTest.java new file mode 100644 index 0000000000000..d46dcdcac5309 --- /dev/null +++ b/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/converters/CliUtilConverterTest.java @@ -0,0 +1,63 @@ +/* + * 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.pulsar.cli.converters; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertThrows; +import com.beust.jcommander.ParameterException; +import org.testng.annotations.Test; + +public class CliUtilConverterTest { + + @Test + public void testTimeUnitToSecondsConverter() { + TimeUnitToSecondsConverter converter = new TimeUnitToSecondsConverter("optionName"); + assertEquals(converter.convert("10s"), Long.valueOf(10)); + assertEquals(converter.convert("1m"), Long.valueOf(60)); + assertThrows(ParameterException.class, () -> converter.convert(null)); + assertThrows(ParameterException.class, () -> converter.convert("")); + } + + @Test + public void testTimeUnitToMillisConverter() { + TimeUnitToMillisConverter converter = new TimeUnitToMillisConverter("optionName"); + assertEquals(converter.convert("1s"), Long.valueOf(1000)); + assertEquals(converter.convert("1m"), Long.valueOf(60000)); + assertThrows(ParameterException.class, () -> converter.convert(null)); + assertThrows(ParameterException.class, () -> converter.convert("")); + } + + @Test + public void testByteUnitToLongConverter() { + ByteUnitToLongConverter converter = new ByteUnitToLongConverter("optionName"); + assertEquals(converter.convert("1"), Long.valueOf(1)); + assertEquals(converter.convert("1K"), Long.valueOf(1024)); + assertThrows(ParameterException.class, () -> converter.convert(null)); + assertThrows(ParameterException.class, () -> converter.convert("")); + } + + @Test + public void testByteUnitIntegerConverter() { + ByteUnitIntegerConverter converter = new ByteUnitIntegerConverter("optionName"); + assertEquals(converter.convert("1"), Integer.valueOf(1)); + assertEquals(converter.convert("1K"), Integer.valueOf(1024)); + assertThrows(ParameterException.class, () -> converter.convert(null)); + assertThrows(ParameterException.class, () -> converter.convert("")); + } +} diff --git a/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/validators/CliUtilValidatorsTest.java b/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/validators/CliUtilValidatorsTest.java new file mode 100644 index 0000000000000..da1f6ec66bd9c --- /dev/null +++ b/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/validators/CliUtilValidatorsTest.java @@ -0,0 +1,65 @@ +/* + * 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.pulsar.cli.validators; + +import static org.testng.Assert.assertThrows; +import com.beust.jcommander.ParameterException; +import org.testng.annotations.Test; + +public class CliUtilValidatorsTest { + + @Test + public void testPositiveLongValueValidator() { + PositiveLongValueValidator validator = new PositiveLongValueValidator(); + assertThrows(ParameterException.class, () -> validator.validate("param", -1L)); + assertThrows(ParameterException.class, () -> validator.validate("param", 0L)); + validator.validate("param", 1L); + } + + @Test + public void testPositiveIntegerValueValidator() { + PositiveIntegerValueValidator validator = new PositiveIntegerValueValidator(); + assertThrows(ParameterException.class, () -> validator.validate("param", -1)); + assertThrows(ParameterException.class, () -> validator.validate("param", 0)); + validator.validate("param", 1); + } + + @Test + public void testNonNegativeValueValidator() { + NonNegativeValueValidator validator = new NonNegativeValueValidator(); + assertThrows(ParameterException.class, () -> validator.validate("param", -1L)); + validator.validate("param", 0L); + validator.validate("param", 1L); + } + + @Test + public void testMinNegativeOneValidator() { + MinNegativeOneValidator validator = new MinNegativeOneValidator(); + assertThrows(ParameterException.class, () -> validator.validate("param", -2L)); + validator.validate("param", -1L); + validator.validate("param", 0L); + } + + @Test + public void testIntegerMaxValueLongValidator() { + IntegerMaxValueLongValidator validator = new IntegerMaxValueLongValidator(); + assertThrows(ParameterException.class, () -> validator.validate("param", Integer.MAX_VALUE + 1L)); + validator.validate("param", (long) Integer.MAX_VALUE); + } +} From 25a93ab9b6ad642ab86118c7a155f624efd0596a Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Tue, 11 Jul 2023 23:35:48 +0900 Subject: [PATCH 08/14] Fix checkstyle --- .../main/java/org/apache/pulsar/cli/ValueValidationUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/ValueValidationUtils.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/ValueValidationUtils.java index 307cb0ada087a..9f0f1c5e839ff 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/ValueValidationUtils.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/ValueValidationUtils.java @@ -24,7 +24,7 @@ @UtilityClass public class ValueValidationUtils { - + public static void maxValueCheck(String paramName, long value, long maxValue) { if (value > maxValue) { throw new ParameterException(paramName + " cannot be bigger than <" + maxValue + ">!"); From b4e297199ef45a9598619bb1141a653e1177498b Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Tue, 11 Jul 2023 23:59:29 +0900 Subject: [PATCH 09/14] Fix build failure wrt findBugsFIlter --- pulsar-cli-utils/pom.xml | 22 ------------------- .../src/main/resources/findbugsExclude.xml | 22 +++++++++++++++++++ 2 files changed, 22 insertions(+), 22 deletions(-) create mode 100644 pulsar-cli-utils/src/main/resources/findbugsExclude.xml diff --git a/pulsar-cli-utils/pom.xml b/pulsar-cli-utils/pom.xml index 917b3a3177f42..1b71d6020ff0a 100644 --- a/pulsar-cli-utils/pom.xml +++ b/pulsar-cli-utils/pom.xml @@ -155,28 +155,6 @@ - - maven-resources-plugin - - - copy-resources - test-compile - - copy-resources - - - ${project.build.testOutputDirectory}/certificate-authority - true - - - ${project.parent.basedir}/tests/certificate-authority - false - - - - - - diff --git a/pulsar-cli-utils/src/main/resources/findbugsExclude.xml b/pulsar-cli-utils/src/main/resources/findbugsExclude.xml new file mode 100644 index 0000000000000..ddde8120ba518 --- /dev/null +++ b/pulsar-cli-utils/src/main/resources/findbugsExclude.xml @@ -0,0 +1,22 @@ + + + \ No newline at end of file From 1634acaa764fb32f4e69043d27c533944419e09e Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Fri, 21 Jul 2023 14:50:35 +0900 Subject: [PATCH 10/14] Remove protobuf dep --- pulsar-cli-utils/pom.xml | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/pulsar-cli-utils/pom.xml b/pulsar-cli-utils/pom.xml index 1b71d6020ff0a..f16e17447727d 100644 --- a/pulsar-cli-utils/pom.xml +++ b/pulsar-cli-utils/pom.xml @@ -40,6 +40,7 @@ jcommander compile + org.apache.commons commons-lang3 @@ -73,20 +74,7 @@ - - - com.github.splunk.lightproto - lightproto-maven-plugin - ${lightproto-maven-plugin.version} - - - - generate - - - - - + pl.project13.maven git-commit-id-plugin @@ -125,6 +113,7 @@ + com.github.spotbugs spotbugs-maven-plugin From 293f358747673bd35391c9fb5239d004047e9ccc Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Fri, 21 Jul 2023 15:04:46 +0900 Subject: [PATCH 11/14] Rephrase errror message in RelativeTimeUtil --- .../java/org/apache/pulsar/cli/converters/RelativeTimeUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/RelativeTimeUtil.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/RelativeTimeUtil.java index 5418210caf3c9..c36b7199e8c54 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/RelativeTimeUtil.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/RelativeTimeUtil.java @@ -30,7 +30,7 @@ public class RelativeTimeUtil { public static long parseRelativeTimeInSeconds(String relativeTime) { if (relativeTime.isEmpty()) { - throw new IllegalArgumentException("expiry time cannot be empty"); + throw new IllegalArgumentException("time cannot be empty"); } int lastIndex = relativeTime.length() - 1; From f0f9f9ca2cb8c0943181bfa796e0b32cc3f6a9f7 Mon Sep 17 00:00:00 2001 From: "Kim, Joo Hyuk" Date: Fri, 4 Aug 2023 21:17:39 +0900 Subject: [PATCH 12/14] Remove whitespace Co-authored-by: ran --- .../java/org/apache/pulsar/cli/converters/RelativeTimeUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/RelativeTimeUtil.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/RelativeTimeUtil.java index c36b7199e8c54..0cdacf3c607c9 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/RelativeTimeUtil.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/RelativeTimeUtil.java @@ -33,7 +33,7 @@ public static long parseRelativeTimeInSeconds(String relativeTime) { throw new IllegalArgumentException("time cannot be empty"); } - int lastIndex = relativeTime.length() - 1; + int lastIndex = relativeTime.length() - 1; char lastChar = relativeTime.charAt(lastIndex); final char timeUnit; From a9eb0d55a3f53fe4ae1b4bebeac037b8302c8529 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Sat, 5 Aug 2023 09:00:50 +0900 Subject: [PATCH 13/14] Add more test covreatge and reformat --- ...ionUtils.java => ValueValidationUtil.java} | 2 +- .../converters/ByteUnitIntegerConverter.java | 2 +- .../converters/ByteUnitToLongConverter.java | 2 +- .../pulsar/cli/converters/ByteUnitUtil.java | 14 ++- .../converters/TimeUnitToMillisConverter.java | 2 +- .../TimeUnitToSecondsConverter.java | 2 +- .../IntegerMaxValueLongValidator.java | 4 +- .../validators/MinNegativeOneValidator.java | 4 +- .../validators/NonNegativeValueValidator.java | 4 +- .../PositiveIntegerValueValidator.java | 4 +- .../PositiveLongValueValidator.java | 4 +- ...Test.java => ValueValidationUtilTest.java} | 38 ++++---- .../cli/converters/ByteConversionTest.java | 95 +++++++++++++++++++ .../cli/converters/CliUtilConverterTest.java | 63 ------------ .../cli/converters/TimeConversionTest.java | 86 +++++++++++++++++ 15 files changed, 224 insertions(+), 102 deletions(-) rename pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/{ValueValidationUtils.java => ValueValidationUtil.java} (98%) rename pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/{ValueValidationUtilsTest.java => ValueValidationUtilTest.java} (67%) create mode 100644 pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/converters/ByteConversionTest.java delete mode 100644 pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/converters/CliUtilConverterTest.java create mode 100644 pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/converters/TimeConversionTest.java diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/ValueValidationUtils.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/ValueValidationUtil.java similarity index 98% rename from pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/ValueValidationUtils.java rename to pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/ValueValidationUtil.java index 9f0f1c5e839ff..c2000e1c7bc5a 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/ValueValidationUtils.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/ValueValidationUtil.java @@ -23,7 +23,7 @@ import org.apache.commons.lang3.StringUtils; @UtilityClass -public class ValueValidationUtils { +public class ValueValidationUtil { public static void maxValueCheck(String paramName, long value, long maxValue) { if (value > maxValue) { diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitIntegerConverter.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitIntegerConverter.java index f0089a185f3d2..b148d238b149d 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitIntegerConverter.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitIntegerConverter.java @@ -18,7 +18,7 @@ */ package org.apache.pulsar.cli.converters; -import static org.apache.pulsar.cli.ValueValidationUtils.emptyCheck; +import static org.apache.pulsar.cli.ValueValidationUtil.emptyCheck; import static org.apache.pulsar.cli.converters.ByteUnitUtil.validateSizeString; import com.beust.jcommander.converters.BaseConverter; diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitToLongConverter.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitToLongConverter.java index 4f2b06631c747..6170fb489d4de 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitToLongConverter.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitToLongConverter.java @@ -18,7 +18,7 @@ */ package org.apache.pulsar.cli.converters; -import static org.apache.pulsar.cli.ValueValidationUtils.emptyCheck; +import static org.apache.pulsar.cli.ValueValidationUtil.emptyCheck; import com.beust.jcommander.converters.BaseConverter; public class ByteUnitToLongConverter extends BaseConverter { diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitUtil.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitUtil.java index 2d0cd56a3039e..cc6140dfced46 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitUtil.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitUtil.java @@ -31,17 +31,21 @@ class ByteUnitUtil { private static Set sizeUnit = Collections.unmodifiableSet( new HashSet<>(Arrays.asList('k', 'K', 'm', 'M', 'g', 'G', 't', 'T'))); - static long validateSizeString(String s) { - char last = s.charAt(s.length() - 1); - String subStr = s.substring(0, s.length() - 1); + static long validateSizeString(String byteStr) { + if (byteStr.isEmpty()) { + throw new IllegalArgumentException("byte string cannot be empty"); + } + + char last = byteStr.charAt(byteStr.length() - 1); + String subStr = byteStr.substring(0, byteStr.length() - 1); long size; try { size = sizeUnit.contains(last) ? Long.parseLong(subStr) - : Long.parseLong(s); + : Long.parseLong(byteStr); } catch (IllegalArgumentException e) { throw new ParameterException(String.format("Invalid size '%s'. Valid formats are: %s", - s, "(4096, 100K, 10M, 16G, 2T)")); + byteStr, "(4096, 100K, 10M, 16G, 2T)")); } switch (last) { case 'k': diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToMillisConverter.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToMillisConverter.java index be8fe00707aba..38ff4f501a67a 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToMillisConverter.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToMillisConverter.java @@ -18,7 +18,7 @@ */ package org.apache.pulsar.cli.converters; -import static org.apache.pulsar.cli.ValueValidationUtils.emptyCheck; +import static org.apache.pulsar.cli.ValueValidationUtil.emptyCheck; import com.beust.jcommander.ParameterException; import com.beust.jcommander.converters.BaseConverter; import java.util.concurrent.TimeUnit; diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToSecondsConverter.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToSecondsConverter.java index 41a34bb33cf0e..3aca2e95d2526 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToSecondsConverter.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/TimeUnitToSecondsConverter.java @@ -18,7 +18,7 @@ */ package org.apache.pulsar.cli.converters; -import static org.apache.pulsar.cli.ValueValidationUtils.emptyCheck; +import static org.apache.pulsar.cli.ValueValidationUtil.emptyCheck; import com.beust.jcommander.ParameterException; import com.beust.jcommander.converters.BaseConverter; import java.util.concurrent.TimeUnit; diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/IntegerMaxValueLongValidator.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/IntegerMaxValueLongValidator.java index 9670c17a25dbd..63115b1418793 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/IntegerMaxValueLongValidator.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/IntegerMaxValueLongValidator.java @@ -20,11 +20,11 @@ import com.beust.jcommander.IValueValidator; import com.beust.jcommander.ParameterException; -import org.apache.pulsar.cli.ValueValidationUtils; +import org.apache.pulsar.cli.ValueValidationUtil; public class IntegerMaxValueLongValidator implements IValueValidator { @Override public void validate(String name, Long value) throws ParameterException { - ValueValidationUtils.maxValueCheck(name, value, Integer.MAX_VALUE); + ValueValidationUtil.maxValueCheck(name, value, Integer.MAX_VALUE); } } diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/MinNegativeOneValidator.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/MinNegativeOneValidator.java index 03c51d1bd8cf5..320e36812bfc2 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/MinNegativeOneValidator.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/MinNegativeOneValidator.java @@ -20,11 +20,11 @@ import com.beust.jcommander.IValueValidator; import com.beust.jcommander.ParameterException; -import org.apache.pulsar.cli.ValueValidationUtils; +import org.apache.pulsar.cli.ValueValidationUtil; public class MinNegativeOneValidator implements IValueValidator { @Override public void validate(String name, Long value) throws ParameterException { - ValueValidationUtils.minValueCheck(name, value, -1L); + ValueValidationUtil.minValueCheck(name, value, -1L); } } diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/NonNegativeValueValidator.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/NonNegativeValueValidator.java index 7f52f5f892bb1..473961be06d83 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/NonNegativeValueValidator.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/NonNegativeValueValidator.java @@ -20,11 +20,11 @@ import com.beust.jcommander.IValueValidator; import com.beust.jcommander.ParameterException; -import org.apache.pulsar.cli.ValueValidationUtils; +import org.apache.pulsar.cli.ValueValidationUtil; public class NonNegativeValueValidator implements IValueValidator { @Override public void validate(String name, Long value) throws ParameterException { - ValueValidationUtils.minValueCheck(name, value, 0L); + ValueValidationUtil.minValueCheck(name, value, 0L); } } diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveIntegerValueValidator.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveIntegerValueValidator.java index 218541a8762e6..c6b4cc43d6825 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveIntegerValueValidator.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveIntegerValueValidator.java @@ -20,12 +20,12 @@ import com.beust.jcommander.IValueValidator; import com.beust.jcommander.ParameterException; -import org.apache.pulsar.cli.ValueValidationUtils; +import org.apache.pulsar.cli.ValueValidationUtil; public class PositiveIntegerValueValidator implements IValueValidator { @Override public void validate(String name, Integer value) throws ParameterException { - ValueValidationUtils.positiveCheck(name, value); + ValueValidationUtil.positiveCheck(name, value); } } diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveLongValueValidator.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveLongValueValidator.java index 1390a21a5e0a5..849a55241c665 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveLongValueValidator.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/validators/PositiveLongValueValidator.java @@ -20,12 +20,12 @@ import com.beust.jcommander.IValueValidator; import com.beust.jcommander.ParameterException; -import org.apache.pulsar.cli.ValueValidationUtils; +import org.apache.pulsar.cli.ValueValidationUtil; public class PositiveLongValueValidator implements IValueValidator { @Override public void validate(String name, Long value) throws ParameterException { - ValueValidationUtils.positiveCheck(name, value); + ValueValidationUtil.positiveCheck(name, value); } } diff --git a/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/ValueValidationUtilsTest.java b/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/ValueValidationUtilTest.java similarity index 67% rename from pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/ValueValidationUtilsTest.java rename to pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/ValueValidationUtilTest.java index 65c1c1d2cd12d..9d44ee41a2e25 100644 --- a/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/ValueValidationUtilsTest.java +++ b/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/ValueValidationUtilTest.java @@ -22,46 +22,46 @@ import com.beust.jcommander.ParameterException; import org.testng.annotations.Test; -public class ValueValidationUtilsTest { +public class ValueValidationUtilTest { @Test public void testMaxValueCheck() { - assertThrows(ParameterException.class, () -> ValueValidationUtils.maxValueCheck("param1", 11L, 10L)); - ValueValidationUtils.maxValueCheck("param2", 10L, 10L); - ValueValidationUtils.maxValueCheck("param3", 9L, 10L); + assertThrows(ParameterException.class, () -> ValueValidationUtil.maxValueCheck("param1", 11L, 10L)); + ValueValidationUtil.maxValueCheck("param2", 10L, 10L); + ValueValidationUtil.maxValueCheck("param3", 9L, 10L); } @Test public void testPositiveCheck() { // Long - assertThrows(ParameterException.class, () -> ValueValidationUtils.positiveCheck("param1", 0L)); - assertThrows(ParameterException.class, () -> ValueValidationUtils.positiveCheck("param2", -1L)); - ValueValidationUtils.positiveCheck("param3", 1L); + assertThrows(ParameterException.class, () -> ValueValidationUtil.positiveCheck("param1", 0L)); + assertThrows(ParameterException.class, () -> ValueValidationUtil.positiveCheck("param2", -1L)); + ValueValidationUtil.positiveCheck("param3", 1L); // Integer - assertThrows(ParameterException.class, () -> ValueValidationUtils.positiveCheck("param4", 0)); - assertThrows(ParameterException.class, () -> ValueValidationUtils.positiveCheck("param5", -1)); - ValueValidationUtils.positiveCheck("param6", 1); + assertThrows(ParameterException.class, () -> ValueValidationUtil.positiveCheck("param4", 0)); + assertThrows(ParameterException.class, () -> ValueValidationUtil.positiveCheck("param5", -1)); + ValueValidationUtil.positiveCheck("param6", 1); } @Test public void testEmptyCheck() { - assertThrows(ParameterException.class, () -> ValueValidationUtils.emptyCheck("param1", "")); - assertThrows(ParameterException.class, () -> ValueValidationUtils.emptyCheck("param2", null)); - ValueValidationUtils.emptyCheck("param3", "nonEmpty"); + assertThrows(ParameterException.class, () -> ValueValidationUtil.emptyCheck("param1", "")); + assertThrows(ParameterException.class, () -> ValueValidationUtil.emptyCheck("param2", null)); + ValueValidationUtil.emptyCheck("param3", "nonEmpty"); } @Test public void testMinValueCheck() { - assertThrows(ParameterException.class, () -> ValueValidationUtils.minValueCheck("param1", 9L, 10L)); - ValueValidationUtils.minValueCheck("param2", 10L, 10L); - ValueValidationUtils.minValueCheck("param3", 11L, 10L); + assertThrows(ParameterException.class, () -> ValueValidationUtil.minValueCheck("param1", 9L, 10L)); + ValueValidationUtil.minValueCheck("param2", 10L, 10L); + ValueValidationUtil.minValueCheck("param3", 11L, 10L); } @Test public void testPositiveCheckInt() { - assertThrows(ParameterException.class, () -> ValueValidationUtils.positiveCheck("param1", 0)); - assertThrows(ParameterException.class, () -> ValueValidationUtils.positiveCheck("param2", -1)); - ValueValidationUtils.positiveCheck("param3", 1); + assertThrows(ParameterException.class, () -> ValueValidationUtil.positiveCheck("param1", 0)); + assertThrows(ParameterException.class, () -> ValueValidationUtil.positiveCheck("param2", -1)); + ValueValidationUtil.positiveCheck("param3", 1); } } diff --git a/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/converters/ByteConversionTest.java b/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/converters/ByteConversionTest.java new file mode 100644 index 0000000000000..d669d455df1eb --- /dev/null +++ b/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/converters/ByteConversionTest.java @@ -0,0 +1,95 @@ +/* + * 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.pulsar.cli.converters; + +import com.beust.jcommander.ParameterException; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertThrows; + +public class ByteConversionTest { + + @DataProvider + public static Object[][] successfulByteUnitUtilTestCases() { + return new Object[][] { + {"4096", 4096L}, + {"1000", 1000L}, + {"100K", 102400L}, + {"100k", 102400L}, + {"100M", 104857600L}, + {"100m", 104857600L}, + {"100G", 107374182400L}, + {"100g", 107374182400L}, + {"100T", 109951162777600L}, + {"100t", 109951162777600L}, + }; + } + + @DataProvider + public static Object[][] failingByteUnitUtilTestCases() { + return new Object[][] { + {""}, // Empty string + {"1Z"}, // Invalid size unit + {"1.5K"}, // Non-integer value + {"K"} // Missing size value + }; + } + + @Test(dataProvider = "successfulByteUnitUtilTestCases") + public void testSuccessfulByteUnitUtilConversion(String input, long expected) { + assertEquals(ByteUnitUtil.validateSizeString(input), expected); + } + + @Test(dataProvider = "successfulByteUnitUtilTestCases") + public void testSuccessfulByteUnitToLongConverter(String input, long expected) { + ByteUnitToLongConverter converter = new ByteUnitToLongConverter("optionName"); + assertEquals(converter.convert(input), Long.valueOf(expected)); + } + + @Test(dataProvider = "successfulByteUnitUtilTestCases") + public void testSuccessfulByteUnitIntegerConverter(String input, long expected) { + ByteUnitIntegerConverter converter = new ByteUnitIntegerConverter("optionName"); + // Since the converter returns an Integer, we need to cast expected to int + assertEquals(converter.convert(input), Integer.valueOf((int) expected)); + } + + @Test(dataProvider = "failingByteUnitUtilTestCases") + public void testFailedByteUnitUtilConversion(String input) { + if (input.isEmpty()) { + assertThrows(IllegalArgumentException.class, () -> ByteUnitUtil.validateSizeString(input)); + } else { + assertThrows(ParameterException.class, () -> ByteUnitUtil.validateSizeString(input)); + } + } + + @Test(dataProvider = "failingByteUnitUtilTestCases") + public void testFailedByteUnitToLongConverter(String input) { + ByteUnitToLongConverter converter = new ByteUnitToLongConverter("optionName"); + assertThrows(ParameterException.class, () -> converter.convert(input)); + } + + @Test(dataProvider = "failingByteUnitUtilTestCases") + public void testFailedByteUnitIntegerConverter(String input) { + ByteUnitIntegerConverter converter = new ByteUnitIntegerConverter("optionName"); + assertThrows(ParameterException.class, () -> converter.convert(input)); + } +} + diff --git a/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/converters/CliUtilConverterTest.java b/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/converters/CliUtilConverterTest.java deleted file mode 100644 index d46dcdcac5309..0000000000000 --- a/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/converters/CliUtilConverterTest.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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.pulsar.cli.converters; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertThrows; -import com.beust.jcommander.ParameterException; -import org.testng.annotations.Test; - -public class CliUtilConverterTest { - - @Test - public void testTimeUnitToSecondsConverter() { - TimeUnitToSecondsConverter converter = new TimeUnitToSecondsConverter("optionName"); - assertEquals(converter.convert("10s"), Long.valueOf(10)); - assertEquals(converter.convert("1m"), Long.valueOf(60)); - assertThrows(ParameterException.class, () -> converter.convert(null)); - assertThrows(ParameterException.class, () -> converter.convert("")); - } - - @Test - public void testTimeUnitToMillisConverter() { - TimeUnitToMillisConverter converter = new TimeUnitToMillisConverter("optionName"); - assertEquals(converter.convert("1s"), Long.valueOf(1000)); - assertEquals(converter.convert("1m"), Long.valueOf(60000)); - assertThrows(ParameterException.class, () -> converter.convert(null)); - assertThrows(ParameterException.class, () -> converter.convert("")); - } - - @Test - public void testByteUnitToLongConverter() { - ByteUnitToLongConverter converter = new ByteUnitToLongConverter("optionName"); - assertEquals(converter.convert("1"), Long.valueOf(1)); - assertEquals(converter.convert("1K"), Long.valueOf(1024)); - assertThrows(ParameterException.class, () -> converter.convert(null)); - assertThrows(ParameterException.class, () -> converter.convert("")); - } - - @Test - public void testByteUnitIntegerConverter() { - ByteUnitIntegerConverter converter = new ByteUnitIntegerConverter("optionName"); - assertEquals(converter.convert("1"), Integer.valueOf(1)); - assertEquals(converter.convert("1K"), Integer.valueOf(1024)); - assertThrows(ParameterException.class, () -> converter.convert(null)); - assertThrows(ParameterException.class, () -> converter.convert("")); - } -} diff --git a/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/converters/TimeConversionTest.java b/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/converters/TimeConversionTest.java new file mode 100644 index 0000000000000..f7adeee0423ae --- /dev/null +++ b/pulsar-cli-utils/src/test/java/org/apache/pulsar/cli/converters/TimeConversionTest.java @@ -0,0 +1,86 @@ +/* + * 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.pulsar.cli.converters; + +import java.util.concurrent.TimeUnit; + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import static org.testng.Assert.*; + +public class TimeConversionTest { + + @DataProvider + public static Object[][] successfulRelativeTimeUtilTestCases() { + return new Object[][] { + {"-1", -1L}, + {"7", 7L}, + {"100", 100L}, // No time unit, assuming seconds + {"3s", 3L}, + {"3S", 3L}, + {"10s", 10L}, + {"1m", 60L}, + {"5m", TimeUnit.MINUTES.toSeconds(5L)}, + {"5M", TimeUnit.MINUTES.toSeconds(5L)}, + {"7h", TimeUnit.HOURS.toSeconds(7L)}, + {"7H", TimeUnit.HOURS.toSeconds(7L)}, + {"9d", TimeUnit.DAYS.toSeconds(9L)}, + {"9D", TimeUnit.DAYS.toSeconds(9L)}, + {"1w", 604800L}, + {"3W", 7 * TimeUnit.DAYS.toSeconds(3L)}, + {"11y", 365 * TimeUnit.DAYS.toSeconds(11L)}, + {"11Y", 365 * TimeUnit.DAYS.toSeconds(11L)}, + {"-5m", -TimeUnit.MINUTES.toSeconds(5L)} + }; + } + + @Test(dataProvider = "successfulRelativeTimeUtilTestCases") + public void testSuccessfulRelativeTimeUtilParsing(String input, long expected) { + assertEquals(RelativeTimeUtil.parseRelativeTimeInSeconds(input), expected); + } + + @Test(dataProvider = "successfulRelativeTimeUtilTestCases") + public void testSuccessfulTimeUnitToSecondsConverter(String input, long expected) { + TimeUnitToSecondsConverter secondsConverter = new TimeUnitToSecondsConverter("optionName"); + assertEquals(secondsConverter.convert(input), Long.valueOf(expected)); + } + + @Test(dataProvider = "successfulRelativeTimeUtilTestCases") + public void testSuccessfulTimeUnitToMillisConverter(String input, long expected) { + TimeUnitToMillisConverter millisConverter = new TimeUnitToMillisConverter("optionName"); + // We multiply the expected by 1000 to convert the seconds into milliseconds + assertEquals(millisConverter.convert(input), Long.valueOf(expected * 1000)); + } + + @Test + public void testFailingParsing() { + assertThrows(IllegalArgumentException.class, () -> RelativeTimeUtil.parseRelativeTimeInSeconds("")); // Empty string + assertThrows(IllegalArgumentException.class, () -> RelativeTimeUtil.parseRelativeTimeInSeconds("s")); // Non-numeric character + assertThrows(IllegalArgumentException.class, () -> RelativeTimeUtil.parseRelativeTimeInSeconds("1z")); // Invalid time unit + assertThrows(IllegalArgumentException.class, () -> RelativeTimeUtil.parseRelativeTimeInSeconds("1.5")); // Floating point number + } + + @Test + public void testNsToSeconds() { + assertEquals(RelativeTimeUtil.nsToSeconds(1_000_000_000), 1.000); + assertEquals(RelativeTimeUtil.nsToSeconds(1_500_000_000), 1.500); + assertEquals(RelativeTimeUtil.nsToSeconds(1_555_555_555), 1.556); + } +} From e0ad3bf9174eb011ddf9207b9ae47b5076fcecf3 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Sat, 12 Aug 2023 21:15:42 +0900 Subject: [PATCH 14/14] Remove unncessary comment --- .../org/apache/pulsar/cli/converters/RelativeTimeUtil.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/RelativeTimeUtil.java b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/RelativeTimeUtil.java index 0cdacf3c607c9..412a6415e3c31 100644 --- a/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/RelativeTimeUtil.java +++ b/pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/RelativeTimeUtil.java @@ -23,9 +23,6 @@ import java.util.concurrent.TimeUnit; import lombok.experimental.UtilityClass; -/** - * Mirror of {@link org.apache.pulsar.common.util.RelativeTimeUtil}. - */ @UtilityClass public class RelativeTimeUtil { public static long parseRelativeTimeInSeconds(String relativeTime) {