Package org.jostraca.util.test

Source Code of org.jostraca.util.test.PropertySetTest

/*
* Name:    PropertySetTest
* Authors: Richard Rodger
*
* Copyright (c) 2001-2004 Richard Rodger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/


// package
package org.jostraca.util.test;


// import
import org.jostraca.util.PropertySet;
import org.jostraca.util.ValueSet;
import org.jostraca.util.ValueCode;

import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;

import java.util.Hashtable;



/** Test cases for PropertySet
*/
public class PropertySetTest extends TestCase {

  public PropertySetTest( String pName ) {
    super( pName );
  }

  public static TestSuite suite() {
    return new TestSuite( PropertySetTest.class );
  }

  public static void main( String[] pArgs ) {
    TestRunner.run( suite() );
  }



  public void testGeneral() throws Exception {

    String data1 =
      "foo=bar\n"
      +"oof=rab\n"
      +"white=black\n"
      +"good=yes\n"
      +"evil=no\n"
      +"q1 = \"\n"
      +"q2 = \"\"\n"
      +"q3 = \"a\"\n"
      +"q4 = \" a \"\n"
      +"q5 = \"\"\"\n"
      +"s1 = s1\n"
      +"s2 = a$<s1>b\n"
      +"s3 = $<s1>b\n"
      +"s4 = a$<s1>\n"
      +"s5 = $<s1>\n"
      +"s6 = $<s7>\n"
      +"s7 = $<s6>\n"
      +"s8 = s8\n"
      +"s9 = $<s1> $<s8>\n"
      +"s10 = [$<nothing>]\n"
      +"s11 = $<s1>$<s1>\n"
      +"s12 = \\\\[\n"
      +"s13 = $<\\\\s12>\n"
      +"s14 = $<~s12>\n"
      +"s15 = \" a \"\n"
      +"s16 = $< s15>\n"
      +"s17 = $<s14>\n"
      +"s18 = $<s18>a\n"
      +"s19 = a$<s19>b$<foo>c$<s19>d$<s18>\n"
      +"l0 = \n"
      +"l1 = 1\n"
      +"l2 = 1,\n"
      +"l3 = 1, 2\n"
      +"l4 = 1, 2 ,\n"
      +"l5 = ,\n"
      +"l6 = ,1\n"
      +"l7 = ,1,\n"
      +"l8 = ,1,2\n"
      +"l9 = 1, ,3\n"
      +"b0 = b0\n"
      +"b1 = ${b0}\n"
      +"b2 = a${b0}b\n"
      +"b3 = a ${b0} b\n"
      +"b4 = $<b0>${b0}\n"
      ;

    PropertySet ps = new PropertySet();
    ps.parse(data1);

    assertEquals( "\"",        ""+ps.get("q1") );
    assertEquals( "\"\"",     ""+ps.get("q2") );
    assertEquals( "a",         ""+ps.get("q3") );
    assertEquals( " a ",       ""+ps.get("q4") );
    assertEquals( "\"",        ""+ps.get("q5") );

    assertEquals"true",     ""+ps.has("foo") );
    assertEquals"false",    ""+ps.has("red") );
    assertEquals"true",     ""+ps.isYes("good") );
    assertEquals"false",    ""+ps.isNo("good") );
    assertEquals"true",     ""+ps.isNo("evil") );
    assertEquals"bar",      ""+ps.get("foo") );
    assertEquals"",         ""+ps.get("red") );

    assertEquals"s1",           ps.get("s1") );
    assertEquals"as1b",         ps.get("s2") );
    assertEquals"s1b",          ps.get("s3") );
    assertEquals"as1",          ps.get("s4") );
    assertEquals"s1",           ps.get("s5") );
    assertEquals"$<s7>",        ps.get("s6") );
    assertEquals"$<s6>",        ps.get("s7") );
    assertEquals"s8",           ps.get("s8") );
    assertEquals"s1 s8",        ps.get("s9") );
    assertEquals"[$<nothing>]", ps.get("s10") );
    assertEquals"s1s1",         ps.get("s11") );
    assertEquals"\\[",          ps.get("s12") );
    assertEquals"\\\\[",        ps.get("s13") );
    assertEquals"[",            ps.get("s14") );
    assertEquals" a ",          ps.get("s15") );
    assertEquals"a",            ps.get("s16") );
    assertEquals"[",            ps.get("s17") );
    assertEquals"$<s18>a",      ps.get("s18") );
    assertEquals"a$<s19>bbarc$<s19>d$<s18>a", ps.get("s19") );

    assertEquals"0", ""+ps.getList("l0",",").length );
    assertEquals"1", ""+ps.getList("l1",",")[0] );
    assertEquals"1", ""+ps.getList("l2",",")[0] );
    assertEquals"1", ""+ps.getList("l3",",")[0] );
    assertEquals"2", ""+ps.getList("l3",",")[1] );
    assertEquals"1", ""+ps.getList("l4",",")[0] );
    assertEquals"2", ""+ps.getList("l4",",")[1] );
    assertEquals"0", ""+ps.getList("l5",",").length );
    assertEquals"1", ""+ps.getList("l6",",")[0] );
    assertEquals"1", ""+ps.getList("l7",",")[0] );
    assertEquals"1", ""+ps.getList("l8",",")[0] );
    assertEquals"2", ""+ps.getList("l8",",")[1] );
    assertEquals"1", ""+ps.getList("l9",",")[0] );
    assertEquals"3", ""+ps.getList("l9",",")[1] );

    String data2 =
      "foo=newfoo\n"
      +"red=blue";
    PropertySet ps2 = new PropertySet();
    ps2.parse( data2 );
    ps.overrideWith( ps2 );

    assertEquals"newfoo",   ""+ps.get("foo") );
    assertEquals"blue",     ""+ps.get("red") );
    assertEquals"black",    ""+ps.get("white") );

    assertEquals"b0",     ""+ps.get("b0") );
    assertEquals"b0",     ""+ps.get("b1") );
    assertEquals"ab0b",   ""+ps.get("b2") );
    assertEquals"a b0 b", ""+ps.get("b3") );
    assertEquals"b0b0",   ""+ps.get("b4") );

  }

 

  public void testCreate() {
   
    Hashtable h = new Hashtable();
    h.put( "n1", "v1" );
    h.put( "n2", "v2" );

    PropertySet ps = new PropertySet( h );
    assertEquals( "v1", ps.get("n1") );
    assertEquals( "v2", ps.get("n2") );


    String[] nvp = new String[] { "n1", "v1", "n2", "v2" };
    ps = new PropertySet( nvp );
    assertEquals( "v1", ps.get("n1") );
    assertEquals( "v2", ps.get("n2") );
  }


  public void testInsertions() {
   
    ValueSet vs      = new ValueSet( ValueCode.VALUE, "v", ValueCode.FILE, "f" );
    String   inserts = vs.insertValues( "value: $<"+ValueCode.VALUE+"> file: $<"+ValueCode.FILE+">" );

    assertEquals( "v", vs.get( ValueCode.VALUE ) );
    assertEquals( "f", vs.get( ValueCode.FILE ) );
    assertEquals( "value: v file: f", inserts );
  }


  public void testOverrideAndInherit() {
    PropertySet a = new PropertySet();
    a.set( "n1", "a1"      );
    a.set( "n2", "$<n1>"   );
    a.set( "n3", "$<n3>"   );
    a.set( "n4", "x$<n1>y" );
    a.set( "n5", "x$<n3>y" );
    a.set( "n7", "a7"      );

    assertEquals( "a1",      a.get( "n1" ) );
    assertEquals( "a1",      a.get( "n2" ) );
    assertEquals( "$<n3>",   a.get( "n3" ) );
    assertEquals( "xa1y",    a.get( "n4" ) );
    assertEquals( "x$<n3>y", a.get( "n5" ) );
    assertEquals( "a7",      a.get( "n7" ) );


    PropertySet b = new PropertySet();
    b.set( "n1", "b1" );
    b.set( "n2", "b2" );
    b.set( "n3", "b3" );
    b.set( "n6", "b6" );
    b.set( "n7", "x$<n7>y" );

    PropertySet bo = (PropertySet) b.clone();
    bo.overrideWith( a );

    System.out.println( bo );

    assertEquals( "a1",      bo.getSourceValue( "n1" ) );
    assertEquals( "a1",      bo.get( "n1" ) );

    assertEquals( "$<n1>",   bo.getSourceValue( "n2" ) );
    assertEquals( "a1",      bo.get( "n2" ) );

    assertEquals( "b3",      bo.getSourceValue( "n3" ) );
    assertEquals( "b3",      bo.get( "n3" ) );

    assertEquals( "x$<n1>y", bo.getSourceValue( "n4" ) );
    assertEquals( "xa1y",    bo.get( "n4" ) );

    assertEquals( "x$<n3>y", bo.getSourceValue( "n5" ) );
    assertEquals( "xb3y",    bo.get( "n5" ) );

    assertEquals( "b6",      bo.getSourceValue( "n6" ) );
    assertEquals( "b6",      bo.get( "n6" ) );

    assertEquals( "a7",      bo.getSourceValue( "n7" ) );
    assertEquals( "a7",      bo.get( "n7" ) );


    PropertySet bi = (PropertySet) b.clone();
    bi.inheritFrom( a );


    assertEquals( "b1",      bi.getSourceValue( "n1" ) );
    assertEquals( "b1",      bi.get( "n1" ) );

    assertEquals( "b2",      bi.getSourceValue( "n2" ) );
    assertEquals( "b2",      bi.get( "n2" ) );

    assertEquals( "b3",      bi.getSourceValue( "n3" ) );
    assertEquals( "b3",      bi.get( "n3" ) );

    assertEquals( "x$<n1>y", bi.getSourceValue( "n4" ) );
    assertEquals( "xb1y",    bi.get( "n4" ) );

    assertEquals( "x$<n3>y", bi.getSourceValue( "n5" ) );
    assertEquals( "xb3y",    bi.get( "n5" ) );

    assertEquals( "b6",      bi.getSourceValue( "n6" ) );
    assertEquals( "b6",      bi.get( "n6" ) );

    assertEquals( "xa7y",    bi.getSourceValue( "n7" ) );
    assertEquals( "xa7y",    bi.get( "n7" ) );

  }

}




TOP

Related Classes of org.jostraca.util.test.PropertySetTest

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.