/*
* Name: StandardExceptionTest
* Author: Richard Rodger
*
* Copyright (c) 2002-2003 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.StandardException;
import org.jostraca.util.PropertySet;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.ArrayList;
/** Test cases for {@see org.jostraca.util.StandardException}
*/
public class StandardExceptionTest extends TestCase {
// Test Framework
public StandardExceptionTest( String pName ) {
super( pName );
}
public static TestSuite suite() {
return new TestSuite( StandardExceptionTest.class );
}
public static void main( String[] pArgs ) {
TestRunner.run( suite() );
}
// test methods
public void testNoArgConstructor() throws Exception {
StandardException se01 = new StandardException();
assertEquals( StandardException.CODE_system, se01.getCode() );
assertEquals( "org.jostraca.util.StandardException: [system/standard] (org.jostraca.util.test.StandardExceptionTest.testNoArgConstructor:64)", se01.toString() );
assertEquals( "[system/standard]", se01.getMessage() );
assertEquals( 2, ((PropertySet)se01.getContextValues()).size() );
assertEquals( "standard", se01.getKey() );
assertEquals( StandardException.Code.CAT_system, se01.getCat() );
}
public void testCodeConstructor() throws Exception {
StandardException se01 = new StandardException( StandardException.CODE_system );
assertEquals( StandardException.CODE_system, se01.getCode() );
assertEquals( 2, ((PropertySet)se01.getContextValues()).size() );
StandardException se02 = new StandardException( StandardException.CODE_user );
assertEquals( StandardException.CODE_user, se02.getCode() );
assertEquals( 2, ((PropertySet)se02.getContextValues()).size() );
}
public void testMsgConstructor() throws Exception {
StandardException se01 = new StandardException( "msg01" );
assertEquals( StandardException.CODE_system, se01.getCode() );
assertEquals( "[system/standard]: msg01", se01.getMessage() );
assertEquals( 2, ((PropertySet)se01.getContextValues()).size() );
StandardException se02 = new StandardException( "msg02" );
assertEquals( StandardException.CODE_system, se02.getCode() );
assertEquals( "[system/standard]: msg02", se02.getMessage() );
assertEquals( 2, ((PropertySet)se02.getContextValues()).size() );
}
public void testCodeMsgConstructor() throws Exception {
StandardException se01 = new StandardException( StandardException.CODE_system, "msg01" );
assertEquals( StandardException.CODE_system, se01.getCode() );
assertEquals( "[system/standard]: msg01", se01.getMessage() );
assertEquals( 2, ((PropertySet)se01.getContextValues()).size() );
StandardException se02 = new StandardException( StandardException.CODE_user, "msg02" );
assertEquals( StandardException.CODE_user, se02.getCode() );
assertEquals( "[user/standard]: msg02", se02.getMessage() );
assertEquals( 2, ((PropertySet)se02.getContextValues()).size() );
}
public void testContextConstructor() throws Exception {
Hashtable ct01 = new Hashtable();
ct01.put( "ct01", "ct01" );
StandardException se01 = new StandardException( ct01 );
assertEquals( StandardException.CODE_system, se01.getCode() );
assertEquals( "[system/standard]: [Context: \nct01=ct01\nmsg=\n]", se01.getMessage() );
assertEquals( 2, ((PropertySet)se01.getContextValues()).size() );
assertEquals( "ct01", ((PropertySet)se01.getContextValues()).get( "ct01" ) );
Hashtable ct02 = new Hashtable();
ct02.put( "ct02", "ct02" );
StandardException se02 = new StandardException( ct02 );
assertEquals( StandardException.CODE_system, se02.getCode() );
assertEquals( "[system/standard]: [Context: \nmsg=\nct02=ct02\n]", se02.getMessage() );
assertEquals( 2, ((PropertySet)se02.getContextValues()).size() );
assertEquals( "ct02", ((PropertySet)se02.getContextValues()).get( "ct02" ) );
StandardException se03 = new StandardException( new String[] {} );
assertEquals( StandardException.CODE_system, se03.getCode() );
assertEquals( "[system/standard]: [Context: \nmsg=\n]", se03.getMessage() );
assertEquals( 0, ((String[])se03.getContext()).length );
assertEquals( 1, ((PropertySet)se03.getContextValues()).size() );
StandardException se04 = new StandardException( new String[] { "n1", "v1" } );
assertEquals( StandardException.CODE_system, se04.getCode() );
assertEquals( "[system/standard]: [Context: \nn1=v1\nmsg=\n]", se04.getMessage() );
assertEquals( 2, ((String[])se04.getContext()).length );
assertEquals( 2, ((PropertySet)se04.getContextValues()).size() );
assertEquals( "v1", ((PropertySet)se04.getContextValues()).get( "n1" ) );
StandardException se05 = new StandardException( new String[] { "n1", "v1", "n2", "v2", "n3" } );
assertEquals( StandardException.CODE_system, se05.getCode() );
assertEquals( "[system/standard]: [Context: \nn3=\nn2=v2\nn1=v1\nmsg=\n]", se05.getMessage() );
assertEquals( 5, ((String[])se05.getContext()).length );
assertEquals( 4, ((PropertySet)se05.getContextValues()).size() );
assertEquals( "v1", ((PropertySet)se05.getContextValues()).get( "n1" ) );
assertEquals( "v2", ((PropertySet)se05.getContextValues()).get( "n2" ) );
assertEquals( "", ((PropertySet)se05.getContextValues()).get( "n3" ) );
HashMap hm = new HashMap();
hm.put("n1","v1");
hm.put("n2","v2");
ArrayList al = new ArrayList();
al.add("a1");
al.add("a2");
StandardException se06 = new StandardException( new Object[] { "hm", hm, "al", al, "s", "string" } );
assertEquals( StandardException.CODE_system, se06.getCode() );
assertEquals( "[system/standard]: [Context: "
+"\ns=string"
+"\nal=[a1, a2]"
+"\nmsg="
+"\nhm={n1=v1, n2=v2}\n]", se06.getMessage() );
assertEquals( 6, ((Object[])se06.getContext()).length );
assertEquals( 4, ((PropertySet)se06.getContextValues()).size() );
assertEquals( "string", ((PropertySet)se06.getContextValues()).get( "s" ) );
assertEquals( "[a1, a2]", ((PropertySet)se06.getContextValues()).get( "al" ) );
assertEquals( "{n1=v1, n2=v2}", ((PropertySet)se06.getContextValues()).get( "hm" ) );
}
}