// Copyright (C) 2003-2009 by Object Mentor, Inc. All rights reserved.
// Released under the terms of the CPL Common Public License version 1.0.
package fitnesseMain;

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

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;

import fitnesse.FitNesse;
import fitnesse.FitNesseContext;
import fitnesse.testutil.FitNesseUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import util.FileUtil;

public class FitNesseMainTest {

  private FitNesseContext context;

  @Before
  public void setUp() throws Exception {
    context = FitNesseUtil.makeTestContext(null, null, "testFitnesseRoot", 80);
  }

  @After
  public void tearDown() throws Exception {
    FileUtil.deleteFileSystemDirectory("testFitnesseRoot");
  }

  @Test
  public void testInstallOnly() throws Exception {
    Arguments args = new Arguments();
    args.setInstallOnly(true);
    FitNesse fitnesse = mock(FitNesse.class);
    FitNesseMain.update(args, fitnesse);
    FitNesseMain.launch(args, context, fitnesse);
    verify(fitnesse, never()).start();
    verify(fitnesse, times(1)).applyUpdates();
  }

  @Test
  public void commandArgCallsExecuteSingleCommand() throws Exception {
    FitNesseMain.dontExitAfterSingleCommand = true;
    Arguments args = new Arguments();
    args.setCommand("command");
    FitNesse fitnesse = mock(FitNesse.class);
    when(fitnesse.start()).thenReturn(true);
    FitNesseMain.update(args, fitnesse);
    FitNesseMain.launch(args, context, fitnesse);
    verify(fitnesse, times(1)).applyUpdates();
    verify(fitnesse, times(1)).start();
    verify(fitnesse, times(1)).executeSingleCommand("command", System.out);
    verify(fitnesse, times(1)).stop();
  }

  @Test
  public void testDirCreations() throws Exception {
    new FitNesse(context);

    assertTrue(new File("testFitnesseRoot").exists());
    assertTrue(new File("testFitnesseRoot/files").exists());
  }

  @Test
  public void testIsRunning() throws Exception {
    context = FitNesseUtil.makeTestContext(null, null, null, FitNesseUtil.PORT);
    FitNesse fitnesse = new FitNesse(context, false);

    assertFalse(fitnesse.isRunning());

    fitnesse.start();
    assertTrue(fitnesse.isRunning());

    fitnesse.stop();
    assertFalse(fitnesse.isRunning());
  }

  @Test
  public void testShouldInitializeFitNesseContext() {
    context = FitNesseUtil.makeTestContext(null, null, null, FitNesseUtil.PORT);
    new FitNesse(context, false);
    assertNotNull(FitNesse.FITNESSE_INSTANCE.getContext());
  }

  @Test
  public void canRunSingleCommand() throws Exception {
    String response = runFitnesseMainWith("-o",  "-c", "/root");
    assertThat(response, containsString("Command Output"));
  }

  @Test
  public void canRunSingleCommandWithAuthentication() throws Exception {
    String response = runFitnesseMainWith("-o", "-a", "user:pwd", "-c", "user:pwd:/FitNesse.ReadProtectedPage");
    assertThat(response, containsString("fitnesse.authentication.OneUserAuthenticator"));
  }

  private String runFitnesseMainWith(String... args) throws Exception {
    FitNesseMain.dontExitAfterSingleCommand = true;
    PrintStream out = System.out;
    ByteArrayOutputStream outputBytes = new ByteArrayOutputStream();
    System.setOut(new PrintStream(outputBytes));
    FitNesseMain.main(args);
    System.setOut(out);
    String response = outputBytes.toString();
    return response;
  }
}

@@ -3015,24 +3015,151 @@
 ctedPage%22);%0A
+    assertTrue(FitNesse.FITNESSE_INSTANCE.getContext().authenticator instanceof fitnesse.authentication.OneUserAuthenticator);%0A
     assertTh
@@ -3232,24 +3232,24 @@
 ticator%22));%0A
-
   %7D%0A%0A  priva
@@ -3381,19 +3381,19 @@
 tStream 
-out
+err
  = Syste
@@ -3394,19 +3394,19 @@
  System.
-out
+err
 ;%0A    By
@@ -3482,19 +3482,19 @@
 stem.set
-Out
+Err
 (new Pri
@@ -3564,15 +3564,15 @@
 .set
-Out(out
+Err(err
 );%0A 

Fix FitNessMain unit test. Stuff is logged on stderr now.