/*
 * Copyright (c) 2006-2014 DMDirc Developers
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.addons.debug;

import com.dmdirc.FrameContainer;
import com.dmdirc.commandparser.CommandArguments;
import com.dmdirc.commandparser.commands.context.CommandContext;
import com.dmdirc.interfaces.CommandController;

import java.util.HashSet;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static com.dmdirc.harness.CommandArgsMatcher.*;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class DebugTest {

    @Mock private CommandArguments arguments;
    @Mock private FrameContainer container;
    @Mock private DebugPlugin plugin;
    @Mock private CommandController controller;
    @Mock private DebugCommand debugCommand;
    @Mock private CommandContext commandContext;
    private Debug debug;

    @Before
    public void setup() {
        when(controller.getCommandChar()).thenReturn('/');
        when(debugCommand.getName()).thenReturn("test");
    }

    /** Checks the debug command with no arguments shows usage. */
    @Test
    public void testNoArgs() {
        debug = new Debug(controller, new HashSet<DebugCommand>());
        when(arguments.isCommand()).thenReturn(true);
        when(arguments.getArguments()).thenReturn(new String[0]);
        debug.execute(container, arguments, null);
        verify(container).addLine(eq("commandUsage"), anyObject(),
                eq("debug"), anyObject());
    }

    /** Checks the debug command with an invalid subcommand shows an error. */
    @Test
    public void testInvalidArg() {
        debug = new Debug(controller, new HashSet<DebugCommand>());
        when(arguments.isCommand()).thenReturn(true);
        when(arguments.getArguments()).thenReturn(new String[]{"test"});

        debug.execute(container, arguments, null);
        verify(container).addLine(eq("commandError"), anyObject());
    }

    /** Checks the debug command executes a subcommand with no args. */
    @Test
    public void testCommandNoArgs() {
        debug = new Debug(controller, new HashSet<DebugCommand>() {{ add(debugCommand); }});
        when(arguments.isCommand()).thenReturn(true);
        when(arguments.getArguments()).thenReturn(new String[]{"test"});
        when(arguments.getArgumentsAsString(1)).thenReturn("");
        when(debugCommand.getName()).thenReturn("test");

        debug.execute(container, arguments, commandContext);

        verify(container, never()).addLine(anyString(), anyObject());
        verify(debugCommand).execute(same(container), eqLine("/test"), same(commandContext));
    }

    /** Checks the debug command executes a subcommand with args. */
    @Test
    public void testCommandWithArgs() {
        debug = new Debug(controller, new HashSet<DebugCommand>() {{ add(debugCommand); }});
        when(arguments.isCommand()).thenReturn(true);
        when(arguments.getArguments()).thenReturn(new String[]{"test", "1", "2", "3"});
        when(arguments.getArgumentsAsString(1)).thenReturn("1 2 3");
        when(debugCommand.getName()).thenReturn("test");

        debug.execute(container, arguments, commandContext);

        verify(container, never()).addLine(anyString(), anyObject());
        verify(debugCommand).execute(same(container), eqLine("/test 1 2 3"), same(commandContext));
    }

}

@@ -1349,24 +1349,64 @@
 ontroller;%0A%0A
+import com.google.common.collect.Sets;%0A%0A
 import java.
@@ -1419,16 +1419,17 @@
 ashSet;%0A
+%0A
 import o
@@ -1630,46 +1630,309 @@
 her.
-*;%0Aimport static org.mockito.Mockito.*
+eqLine;%0Aimport static org.mockito.Matchers.anyObject;%0Aimport static org.mockito.Matchers.anyString;%0Aimport static org.mockito.Matchers.eq;%0Aimport static org.mockito.Matchers.same;%0Aimport static org.mockito.Mockito.never;%0Aimport static org.mockito.Mockito.verify;%0Aimport static org.mockito.Mockito.when
 ;%0A%0A@
@@ -3070,35 +3070,29 @@
 controller, 
-new Hash
 Set
+s.
 %3CDebugComman
@@ -3085,32 +3085,42 @@
 s.%3CDebugCommand%3E
+newHashSet
 ());%0A        whe
@@ -3340,30 +3340,30 @@
 Error%22), any
-Object
+String
 ());%0A    %7D%0A%0A
@@ -3524,42 +3524,23 @@
 er, 
+Sets.
 new
- 
 HashSet
-%3CDebugCommand%3E() %7B%7B add
 (deb
@@ -3541,36 +3541,32 @@
 et(debugCommand)
-; %7D%7D
 );%0A        when(
@@ -3914,38 +3914,38 @@
 anyString(), any
-Object
+String
 ());%0A        ver
@@ -4195,42 +4195,23 @@
 er, 
+Sets.
 new
- 
 HashSet
-%3CDebugCommand%3E() %7B%7B add
 (deb
@@ -4224,12 +4224,8 @@
 and)
-; %7D%7D
 );%0A 
@@ -4609,30 +4609,30 @@
 tring(), any
-Object
+String
 ());%0A       

Fix up DebugTest.