Package org.jostraca.util.test

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

/*
* Name:    OrderedPropertySetManagerTest
* 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.Tools;

import org.jostraca.util.PropertySet;
import org.jostraca.util.PropertySetManager;
import org.jostraca.util.OrderedPropertySetManager;

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

import java.io.File;

import java.util.Hashtable;
import java.util.Vector;


/** Test cases for PropertySetManager
*/
public class OrderedPropertySetManagerTest extends TestCase {

  // text framework

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

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

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



  // test methods

  public void testConstructor() {
    Hashtable defps = new Hashtable();
    Vector    names = new Vector();

    try {
      OrderedPropertySetManager opsm = new OrderedPropertySetManager( null, null );
      fail();
    } catch( Exception iae ) {}
    try {
      OrderedPropertySetManager opsm = new OrderedPropertySetManager( null, defps );
      fail();
    } catch( Exception iae ) {}
    try {
      OrderedPropertySetManager opsm = new OrderedPropertySetManager( names, null );
      fail();
    } catch( Exception iae ) {}

    OrderedPropertySetManager opsm01 = new OrderedPropertySetManager();
    OrderedPropertySetManager opsm02 = new OrderedPropertySetManager( names, defps );


    PropertySet ps01 = new PropertySet();
    defps.put( "ps01", ps01 );
    names.add( "ps01" );
    OrderedPropertySetManager opsm03 = new OrderedPropertySetManager( names, defps );

    defps.put( "ps02", new Object() );
    try {
      OrderedPropertySetManager opsm04 = new OrderedPropertySetManager( names, defps );
      fail();
    } catch( Exception iae ) {}

    defps.put( "ps02", new PropertySet() );
    names.add( "ps02" );
    OrderedPropertySetManager opsm05 = new OrderedPropertySetManager( names, defps );

    defps.remove( "ps02" );

    try {
      OrderedPropertySetManager opsm06 = new OrderedPropertySetManager( names, defps );
      fail();
    } catch( Exception iae ) {}

    names.remove( "ps02" );
    OrderedPropertySetManager opsm07 = new OrderedPropertySetManager( names, defps );

    names.add( new Object() );
    try {
      OrderedPropertySetManager opsm06 = new OrderedPropertySetManager( names, defps );
      fail();
    } catch( Exception iae ) {}
  }


  public void testAdd() {
    OrderedPropertySetManager opsm01 = new OrderedPropertySetManager( new Vector(), new Hashtable() );
    opsm01.add( "foo", new PropertySet() );
    opsm01.add( "bar", new PropertySet() );
    opsm01.put( "foo", new PropertySet() );
  }


  public void testRemove() {
    OrderedPropertySetManager opsm01 = new OrderedPropertySetManager( new Vector(), new Hashtable() );
    try {
      opsm01.remove( "foo" );
      fail();
    } catch( Exception uoe ) {}
  }


  public void testPut() {
    Vector names = new Vector();
    names.add( "ps01" );
    names.add( "ps02" );

    Hashtable defps = new Hashtable();
    defps.put( "ps01", new PropertySet() );
    defps.put( "ps02", new PropertySet() );

    OrderedPropertySetManager opsm01 = new OrderedPropertySetManager( names, defps );
    opsm01.put( "ps01", new PropertySet() );

    try {
      opsm01.put( "ps02", null );
      fail();
    } catch( Exception iae ) {}

    try {
      opsm01.put( "ps03", new PropertySet() );
      fail();
    } catch( Exception ise ) {}
  }


  public void testGet() {
    Vector names = new Vector();
    names.add( "ps01" );
    names.add( "ps02" );

    Hashtable defps = new Hashtable();
    PropertySet ps01 = new PropertySet();
    PropertySet ps02 = new PropertySet();

    defps.put( "ps01", ps01 );
    defps.put( "ps02", ps02 );

    OrderedPropertySetManager opsm01 = new OrderedPropertySetManager( names, defps );
    assertEquals( ps01, opsm01.get( "ps01" ) );
    assertEquals( ps02, opsm01.get( "ps02" ) );

    try {
      opsm01.get( "ps03" );
      fail();
    } catch( Exception ise ) {}
  }


  public void testMerge() {
    Vector names = new Vector();
    names.add( "ps01" );
    names.add( "ps02" );
    names.add( "ps03" );

    Hashtable defps = new Hashtable();
    PropertySet ps01 = new PropertySet();
    PropertySet ps02 = new PropertySet();
    PropertySet ps03 = new PropertySet();

    ps01.set( "a", "a01" );
    ps01.set( "b", "b01" );

    ps02.set( "b", "b02" );
    ps02.set( "c", "c02" );

    ps03.set( "c", "c03" );
    ps03.set( "d", "d03" );

    defps.put( "ps01", ps01 );
    defps.put( "ps02", ps02 );
    defps.put( "ps03", ps03 );

    OrderedPropertySetManager opsm01 = new OrderedPropertySetManager( names, defps );
    // also test cloning
    ps01.set( "a", "-" );
    ps01.set( "b", "-" );
    ps01.set( "c", "-" );

    PropertySet ops = opsm01.merge();
    assertEquals( "a01", ops.get("a") );
    assertEquals( "b02", ops.get("b") );
    assertEquals( "c03", ops.get("c") );
    assertEquals( "d03", ops.get("d") );
  }


  public void testLoad() throws Exception {
    Vector names = new Vector();
    names.addElement( "test01" );
    names.addElement( "test02" );
    Hashtable psdefs = new Hashtable();
    PropertySet ps01 = new PropertySet();
    ps01.set( "default01", "true" );
    PropertySet ps02 = new PropertySet();
    ps02.set( "default02", "true" );
    psdefs.put( "test01", ps01 );
    psdefs.put( "test02", ps02 );

    OrderedPropertySetManager opsm01 = new OrderedPropertySetManager( names, psdefs );
   
    PropertySet ops01 = opsm01.load( "test01", Tools.findRelativeSystemPath("src/org/jostraca/util/test/test.conf") );
    assertEquals( "0", ops01.get("a") );
    assertEquals( "1", ops01.get("b") );
    assertEquals( "",  ops01.get("default01") );

    try {
      PropertySet ops02 = opsm01.load( "test02", new File("no-test.conf") );
      fail();
    } catch( Exception ioe ) {}
    try {
      PropertySet ops02 = opsm01.load( "test02", new File("no-test.conf"), PropertySetManager.FILE_MUST_EXIST );
      fail();
    } catch( Exception ioe ) {}


    try {
      PropertySet ops02 = opsm01.load( "test02", new File("no-test.conf"), PropertySetManager.USE_DEFAULT_IF_FILE_DOES_NOT_EXIST );
      assertEquals( "true",  ops02.get("default02") );
    } catch( Exception ioe ) { fail(); }
   

    /*
    try {
      psm01.load( "test02", new File("notest.conf") );
      fail();
    } catch( IOException ioe ) {}

    try {
      psm01.load( "test02", new File("notest.conf"), PropertySetManager.FILE_MUST_EXIST );
      fail();
    } catch( IOException ioe ) {}

    try {
      PropertySet ps01o = psm01.load( "test01", new File("notest.conf"), PropertySetManager.USE_DEFAULT_IF_FILE_DOES_NOT_EXIST );
      assertEquals( ps01o, ps01 );
    } catch( IOException ioe ) { fail(); }
    */
  }

}




TOP

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

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.