/*
 * Copyright (c) 2010. Axon Framework
 *
 * 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.axonframework.commandhandling.annotation;

import org.axonframework.commandhandling.CommandBus;
import org.junit.*;

import java.util.ArrayList;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

/**
 * @author Allard Buijze
 */
public class AnnotationCommandHandlerAdapterTest {

    private AnnotationCommandHandlerAdapter testSubject;
    private CommandBus mockBus;
    private MyCommandHandler mockTarget;

    @Before
    public void setUp() {
        mockBus = mock(CommandBus.class);
        mockTarget = new MyCommandHandler();
        testSubject = new AnnotationCommandHandlerAdapter(mockTarget, mockBus);
    }

    @Test
    public void testHandlerDispatching_VoidReturnType() {
        Object actualReturnValue = testSubject.handle("");
        assertEquals(void.class, actualReturnValue);
        assertEquals(1, mockTarget.voidHandlerInvoked);
        assertEquals(0, mockTarget.returningHandlerInvoked);
    }

    @Test
    public void testHandlerDispatching_WithReturnType() {
        Object actualReturnValue = testSubject.handle(1L);
        assertEquals(1L, actualReturnValue);
        assertEquals(0, mockTarget.voidHandlerInvoked);
        assertEquals(1, mockTarget.returningHandlerInvoked);
    }

    @Test
    public void testHandlerDispatching_ThrowingCheckedException() {
        try {
            testSubject.handle(new Object());
            fail("Expected exception");
        }
        catch (CommandHandlerInvocationException ex) {
            assertEquals(Exception.class, ex.getCause().getClass());
        }
    }

    @Test
    public void testHandlerDispatching_ThrowingRuntimeException() {
        try {
            testSubject.handle(new ArrayList());
            fail("Expected exception");
        }
        catch (RuntimeException ex) {
            assertEquals(RuntimeException.class, ex.getClass());
        }
    }

    @Test
    public void testSubscribe() {
        testSubject.subscribe();

        verify(mockBus).subscribe(Long.class, testSubject);
        verify(mockBus).subscribe(String.class, testSubject);
        verify(mockBus).subscribe(Object.class, testSubject);
        verify(mockBus).subscribe(ArrayList.class, testSubject);
        verifyNoMoreInteractions(mockBus);
    }

    private static class MyCommandHandler {

        private int voidHandlerInvoked;
        private int returningHandlerInvoked;

        @CommandHandler
        public void myVoidHandler(String stringCommand) {
            voidHandlerInvoked++;
        }

        @CommandHandler
        public Long myReturningHandler(Long longCommand) {
            returningHandlerInvoked++;
            return longCommand;
        }

        @CommandHandler
        public void exceptionThrowingHandler(Object o) throws Exception {
            throw new Exception("Some exception");
        }

        @CommandHandler
        public void exceptionThrowingHandler(ArrayList o) throws Exception {
            throw new RuntimeException("Some exception");
        }
    }
}

@@ -739,42 +739,205 @@
 org.
-junit.*;%0D%0A%0D%0Aimport java.util.Array
+axonframework.commandhandling.NoHandlerForCommandException;%0D%0Aimport org.junit.*;%0D%0A%0D%0Aimport java.lang.reflect.Method;%0D%0Aimport java.util.ArrayList;%0D%0Aimport java.util.HashSet;%0D%0Aimport java.util.Linked
 List
@@ -2206,21 +2206,22 @@
 dle(new 
-Objec
+HashSe
 t());%0D%0A 
@@ -2979,21 +2979,22 @@
 bscribe(
-Objec
+HashSe
 t.class,
@@ -3124,24 +3124,381 @@
 ;%0D%0A    %7D%0D%0A%0D%0A
+    @Test%0D%0A    public void testFindHandlerMethod() %7B%0D%0A        Method method = testSubject.findCommandHandlerMethodFor(%22%22);%0D%0A        assertEquals(%22myVoidHandler%22, method.getName());%0D%0A    %7D%0D%0A%0D%0A    @Test(expected = NoHandlerForCommandException.class)%0D%0A    public void testHandle_NoHandlerForCommand() %7B%0D%0A        testSubject.handle(new LinkedList());%0D%0A    %7D%0D%0A%0D%0A
     private 
@@ -3994,21 +3994,22 @@
 Handler(
-Objec
+HashSe
 t o) thr

Test improvement