GEODE-1617: add test for region names Added comprehensive test to validate region names
This closes #285 Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/0c9002b2 Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/0c9002b2 Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/0c9002b2 Branch: refs/heads/feature/GEODE-288 Commit: 0c9002b205e64cb248ab6ab26f2df27bf821c54c Parents: b35e2a4 Author: Kevin Duling <kdul...@pivotal.io> Authored: Tue Nov 15 09:33:59 2016 -0800 Committer: Kirk Lund <kl...@apache.org> Committed: Tue Nov 15 12:02:53 2016 -0800 ---------------------------------------------------------------------- .../geode/internal/cache/LocalRegion.java | 2 +- .../cache/RegionNameValidationJUnitTest.java | 91 ++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/0c9002b2/geode-core/src/main/java/org/apache/geode/internal/cache/LocalRegion.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/internal/cache/LocalRegion.java b/geode-core/src/main/java/org/apache/geode/internal/cache/LocalRegion.java index fd4b6c7..80fc5da 100644 --- a/geode-core/src/main/java/org/apache/geode/internal/cache/LocalRegion.java +++ b/geode-core/src/main/java/org/apache/geode/internal/cache/LocalRegion.java @@ -7578,7 +7578,7 @@ public class LocalRegion extends AbstractRegion implements LoaderHelperFactory, this.entries.removeEntry(event.getKey(), re, false); } - static void validateRegionName(String name, InternalRegionArguments internalRegionArgs) { + public static void validateRegionName(String name, InternalRegionArguments internalRegionArgs) { if (name == null) { throw new IllegalArgumentException( LocalizedStrings.LocalRegion_NAME_CANNOT_BE_NULL.toLocalizedString()); http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/0c9002b2/geode-core/src/test/java/org/apache/geode/cache/RegionNameValidationJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/org/apache/geode/cache/RegionNameValidationJUnitTest.java b/geode-core/src/test/java/org/apache/geode/cache/RegionNameValidationJUnitTest.java new file mode 100644 index 0000000..365b68c --- /dev/null +++ b/geode-core/src/test/java/org/apache/geode/cache/RegionNameValidationJUnitTest.java @@ -0,0 +1,91 @@ +/* + * 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.geode.cache; + +import static org.junit.Assert.fail; + +import org.apache.geode.internal.cache.InternalRegionArguments; +import org.apache.geode.internal.cache.LocalRegion; +import org.apache.geode.test.junit.categories.UnitTest; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +@Category(UnitTest.class) +public class RegionNameValidationJUnitTest { + private static final Pattern NAME_PATTERN = Pattern.compile("[aA-zZ0-9-_.]+"); + private static final String REGION_NAME = "MyRegion"; + + + @Test + public void testInvalidNames() { + InternalRegionArguments ira = new InternalRegionArguments(); + ira.setInternalRegion(false); + try { + LocalRegion.validateRegionName(null, ira); + fail(); + } catch (IllegalArgumentException ignore) { + } + try { + LocalRegion.validateRegionName("", ira); + fail(); + } catch (IllegalArgumentException ignore) { + } + try { + LocalRegion.validateRegionName("FOO" + Region.SEPARATOR, ira); + fail(); + } catch (IllegalArgumentException ignore) { + } + + } + + @Test + public void testExternalRegionNames() { + InternalRegionArguments ira = new InternalRegionArguments(); + ira.setInternalRegion(false); + validateCharacters(ira); + try { + LocalRegion.validateRegionName("__InvalidInternalRegionName", ira); + fail(); + } catch (IllegalArgumentException ignore) { + } + } + + @Test + public void testInternalRegionNames() { + InternalRegionArguments ira = new InternalRegionArguments(); + ira.setInternalRegion(true); + LocalRegion.validateRegionName("__ValidInternalRegionName", ira); + } + + private void validateCharacters(InternalRegionArguments ira) { + for (int x = 0; x < Character.MAX_VALUE; x++) { + String name = (char) x + REGION_NAME; + Matcher matcher = NAME_PATTERN.matcher(name); + if (matcher.matches()) { + LocalRegion.validateRegionName(name, ira); + } else { + try { + LocalRegion.validateRegionName(name, ira); + fail("Should have received an IllegalArgumentException for character: " + (char) x + "[" + + x + "]"); + } catch (IllegalArgumentException ignore) { + } + } + } + } +}