Search in sources :

Example 31 with CqAttributesFactory

use of org.apache.geode.cache.query.CqAttributesFactory in project geode by apache.

the class CQListGIIDUnitTest method createCQ.

/* Register CQs */
public static void createCQ(String cqName, String queryStr) {
    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("### Create CQ. ###" + cqName);
    // Get CQ Service.
    QueryService cqService = null;
    try {
        cqService = cache.getQueryService();
    } catch (Exception cqe) {
        Assert.fail("Failed to getCQService.", cqe);
    }
    // Create CQ Attributes.
    CqAttributesFactory cqf = new CqAttributesFactory();
    CqListener[] cqListeners = { new CqQueryTestListener(org.apache.geode.test.dunit.LogWriterUtils.getLogWriter()) };
    ((CqQueryTestListener) cqListeners[0]).cqName = cqName;
    cqf.initCqListeners(cqListeners);
    CqAttributes cqa = cqf.create();
    // Create CQ.
    try {
        CqQuery cq1 = cqService.newCq(cqName, queryStr, cqa);
        assertTrue("newCq() state mismatch", cq1.getState().isStopped());
    } catch (Exception ex) {
        fail("Failed to create CQ " + cqName + " . ", ex);
    }
}
Also used : CqQueryTestListener(org.apache.geode.cache.query.cq.dunit.CqQueryTestListener) QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) CqListener(org.apache.geode.cache.query.CqListener) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqQuery(org.apache.geode.cache.query.CqQuery) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException)

Example 32 with CqAttributesFactory

use of org.apache.geode.cache.query.CqAttributesFactory in project geode by apache.

the class ClientSnapshotDUnitTest method testImport.

@Test
public void testImport() throws Exception {
    int count = 1000;
    for (int i = 0; i < count; i++) {
        region.put(i, new MyObject(i, "clienttest " + i));
    }
    SerializableCallable export = new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            File f = new File(getDiskDirs()[0], "client-import.snapshot");
            Region<Integer, MyObject> r = getCache().getRegion("clienttest");
            r.getSnapshotService().save(f, SnapshotFormat.GEMFIRE);
            return f;
        }
    };
    Host.getHost(0).getVM(3).invoke(export);
    for (int i = 0; i < count; i++) {
        region.put(i, new MyObject(i, "XXX"));
    }
    SerializableCallable imp = new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            final AtomicBoolean cqtest = new AtomicBoolean(false);
            CqAttributesFactory af = new CqAttributesFactory();
            af.addCqListener(new CqListenerAdapter() {

                @Override
                public void onEvent(CqEvent aCqEvent) {
                    cqtest.set(true);
                }
            });
            Region<Integer, MyObject> r = getCache().getRegion("clienttest");
            CqQuery cq = r.getRegionService().getQueryService().newCq("SELECT * FROM /clienttest", af.create());
            cq.execute();
            File f = new File(getDiskDirs()[0], "client-import.snapshot");
            r.getSnapshotService().load(f, SnapshotFormat.GEMFIRE);
            return cqtest.get();
        }
    };
    // add callbacks
    region.getAttributesMutator().setCacheWriter(new CacheWriterAdapter<Integer, MyObject>() {

        @Override
        public void beforeUpdate(EntryEvent<Integer, MyObject> event) {
            fail("CacheWriter invoked during import");
        }
    });
    final AtomicBoolean cltest = new AtomicBoolean(false);
    region.getAttributesMutator().addCacheListener(new CacheListenerAdapter<Integer, MyObject>() {

        @Override
        public void afterUpdate(EntryEvent<Integer, MyObject> event) {
            cltest.set(true);
        }
    });
    boolean cqtest = (Boolean) Host.getHost(0).getVM(3).invoke(imp);
    assertEquals("CacheListener invoked during import", false, cltest.get());
    assertEquals("CqListener invoked during import", false, cqtest);
    for (MyObject obj : region.values()) {
        assertTrue(obj.getF2().startsWith("clienttest"));
    }
}
Also used : CqListenerAdapter(org.apache.geode.cache.util.CqListenerAdapter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CqEvent(org.apache.geode.cache.query.CqEvent) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) MyObject(com.examples.snapshot.MyObject) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqQuery(org.apache.geode.cache.query.CqQuery) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) File(java.io.File) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 33 with CqAttributesFactory

