This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.hc.support-1.0.4 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-hc-support.git
commit 1315652f7345863297beb6018f91b024ba9f910e Author: Carsten Ziegeler <[email protected]> AuthorDate: Mon Aug 19 07:21:14 2013 +0000 SLING-3024 : Move SimpleConstraintChecker to healthchecks bundle git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/healthcheck/healthchecks@1515292 13f79535-47bb-0310-9956-ffa450edef68 --- .../healthchecks/impl/JmxAttributeHealthCheck.java | 2 +- .../healthchecks/util/SimpleConstraintChecker.java | 80 ++++++++++ .../util/SimpleConstraintCheckerTest.java | 167 +++++++++++++++++++++ 3 files changed, 248 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/sling/hc/healthchecks/impl/JmxAttributeHealthCheck.java b/src/main/java/org/apache/sling/hc/healthchecks/impl/JmxAttributeHealthCheck.java index 193615d..cc67845 100644 --- a/src/main/java/org/apache/sling/hc/healthchecks/impl/JmxAttributeHealthCheck.java +++ b/src/main/java/org/apache/sling/hc/healthchecks/impl/JmxAttributeHealthCheck.java @@ -31,7 +31,7 @@ import org.apache.sling.commons.osgi.PropertiesUtil; import org.apache.sling.hc.api.HealthCheck; import org.apache.sling.hc.api.Result; import org.apache.sling.hc.healthchecks.util.FormattingResultLog; -import org.apache.sling.hc.util.SimpleConstraintChecker; +import org.apache.sling.hc.healthchecks.util.SimpleConstraintChecker; import org.osgi.service.component.ComponentContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/java/org/apache/sling/hc/healthchecks/util/SimpleConstraintChecker.java b/src/main/java/org/apache/sling/hc/healthchecks/util/SimpleConstraintChecker.java new file mode 100644 index 0000000..13c3b23 --- /dev/null +++ b/src/main/java/org/apache/sling/hc/healthchecks/util/SimpleConstraintChecker.java @@ -0,0 +1,80 @@ +/* + * 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 SF 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.sling.hc.healthchecks.util; + +import org.apache.sling.hc.api.Result; +import org.apache.sling.hc.api.ResultLog; + +/** Simple check of numeric values against expressions + * like < N, > N, between two values etc. + * See {@link SimpleConstraintCheckerTest} for examples. + */ +public class SimpleConstraintChecker { + + public static final String CONTAINS = "contains"; + + /** Check value against expression and report to result */ + public void check(Object inputValue, String constraint, ResultLog resultLog) { + + final String stringValue = inputValue == null ? "" : inputValue.toString(); + + if(constraint == null || constraint.trim().length() == 0) { + throw new IllegalArgumentException("Empty constraint, cannot evaluate"); + } + + final String [] parts = constraint.split(" "); + boolean matches = false; + try { + if(constraint.startsWith(">") && parts.length == 2) { + final int value = Integer.valueOf(stringValue).intValue(); + matches = value > Integer.valueOf(parts[1]); + + } else if(constraint.startsWith("<") && parts.length == 2) { + final int value = Integer.valueOf(stringValue).intValue(); + matches = value < Integer.valueOf(parts[1]); + + } else if(parts.length == 4 && "between".equalsIgnoreCase(parts[0]) && "and".equalsIgnoreCase(parts[2]) ) { + final int value = Integer.valueOf(stringValue).intValue(); + final int lowerBound = Integer.valueOf(parts[1]); + final int upperBound = Integer.valueOf(parts[3]); + matches = value > lowerBound && value < upperBound; + + } else if(parts.length >1 && CONTAINS.equalsIgnoreCase(parts[0])) { + final String pattern = constraint.substring(CONTAINS.length()).trim(); + matches = stringValue.contains(pattern); + + } else { + matches = constraint.equals(stringValue); + } + } catch(NumberFormatException nfe) { + resultLog.add(new ResultLog.Entry( + Result.Status.WARN, + "Invalid numeric value [" + inputValue + "] while evaluating " + constraint)); + } + + if(matches) { + resultLog.add(new ResultLog.Entry( + Result.Status.DEBUG, + "Value [" + inputValue + "] matches constraint [" + constraint + "]")); + } else { + resultLog.add(new ResultLog.Entry( + Result.Status.WARN, + "Value [" + inputValue + "] does not match constraint [" + constraint + "]")); + } + } +} \ No newline at end of file diff --git a/src/test/java/org/apache/sling/hc/healthchecks/util/SimpleConstraintCheckerTest.java b/src/test/java/org/apache/sling/hc/healthchecks/util/SimpleConstraintCheckerTest.java new file mode 100644 index 0000000..d981876 --- /dev/null +++ b/src/test/java/org/apache/sling/hc/healthchecks/util/SimpleConstraintCheckerTest.java @@ -0,0 +1,167 @@ +/* + * 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 SF 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.sling.hc.healthchecks.util; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.apache.sling.hc.api.Result; +import org.apache.sling.hc.api.ResultLog; +import org.apache.sling.hc.healthchecks.util.SimpleConstraintChecker; +import org.junit.Before; +import org.junit.Test; + +public class SimpleConstraintCheckerTest { + private ResultLog resultLog; + + private final SimpleConstraintChecker checker = new SimpleConstraintChecker(); + + @Before + public void setup() { + resultLog = new ResultLog(); + } + + @Test + public void testStringEquals() { + final String s = "test_" + System.currentTimeMillis(); + checker.check(s, s, resultLog); + assertTrue(resultLog.getAggregateStatus().equals(Result.Status.OK)); + } + + @Test + public void testStringNotEquals() { + final String s = "test_" + System.currentTimeMillis(); + checker.check(s, "something else", resultLog); + assertFalse(resultLog.getAggregateStatus().equals(Result.Status.OK)); + } + + @Test + public void testFiveEquals() { + final String s = "5"; + checker.check(s, s, resultLog); + assertTrue(resultLog.getAggregateStatus().equals(Result.Status.OK)); + } + + @Test + public void testIntTwelveEquals() { + checker.check(12, "12", resultLog); + assertTrue(resultLog.getAggregateStatus().equals(Result.Status.OK)); + } + + @Test + public void testIntTwelveGreaterThan() { + checker.check(12, "> 11", resultLog); + assertTrue(resultLog.getAggregateStatus().equals(Result.Status.OK)); + } + + @Test + public void testFiveNotEquals() { + checker.check("5", "foo", resultLog); + assertFalse(resultLog.getAggregateStatus().equals(Result.Status.OK)); + } + + @Test + public void testNullNotEquals() { + checker.check(null, "foo", resultLog); + assertFalse(resultLog.getAggregateStatus().equals(Result.Status.OK)); + } + + @Test + public void testNullNotGreater() { + checker.check(null, "> 2", resultLog); + assertFalse(resultLog.getAggregateStatus().equals(Result.Status.OK)); + } + + @Test + public void testGreaterThanTrue() { + checker.check("5", "> 2", resultLog); + assertTrue(resultLog.getAggregateStatus().equals(Result.Status.OK)); + } + + @Test + public void testGreaterThanFalse() { + checker.check("5", "> 12", resultLog); + assertFalse(resultLog.getAggregateStatus().equals(Result.Status.OK)); + } + + @Test + public void testLessThanTrue() { + checker.check("5", "< 12", resultLog); + assertTrue(resultLog.getAggregateStatus().equals(Result.Status.OK)); + } + + @Test + public void testLessThanFalse() { + checker.check("5", "< 2", resultLog); + assertFalse(resultLog.getAggregateStatus().equals(Result.Status.OK)); + } + + @Test + public void testBetweenA() { + checker.check("5", "between 2 and 6", resultLog); + assertTrue(resultLog.getAggregateStatus().equals(Result.Status.OK)); + } + + @Test + public void testBetweenB() { + checker.check("5", "between 12 and 16", resultLog); + assertFalse(resultLog.getAggregateStatus().equals(Result.Status.OK)); + } + + @Test + public void testBetweenC() { + checker.check(5L, "between 12 and 16", resultLog); + assertFalse(resultLog.getAggregateStatus().equals(Result.Status.OK)); + } + + @Test + public void testBetweenD() { + checker.check(5L, "between 4 and 16", resultLog); + assertTrue(resultLog.getAggregateStatus().equals(Result.Status.OK)); + } + + @Test + public void testBetweenE() { + checker.check(5L, "betWEEN 4 aND 16", resultLog); + assertTrue(resultLog.getAggregateStatus().equals(Result.Status.OK)); + } + + @Test + public void testNotAnInteger() { + checker.check("foo", "between 12 and 16", resultLog); + assertFalse(resultLog.getAggregateStatus().equals(Result.Status.OK)); + } + + @Test + public void testContainsA() { + checker.check("This is a NICE STRING ok?", "contains NICE STRING", resultLog); + assertTrue(resultLog.getAggregateStatus().equals(Result.Status.OK)); + } + + @Test + public void testContainsB() { + checker.check("This is a NICE TOUCH ok?", "contains NICE STRING", resultLog); + assertFalse(resultLog.getAggregateStatus().equals(Result.Status.OK)); + } + + @Test + public void testContainsC() { + checker.check("This is a NICE TOUCH ok?", "contains NICE", resultLog); + assertTrue(resultLog.getAggregateStatus().equals(Result.Status.OK)); + } +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
