Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
/*
* 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.pdx;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Properties;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import util.TestException;

import org.apache.geode.cache.DataPolicy;
import org.apache.geode.cache.client.ClientCache;
import org.apache.geode.cache.client.ClientRegionShortcut;
import org.apache.geode.internal.cache.InternalCache;
import org.apache.geode.pdx.internal.PeerTypeRegistration;
import org.apache.geode.test.dunit.AsyncInvocation;
import org.apache.geode.test.dunit.rules.ClientVM;
import org.apache.geode.test.dunit.rules.ClusterStartupRule;
import org.apache.geode.test.dunit.rules.MemberVM;

public class PdxTypeGenerationDUnitTest {

private MemberVM locator, server1, server2;
private static final int numOfTypes = 15;
private static final int numOfEnums = 10;

@Rule
public ClusterStartupRule cluster = new ClusterStartupRule();

@Before
public void before() {
Properties props = new Properties();
props.setProperty("log-level", "WARN");

locator = cluster.startLocatorVM(0, props);

int locatorPort1 = locator.getPort();
server1 = cluster.startServerVM(1,
x -> x.withProperties(props).withConnectionToLocator(locatorPort1));

int locatorPort2 = locator.getPort();
server2 = cluster.startServerVM(2,
x -> x.withProperties(props).withConnectionToLocator(locatorPort2));
}

@Test
public void testLocalMapsRecoveredAfterServerRestart() {
createPdxOnServer(server1, numOfTypes, numOfEnums);

server2.invoke(() -> {
InternalCache cache = ClusterStartupRule.getCache();
PeerTypeRegistration registration =
(PeerTypeRegistration) (cache.getPdxRegistry().getTypeRegistration());

assertThat(registration.getLocalSize()).isEqualTo(numOfTypes + numOfEnums);
assertThat(registration.getTypeToIdSize()).isEqualTo(0);
assertThat(registration.getEnumToIdSize()).isEqualTo(0);

});

server2.stop(false);
Properties props = new Properties();
props.setProperty("log-level", "WARN");
int locatorPort1 = locator.getPort();
server2 = cluster.startServerVM(2,
x -> x.withProperties(props).withConnectionToLocator(locatorPort1));

server2.invoke(() -> {
InternalCache cache = ClusterStartupRule.getCache();
PeerTypeRegistration registration =
(PeerTypeRegistration) (cache.getPdxRegistry().getTypeRegistration());

assertThat(registration.getLocalSize()).isEqualTo(numOfTypes + numOfEnums);
assertThat(registration.getTypeToIdSize()).isEqualTo(numOfTypes);
assertThat(registration.getEnumToIdSize()).isEqualTo(numOfEnums);
});
}

@Test
public void definingNewTypeUpdatesLocalMaps() {
createPdxOnServer(server1, numOfTypes, numOfEnums);

server2.invoke(() -> {
InternalCache cache = ClusterStartupRule.getCache();
PeerTypeRegistration registration =
(PeerTypeRegistration) (cache.getPdxRegistry().getTypeRegistration());

assertThat(registration.getLocalSize()).isEqualTo(numOfTypes + numOfEnums);
assertThat(registration.getTypeToIdSize()).isEqualTo(0);
assertThat(registration.getEnumToIdSize()).isEqualTo(0);

// Creating a new PdxType to trigger the pending local maps to be flushed
JSONFormatter.fromJSON("{\"fieldName\": \"value\"}");

assertThat(registration.getLocalSize()).isEqualTo(numOfTypes + numOfEnums + 1);
assertThat(registration.getTypeToIdSize()).isEqualTo(numOfTypes + 1);
assertThat(registration.getEnumToIdSize()).isEqualTo(numOfEnums);
});
}

@Test
public void testNoConflictsWhenGeneratingPdxTypesFromJSONOnMultipleServers() {
int repeats = 10000;

AsyncInvocation invocation1 = server1.invokeAsync(() -> {
for (int i = 0; i < repeats; ++i) {
JSONFormatter.fromJSON("{\"counter" + i + "\": " + i + "}");
}
});
AsyncInvocation invocation2 = server2.invokeAsync(() -> {
for (int i = 0; i < repeats; ++i) {
JSONFormatter.fromJSON("{\"counter" + i + "\": " + i + "}");
}
});

try {
invocation1.await();
invocation2.await();
} catch (Exception ex) {
throw new TestException("Exception while awaiting async invocation: " + ex);
}

server1.invoke(() -> {
InternalCache cache = ClusterStartupRule.getCache();
int numberOfTypesInRegion = cache.getPdxRegistry().getTypeRegistration().getLocalSize();
int numberOfTypesInLocalMap =
((PeerTypeRegistration) cache.getPdxRegistry().getTypeRegistration()).getTypeToIdSize();

assertThat(numberOfTypesInRegion)
.withFailMessage("Expected number of PdxTypes in region to be %s but was %s",
repeats, numberOfTypesInRegion)
.isEqualTo(repeats);

assertThat(numberOfTypesInLocalMap)
.withFailMessage("Expected number of PdxTypes in local map to be %s but was %s",
repeats, numberOfTypesInLocalMap)
.isEqualTo(repeats);
});

server2.invoke(() -> {
InternalCache cache = ClusterStartupRule.getCache();
int numberOfTypesInRegion = cache.getRegion(PeerTypeRegistration.REGION_FULL_PATH).size();
int numberOfTypesInLocalMap =
((PeerTypeRegistration) cache.getPdxRegistry().getTypeRegistration()).getTypeToIdSize();

assertThat(numberOfTypesInRegion)
.withFailMessage("Expected number of PdxTypes in region to be %s but was %s",
repeats, numberOfTypesInRegion)
.isEqualTo(repeats);

assertThat(numberOfTypesInLocalMap)
.withFailMessage("Expected number of PdxTypes in local map to be %s but was %s",
repeats, numberOfTypesInLocalMap)
.isEqualTo(repeats);
});
}

@Test
public void testEnumsAndPdxTypesCreatedOnClientAreEnteredIntoTypeRegistry() throws Exception {
final String regionName = "regionName";
server1.invoke(() -> {
ClusterStartupRule.getCache().createRegionFactory().setDataPolicy(
DataPolicy.REPLICATE).create(regionName);
});
server2.invoke(() -> {
ClusterStartupRule.getCache().createRegionFactory().setDataPolicy(
DataPolicy.REPLICATE).create(regionName);
});
int port = locator.getPort();

Properties props = new Properties();
props.setProperty("log-level", "WARN");
ClientVM client = cluster.startClientVM(3,
cf -> cf.withLocatorConnection(port).withPoolSubscription(true).withProperties(props));

client.invoke(() -> {
ClientCache cache = ClusterStartupRule.getClientCache();
cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regionName);

for (int i = 0; i < numOfTypes; ++i) {
JSONFormatter.fromJSON("{\"counter" + i + "\": " + i + "}");
}
for (int i = 0; i < numOfEnums; ++i) {
cache.createPdxEnum("ClassName", "EnumName" + i, i);
}
});

server1.invoke(() -> {
InternalCache cache = ClusterStartupRule.getCache();
assertThat(cache).isNotNull();
int numberOfTypesInRegion = cache.getPdxRegistry().getTypeRegistration().getLocalSize();

assertThat(numberOfTypesInRegion)
.withFailMessage("Expected number of PdxTypes and Enums in region to be %s but was %s",
numOfEnums, numberOfTypesInRegion)
.isEqualTo(numOfTypes + numOfEnums);
});
}

private void createPdxOnServer(MemberVM server, int numOfTypes, int numOfEnums) {
server.invoke(() -> {
InternalCache cache = ClusterStartupRule.getCache();

for (int i = 0; i < numOfTypes; ++i) {
JSONFormatter.fromJSON("{\"counter" + i + "\": " + i + "}");
}
for (int i = 0; i < numOfEnums; ++i) {
cache.createPdxEnum("ClassName", "EnumName" + i, i);
}
PeerTypeRegistration registration =
(PeerTypeRegistration) (cache.getPdxRegistry().getTypeRegistration());

assertThat(registration.getLocalSize()).isEqualTo(numOfTypes + numOfEnums);
assertThat(registration.getTypeToIdSize()).isEqualTo(numOfTypes);
assertThat(registration.getEnumToIdSize()).isEqualTo(numOfEnums);
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"configGlossary:installationAt": "Philadelphia, PA",
"configGlossary:adminEmail": "ksm@pobox.com",
"configGlossary:poweredBy": "Cofax",
"configGlossary:poweredByIcon": "/images/cofax.gif",
"configGlossary:staticPath": "/content/static",
"templateProcessorClass": "org.cofax.WysiwygTemplate",
"templateLoaderClass": "org.cofax.FilesTemplateLoader",
"templatePath": "templates",
"templateOverridePath": "",
"defaultListTemplate": "listTemplate.htm",
"defaultFileTemplate": "articleTemplate.htm",
"useJSP": false,
"jspListTemplate": "listTemplate.jsp",
"jspFileTemplate": "articleTemplate.jsp",
"cachePackageTagsTrack": 200,
"cachePackageTagsStore": 200,
"cachePackageTagsRefresh": 60,
"cacheTemplatesTrack": 100,
"cacheTemplatesStore": 50,
"cacheTemplatesRefresh": 15,
"cachePagesTrack": 200,
"cachePagesStore": 100,
"cachePagesRefresh": 10,
"cachePagesDirtyRead": 10,
"searchEngineListTemplate": "forSearchEnginesList.htm",
"searchEngineFileTemplate": "forSearchEngines.htm",
"searchEngineRobotsDb": "WEB-INF/robots.db",
"useDataStore": true,
"dataStoreClass": "org.cofax.SqlDataStore",
"redirectionClass": "org.cofax.SqlRedirection",
"dataStoreName": "cofax",
"dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver",
"dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon",
"dataStoreUser": "sa",
"dataStorePassword": "dataStoreTestQuery",
"dataStoreTestQuery": "SET NOCOUNT ON;select test='test';",
"dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log",
"dataStoreInitConns": 10,
"dataStoreMaxConns": 100,
"dataStoreConnUsageLimit": 100,
"dataStoreLogLevel": "debug",
"maxUrlLength": 500}
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,18 @@
@Category({SerializationTest.class})
public class JSONFormatterJUnitTest {
public static final String REGION_NAME = "primitiveKVStore";

private Cache cache;

private Region<Object, Object> region;

@Before
public void setUp() throws Exception {
this.cache = new CacheFactory().set(MCAST_PORT, "0").setPdxReadSerialized(true).create();
this.cache = new CacheFactory().set(MCAST_PORT, "0")
.set("log-level", "WARN").setPdxReadSerialized(true).create();

region = cache.createRegionFactory().setDataPolicy(DataPolicy.PARTITION).create(REGION_NAME);
region = cache.createRegionFactory().setDataPolicy(DataPolicy.PARTITION)
.create(REGION_NAME);

}

Expand Down Expand Up @@ -89,7 +93,8 @@ public void verifyJsonToPdxInstanceConversion() throws Exception {
// 2. Get the JSON string from actualTestObject using jackson ObjectMapper.
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat("MM/dd/yyyy"));
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

String json = objectMapper.writeValueAsString(expectedTestObject);

Expand All @@ -111,14 +116,16 @@ public void verifyJsonToPdxInstanceConversion() throws Exception {
}

@Test
public void verifyJsonToPdxInstanceConversionWithJSONFormatter() throws Exception {
public void verifyJsonToPdxInstanceConversionWithJSONFormatter()
throws Exception {
TestObjectForJSONFormatter expectedTestObject = new TestObjectForJSONFormatter();
expectedTestObject.defaultInitialization();

// 1.gets pdxInstance using R.put() and R.get()
region.put("501", expectedTestObject);
Object receivedObject = region.get("501");
assertEquals("receivedObject is expected to be of type PdxInstance", PdxInstanceImpl.class,
assertEquals("receivedObject is expected to be of type PdxInstance",
PdxInstanceImpl.class,
receivedObject.getClass());

PdxInstance expectedPI = (PdxInstance) receivedObject;
Expand All @@ -140,7 +147,8 @@ public void verifyJsonToPdxInstanceConversionWithJSONFormatter() throws Exceptio
assertEquals("receivedObject is expected to be of type PdxInstance",
TestObjectForJSONFormatter.class, actualTestObject.getClass());

assertEquals("actualTestObject and expectedTestObject should be equal", expectedTestObject,
assertEquals("actualTestObject and expectedTestObject should be equal",
expectedTestObject,
actualTestObject);
}

Expand All @@ -153,7 +161,8 @@ public void testJSONStringAsPdxObject() {
int pdxTypes = 0;

if (cache.getRegion(PeerTypeRegistration.REGION_FULL_PATH) != null) {
pdxTypes = cache.getRegion(PeerTypeRegistration.REGION_FULL_PATH).keySet().size();
pdxTypes = cache.getRegion(PeerTypeRegistration.REGION_FULL_PATH).keySet()
.size();
}

String js = "{name:\"ValueExist\", age:14}";
Expand Down Expand Up @@ -197,7 +206,8 @@ public void testJSONStringSortedFields() {
int pdxTypes = 0;

if (cache.getRegion(PeerTypeRegistration.REGION_FULL_PATH) != null) {
pdxTypes = cache.getRegion(PeerTypeRegistration.REGION_FULL_PATH).keySet().size();
pdxTypes = cache.getRegion(PeerTypeRegistration.REGION_FULL_PATH)
.keySet().size();
}

String js2 = "{c:\"c' go\", bb:23, b:\"b\", age:14 }";
Expand Down
Loading