/*
 * 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.remoting.broadcast;

import com.gigaspaces.annotation.pojo.SpaceClass;
import com.gigaspaces.annotation.pojo.SpaceRouting;
import org.openspaces.core.GigaSpace;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author kimchy
 */
public class BroadcastRemotingTests extends AbstractDependencyInjectionSpringContextTests {

    protected SimpleService syncService;

    protected SimpleService executorService;

    protected GigaSpace gigaSpace;

    public BroadcastRemotingTests() {
        setPopulateProtectedVariables(true);
    }

    protected String[] getConfigLocations() {
        return new String[]{"/org/openspaces/itest/remoting/broadcast/broadcast-remoting.xml"};
    }

    public void testTakeMulitpleWorking() {
        gigaSpace.write(new Message(1));
        gigaSpace.write(new Message(2));
        Message[] result = gigaSpace.takeMultiple(new Message(), Integer.MAX_VALUE);
        assertEquals(2, result.length);
        gigaSpace.clear(new Message());
    }

    public void testExecutorSyncBroadcast() {
        innerTestSyncBroadcast(executorService);
    }

    public void testSyncSyncBroadcast() {
        innerTestSyncBroadcast(syncService);
    }

    public void testExecutorAsyncBroadcast() throws ExecutionException, InterruptedException {
        innerTestAsyncBroadcast(executorService);
    }

    public void testSyncAsyncBroadcast() throws ExecutionException, InterruptedException {
        innerTestAsyncBroadcast(syncService);
    }

    public void testExecutorAsyncException() throws ExecutionException, InterruptedException {
        innerTestAsyncException(executorService);
    }

    public void testSyncAsyncException() throws ExecutionException, InterruptedException {
        innerTestAsyncException(syncService);
    }

    public void testExecutorSyncException() throws ExecutionException, InterruptedException {
        innerTestSyncException(executorService);
    }

    public void testSyncSyncException() throws ExecutionException, InterruptedException {
        innerTestSyncException(syncService);
    }

    private void innerTestSyncBroadcast(SimpleService service) {
        int value = service.sum(2);
        assertEquals(4, value);
    }

    private void innerTestAsyncBroadcast(SimpleService service) throws ExecutionException, InterruptedException {
        Future<Integer> future = service.asyncSum(2);
        assertEquals(4, (int) future.get());
    }

    public void innerTestSyncException(SimpleService service) {
        try {
            service.testException();
            fail();
        } catch (SimpleService.MyException e) {
            // all is well
        }
    }

    public void innerTestAsyncException(SimpleService service) {
        try {
            service.asyncTestException().get();
            fail();
        } catch (InterruptedException e) {
            fail();
        } catch (ExecutionException e) {
            if (!(e.getCause() instanceof SimpleService.MyException)) {
                fail();
            }
        }
    }

    @SpaceClass
    public static class Message {

        private Integer routing;

        public Message() {
        }

        public Message(int routing) {
            this.routing = routing;
        }

        @SpaceRouting
        public Integer getRouting() {
            return routing;
        }

        public void setRouting(Integer routing) {
            this.routing = routing;
        }
    }
}
@@ -1844,32 +1844,34 @@
 ervice);%0A    %7D%0A%0A
+//
     public void 
@@ -1888,32 +1888,34 @@
 ncBroadcast() %7B%0A
+//
         innerTes
@@ -1935,32 +1935,34 @@
 t(syncService);%0A
+//
     %7D%0A%0A    publi
@@ -2096,32 +2096,34 @@
 ervice);%0A    %7D%0A%0A
+//
     public void 
@@ -2189,32 +2189,34 @@
 ptedException %7B%0A
+//
         innerTes
@@ -2237,32 +2237,34 @@
 t(syncService);%0A
+//
     %7D%0A%0A    publi

remove deprecated tests