KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > alert > UnitTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.appserv.management.alert;
24
25 import java.util.StringTokenizer JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import java.util.logging.Level JavaDoc;
28
29
30 /**
31  * This is a Unit Test class provided solely for internal unit testing.
32  *
33  * @AUTHOR: Hemanth Puttaswamy
34  */

35 public class UnitTest extends Thread JavaDoc {
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 JavaDoc unitTestData = null;
43
44     private Object JavaDoc mailAlertOrFilter = null;
45
46     /**
47      * UnitTest class is used to test MailAlert and Filter facility.
48      * unitTestMode can be to test Mail Alert or Mail Filter
49      * unitTestData is a list of NameValue pairs seprated by ';'
50      * mailAlertOrFilter is the instance on which the test needs to be done.
51      */

52     UnitTest( int unitTestMode, String JavaDoc unitTestData, Object JavaDoc mailAlertOrFilter) {
53         this.unitTestMode = unitTestMode;
54         this.unitTestData = unitTestData;
55         this.mailAlertOrFilter = mailAlertOrFilter;
56     }
57
58     public void run( ) {
59         try {
60             // We need to sleep for 1 second to allow the complete
61
// initialization of the object which needs to be tested.
62
sleep( 1000 );
63         } catch( Exception JavaDoc e ) { }
64         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc( 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     /**
81      * Unit Tests Mail Alert class.
82      */

83     private void unitTestMailAlert(StringTokenizer JavaDoc tokenizer) {
84         boolean testStatus = true;
85         MailAlert mailAlert = (MailAlert) mailAlertOrFilter;
86         while( tokenizer.hasMoreTokens( ) ) {
87             String JavaDoc token = tokenizer.nextToken( );
88             StringTokenizer JavaDoc nameAndValue = new StringTokenizer JavaDoc( token, "=" );
89             String JavaDoc name = nameAndValue.nextToken( );
90             String JavaDoc 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 JavaDoc(
116                             mailAlert.getIncludeDiagnostics())).toString() ) )
117                     {
118                         unitTestFailed( name, value,
119                         (new Boolean JavaDoc(
120                             mailAlert.getIncludeDiagnostics())).toString() );
121                         testStatus = false;
122                     }
123                 }
124             }catch( Exception JavaDoc 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 JavaDoc tokenizer) {
138         boolean testStatus = true;
139         MailFilter mailFilter = (MailFilter) mailAlertOrFilter;
140         while( tokenizer.hasMoreTokens( ) ) {
141             String JavaDoc token = tokenizer.nextToken( );
142             StringTokenizer JavaDoc nameAndValue = new StringTokenizer JavaDoc( token, "=" );
143             String JavaDoc name = nameAndValue.nextToken( );
144             String JavaDoc value = nameAndValue.nextToken( );
145             try {
146                 if( name.equals("filterWarningMessages" ) ) {
147                     if( !value.equals(
148                         (new Boolean JavaDoc(
149                             mailFilter.getFilterWarningMessages())).toString()))
150                     {
151                         unitTestFailed( name, value,
152                             (new Boolean JavaDoc(
153                             mailFilter.getFilterWarningMessages())).toString());
154                         testStatus = false;
155                     }
156                 }
157             }catch( Exception JavaDoc 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 JavaDoc propertyName, String JavaDoc expectedValue,
169         String JavaDoc realValue )
170     {
171         System.out.println( "UNIT TEST FAILED FOR ALERTS : " +
172                             "\nPropertyName -> " + propertyName +
173                             "\nexpectedValue -> " + expectedValue +
174                             "\nrealValue -> " + realValue );
175          new RuntimeException JavaDoc( "Unit Test Failed For Alerts.." );
176     }
177 }
178
179
180
181
182
Popular Tags