Package net.raymanoz.command

Source Code of net.raymanoz.command.ListCommandTest

package net.raymanoz.command;

import static org.junit.Assert.assertEquals;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

import net.raymanoz.migrate.Script;
import net.raymanoz.migrate.ScriptList;
import net.raymanoz.ui.UserInteractionStrategy;

import org.junit.Test;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyString;

public class ListCommandTest {

  private final CommandAssembler commandAssembler = mock(CommandAssembler.class);
 
  @Test
  public void shouldReturnCorrectCommandString() {
    assertEquals("Wrong command string", "list", new ListCommand(null)
        .getCommandString());
  }

  private ScriptList mockScripts(final long dbVersion, final String ...scriptNames){
    List<Script> scripts = new ArrayList<Script>();
    long scriptNo = 0L;
    ScriptList result = mock(ScriptList.class);
    for (String scriptName: scriptNames){
      scriptNo++;
      Script script = mock(Script.class);
      when(script.getDBVersion()).thenReturn(dbVersion);
      when(script.getFileName()).thenReturn(scriptName);
      when(script.getPatch()).thenReturn(scriptNo);
     
      when(script.condtionStatus()).thenReturn("Test");
      when(script.execute(any(UserInteractionStrategy.class))).thenThrow(new RuntimeException("TBI"));
      when(script.compareTo(any(Script.class))).thenThrow(new RuntimeException("TBI"));
      scripts.add(script);
      when(result.get((int)scriptNo)).thenReturn(script);
    }
    when(result.iterator()).thenReturn(scripts.iterator());
    when(result.size()).thenReturn(scripts.size());

    when(result.nextScriptName(anyString())).thenThrow(new RuntimeException("TBI"));
    when(result.maxScriptVersion()).thenReturn(scriptNo);
    return result;
  }
 
  private void validateScripts(String expectedMsg, ScriptList list){
    OutputStream stream = new ByteArrayOutputStream();
    System.setOut(new PrintStream(stream));

    when(commandAssembler.buildScriptList(1)).thenReturn(list);

    new ListCommand(commandAssembler).execute();
  
    assertEquals("Incorrect list displayed", expectedMsg, stream.toString());
  }

  final private static String LINE_SEP = System.getProperty("line.separator");
 
  private String expectedOutput(final String ...scriptNames){
    String result = "";
    long scriptNo = 0L;
    for (String scriptName: scriptNames){
      scriptNo++;
      result += String.format("%d - %s%s", scriptNo, scriptName, LINE_SEP);
    }
    return result;
  }
 
  @Test
  public void noListShouldDisplaySuchMessage() {
    final String EXPECTED_OUTPUT = "No migrate actions found" + LINE_SEP;
    validateScripts(EXPECTED_OUTPUT, mockScripts(0));
  }

  @Test
  public void shouldBeAbleToListOneAction() {
    final String Script_1 = "1_create_table.sql";
    final String EXPECTED_OUTPUT = expectedOutput(Script_1);

    validateScripts(EXPECTED_OUTPUT, mockScripts(1, Script_1));
  }

  @Test
  public void shouldBeAbleToListManyActions() {
    final String[] SCRIPT_NAMES = new String[]{"1_create_table.sql","2_add_column.sql"};

    final String EXPECTED_OUTPUT = expectedOutput(SCRIPT_NAMES);

    validateScripts(EXPECTED_OUTPUT, mockScripts(1, SCRIPT_NAMES));

  }

}
TOP

Related Classes of net.raymanoz.command.ListCommandTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.