Package net.raymanoz.config

Source Code of net.raymanoz.config.ConfigurationImplTest

package net.raymanoz.config;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import net.raymanoz.io.File;
import net.raymanoz.ui.GUIInteractionStrategy;
import net.raymanoz.ui.NoUserInputStrategy;
import net.raymanoz.ui.UserInteractionStrategy;
import net.raymanoz.util.DirType;
import net.raymanoz.util.FileUtil;
import net.raymanoz.util.Properties;

import org.junit.Test;

import static net.raymanoz.config.UMigrateProperty.*;

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

public class ConfigurationImplTest {
 
  private final Properties uMigrateproperties = mock(Properties.class);
  private final ApplicationProperties properties = mock(ApplicationProperties.class);
  private final FileUtil fileUtil = mock(FileUtil.class);
  private final File file = mock(File.class);
  private final Configuration config = new ConfigurationImpl(properties, fileUtil, uMigrateproperties);
 
  @Test
  public void shouldReturnCorrectScriptsLocation() {
    final String scriptDir = "scriptDir";
    when(properties.getString(SCRIPTS_DIRECTORY.getKey())).thenReturn(scriptDir);
    when(fileUtil.newFile(scriptDir, DirType.SCRIPT, 1L)).thenReturn(file);
    when(file.exists(true)).thenReturn(true);
    assertThat(config.getScriptDirectory(1), is(equalTo(file)));
  }

  @Test
  public void shouldReturnVerionScriptsLocation() {
    final String scriptDir = "scripts_%03d";
    when(properties.getString(SCRIPTS_DIRECTORY.getKey())).thenReturn(scriptDir);
    when(fileUtil.newFile(String.format(scriptDir, 1), DirType.SCRIPT, 1L)).thenReturn(file);
    when(file.exists(true)).thenReturn(true);
    assertThat(config.getScriptDirectory(1), is(equalTo(file)));
  }

  @Test(expected=IllegalStateException.class)
  public void shouldReturnThrowErrorIfDBVersionLessthan0() {
    final String scriptDir = "scripts_%03d";
    when(properties.getString(SCRIPTS_DIRECTORY.getKey())).thenReturn(scriptDir);
    config.getScriptDirectory(-1);
  }

  @Test(expected=IllegalStateException.class)
  public void shouldReturnThrowErrorIfScriptDirCfgNoIntfmt() {
    final String scriptDir = "scripts";
    when(properties.getString(SCRIPTS_DIRECTORY.getKey())).thenReturn(scriptDir);
    config.getScriptDirectory(2);
  }
 
  @Test
  public void shouldntReturnThrowErrorIfScriptDirCfgNoIntfmtbutVersion1() {
    final String scriptDir = "scripts";
    when(properties.getString(SCRIPTS_DIRECTORY.getKey())).thenReturn(scriptDir);
    when(fileUtil.newFile(String.format(scriptDir, 1), DirType.SCRIPT, 1L)).thenReturn(file);
    when(file.exists(true)).thenReturn(true);
    config.getScriptDirectory(1);
  }

  @Test
  public void shouldReturnTemplateFile() {
    final String expectedFilename = "aFile.name";
    when(properties.getString(TEMPLATE_FILENAME.getKey())).thenReturn(expectedFilename);
    assertThat(config.getTemplateFile().toString(), is(equalTo(expectedFilename)));
  }
 
  @Test(expected=IllegalStateException.class)
  public void illegalStateExceptionShouldBeThrownForNonExistantScriptsDir() {
    final String scriptDir = "scriptDir";
    when(properties.getString(SCRIPTS_DIRECTORY.getKey())).thenReturn(scriptDir);
    when(fileUtil.newFile(String.format(scriptDir, 1), DirType.SCRIPT, 1L)).thenReturn(file);
    when(file.exists(true)).thenReturn(false);
    config.getScriptDirectory(1);
  }
 
