1 23 package com.sun.appserv.management.alert; 24 25 import java.util.StringTokenizer ; 26 import java.util.logging.Logger ; 27 import java.util.logging.Level ; 28 29 30 35 public class UnitTest extends Thread { 36 static final int UNIT_TEST_MAIL_ALERT = 1; 37 38 static final int UNIT_TEST_MAIL_FILTER = 2; 39 40 private int unitTestMode = 0; 41 42 private String unitTestData = null; 43 44 private Object mailAlertOrFilter = null; 45 46 52 UnitTest( int unitTestMode, String unitTestData, Object mailAlertOrFilter) { 53 this.unitTestMode = unitTestMode; 54 this.unitTestData = unitTestData; 55 this.mailAlertOrFilter = mailAlertOrFilter; 56 } 57 58 public void run( ) { 59 try { 60 sleep( 1000 ); 63 } catch( Exception e ) { } 64 StringTokenizer tokenizer = new StringTokenizer ( unitTestData, ";" ); 65 switch( unitTestMode ) { 66 case UNIT_TEST_MAIL_ALERT: 67 unitTestMailAlert( tokenizer ); 68 break; 69 70 case UNIT_TEST_MAIL_FILTER: 71 unitTestMailFilter( tokenizer ); 72 break; 73 74 default: 75 System.out.println( "INVALID OPTION FOR UNIT TEST OF ALERTS"); 76 break; 77 } 78 } 79 80 83 private void unitTestMailAlert(StringTokenizer tokenizer) { 84 boolean testStatus = true; 85 MailAlert mailAlert = (MailAlert) mailAlertOrFilter; 86 while( tokenizer.hasMoreTokens( ) ) { 87 String token = tokenizer.nextToken( ); 88 StringTokenizer nameAndValue = new StringTokenizer ( token, "=" ); 89 String name = nameAndValue.nextToken( ); 90 String value = nameAndValue.nextToken( ); 91 try { 92 if( name.equals("subject" ) ) { 93 if( !value.equals(mailAlert.getSubject() ) ){ 94 unitTestFailed( name, value, mailAlert.getSubject( ) ); 95 testStatus = false; 96 } 97 } else if( name.equals( "recipients" ) ) { 98 if( !value.equals(mailAlert.getRecipients() ) ){ 99 unitTestFailed( name, value, mailAlert.getRecipients()); 100 testStatus = false; 101 } 102 } else if( name.equals( "mailSMTPHost" ) ) { 103 if( !value.equals(mailAlert.getMailSMTPHost() ) ){ 104 unitTestFailed( name, value, 105 mailAlert.getMailSMTPHost()); 106 testStatus = false; 107 } 108 } else if( name.equals( "fromAddress" ) ) { 109 if( !value.equals(mailAlert.getFromAddress() ) ){ 110 unitTestFailed( name, value,mailAlert.getFromAddress()); 111 testStatus = false; 112 } 113 } else if( name.equals( "includeDiagnostics" ) ) { 114 if( !value.equals( 115 (new Boolean ( 116 mailAlert.getIncludeDiagnostics())).toString() ) ) 117 { 118 unitTestFailed( name, value, 119 (new Boolean ( 120 mailAlert.getIncludeDiagnostics())).toString() ); 121 testStatus = false; 122 } 123 } 124 }catch( Exception e ) { 125 System.out.println( "EXCEPTION IN UNIT TEST FOR MAIL ALERT " + 126 e ); 127 testStatus = false; 128 } 129 } 130 if( testStatus ) { 131 LogDomains.getAlertLogger().log( Level.SEVERE, 132 "Testing SEVERE alert.." ); 133 System.out.println( "UNIT TEST FOR MAIL ALERT PASSED..." ); 134 } 135 } 136 137 private void unitTestMailFilter(StringTokenizer tokenizer) { 138 boolean testStatus = true; 139 MailFilter mailFilter = (MailFilter) mailAlertOrFilter; 140 while( tokenizer.hasMoreTokens( ) ) { 141 String token = tokenizer.nextToken( ); 142 StringTokenizer nameAndValue = new StringTokenizer ( token, "=" ); 143 String name = nameAndValue.nextToken( ); 144 String value = nameAndValue.nextToken( ); 145 try { 146 if( name.equals("filterWarningMessages" ) ) { 147 if( !value.equals( 148 (new Boolean ( 149 mailFilter.getFilterWarningMessages())).toString())) 150 { 151 unitTestFailed( name, value, 152 (new Boolean ( 153 mailFilter.getFilterWarningMessages())).toString()); 154 testStatus = false; 155 } 156 } 157 }catch( Exception e ) { 158 System.out.println( "EXCEPTION IN UNIT TEST FOR MAIL FILTER " + 159 e ); 160 testStatus = false; 161 } 162 } 163 if( testStatus ) { 164 System.out.println( "UNIT TEST FOR MAIL FILTER PASSED..." ); 165 } 166 } 167 168 private void unitTestFailed( String propertyName, String expectedValue, 169 String realValue ) 170 { 171 System.out.println( "UNIT TEST FAILED FOR ALERTS : " + 172 "\nPropertyName -> " + propertyName + 173 "\nexpectedValue -> " + expectedValue + 174 "\nrealValue -> " + realValue ); 175 new RuntimeException ( "Unit Test Failed For Alerts.." ); 176 } 177 } 178 179 180 181 182 | Popular Tags |