use of org.apache.geode.cache.query.CqAttributesFactory in project geode by apache.

the class ClientSnapshotDUnitTest method testClientCallbacks.

@Test
public void testClientCallbacks() throws Exception {
    int count = 1000;
    for (int i = 0; i < count; i++) {
        region.put(i, new MyObject(i, "clienttest " + i));
    }
    File f = new File(getDiskDirs()[0], "client-callback.snapshot");
    region.getSnapshotService().save(f, SnapshotFormat.GEMFIRE);
    for (int i = 0; i < count; i++) {
        region.put(i, new MyObject(i, "XXX"));
    }
    SerializableCallable callbacks = new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            Region<Integer, MyObject> r = getCache().getRegion("clienttest");
            r.registerInterestRegex(".*");
            r.getAttributesMutator().setCacheWriter(new CacheWriterAdapter<Integer, MyObject>() {

                @Override
                public void beforeUpdate(EntryEvent<Integer, MyObject> event) {
                    fail("CacheWriter invoked during import");
                }
            });
            r.getAttributesMutator().addCacheListener(new CacheListenerAdapter<Integer, MyObject>() {

                @Override
                public void afterUpdate(EntryEvent<Integer, MyObject> event) {
                    fail("CacheListener was invoked during import");
                }
            });
            final AtomicBoolean cqtest = new AtomicBoolean(false);
            CqAttributesFactory af = new CqAttributesFactory();
            af.addCqListener(new CqListenerAdapter() {

                @Override
                public void onEvent(CqEvent aCqEvent) {
                    fail("Cq was invoked during import");
                }
            });
            CqQuery cq = r.getRegionService().getQueryService().newCq("SELECT * FROM /clienttest", af.create());
            cq.execute();
            return null;
        }
    };
    Host.getHost(0).getVM(3).invoke(callbacks);
    region.getSnapshotService().load(f, SnapshotFormat.GEMFIRE);
}
Also used : CqListenerAdapter(org.apache.geode.cache.util.CqListenerAdapter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CqEvent(org.apache.geode.cache.query.CqEvent) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) MyObject(com.examples.snapshot.MyObject) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqQuery(org.apache.geode.cache.query.CqQuery) File(java.io.File) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 34 with CqAttributesFactory

use of org.apache.geode.cache.query.CqAttributesFactory in project geode by apache.

the class ClientCQPostAuthorizationDUnitTest method createCQ.

private void createCQ(final int num) throws CqException, CqExistsException {
    for (int i = 0; i < num; i++) {
        QueryService cqService = getProxyCaches(i).getQueryService();
        String cqName = "CQ_" + i;
        String queryStr = cqNameToQueryStrings.get(cqName) + getProxyCaches(i).getRegion(REGION_NAME).getFullPath();
        // Create CQ Attributes.
        CqAttributesFactory cqf = new CqAttributesFactory();
        CqListener[] cqListeners = { new CqQueryTestListener(getLogWriter()) };
        ((CqQueryTestListener) cqListeners[0]).cqName = cqName;
        cqf.initCqListeners(cqListeners);
        CqAttributes cqa = cqf.create();
        // Create CQ.
        CqQuery cq1 = cqService.newCq(cqName, queryStr, cqa);
        assertTrue("newCq() state mismatch", cq1.getState().isStopped());
    }
}
Also used : CqQueryTestListener(org.apache.geode.cache.query.cq.dunit.CqQueryTestListener) QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) CqListener(org.apache.geode.cache.query.CqListener) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) InternalCqQuery(org.apache.geode.cache.query.internal.cq.InternalCqQuery) CqQuery(org.apache.geode.cache.query.CqQuery)