  @Test
  public void shouldReturnCorrectExecutionLocation() {
    final String scriptDir = "scriptDir";
    when(properties.getString(EXECUTION_DIRECTORY.getKey())).thenReturn(scriptDir);
    when(fileUtil.newFile(scriptDir, DirType.EXECUTED_SCRIPTS, null)).thenReturn(file);
    when(file.exists(true)).thenReturn(true);
    assertThat(config.getExecuteDirectory(), is(equalTo(file)));
  }
 
  @Test(expected=IllegalStateException.class)
  public void shouldThrowExceptionIfDirectoryDoesNotExist() {
    final String scriptDir = "badScriptDir";
    when(properties.getString(EXECUTION_DIRECTORY.getKey())).thenReturn(scriptDir);
    when(fileUtil.newFile(scriptDir, DirType.EXECUTED_SCRIPTS, null)).thenReturn(file);
    when(file.exists(true)).thenReturn(false);
    config.getExecuteDirectory();
  }
 
  @Test
  public void shouldReturnExecutionString() {
    final String expectedCommand = "something.sh ${script}";
    when(properties.getString(EXECUTION_COMMAND.getKey())).thenReturn(expectedCommand);
    assertThat(config.getExecutionCommand(), is(equalTo(expectedCommand)));
  }
 
 
  @Test
  public void shouldReturnNumberOfDigits() {
    final String expectedDigits = "5";
    when(properties.getString(NUMBER_OF_DIGITS.getKey())).thenReturn("5");
    assertThat(config.getNumberOfDigits(), is(equalTo(Integer.valueOf(expectedDigits))));
  }
 
  @Test
  public void numberOfDigitsShouldBeOneIfNotInFile() {
    when(properties.getString(NUMBER_OF_DIGITS.getKey())).thenReturn(null);
    assertThat( config.getNumberOfDigits(), is(equalTo(1)));
  }
 
  @Test
  public void shouldBeAbleToGetConfirmationMessage() {
    final String expectedMessage = "really?";
    when(properties.getString(CONFIRMATION_MESSAGE.getKey())).thenReturn(expectedMessage);
    UserInteractionStrategy confirmation = config.getUserInteractionStrategy();
    assertThat(confirmation, is(notNullValue()));
    assertThat(confirmation, is(GUIInteractionStrategy.class));
  }
 
  @Test
  public void shouldBeAbleToGetConfirmationMessageEvenIfNotDefined() {
    when(properties.getString(CONFIRMATION_MESSAGE.getKey())).thenReturn(null);
    UserInteractionStrategy confirmation = config.getUserInteractionStrategy();
    assertThat(confirmation, is(notNullValue()));
    assertThat(confirmation, is(NoUserInputStrategy.class));
  }
 
  @Test
  public void shouldBeAbleToGetSchemaVersionTable() {
    final String expectedTableName = "schema_version_test";
    when(properties.getString(SCHEMA_VERSION_TABLE.getKey())).thenReturn(expectedTableName);
    assertThat(config.getSchemaVersionTable(), is(equalTo(expectedTableName)));   
  }

  @Test
  public void shouldBeAbleToGetScriptHistoryTable() {
    final String expectedTableName = "script_history_test";
    when(properties.getString(SCRIPT_HISTORY_TABLE.getKey())).thenReturn(expectedTableName);
    assertThat(config.getScriptHistoryTable(), is(equalTo(expectedTableName)));   
  }

  @Test
  public void getSchemaRepositryRootDirReturnNullIfNotDefined() {
    assertThat(config.getSchemaRepositryRootDir(), is(nullValue()));   
  }
 
  @Test
  public void getSchemaRepositryRootDirReturnFileIfDefined() {
    final String expectedDir = "This is a Dir";
    when(properties.getString(SCHEMA_REPOSITORY_ROOT_DIR.getKey())).thenReturn(expectedDir);
    assertThat(config.getSchemaRepositryRootDir().getName(), is(equalTo(expectedDir)));   
  }
}
TOP

Related Classes of net.raymanoz.config.ConfigurationImplTest

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.