KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > bsf > listOfValues > TestLovService


1 /**
2  * Copyright (c) 2002 Bright Side Factory. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * 3. The end-user documentation included with the redistribution,
17  * if any, must include the following acknowledgment:
18  * "This product includes software developed by the
19  * Bright Side Factory (http://www.bs-factory.org/)."
20  * Alternately, this acknowledgment may appear in the software itself,
21  * if and wherever such third-party acknowledgments normally appear.
22  *
23  * 4. The names "Bright Side", "BS Factory" and "Bright Side Factory" must
24  * not be used to endorse or promote products derived from this
25  * software without prior written permission. For written
26  * permission, please contact info@bs-factory.org.
27  *
28  * 5. Products derived from this software may not be called "Bright Side",
29  * nor may "Bright Side" appear in their name, without prior written
30  * permission of the Apache Software Foundation.
31  *
32  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
33  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
36  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
39  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
41  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
42  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43  * SUCH DAMAGE.
44  * ====================================================================
45  *
46  * This software consists of voluntary contributions made by many
47  * individuals on behalf of the Bright Side Factory. For more
48  * information on the Bright Side Factory, please see
49  * <http://www.bs-factory.org/>.
50  */

51
52 package org.bsf.listOfValues;
53
54 import junit.framework.TestCase;
55 import org.bsf.listOfValues.client.LovManager;
56 import org.bsf.listOfValues.exceptions.NoSuchLovException;
57 import org.bsf.listOfValues.exceptions.NoSuchLovValueException;
58 import org.bsf.remoting.EJBDefinition;
59 import org.bsf.remoting.http.HttpServiceFactory;
60
61 import java.util.List JavaDoc;
62
63 /**
64  * The LOVs with OIDs 1 and 2 must exist in order for this test to succeed.
65  * You can launch them on other lovs as well, just choose the OIDs to use in
66  * FIRST_LOV_TO_TEST and SECOND_LOV_TO_TEST. Check the MySQL_LOV_EXAMPLES.sql
67  * file to have some LOVs examples.
68  */

69 public class TestLovService extends TestCase {
70     private HttpServiceFactory factory = new HttpServiceFactory();
71
72     // For the retrieval tests
73
private static final Long JavaDoc FIRST_LOV_TO_TEST = new Long JavaDoc( 1 );
74     private static final Long JavaDoc SECOND_LOV_TO_TEST = new Long JavaDoc( 3 );
75
76     // For the Missing Lov test
77
private static final Long JavaDoc NOT_A_LOV_OID = new Long JavaDoc( 9999 );
78     private static final Long JavaDoc NOT_A_LOV_VALUE_OID_IN_FIRST_LOV_TO_TEST = new Long JavaDoc( 0 );
79
80     EJBDefinition lovServiceDefinition = new EJBDefinition( "ejb/LOVService",
81                                                             "org.bsf.listOfValues.LOVServiceHome",
82                                                             "org.bsf.listOfValues.LOVService" );
83
84     public TestLovService( String JavaDoc s ) {
85         super( s );
86
87         // The port to use, on the server, to communicate (default is 8080)
88
factory.setPort( 8080 );
89
90         // The location of the server on which the server is deployed
91
factory.setHost( "localhost" );
92
93         // The context of the remoting (in the ear def)
94
factory.setServerContext( "testLov" );
95
96         try {
97             // We store the _lovService in the LovManager... It will handle the communication
98
// and the caches on the client side (useless here :o) for us. Just to explain how
99
// to use the LovManager along the LOVs.
100
LovManager.setLovService( (LOVService) factory.getService( lovServiceDefinition ) );
101         } catch( Exception JavaDoc e ) {
102             System.out.println( "Unable to retrieve the LovService... Will exit..." );
103
104             // We display the stack trace
105
e.printStackTrace();
106
107             // And exit
108
System.exit( 0 );
109         }
110     }
111
112     /**
113      * To test that the LOV retrieval is working. If t is not working check the
114      * FIRST and SECOND_LOV_TO_TEST values (should be the one of existing and
115      * working LOVs...)
116      */

117     public void testLovRetrieval() throws Exception JavaDoc {
118         try {
119             System.out.println( "\nRunning: testLovRetrieval()" );
120
121             // First LOV
122
printLov( FIRST_LOV_TO_TEST, LovManager.getListOfValues( FIRST_LOV_TO_TEST ) );
123
124             // Second LOV
125
printLov( SECOND_LOV_TO_TEST, LovManager.getListOfValues( SECOND_LOV_TO_TEST ) );
126         } catch( NoSuchLovException e ) {
127             fail();
128         }
129     }
130
131     /**
132      * To test that a NoSuchLovException is thrown when requesting a non
133      * existing LOV.
134      */

135     public void testMissingLov() throws Exception JavaDoc {
136         try {
137             System.out.println( "\nRunning: testMissingLov()" );
138
139             LovManager.getListOfValues( NOT_A_LOV_OID );
140         } catch( NoSuchLovException e ) {
141             // Should be raised
142
System.out.println( "\n[Success] testMissingLov: We managed to get the NoSuchLovException..." );
143
144             // We stop here
145
return;
146         }
147
148         // If we reach this point it is a faillure !
149
fail();
150     }
151
152     /**
153      * To test that a NoSuchLovValueException is thrown when requesting a non
154      * existing LovValue.
155      */

156     public void testMissingLovValue() throws Exception JavaDoc {
157         try {
158             System.out.println( "\nRunning: testMissingLovValue()" );
159
160             LovManager.getLovValue( FIRST_LOV_TO_TEST, NOT_A_LOV_VALUE_OID_IN_FIRST_LOV_TO_TEST );
161         } catch( NoSuchLovException e ) {
162             // The LOV should be present...
163
fail();
164         } catch( NoSuchLovValueException e ) {
165             // Should be raised
166
System.out.println( "\n[Success] testMissingLovValue: We managed to get the NoSuchLovValueException..." );
167
168             // Success, we stop here !!!
169
return;
170         }
171
172         // If we reach this point it is a faillure !
173
fail();
174     }
175
176     private void printLov( Long JavaDoc p_lovOID, List JavaDoc p_lov ) {
177         System.out.println( "\nDisplaying LOV: " + p_lovOID );
178
179         if ( p_lov == null ) {
180             System.out.println( "Null LOV..." );
181         } else {
182             for ( int index = 0 ; index < p_lov.size() ; index++ ) {
183                 System.out.println( ( p_lov.get( index ) ) );
184             }
185         }
186     }
187 }
Popular Tags