You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by ds...@apache.org on 2017/04/21 23:42:07 UTC
[11/51] [abbrv] geode git commit: GEODE-2764: Addition checks on
region name
GEODE-2764: Addition checks on region name
* If a region name contains function calls like entrySet()
* The from clause is substring-ed at '.' from the right
* Each substing is checked to see if the region name is valid.
This closes #449
Project: http://git-wip-us.apache.org/repos/asf/geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/0714c27c
Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/0714c27c
Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/0714c27c
Branch: refs/heads/feature/GEODE-2097
Commit: 0714c27c9c9262d29baead9c594ee44f740eb122
Parents: ed300c5
Author: nabarun <nn...@pivotal.io>
Authored: Thu Apr 13 15:38:16 2017 -0700
Committer: nabarun <nn...@pivotal.io>
Committed: Fri Apr 14 10:14:52 2017 -0700
----------------------------------------------------------------------
.../cli/functions/CreateIndexFunction.java | 31 ++++-
...nfigurationIndexWithFromClauseDUnitTest.java | 120 +++++++++++++++++++
2 files changed, 147 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/geode/blob/0714c27c/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/CreateIndexFunction.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/CreateIndexFunction.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/CreateIndexFunction.java
index ca164ac..8a65f7a 100644
--- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/CreateIndexFunction.java
+++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/CreateIndexFunction.java
@@ -25,7 +25,6 @@ import org.apache.geode.cache.query.QueryService;
import org.apache.geode.cache.query.RegionNotFoundException;
import org.apache.geode.internal.InternalEntity;
import org.apache.geode.internal.cache.xmlcache.CacheXml;
-import org.apache.geode.management.internal.cli.CliUtil;
import org.apache.geode.management.internal.cli.domain.IndexInfo;
import org.apache.geode.management.internal.cli.i18n.CliStrings;
import org.apache.geode.management.internal.configuration.domain.XmlEntity;
@@ -69,9 +68,8 @@ public class CreateIndexFunction extends FunctionAdapter implements InternalEnti
queryService.createIndex(indexName, indexedExpression, fromClause);
}
- XmlEntity xmlEntity =
- new XmlEntity(CacheXml.REGION, "name", cache.getRegion(regionPath).getName());
- context.getResultSender().lastResult(new CliFunctionResult(memberId, xmlEntity));
+ regionPath = getValidRegionName(cache, regionPath);
+ setResultInSender(context, indexInfo, memberId, cache, regionPath);
} catch (IndexExistsException e) {
String message =
CliStrings.format(CliStrings.CREATE_INDEX__INDEX__EXISTS, indexInfo.getIndexName());
@@ -93,6 +91,31 @@ public class CreateIndexFunction extends FunctionAdapter implements InternalEnti
}
}
+ private void setResultInSender(FunctionContext context, IndexInfo indexInfo, String memberId,
+ Cache cache, String regionPath) {
+ if (regionPath == null) {
+ String message = CliStrings.format(CliStrings.CREATE_INDEX__INVALID__REGIONPATH,
+ indexInfo.getRegionPath());
+ context.getResultSender().lastResult(new CliFunctionResult(memberId, false, message));
+ } else {
+ XmlEntity xmlEntity =
+ new XmlEntity(CacheXml.REGION, "name", cache.getRegion(regionPath).getName());
+ context.getResultSender().lastResult(new CliFunctionResult(memberId, xmlEntity));
+ }
+ }
+
+ private String getValidRegionName(Cache cache, String regionPath) {
+ while (regionPath != null && cache.getRegion(regionPath) == null) {
+ int dotPosition;
+ if (regionPath.contains(".") && ((dotPosition = regionPath.lastIndexOf('.')) != -1)) {
+ regionPath = regionPath.substring(0, dotPosition);
+ } else {
+ regionPath = null;
+ }
+ }
+ return regionPath;
+ }
+
@Override
public String getId() {
return CreateIndexFunction.class.getName();
http://git-wip-us.apache.org/repos/asf/geode/blob/0714c27c/geode-wan/src/test/java/org/apache/geode/management/internal/configuration/ClusterConfigurationIndexWithFromClauseDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-wan/src/test/java/org/apache/geode/management/internal/configuration/ClusterConfigurationIndexWithFromClauseDUnitTest.java b/geode-wan/src/test/java/org/apache/geode/management/internal/configuration/ClusterConfigurationIndexWithFromClauseDUnitTest.java
new file mode 100644
index 0000000..5a15506
--- /dev/null
+++ b/geode-wan/src/test/java/org/apache/geode/management/internal/configuration/ClusterConfigurationIndexWithFromClauseDUnitTest.java
@@ -0,0 +1,120 @@
+/*
+ * 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.management.internal.configuration;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.geode.cache.RegionShortcut;
+import org.apache.geode.management.cli.Result;
+import org.apache.geode.management.internal.cli.i18n.CliStrings;
+import org.apache.geode.management.internal.cli.result.CommandResult;
+import org.apache.geode.management.internal.cli.util.CommandStringBuilder;
+import org.apache.geode.test.dunit.rules.GfshShellConnectionRule;
+import org.apache.geode.test.dunit.rules.LocatorServerStartupRule;
+import org.apache.geode.test.dunit.rules.MemberVM;
+import org.apache.geode.test.junit.categories.DistributedTest;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.RunWith;
+
+import java.util.Properties;
+import junitparams.JUnitParamsRunner;
+import junitparams.Parameters;
+
+@Category(DistributedTest.class)
+@RunWith(JUnitParamsRunner.class)
+public class ClusterConfigurationIndexWithFromClauseDUnitTest {
+
+ final String REGION_NAME = "region";
+ final String INDEX_NAME = "index";
+
+ protected RegionShortcut[] getRegionTypes() {
+ return new RegionShortcut[] {RegionShortcut.PARTITION, RegionShortcut.PARTITION_PERSISTENT,
+ RegionShortcut.PARTITION_REDUNDANT, RegionShortcut.PARTITION_REDUNDANT_PERSISTENT,
+ RegionShortcut.REPLICATE, RegionShortcut.REPLICATE_PERSISTENT};
+
+ }
+
+ @Rule
+ public LocatorServerStartupRule lsRule = new LocatorServerStartupRule();
+
+ @Rule
+ public GfshShellConnectionRule gfshShellConnectionRule = new GfshShellConnectionRule();
+
+ private MemberVM locator = null;
+
+ @Before
+ public void before() throws Exception {
+ locator = lsRule.startLocatorVM(0);
+ }
+
+ @Test
+ @Parameters(method = "getRegionTypes")
+ public void indexCreatedWithEntrySetInFromClauseMustPersist(RegionShortcut regionShortcut)
+ throws Exception {
+ MemberVM vm1 = lsRule.startServerVM(1, locator.getPort());
+ gfshShellConnectionRule.connectAndVerify(locator);
+ createRegionUsingGfsh(REGION_NAME, regionShortcut, null);
+ createIndexUsingGfsh("\"" + REGION_NAME + ".entrySet() z\"", "z.key", INDEX_NAME);
+ String serverName = vm1.getName();
+ CommandStringBuilder csb = new CommandStringBuilder(CliStrings.LIST_MEMBER);
+ gfshShellConnectionRule.executeAndVerifyCommand(csb.toString());
+ lsRule.stopMember(1);
+ lsRule.startServerVM(1, lsRule.getMember(0).getPort());;
+ verifyIndexRecreated(INDEX_NAME);
+ }
+
+ private void verifyIndexRecreated(String indexName) throws Exception {
+ CommandStringBuilder csb = new CommandStringBuilder(CliStrings.LIST_INDEX);
+ CommandResult commandResult = gfshShellConnectionRule.executeAndVerifyCommand(csb.toString());
+ String resultAsString = commandResultToString(commandResult);
+ assertEquals(Result.Status.OK, commandResult.getStatus());
+ assertTrue(resultAsString.contains(indexName));
+ }
+
+ private String commandResultToString(final CommandResult commandResult) {
+ assertNotNull(commandResult);
+ commandResult.resetToFirstLine();
+ StringBuilder buffer = new StringBuilder(commandResult.getHeader());
+ while (commandResult.hasNextLine()) {
+ buffer.append(commandResult.nextLine());
+ }
+ buffer.append(commandResult.getFooter());
+ return buffer.toString();
+ }
+
+ private void createIndexUsingGfsh(String regionName, String expression, String indexName)
+ throws Exception {
+ CommandStringBuilder csb = new CommandStringBuilder(CliStrings.CREATE_INDEX);
+ csb.addOption(CliStrings.CREATE_INDEX__EXPRESSION, expression);
+ csb.addOption(CliStrings.CREATE_INDEX__REGION, regionName);
+ csb.addOption(CliStrings.CREATE_INDEX__NAME, indexName);
+ gfshShellConnectionRule.executeAndVerifyCommand(csb.toString());
+ }
+
+ private void createRegionUsingGfsh(String regionName, RegionShortcut regionShortCut, String group)
+ throws Exception {
+ CommandStringBuilder csb = new CommandStringBuilder(CliStrings.CREATE_REGION);
+ csb.addOption(CliStrings.CREATE_REGION__REGION, regionName);
+ csb.addOption(CliStrings.CREATE_REGION__REGIONSHORTCUT, regionShortCut.name());
+ csb.addOptionWithValueCheck(CliStrings.CREATE_REGION__GROUP, group);
+ gfshShellConnectionRule.executeAndVerifyCommand(csb.toString());
+ }
+}