use of org.apache.geode.pdx.PdxInstance in project geode by apache.
the class PdxFieldMapperJUnitTest method testIgnoreMissing.
@Test
public void testIgnoreMissing() {
String[] fields = new String[] { "s", "i", "s2", "o" };
PdxLuceneSerializer mapper = new PdxLuceneSerializer(fields);
PdxInstance i = mock(PdxInstance.class);
when(i.hasField("s")).thenReturn(true);
when(i.hasField("i")).thenReturn(true);
when(i.hasField("o")).thenReturn(true);
when(i.hasField("o2")).thenReturn(true);
when(i.getField("s")).thenReturn("a");
when(i.getField("i")).thenReturn(5);
when(i.getField("o")).thenReturn(new Object());
when(i.getField("o2")).thenReturn(new Object());
Document doc = new Document();
mapper.toDocument(i, doc);
assertEquals(2, doc.getFields().size());
assertEquals("a", doc.getField("s").stringValue());
assertEquals(5, doc.getField("i").numericValue());
}
use of org.apache.geode.pdx.PdxInstance in project geode by apache.
the class PdxFieldMapperJUnitTest method testNullField.
@Test
public void testNullField() {
String[] fields = new String[] { "s", "i" };
PdxLuceneSerializer mapper = new PdxLuceneSerializer(fields);
PdxInstance i = mock(PdxInstance.class);
when(i.hasField("s")).thenReturn(true);
when(i.hasField("i")).thenReturn(true);
when(i.getField("s")).thenReturn("a");
when(i.getField("i")).thenReturn(null);
Document doc = new Document();
mapper.toDocument(i, doc);
assertEquals(1, doc.getFields().size());
assertEquals("a", doc.getField("s").stringValue());
assertNull(doc.getField("i"));
}
use of org.apache.geode.pdx.PdxInstance in project geode by apache.
the class PdxFieldMapperJUnitTest method testWriteFields.
@Test
public void testWriteFields() {
String[] fields = new String[] { "s", "i" };
PdxLuceneSerializer mapper = new PdxLuceneSerializer(fields);
PdxInstance i = mock(PdxInstance.class);
when(i.hasField("s")).thenReturn(true);
when(i.hasField("i")).thenReturn(true);
when(i.getField("s")).thenReturn("a");
when(i.getField("i")).thenReturn(5);
Document doc = new Document();
mapper.toDocument(i, doc);
assertEquals(2, doc.getFields().size());
assertEquals("a", doc.getField("s").stringValue());
assertEquals(5, doc.getField("i").numericValue());
}
use of org.apache.geode.pdx.PdxInstance in project geode by apache.
the class PdxQueryDUnitTest method testPdxInstanceNoFieldNoMethod.
/**
* Test to query a field that is not present in the Pdx object Also the implicit method is absent
* in the class
*
* @throws CacheException
*/
@Test
public void testPdxInstanceNoFieldNoMethod() throws CacheException {
final Host host = Host.getHost(0);
final VM vm0 = host.getVM(0);
final VM vm3 = host.getVM(3);
final int numberOfEntries = 10;
final String name = "/" + regionName;
final String[] qs = { "select * from " + name + " where pdxStatus = 'active'", "select pdxStatus from " + name + " where id > 4" };
// Start server1
final int port1 = (Integer) vm0.invoke(new SerializableCallable("Create Server1") {
@Override
public Object call() throws Exception {
Region r1 = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
CacheServer server = getCache().addCacheServer();
int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
server.setPort(port);
server.start();
return port;
}
});
// create client and load only version 1 objects with no pdxStatus field
vm3.invoke(new SerializableCallable("Create client") {
@Override
public Object call() throws Exception {
ClientCacheFactory cf = new ClientCacheFactory();
cf.addPoolServer(NetworkUtils.getServerHostName(vm0.getHost()), port1);
ClientCache cache = getClientCache(cf);
Region region = cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(regionName);
// Load version 1 objects
for (int i = 0; i < numberOfEntries; i++) {
PdxInstanceFactory pdxInstanceFactory = PdxInstanceFactoryImpl.newCreator("PdxVersionedNewPortfolio", false);
pdxInstanceFactory.writeInt("id", i);
pdxInstanceFactory.writeString("status", (i % 2 == 0 ? "active" : "inactive"));
PdxInstance pdxInstance = pdxInstanceFactory.create();
region.put("key-" + i, pdxInstance);
}
return null;
}
});
// Version1 class loader
vm3.invoke(new SerializableCallable("Create client") {
@Override
public Object call() throws Exception {
// Load version 1 classloader
QueryService remoteQueryService = null;
// Execute query remotely
try {
remoteQueryService = getCache().getQueryService();
} catch (Exception e) {
Assert.fail("Failed to get QueryService.", e);
}
for (int i = 0; i < qs.length; i++) {
try {
SelectResults sr = (SelectResults) remoteQueryService.newQuery(qs[i]).execute();
if (i == 1) {
assertEquals(5, sr.size());
for (Object o : sr) {
if (!(o instanceof Undefined)) {
fail("Result should be Undefined and not " + o.getClass());
}
}
} else {
assertEquals(0, sr.size());
}
} catch (Exception e) {
Assert.fail("Failed executing " + qs[i], e);
}
}
return null;
}
});
Invoke.invokeInEveryVM(DistributedTestCase.class, "disconnectFromDS");
}
use of org.apache.geode.pdx.PdxInstance in project geode by apache.
the class PdxQueryDUnitTest method testDefaultValuesInPdxFieldTypes.
/**
* Test query execution when default values of {@link FieldType} are used. This happens when one
* version of Pdx object does not have a field but other version has.
*
* @throws Exception
*/
@Test
public void testDefaultValuesInPdxFieldTypes() throws Exception {
final Host host = Host.getHost(0);
final VM vm0 = host.getVM(0);
final VM vm1 = host.getVM(1);
final int numberOfEntries = 10;
final String name = "/" + regionName;
final String query = "select stringField, booleanField, charField, shortField, intField, longField, floatField, doubleField from " + name;
// Start server1
final int port1 = (Integer) vm0.invoke(new SerializableCallable("Create Server1") {
@Override
public Object call() throws Exception {
Region r1 = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
CacheServer server = getCache().addCacheServer();
int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
server.setPort(port);
server.start();
return port;
}
});
// client loads version1 and version2 objects on server
vm1.invoke(new SerializableCallable("Create client") {
@Override
public Object call() throws Exception {
ClientCacheFactory cf = new ClientCacheFactory();
cf.addPoolServer(NetworkUtils.getServerHostName(vm0.getHost()), port1);
ClientCache cache = getClientCache(cf);
Region region = cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regionName);
// Load version 1 objects
for (int i = 0; i < numberOfEntries; i++) {
PdxInstanceFactory pdxInstanceFactory = PdxInstanceFactoryImpl.newCreator("PdxVersionedFieldType", false);
pdxInstanceFactory.writeString("stringField", "" + i);
pdxInstanceFactory.writeBoolean("booleanField", (i % 2 == 0 ? true : false));
pdxInstanceFactory.writeChar("charField", ((char) i));
pdxInstanceFactory.writeShort("shortField", new Integer(i).shortValue());
PdxInstance pdxInstance = pdxInstanceFactory.create();
logger.info("Putting object: " + pdxInstance);
region.put("key-" + i, pdxInstance);
}
// Load version 2 objects
for (int i = numberOfEntries; i < numberOfEntries * 2; i++) {
PdxInstanceFactory pdxInstanceFactory = PdxInstanceFactoryImpl.newCreator("PdxVersionedFieldType", false);
pdxInstanceFactory.writeInt("intField", i);
pdxInstanceFactory.writeLong("longField", new Integer(i + 1).longValue());
pdxInstanceFactory.writeFloat("floatField", new Integer(i + 2).floatValue());
pdxInstanceFactory.writeDouble("doubleField", new Integer(i + 3).doubleValue());
PdxInstance pdxInstance = pdxInstanceFactory.create();
logger.info("Putting object: " + pdxInstance);
region.put("key-" + i, pdxInstance);
}
return null;
}
});
// query locally on server, create index, verify results with and without index
vm0.invoke(new SerializableCallable("Create index") {
@Override
public Object call() throws Exception {
GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
cache.setReadSerialized(true);
QueryService qs = null;
SelectResults[][] sr = new SelectResults[1][2];
// Execute query locally
try {
qs = getCache().getQueryService();
} catch (Exception e) {
Assert.fail("Failed to get QueryService.", e);
}
try {
sr[0][0] = (SelectResults) qs.newQuery(query).execute();
assertEquals(20, sr[0][0].size());
} catch (Exception e) {
Assert.fail("Failed executing " + qs, e);
}
// create index
try {
qs.createIndex("stringIndex", "stringField", name);
qs.createIndex("booleanIndex", "booleanField", name);
qs.createIndex("shortIndex", "shortField", name);
qs.createIndex("charIndex", "charField", name);
qs.createIndex("intIndex", "intField", name);
qs.createIndex("longIndex", "longField", name);
qs.createIndex("floatIndex", "floatField", name);
qs.createIndex("doubleIndex", "doubleField", name);
} catch (Exception e) {
Assert.fail("Exception creating index ", e);
}
// query after index creation
try {
sr[0][1] = (SelectResults) qs.newQuery(query).execute();
assertEquals(20, sr[0][1].size());
} catch (Exception e) {
Assert.fail("Failed executing " + qs, e);
}
CacheUtils.compareResultsOfWithAndWithoutIndex(sr);
return null;
}
});
// Update index
vm1.invoke(new SerializableCallable("update index") {
@Override
public Object call() throws Exception {
Region region = getCache().getRegion(regionName);
// Load version 1 objects
for (int i = numberOfEntries; i < numberOfEntries * 2; i++) {
PdxInstanceFactory pdxInstanceFactory = PdxInstanceFactoryImpl.newCreator("PdxVersionedFieldType", false);
pdxInstanceFactory.writeString("stringField", "" + i);
pdxInstanceFactory.writeBoolean("booleanField", (i % 2 == 0 ? true : false));
pdxInstanceFactory.writeChar("charField", ((char) i));
pdxInstanceFactory.writeShort("shortField", new Integer(i).shortValue());
PdxInstance pdxInstance = pdxInstanceFactory.create();
logger.info("Putting object: " + pdxInstance);
region.put("key-" + i, pdxInstance);
}
// Load version 2 objects
for (int i = 0; i < numberOfEntries; i++) {
PdxInstanceFactory pdxInstanceFactory = PdxInstanceFactoryImpl.newCreator("PdxVersionedFieldType", false);
pdxInstanceFactory.writeInt("intField", i);
pdxInstanceFactory.writeLong("longField", new Integer(i + 1).longValue());
pdxInstanceFactory.writeFloat("floatField", new Integer(i + 2).floatValue());
pdxInstanceFactory.writeDouble("doubleField", new Integer(i + 3).doubleValue());
PdxInstance pdxInstance = pdxInstanceFactory.create();
logger.info("Putting object: " + pdxInstance);
region.put("key-" + i, pdxInstance);
}
return null;
}
});
// query remotely from client
vm1.invoke(new SerializableCallable("query") {
@Override
public Object call() throws Exception {
QueryService remoteQueryService = null;
QueryService localQueryService = null;
SelectResults[][] sr = new SelectResults[1][2];
// Execute query locally
try {
remoteQueryService = getCache().getQueryService();
localQueryService = ((ClientCache) getCache()).getLocalQueryService();
} catch (Exception e) {
Assert.fail("Failed to get QueryService.", e);
}
try {
sr[0][0] = (SelectResults) remoteQueryService.newQuery(query).execute();
assertEquals(20, sr[0][0].size());
sr[0][1] = (SelectResults) localQueryService.newQuery(query).execute();
assertEquals(20, sr[0][1].size());
} catch (Exception e) {
fail("Failed executing query " + e);
}
CacheUtils.compareResultsOfWithAndWithoutIndex(sr);
return null;
}
});
Invoke.invokeInEveryVM(DistributedTestCase.class, "disconnectFromDS");
}
Aggregations