Package org.jostraca.directive.test

Source Code of org.jostraca.directive.test.DirectiveParserTest

/*
* Name:    DirectiveParserTest
* Author:  Richard Rodger
* Release: 0.3
*
* Copyright (c) 2002 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.directive.test;


/* Import << */
import org.jostraca.directive.DirectiveParser;
import org.jostraca.directive.DirectiveException;

import org.jostraca.util.StandardException;

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

import java.util.Vector;
import java.util.Hashtable;
/* Import >> */


/** <b>Description:</b><br>
*  Test cases for Pair.
*/
public class DirectiveParserTest extends TestCase {

  // Test Framework

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

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

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






  // tests

  public void testParse() throws Exception {
    DirectiveParser dp = new DirectiveParser();

    Vector unamednames = new Vector();
    unamednames.addElement( "arg1" );
    unamednames.addElement( "arg2" );
   
    Hashtable nameddefaults = new Hashtable();
    nameddefaults.put( "arg1", "v1" );
    nameddefaults.put( "arg2", "v2" );

    Hashtable ca;

    dp.parse( " @blah arg1=\"a 'x' \\\" \\\\ \\\" \" " );
    //System.out.println( dp );
    assertEquals( "blah", dp.getName() );
    ca = new Hashtable(); ca.put( "arg1", "a 'x' \" \\ \" " ); ca.put( "arg2", "v2" );
    assertEquals( ca.get("arg1"), dp.getCanonicalArgs( unamednames, nameddefaults ).get( "arg1" ) );
    assertEquals( ca.get("arg2"), dp.getCanonicalArgs( unamednames, nameddefaults ).get( "arg2" ) );


    dp.parse( " @blah  arg1='a \"x\" \\' \\\\ \\'' " );
    //System.out.println( dp );
    assertEquals( "blah", dp.getName() );
    ca = new Hashtable(); ca.put( "arg1", "a \"x\" ' \\ '" ); ca.put( "arg2", "v2" );
    assertEquals( ca.get("arg1"), dp.getCanonicalArgs( unamednames, nameddefaults ).get( "arg1" ) );
    assertEquals( ca.get("arg2"), dp.getCanonicalArgs( unamednames, nameddefaults ).get( "arg2" ) );


    dp.parse( "@ blah  un arg2=ns arg2= 02 " );
    //System.out.println( dp );
    assertEquals( "blah", dp.getName() );
    ca = new Hashtable(); ca.put( "arg1", "un" ); ca.put( "arg2", "02" );
    assertEquals( ca.get("arg1"), dp.getCanonicalArgs( unamednames, nameddefaults ).get( "arg1" ) );
    assertEquals( ca.get("arg2"), dp.getCanonicalArgs( unamednames, nameddefaults ).get( "arg2" ) );

    dp.parse( " un arg2=ns arg2= 02 " );
    //System.out.println( dp );
    assertEquals( "", dp.getName() );
    ca = new Hashtable(); ca.put( "arg1", "un" ); ca.put( "arg2", "02" );
    assertEquals( ca.get("arg1"), dp.getCanonicalArgs( unamednames, nameddefaults ).get( "arg1" ) );
    assertEquals( ca.get("arg2"), dp.getCanonicalArgs( unamednames, nameddefaults ).get( "arg2" ) );


    dp.parse( " @ blah 'u n 01' un02 arg2=' 02 '" );
    //System.out.println( dp );
    assertEquals( "blah", dp.getName() );
    ca = new Hashtable(); ca.put( "arg1", "u n 01" ); ca.put( "arg2", " 02 " );
    assertEquals( ca.get("arg1"), dp.getCanonicalArgs( unamednames, nameddefaults ).get( "arg1" ) );
    assertEquals( ca.get("arg2"), dp.getCanonicalArgs( unamednames, nameddefaults ).get( "arg2" ) );

    dp.parse( " 'u n 01' un02 arg2=' 02 '" );
    //System.out.println( dp );
    assertEquals( "", dp.getName() );
    ca = new Hashtable(); ca.put( "arg1", "u n 01" ); ca.put( "arg2", " 02 " );
    assertEquals( ca.get("arg1"), dp.getCanonicalArgs( unamednames, nameddefaults ).get( "arg1" ) );
    assertEquals( ca.get("arg2"), dp.getCanonicalArgs( unamednames, nameddefaults ).get( "arg2" ) );


    dp.parse( "@blah not_an_id not_an_arg()=all_the_one " );
    //System.out.println( dp );
    assertEquals( "blah", dp.getName() );
    ca = new Hashtable(); ca.put( "arg1", "not_an_id" ); ca.put( "arg2", "not_an_arg()=all_the_one" );
    assertEquals( ca.get("arg1"), dp.getCanonicalArgs( unamednames, nameddefaults ).get( "arg1" ) );
    assertEquals( ca.get("arg2"), dp.getCanonicalArgs( unamednames, nameddefaults ).get( "arg2" ) );


    dp.parse( "@blah @not_a_dname ignored_arg=all_the_one arg2=@av2" );
    //System.out.println( dp );
    assertEquals( "blah", dp.getName() );
    ca = new Hashtable(); ca.put( "arg1", "@not_a_dname" ); ca.put( "arg2", "@av2" );
    assertEquals( ca.get("arg1"), dp.getCanonicalArgs( unamednames, nameddefaults ).get( "arg1" ) );
    assertEquals( ca.get("arg2"), dp.getCanonicalArgs( unamednames, nameddefaults ).get( "arg2" ) );

    // for argvalue with trailing =, you must quote
    dp.parse( "@foo-bar_ =av1 arg2='av2='" );
    //System.out.println( dp );
    assertEquals( "foo-bar_", dp.getName() );
    ca = new Hashtable(); ca.put( "arg1", "=av1" ); ca.put( "arg2", "av2=" );
    assertEquals( ca.get("arg1"), dp.getCanonicalArgs( unamednames, nameddefaults ).get( "arg1" ) );
    assertEquals( ca.get("arg2"), dp.getCanonicalArgs( unamednames, nameddefaults ).get( "arg2" ) );

    dp.parse( "@foo-bar_ =av1 arg2='av2='" );
    //System.out.println( dp );
    assertEquals( "foo-bar_", dp.getName() );
    ca = new Hashtable(); ca.put( "arg1", "=av1" ); ca.put( "arg2", "av2=" );
    assertEquals( ca.get("arg1"), dp.getCanonicalArgs( unamednames, nameddefaults ).get( "arg1" ) );
    assertEquals( ca.get("arg2"), dp.getCanonicalArgs( unamednames, nameddefaults ).get( "arg2" ) );


    dp.parse( "@foo/bar un/01 /un02/ " );
    //System.out.println( dp );
    assertEquals( "foo", dp.getName() );
    assertEquals( "foo/bar", dp.getFullName() );
    assertEquals( "foo", dp.getNameElements()[0] );
    assertEquals( "bar", dp.getNameElements()[1] );
    ca = new Hashtable(); ca.put( "arg1", "un/01" ); ca.put( "arg2", "/un02/" );
    assertEquals( ca.get("arg1"), dp.getCanonicalArgs( unamednames, nameddefaults ).get( "arg1" ) );
    assertEquals( ca.get("arg2"), dp.getCanonicalArgs( unamednames, nameddefaults ).get( "arg2" ) );

  }


  public void testSyntaxErrors() throws Exception {
    DirectiveParser dp = new DirectiveParser();     
     
    try {
      dp.parse( " 'a\\a' " );
    }
    catch( DirectiveException de ) {
      assertEquals( DirectiveException.CODE_parse, de.getCode() );
      assertEquals( StandardException.Code.CAT_user, de.getCat() );
      //System.out.println( de );
    }
  }


}
TOP

Related Classes of org.jostraca.directive.test.DirectiveParserTest

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.