You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by re...@apache.org on 2024/10/14 16:51:10 UTC
(jackrabbit-oak) branch OAK-11198b created (now 634f8852d7)
This is an automated email from the ASF dual-hosted git repository.
reschke pushed a change to branch OAK-11198b
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
at 634f8852d7 OAK-11198: commons: add helper class for argument checking
This branch includes the following new commits:
new 634f8852d7 OAK-11198: commons: add helper class for argument checking
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(jackrabbit-oak) 01/01: OAK-11198: commons: add helper class for argument checking
Posted by re...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
reschke pushed a commit to branch OAK-11198b
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
commit 634f8852d7129024a6e20b444dbb647785d30e44
Author: Julian Reschke <re...@apache.org>
AuthorDate: Mon Oct 14 17:50:49 2024 +0100
OAK-11198: commons: add helper class for argument checking
---
.../jackrabbit/oak/commons/conditions/Checks.java | 118 +++++++++++++++++++++
.../oak/commons/conditions/package-info.java | 28 +++++
.../oak/commons/conditions/ChecksTest.java | 110 +++++++++++++++++++
3 files changed, 256 insertions(+)
diff --git a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/conditions/Checks.java b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/conditions/Checks.java
new file mode 100644
index 0000000000..129d628f87
--- /dev/null
+++ b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/conditions/Checks.java
@@ -0,0 +1,118 @@
+package org.apache.jackrabbit.oak.commons.conditions;
+
+import java.util.Objects;
+
+import org.apache.jackrabbit.oak.commons.properties.SystemPropertySupplier;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public final class Checks {
+
+ private Checks() {
+ // no instances for you
+ }
+
+ private static Logger LOG = LoggerFactory.getLogger(Checks.class);
+
+ // when true, message template are arguments checked even when the condition
+ // to check for is true
+ private static boolean CHECKMESSAGETEMPLATE = SystemPropertySupplier
+ .create("oak.precondition.checks.CHECKMESSAGETEMPLATE", false).loggingTo(LOG).get();
+
+ /**
+ * Checks the specified expression
+ *
+ * @param expression
+ * to check
+ * @throws IllegalArgumentException
+ * when false
+ */
+ public static void checkArgument(boolean expression) throws IllegalArgumentException {
+ if (!expression) {
+ throw new IllegalArgumentException();
+ }
+ }
+
+ /**
+ * Checks the specified expression
+ *
+ * @param expression
+ * to check
+ * @param message
+ * to use in exception
+ * @throws IllegalArgumentException
+ * when false
+ */
+ public static void checkArgument(boolean expression, @NotNull String message) throws IllegalArgumentException {
+
+ Objects.requireNonNull(message);
+
+ if (!expression) {
+ throw new IllegalArgumentException(message);
+ }
+ }
+
+ /**
+ * Checks the specified expression
+ *
+ * @param expression
+ * to checks
+ * @param messageTemplate
+ * to use in exception (using {@link String#format} syntax
+ * @throws IllegalArgumentException
+ * when false
+ */
+ public static void checkArgument(boolean expression, @NotNull String messageTemplate, @Nullable Object... messageArgs) {
+
+ Objects.requireNonNull(messageTemplate);
+
+ if (CHECKMESSAGETEMPLATE) {
+ checkTemplate(messageTemplate, messageArgs);
+ }
+
+ if (!expression) {
+ if (!CHECKMESSAGETEMPLATE) {
+ checkTemplate(messageTemplate, messageArgs);
+ }
+
+ String message = String.format(messageTemplate, messageArgs);
+ throw new IllegalArgumentException(message);
+ }
+ }
+
+ protected static boolean checkTemplate(@NotNull String messageTemplate, @Nullable Object... messageArgs) {
+ int argsSpecified = messageArgs.length;
+ int argsInTemplate = countArguments(messageTemplate);
+ boolean result = argsSpecified == argsInTemplate;
+ if (argsSpecified != argsInTemplate) {
+ LOG.error("Invalid message format: template '{}', argument count {}", messageTemplate, argsSpecified);
+ }
+ return result;
+ }
+
+ protected static int countArguments(String template) {
+ int count = 0;
+ boolean inEscape = false;
+
+ for (char c : template.toCharArray()) {
+ if (inEscape) {
+ if (c != '%') {
+ count += 1;
+ }
+ inEscape = false;
+ } else {
+ if (c == '%') {
+ inEscape = true;
+ }
+ }
+ }
+
+ if (inEscape) {
+ LOG.error("trailing escape character '%' found", new Exception("call stack"));
+ }
+
+ return count;
+ }
+}
diff --git a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/conditions/package-info.java b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/conditions/package-info.java
new file mode 100644
index 0000000000..f13977ce42
--- /dev/null
+++ b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/conditions/package-info.java
@@ -0,0 +1,28 @@
+/*
+ * 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.
+ */
+
+/**
+ * Utilities for argument checking.
+ */
+@Internal(since = "1.0.0")
+@Version("1.0.0")
+package org.apache.jackrabbit.oak.commons.conditions;
+import org.apache.jackrabbit.oak.commons.annotations.Internal;
+import org.osgi.annotation.versioning.Version;
+
diff --git a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/conditions/ChecksTest.java b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/conditions/ChecksTest.java
new file mode 100644
index 0000000000..a9e37f3d5e
--- /dev/null
+++ b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/conditions/ChecksTest.java
@@ -0,0 +1,110 @@
+/*
+ * 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.jackrabbit.oak.commons.conditions;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import org.junit.Test;
+
+public class ChecksTest {
+
+ @Test(expected = IllegalArgumentException.class)
+ public void checkArgumentFalse() {
+ Checks.checkArgument(false);
+ }
+
+ @Test
+ public void checkArgumentTrue() {
+ Checks.checkArgument(true);
+ }
+
+ @Test
+ public void checkArgumentFalseWithMessage() {
+ try {
+ Checks.checkArgument(false, "foo%");
+ fail("exception expected");
+ } catch (IllegalArgumentException ex) {
+ assertEquals("foo%", ex.getMessage());
+ }
+ }
+
+ @Test
+ public void checkArgumentTrueWithMessage() {
+ Checks.checkArgument(true, "foo%");
+ }
+
+ @Test
+ public void checkArgumentFalseWithMessageTemplate() {
+ try {
+ Checks.checkArgument(false, "foo %s bar %s", "qux1", "qux2");
+ fail("exception expected");
+ } catch (IllegalArgumentException ex) {
+ assertEquals("foo qux1 bar qux2", ex.getMessage());
+ }
+ }
+
+ @Test
+ public void checkArgumentFalseWithMessageTemplateNull() {
+ try {
+ Checks.checkArgument(false, "foo %s bar %s", null, "qux2");
+ fail("exception expected");
+ } catch (IllegalArgumentException ex) {
+ assertEquals("foo null bar qux2", ex.getMessage());
+ }
+ }
+
+ @Test
+ public void checkArgumentFalseWithMessageTemplateTooFew() {
+ try {
+ Checks.checkArgument(false, "foo %s bar %s", "qux2");
+ fail("exception expected");
+ } catch (IllegalArgumentException ex) {
+ // expected, thrown by String.format
+ }
+ }
+
+ @Test
+ public void checkArgumentFalseWithMessageTemplateTooMany() {
+ try {
+ Checks.checkArgument(false, "foo %s bar %s", 1, 2, 3);
+ fail("exception expected");
+ } catch (IllegalArgumentException ex) {
+ assertEquals("foo 1 bar 2", ex.getMessage());
+ }
+ }
+
+ @Test
+ public void countArguments() {
+ assertEquals(3, Checks.countArguments("1%s2%d%%%c"));
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void checkTemplatemullParam() {
+ Checks.checkTemplate("foo", (Object[]) null);
+ }
+
+ @Test
+ public void checkTemplate() {
+ assertFalse(Checks.checkTemplate("foo", "x"));
+ assertTrue(Checks.checkTemplate("foo"));
+ }
+}
\ No newline at end of file