Example 35 with CqAttributesFactory

use of org.apache.geode.cache.query.CqAttributesFactory in project geode by apache.

the class MultiUserAPIDUnitTest method verifyDisallowedOps.

private void verifyDisallowedOps(final boolean multiUserMode) throws Exception {
    String op = "unknown";
    boolean success = false;
    if (!multiUserMode) {
        success = false;
        try {
            // Attempt cache.createAuthenticatedCacheView() and expect an exception, fail otherwise
            op = "Pool.createSecureUserCache()";
            GemFireCacheImpl.getInstance().createAuthenticatedView(new Properties(), "testPool");
        } catch (IllegalStateException uoe) {
            getLogWriter().info(op + ": Got expected exception: " + uoe);
            success = true;
        }
        if (!success) {
            fail("Did not get exception while doing " + op);
        }
    } else {
        // multiuser mode
        Region realRegion = GemFireCacheImpl.getInstance().getRegion(SecurityTestUtils.REGION_NAME);
        Region proxyRegion = SecurityTestUtils.getProxyCaches(0).getRegion(SecurityTestUtils.REGION_NAME);
        Pool pool = PoolManagerImpl.getPMI().find("testPool");
        for (int i = 0; i <= 27; i++) {
            success = false;
            try {
                switch(i) {
                    // and expect an exception, fail otherwise.
                    case 0:
                        op = "Region.create()";
                        realRegion.create("key", "value");
                        break;
                    case 1:
                        op = "Region.put()";
                        realRegion.put("key", "value");
                        break;
                    case 2:
                        op = "Region.get()";
                        realRegion.get("key");
                        break;
                    case 3:
                        op = "Region.containsKeyOnServer()";
                        realRegion.containsKeyOnServer("key");
                        break;
                    case 4:
                        op = "Region.remove()";
                        realRegion.remove("key");
                        break;
                    case 5:
                        op = "Region.destroy()";
                        realRegion.destroy("key");
                        break;
                    case 6:
                        op = "Region.destroyRegion()";
                        realRegion.destroyRegion();
                        break;
                    case 7:
                        op = "Region.registerInterest()";
                        realRegion.registerInterest("key");
                        break;
                    // break;
                    case 8:
                        op = "Region.clear()";
                        realRegion.clear();
                        break;
                    // and expect an exception, fail otherwise.
                    case 9:
                        op = "ProxyRegion.createSubregion()";
                        proxyRegion.createSubregion("subregion", null);
                        break;
                    case 10:
                        op = "ProxyRegion.forceRolling()";
                        proxyRegion.forceRolling();
                        break;
                    case 11:
                        op = "ProxyRegion.getAttributesMutator()";
                        proxyRegion.getAttributesMutator();
                        break;
                    case 12:
                        op = "ProxyRegion.registerInterest()";
                        proxyRegion.registerInterest("key");
                        break;
                    case 13:
                        op = "ProxyRegion.loadSnapshot()";
                        proxyRegion.loadSnapshot(null);
                        break;
                    case 14:
                        op = "ProxyRegion.saveSnapshot()";
                        proxyRegion.saveSnapshot(null);
                        break;
                    case 15:
                        op = "ProxyRegion.setUserAttribute()";
                        proxyRegion.setUserAttribute(null);
                        break;
                    case 16:
                        op = "ProxyRegion.unregisterInterestRegex()";
                        proxyRegion.unregisterInterestRegex("*");
                        break;
                    // exception, fail otherwise.
                    case 17:
                        op = "FunctionService.onRegion()";
                        FunctionService.onRegion(realRegion);
                        break;
                    case 18:
                        op = "FunctionService.onServer(pool)";
                        FunctionService.onServer(pool);
                        break;
                    case 19:
                        op = "FunctionService.onServers(pool)";
                        FunctionService.onServers(pool);
                        break;
                    // QueryService.newQuery().execute()/newCq().execute/executeWithInitialResults()
                    case 20:
                        op = "QueryService.newQuery.execute()";
                        Query query = pool.getQueryService().newQuery("SELECT * FROM /" + SecurityTestUtils.REGION_NAME);
                        query.execute();
                        break;
                    case 21:
                        op = "QueryService.newCq.execute()";
                        CqQuery cqQuery = pool.getQueryService().newCq("SELECT * FROM /" + SecurityTestUtils.REGION_NAME, new CqAttributesFactory().create());
                        try {
                            cqQuery.execute();
                        } catch (CqException ce) {
                            throw (Exception) ce.getCause();
                        }
                        break;
                    case 22:
                        op = "QueryService.newCq.executeWithInitialResults()";
                        cqQuery = pool.getQueryService().newCq("SELECT * FROM /" + SecurityTestUtils.REGION_NAME, new CqAttributesFactory().create());
                        try {
                            cqQuery.executeWithInitialResults();
                        } catch (CqException ce) {
                            throw (Exception) ce.getCause();
                        }
                        break;
                    // expect an exception, fail otherwise.
                    case 23:
                        op = "ProxyQueryService().getIndexes()";
                        SecurityTestUtils.getProxyCaches(0).getQueryService().getIndexes(null);
                        break;
                    case 24:
                        op = "ProxyQueryService().createIndex()";
                        SecurityTestUtils.getProxyCaches(0).getQueryService().createIndex(null, null, null);
                        break;
                    case 25:
                        op = "ProxyQueryService().removeIndexes()";
                        SecurityTestUtils.getProxyCaches(0).getQueryService().removeIndexes();
                        break;
                    case 26:
                        op = "ProxyRegion.localDestroy()";
                        proxyRegion.localDestroy("key");
                        break;
                    case 27:
                        op = "ProxyRegion.localInvalidate()";
                        proxyRegion.localInvalidate("key");
                        break;
                    default:
                        fail("Unknown op code: " + i);
                        break;
                }
            } catch (UnsupportedOperationException uoe) {
                getLogWriter().info(op + ": Got expected exception: " + uoe);
                success = true;
            }
            if (!success) {
                fail("Did not get exception while doing " + op);
            }
        }
    }
}
Also used : Query(org.apache.geode.cache.query.Query) CqQuery(org.apache.geode.cache.query.CqQuery) CqException(org.apache.geode.cache.query.CqException) Region(org.apache.geode.cache.Region) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) Pool(org.apache.geode.cache.client.Pool) CqQuery(org.apache.geode.cache.query.CqQuery) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties)

Aggregations

CqAttributesFactory (org.apache.geode.cache.query.CqAttributesFactory)36 CqAttributes (org.apache.geode.cache.query.CqAttributes)31 CqQuery (org.apache.geode.cache.query.CqQuery)30 QueryService (org.apache.geode.cache.query.QueryService)29 CacheException (org.apache.geode.cache.CacheException)16 Test (org.junit.Test)16 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)15 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)15 Region (org.apache.geode.cache.Region)11 CqEvent (org.apache.geode.cache.query.CqEvent)11 CqExistsException (org.apache.geode.cache.query.CqExistsException)10 CqListener (org.apache.geode.cache.query.CqListener)10 RegionNotFoundException (org.apache.geode.cache.query.RegionNotFoundException)8 Host (org.apache.geode.test.dunit.Host)8 VM (org.apache.geode.test.dunit.VM)8 IOException (java.io.IOException)7 CqException (org.apache.geode.cache.query.CqException)7 SelectResults (org.apache.geode.cache.query.SelectResults)7 DefaultQueryService (org.apache.geode.cache.query.internal.DefaultQueryService)7 ClientSubscriptionTest (org.apache.geode.test.junit.categories.ClientSubscriptionTest)7