package fitnesse.testsystems.slim;

import fitnesse.slim.fixtureInteraction.DefaultInteraction;
import fitnesse.slim.fixtureInteraction.FixtureInteraction;
import fitnesse.slim.fixtureInteraction.InteractionDemo;
import fitnesse.testsystems.Descriptor;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

import static org.junit.Assert.*;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.*;

public class InProcessSlimClientBuilderTest {
  @Before
  public void setUp() throws Exception {
    // Enforce the test runner here, to make sure we're talking to the right system
    SlimClientBuilder.clearSlimPortOffset();
  }

  @Test
  public void parseInteractionFlags() {
    Descriptor descriptor = mock(Descriptor.class);
    when(descriptor.getVariable("slim.flags")).thenReturn("-i " + InteractionDemo.class.getName());

    InProcessSlimClientBuilder slimClientBuilder = new InProcessSlimClientBuilder(descriptor);

    // Check that the arguments were processed correctly and are in the client
    // builder's slim flags
    String[] found = slimClientBuilder.getSlimFlags();
    assertNotNull(found);
    assertEquals(2, found.length);
    assertEquals("-i", found[0]);
    assertEquals(InteractionDemo.class.getName(), found[1]);
  }

  @Test
  public void createSlimClientWithFixtureInteraction() {
    Descriptor descriptor = mock(Descriptor.class);
    when(descriptor.getVariable("slim.flags")).thenReturn("-i " + InteractionDemo.class.getName());

    InProcessSlimClientBuilder slimClientBuilder = spy(new InProcessSlimClientBuilder(descriptor));

    // Capture the interaction to see if it's the right one
    ArgumentCaptor<FixtureInteraction> interactionCaptor = ArgumentCaptor.forClass(FixtureInteraction.class);

    slimClientBuilder.build();
    verify(slimClientBuilder).createSlimServer(interactionCaptor.capture(), anyInt(), anyBoolean());
    FixtureInteraction interaction = interactionCaptor.getValue();

    assertNotNull(interaction);
    assertNotEquals(DefaultInteraction.class, interaction.getClass());
    assertTrue(interaction instanceof InteractionDemo);
  }

  @Test
  public void createSlimClientWithoutFixtureInteraction() throws Exception {
    Descriptor descriptor = spy(Descriptor.class);
    when(descriptor.getVariable("slim.flags")).thenReturn(null);

    InProcessSlimClientBuilder slimClientBuilder = spy(new InProcessSlimClientBuilder(descriptor));

    // Capture the interaction to see if it's the right one
    ArgumentCaptor<FixtureInteraction> interactionCaptor = ArgumentCaptor.forClass(FixtureInteraction.class);

    slimClientBuilder.build();
    verify(slimClientBuilder).createSlimServer(interactionCaptor.capture(), anyInt(), anyBoolean());
    FixtureInteraction interaction = interactionCaptor.getValue();

    assertNotNull(interaction);
    assertEquals(DefaultInteraction.class, interaction.getClass());
  }
}

@@ -1569,184 +1569,8 @@
     
-InProcessSlimClientBuilder slimClientBuilder = spy(new InProcessSlimClientBuilder(descriptor));%0A%0A    // Capture the interaction to see if it's the right one%0A    ArgumentCaptor%3C
 Fixt
@@ -1575,33 +1575,32 @@
 xtureInteraction
-%3E
  interactionCapt
@@ -1599,265 +1599,40 @@
 tion
-Captor = ArgumentCaptor.forClass(FixtureInteraction.class);%0A%0A    slimClientBuilder.build();%0A    verify(slimClientBuilder).createSlimServer(interactionCaptor.capture(), anyInt(), anyBoolean());%0A    FixtureInteraction interaction = interactionCaptor.getValue(
+ = captureInteraction(descriptor
 );%0A%0A
@@ -1993,24 +1993,272 @@
 urn(null);%0A%0A
+    FixtureInteraction interaction = captureInteraction(descriptor);%0A%0A    assertNotNull(interaction);%0A    assertEquals(DefaultInteraction.class, interaction.getClass());%0A  %7D%0A%0A  private FixtureInteraction captureInteraction(Descriptor descriptor) %7B%0A
     InProces
@@ -2350,68 +2350,8 @@
 );%0A%0A
-    // Capture the interaction to see if it's the right one%0A
     
@@ -2597,169 +2597,42 @@
     
-FixtureInteraction interaction = interactionCaptor.getValue();%0A%0A    assertNotNull(interaction);%0A    assertEquals(DefaultInteraction.class, interaction.getClass()
+return interactionCaptor.getValue(
 );%0A 

Remove duplication