Package net.raymanoz.config

Source Code of net.raymanoz.config.VariableEvaluaterTest

package net.raymanoz.config;

import static org.junit.Assert.*;

import java.io.IOException;
import java.util.Set;

import net.raymanoz.util.PlainProperties;
import net.raymanoz.util.Properties;

import org.junit.Test;


public class VariableEvaluaterTest {

  @Test
  public void shouldIgnore(){
    assertTrue(VariableEvaluaterImpl.shouldIgnore(null));
    assertFalse(VariableEvaluaterImpl.shouldIgnore(VariableEvaluaterImpl.ASK_PROPERTY_VALUE + "x"));
    assertTrue(VariableEvaluaterImpl.shouldIgnore(VariableEvaluaterImpl.ASK_PROPERTY_VALUE));
    assertTrue(VariableEvaluaterImpl.shouldIgnore(VariableEvaluaterImpl.ASK_PROPERTY_VALUE.toLowerCase()));
    assertFalse(VariableEvaluaterImpl.shouldIgnore("var"));
    assertFalse(VariableEvaluaterImpl.shouldIgnore(" "));
  }
 
  @Test
  public void shouldBeAbleToDetectAStringWithAVariable() {
    assertVariable("Hello ${name}", "name");
  }
 
  @Test
  public void shouldBeAbleToDetectAStringWithoutAVariable() {
    assertVariable("boring", NoVariable.NAME);
 
 
  @Test
  public void shouldNotDetectEmptyVarAsAVar() {
    assertVariable("test ${}", NoVariable.NAME);
  }
 
  @Test
  public void shouldDetectAVaraibleEvenForMultipleVars() {
    assertVariable("Test ${var} ${var2}", "var");
  }
 
  @Test
  public void shouldDetectAVaraibleEvenIfInAVariable() {
    assertVariable("Test ${var1${var2}}", "var2");
  }
 
  @Test
  public void shouldDetectAVariablesWithNumbersAndUnderscores() {
    assertVariable("${_test}", "_test");
    assertVariable("${1}", "1");
    assertVariable("${1_a_4}", "1_a_4");
  }
 
  @Test
  public void shouldBeAbleToReplaceAVariable() {
    Variable variable = new DollarBracketVariable("${name}");
    variable.setValue("bob");
    assertEquals("Variable was not replaced correctly", "hello bob",
        VariableEvaluaterImpl.replaceVariable("hello ${name}", variable));
  }
 
  @Test
  public void shouldBeAbleToReplaceMultipleVariables() {
    Variable variable = new DollarBracketVariable("${name}");
    variable.setValue("bob");   
    assertEquals("Variables were not replaced correctly", "hello bob bob",
        VariableEvaluaterImpl.replaceVariable("hello ${name} ${name}", variable));
  }
 
  @Test
  public void shouldReplaceTheCorrectVariable() {
    Variable variable = new DollarBracketVariable("${var2}");
    variable.setValue("hi");   
    assertEquals("Variables were not replaced correctly", "${var1} hi",
        VariableEvaluaterImpl.replaceVariable("${var1} ${var2}", variable));
  }
 
  @Test
  public void shouldNotReplaceVariablesNotInProperties() throws IOException {
    String findVariable = "findMe";
    Properties properties = new PlainProperties(findVariable + "=replaced");
    VariableEvaluaterImpl evaluater  = new VariableEvaluaterImpl(properties);
    assertEquals("Variables were not replaced correctly", "${ignoreMe} replaced", evaluater.replaceAllVariables("${ignoreMe} ${findMe}"));
  }
 
  @Test
  public void shouldBeAbleToFindAVariableWithADotInIt() {
    VariableEvaluaterImpl evaluater = new VariableEvaluaterImpl(new PlainProperties(""));
    Variable variable = evaluater.nextVariable("find this variable: ${script.name}");
    assertNotNull("Varaible could not be found", variable);
  }
 
  @Test
  public void shouldBeAbleToReplaceAVariableWithADotInIt() throws IOException {
    String findVariable = "script.name";
    Properties properties = new PlainProperties(findVariable + "=doit.sql");
    VariableEvaluaterImpl evaluater = new VariableEvaluaterImpl(properties);
    assertEquals("Variables were not replaced correctly", "find this variable: doit.sql",
        evaluater.replaceAllVariables("find this variable: ${" + findVariable + "}"));
  }

  @Test
  public void shouldHandleBackSlashInVariable() throws IOException {
    String findVariable = "sql";
    Properties properties = new PlainProperties(findVariable + "='\\\\'");
    assertEquals("'\\'", properties.getProperty(findVariable));
    VariableEvaluaterImpl evaluater = new VariableEvaluaterImpl(properties);
    assertEquals("Variables were not replaced correctly", "find this variable: '\\'",
        evaluater.replaceAllVariables("find this variable: ${" + findVariable + "}"));
  }
 
  @Test
  public void shouldHandleEscapedDollarInVariable() throws IOException {
    String findVariable = "sql";
    Properties properties = new PlainProperties(findVariable + "=\\$");
    VariableEvaluaterImpl evaluater = new VariableEvaluaterImpl(properties);
    assertEquals("Variables were not replaced correctly", "find this variable: $",
        evaluater.replaceAllVariables("find this variable: ${" + findVariable + "}"));
  }
 
  @Test
  public void shouldHandleNullString() {
    assertNull("Should return null", new VariableEvaluaterImpl(new PlainProperties("a=b")).replaceAllVariables(null));
  }
 
  private void assertVariable(String value, String variable) {
    assertEquals("Evaluater should find variable " + variable, variable,
        new VariableEvaluaterImpl(new PlainProperties("")).nextVariable(value).getName());
  }

  @Test
  public void findVariablesRequiringDialog() {
    final PlainProperties properties = new PlainProperties("a=b\nc=<ASK>");
    final VariableEvaluaterImpl variableEvaluaterImpl = new VariableEvaluaterImpl(properties);
    Set<String> vars = variableEvaluaterImpl.findVariablesRequiringDialog("${a} ${c} ${d} ${a}");
    assertNotNull(vars);
    assertEquals(2, vars.size());
    assertTrue(vars.contains("c"));
    assertTrue(vars.contains("d"));
  }
 
  @Test
  public void findVariablesRequiringDialogHandlesNull() {
    final PlainProperties properties = new PlainProperties("a=b\nc=<ASK>");
    final VariableEvaluaterImpl variableEvaluaterImpl = new VariableEvaluaterImpl(properties);
    Set<String> vars = variableEvaluaterImpl.findVariablesRequiringDialog(null);
    assertNotNull(vars);
    assertEquals(0, vars.size());
  }
}
TOP

Related Classes of net.raymanoz.config.VariableEvaluaterTest

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.