<commit_before>/*
 * Copyright 2006-2007 the original author or authors.
 *
 * Licensed 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.openspaces.itest.core.space.filter.adapter;

import com.gigaspaces.annotation.pojo.SpaceRouting;
import com.gigaspaces.async.AsyncFuture;
import com.j_spaces.core.client.ReadModifiers;
import com.j_spaces.core.client.UpdateModifiers;
import com.j_spaces.core.filters.FilterOperationCodes;
import com.j_spaces.core.filters.entry.ExecutionFilterEntry;
import junit.framework.Assert;
import net.jini.core.lease.Lease;
import org.openspaces.core.GigaSpace;
import org.openspaces.core.executor.Task;
import org.openspaces.events.adapter.SpaceDataEvent;
import org.openspaces.events.notify.SimpleNotifyContainerConfigurer;
import org.openspaces.events.notify.SimpleNotifyEventListenerContainer;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author kimchy
 */
public abstract class AbstractAdapterFilterTests extends AbstractDependencyInjectionSpringContextTests {

    protected SimpleFilter simpleFilter;

    protected GigaSpace gigaSpace;

    protected GigaSpace txnGigaSpace;

    protected PlatformTransactionManager mahaloTxManager;

    public AbstractAdapterFilterTests() {
        setPopulateProtectedVariables(true);
    }

    protected void onSetUp() throws Exception {
        gigaSpace.clear(new Object());
        simpleFilter.clearExecutions();
    }

    protected void onTearDown() {
        gigaSpace.clear(new Object());
        simpleFilter.clearExecutions();
    }

    public void testOnInit() {
        assertTrue(simpleFilter.isOnInitCalled());
    }

    public void testWrite() {
        Message message = new Message(1);
        message.setMessage("test");
        message.setData("testWrite");
        gigaSpace.write(message);

        assertEquals(2, simpleFilter.getLastExecutions().size());

        Object[] params = simpleFilter.getLastExecutions().get(0);
        assertEquals(2, params.length);
        assertEquals("test", ((Message) params[0]).getMessage());
        assertEquals(FilterOperationCodes.BEFORE_WRITE, params[1]);

        params = simpleFilter.getLastExecutions().get(1);
        assertEquals(2, params.length);
        assertEquals("test", ((Message) params[0]).getMessage());
        assertEquals(FilterOperationCodes.AFTER_WRITE, params[1]);
        gigaSpace.clear(null);
        simpleFilter.getLastExecutions().clear();
    }

    public void testNotify() throws Exception {
        SimpleNotifyEventListenerContainer notifyEventListenerContainer = new SimpleNotifyContainerConfigurer(gigaSpace)
                .template(new Message())
                .eventListenerAnnotation(new Object() {
                    @SpaceDataEvent
                    public void gotAnEvent(Message message) {
                        System.out.println(message);
                    }
                }).notifyContainer();
        notifyEventListenerContainer.start();
        int size = simpleFilter.getLastExecutions().size();
        assertEquals(1, size);
        assertNull(((Message) simpleFilter.getLastExecutions().get(size - 1)[0]).getMessage());    //template
        assertEquals("BEFORE_NOTIFY", simpleFilter.getLastExecutions().get(size - 1)[1]);
        simpleFilter.getLastExecutions().clear();
        Message message = new Message(1);
        message.setMessage("test");
        message.setData("testNotify");
        gigaSpace.write(message);
        Thread.sleep(5000);
        size = simpleFilter.getLastExecutions().size();<commit_msg>Wait for notifications to arrive<commit_after>/*
 * Copyright 2006-2007 the original author or authors.
 *
 * Licensed 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.openspaces.itest.core.space.filter.adapter;

import com.gigaspaces.annotation.pojo.SpaceRouting;
import com.gigaspaces.async.AsyncFuture;
import com.j_spaces.core.client.ReadModifiers;
import com.j_spaces.core.client.UpdateModifiers;
import com.j_spaces.core.filters.FilterOperationCodes;
import com.j_spaces.core.filters.entry.ExecutionFilterEntry;
import junit.framework.Assert;
import net.jini.core.lease.Lease;
import org.openspaces.core.GigaSpace;
import org.openspaces.core.executor.Task;
import org.openspaces.events.adapter.SpaceDataEvent;
import org.openspaces.events.notify.SimpleNotifyContainerConfigurer;
import org.openspaces.events.notify.SimpleNotifyEventListenerContainer;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author kimchy
 */
public abstract class AbstractAdapterFilterTests extends AbstractDependencyInjectionSpringContextTests {

    protected SimpleFilter simpleFilter;

    protected GigaSpace gigaSpace;

    protected GigaSpace txnGigaSpace;

    protected PlatformTransactionManager mahaloTxManager;

    public AbstractAdapterFilterTests() {
        setPopulateProtectedVariables(true);
    }

    protected void onSetUp() throws Exception {
        gigaSpace.clear(new Object());
        simpleFilter.clearExecutions();
    }

    protected void onTearDown() {
        gigaSpace.clear(new Object());
        simpleFilter.clearExecutions();
    }

    public void testOnInit() {
        assertTrue(simpleFilter.isOnInitCalled());
    }

    public void testWrite() {
        Message message = new Message(1);
        message.setMessage("test");
        message.setData("testWrite");
        gigaSpace.write(message);

        assertEquals(2, simpleFilter.getLastExecutions().size());

        Object[] params = simpleFilter.getLastExecutions().get(0);
        assertEquals(2, params.length);
        assertEquals("test", ((Message) params[0]).getMessage());
        assertEquals(FilterOperationCodes.BEFORE_WRITE, params[1]);

        params = simpleFilter.getLastExecutions().get(1);
        assertEquals(2, params.length);
        assertEquals("test", ((Message) params[0]).getMessage());
        assertEquals(FilterOperationCodes.AFTER_WRITE, params[1]);
        gigaSpace.clear(null);
        simpleFilter.getLastExecutions().clear();
    }

    public void testNotify() throws Exception {
        SimpleNotifyEventListenerContainer notifyEventListenerContainer = new SimpleNotifyContainerConfigurer(gigaSpace)
                .template(new Message())
                .eventListenerAnnotation(new Object() {
                    @SpaceDataEvent
                    public void gotAnEvent(Message message) {
                        System.out.println(message);
                    }
                }).notifyContainer();
        notifyEventListenerContainer.start();
        Thread.sleep(5000);
        int size = simpleFilter.getLastExecutions().size();
        assertEquals(1, size);
        assertNull(((Message) simpleFilter.getLastExecutions().get(size - 1)[0]).getMessage());    //template
        assertEquals("BEFORE_NOTIFY", simpleFilter.getLastExecutions().get(size - 1)[1]);
        simpleFilter.getLastExecutions().clear();
        Message message = new Message(1);
        message.setMessage("test");
        message.setData("testNotify");
        gigaSpace.write(message);
        Thread.sleep(5000);
        size = simpleFilter.getLastExecutions().size();
{'fraction_non_alphanumeric': 0.10024030209406111, 'fraction_numerical': 0.007781210664835793, 'mean_word_length': 3.67129877071085, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '987887', 'n_tokens_mistral': 2249, 'n_tokens_neox': 2184, 'n_words': 492}