FileDocCategorySizeDatePackage
UnitTest.javaAPI DocGlassfish v2 API8064Fri May 04 22:32:52 BST 2007com.sun.appserv.management.alert

UnitTest

public class UnitTest extends Thread
This is a Unit Test class provided solely for internal unit testing.
AUTHOR:
Hemanth Puttaswamy

Fields Summary
static final int
UNIT_TEST_MAIL_ALERT
static final int
UNIT_TEST_MAIL_FILTER
private final int
unitTestMode
private final String
unitTestData
private final Object
mailAlertOrFilter
Constructors Summary
UnitTest(int unitTestMode, String unitTestData, Object mailAlertOrFilter)
UnitTest class is used to test MailAlert and Filter facility. unitTestMode can be to test Mail Alert or Mail Filter unitTestData is a list of NameValue pairs seprated by ';' mailAlertOrFilter is the instance on which the test needs to be done.


                                                    
           
        this.unitTestMode = unitTestMode;
        this.unitTestData = unitTestData;
        this.mailAlertOrFilter = mailAlertOrFilter;
    
Methods Summary
public voidrun()

        try { 
            // We need to sleep for 1 second to allow the complete 
            // initialization of the object which needs to be tested.
            sleep( 1000 );
        }
        catch( Exception e )
        {
            e.hashCode();  // silence FindBugs
        }
        StringTokenizer tokenizer = new StringTokenizer( unitTestData, ";" );
        switch( unitTestMode ) {
            case UNIT_TEST_MAIL_ALERT:  
                unitTestMailAlert( tokenizer );
                break;
 
            case UNIT_TEST_MAIL_FILTER:
                unitTestMailFilter( tokenizer );
                break;

            default:
                System.out.println( "INVALID OPTION FOR UNIT TEST OF ALERTS");
                break;
         }
    
private voidunitTestFailed(java.lang.String propertyName, java.lang.String expectedValue, java.lang.String realValue)

        final String msg =  "UNIT TEST FAILED FOR ALERTS : " +
                            "\nPropertyName -> " + propertyName +
                            "\nexpectedValue -> " + expectedValue +
                            "\nrealValue -> " + realValue;
        System.out.println( msg );
        throw new RuntimeException( msg );
    
private voidunitTestMailAlert(java.util.StringTokenizer tokenizer)
Unit Tests Mail Alert class.

        boolean testStatus = true;
        MailAlert mailAlert = (MailAlert) mailAlertOrFilter;
        while( tokenizer.hasMoreTokens( ) ) {
            String token = tokenizer.nextToken( );
            StringTokenizer nameAndValue = new StringTokenizer( token, "=" );
            String name = nameAndValue.nextToken( );
            String value = nameAndValue.nextToken( );  
            try {
                if( name.equals("subject" ) ) {
                    if( !value.equals(mailAlert.getSubject() ) ){
                        unitTestFailed( name, value, mailAlert.getSubject( ) );
                        testStatus = false;
                    }
                } else if( name.equals( "recipients" ) ) {
                    if( !value.equals(mailAlert.getRecipients() ) ){
                        unitTestFailed( name, value, mailAlert.getRecipients());
                        testStatus = false;
                    }
                } else if( name.equals( "mailSMTPHost" ) ) {
                    if( !value.equals(mailAlert.getMailSMTPHost() ) ){
                        unitTestFailed( name, value, 
                            mailAlert.getMailSMTPHost());
                        testStatus = false;
                    }
                } else if( name.equals( "fromAddress" ) ) {
                    if( !value.equals(mailAlert.getFromAddress() ) ){
                        unitTestFailed( name, value,mailAlert.getFromAddress());
                        testStatus = false;
                    }
                } else if( name.equals( "includeDiagnostics" ) ) {
                    if( !value.equals(
                        (new Boolean(
                            mailAlert.getIncludeDiagnostics())).toString() ) )
                    {
                        unitTestFailed( name, value, 
                        (new Boolean(
                            mailAlert.getIncludeDiagnostics())).toString() );
                        testStatus = false;
                    }
                }
            }catch( Exception e ) {
                System.out.println( "EXCEPTION IN UNIT TEST FOR MAIL ALERT " +
                    e );
                testStatus = false;
            }
        }
        if( testStatus ) {
            LogDomains.getAlertLogger().log( Level.SEVERE, 
                "Testing SEVERE alert.." );
            System.out.println( "UNIT TEST FOR MAIL ALERT PASSED..." );
        }
    
private voidunitTestMailFilter(java.util.StringTokenizer tokenizer)

        boolean testStatus = true;
        MailFilter mailFilter = (MailFilter) mailAlertOrFilter;
        while( tokenizer.hasMoreTokens( ) ) {
            String token = tokenizer.nextToken( );
            StringTokenizer nameAndValue = new StringTokenizer( token, "=" );
            String name = nameAndValue.nextToken( );
            String value = nameAndValue.nextToken( );  
            try {
                if( name.equals("filterWarningMessages" ) ) {
                    if( !value.equals(
                        (new Boolean(
                            mailFilter.getFilterWarningMessages())).toString()))
                    {
                        unitTestFailed( name, value, 
                            (new Boolean(
                            mailFilter.getFilterWarningMessages())).toString());
                        testStatus = false;
                    }
                } 
            }catch( Exception e ) {
                System.out.println( "EXCEPTION IN UNIT TEST FOR MAIL FILTER " +
                    e );
                testStatus = false;
            }
        }
        if( testStatus ) {
            System.out.println( "UNIT TEST FOR MAIL FILTER PASSED..." );
        }