KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > bsf > remoteIterator > TestRemoteIterator


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

46 package org.bsf.remoteIterator;
47
48 import junit.framework.TestCase;
49 import org.bsf.remoteIterator.client.RemoteIteratorClient;
50 import org.bsf.remoteIterator.common.ColumnMetadata;
51 import org.bsf.remoteIterator.server.RemoteIteratorService;
52 import org.bsf.remoteIterator.server.RemoteIteratorServiceHome;
53 import org.bsf.remoting.EJBDefinition;
54 import org.bsf.remoting.http.HttpServiceFactory;
55
56 import javax.naming.Context JavaDoc;
57 import javax.naming.InitialContext JavaDoc;
58 import javax.naming.NamingException JavaDoc;
59 import java.util.Iterator JavaDoc;
60 import java.util.List JavaDoc;
61 import java.util.Properties JavaDoc;
62
63 /**
64  * TestRemoteIterator
65  */

66 public class TestRemoteIterator extends TestCase {
67     // Set to true to use the remoting, false for rmi
68
private boolean _mustUseRemoting = true;
69
70     private RemoteIteratorClient _riClient = null;
71     private RemoteIteratorService _riService = null;
72     private HttpServiceFactory _factory = new HttpServiceFactory();
73
74     EJBDefinition REMOTE_ITERATOR_SERVICE_DEFINITION = new EJBDefinition( "ejb/RemoteIteratorService",
75                                                                           "org.bsf.remoteIterator.server.RemoteIteratorServiceHome",
76                                                                           "org.bsf.remoteIterator.server.RemoteIteratorService" );
77
78     private static final String JavaDoc SQL_STATEMENT = "select * from " + RITestDatabaseManager.TABLE_NAME;
79
80     public TestRemoteIterator( String JavaDoc s ) {
81         super( s );
82
83         try {
84             if ( _mustUseRemoting ) {
85                 // The port to use, on the server, to communicate (default is 8080)
86
_factory.setPort( 8080 );
87                 // The location of the server on which the server is deployed
88
_factory.setHost( "localhost" );
89                 // The context of the remoting (in the ear def)
90
_factory.setServerContext( "testRI" );
91
92                 _riService = (RemoteIteratorService) _factory.getService( REMOTE_ITERATOR_SERVICE_DEFINITION );
93             } else {
94                 Context JavaDoc jndiContext = getInitialContext();
95                 _riService = ( (RemoteIteratorServiceHome) jndiContext.lookup( "ejb/RemoteIteratorService" ) ).create();
96             }
97         } catch( Exception JavaDoc e ) {
98             // We display the stack trace
99
e.printStackTrace();
100
101             // And exit
102
System.exit( 0 );
103         }
104     }
105
106     private Context JavaDoc getInitialContext() throws NamingException JavaDoc {
107         Properties JavaDoc properties = new Properties JavaDoc();
108         properties.put( "java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory" );
109         properties.put( "java.naming.provider.url", "jnp://localhost:1099" );
110         properties.put( "java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces" );
111
112         return new InitialContext JavaDoc( properties );
113     }
114
115     // *************************************************************************
116
// ******************************* Tests ***********************************
117
// *************************************************************************
118

119     /**
120      * Testing the results of a call to getColumnMetaData()...
121      */

122     public void testRemoteIteratorMetaData() throws Exception JavaDoc {
123         System.out.print( "\nRunning testRemoteIteratorMetaData" );
124
125         _riClient = getRemoteIteratorClient( SQL_STATEMENT );
126
127         List JavaDoc columnsMetaData = _riClient.getColumnMetatData();
128
129         for ( int index = 0 ; index < columnsMetaData.size() ; index++ ) {
130             ColumnMetadata columnMetaData = (ColumnMetadata) columnsMetaData.get( index );
131
132             String JavaDoc tableName = columnMetaData.getTableName();
133             String JavaDoc columnName = columnMetaData.getColumnName();
134             String JavaDoc columnClassName = columnMetaData.getColumnClassName();
135
136             String JavaDoc expectedColumnName = ( index == 0 ) ? "OID" : "LABEL";
137             String JavaDoc expectedColumnClassName = ( index == 0 ) ? "java.lang.Integer" : "java.lang.String";
138
139             if ( !RITestDatabaseManager.TABLE_NAME.equalsIgnoreCase( tableName ) ) {
140                 fail( "\nInvalid table name... Expected " + RITestDatabaseManager.TABLE_NAME +
141                       " found " + tableName );
142             }
143
144             if ( !expectedColumnName.equalsIgnoreCase( columnName ) ) {
145                 fail( "\nInvalid column name... Expected " + expectedColumnName + " found " + columnName );
146             }
147
148             if ( !expectedColumnClassName.equalsIgnoreCase( columnClassName ) ) {
149                 fail( "\nInvalid column name... Expected " + expectedColumnName + " found " + columnName );
150             }
151         }
152
153         System.out.println( " => SUCCESS !!!" );
154     }
155
156     /**
157      * Testing the results of a call to getRowCount()...
158      */

159     public void testRemoteIteratorRecordsCount() throws Exception JavaDoc {
160         System.out.print( "\nRunning testRemoteIteratorRecordsCount" );
161
162         _riClient = getRemoteIteratorClient( SQL_STATEMENT );
163
164         int recordsCount = _riClient.getRecordCount();
165
166         if ( RITestDatabaseManager.NB_RECORDS != recordsCount ) {
167             fail( "\nInvalid records count... Expected " + RITestDatabaseManager.NB_RECORDS + " found " + recordsCount );
168         }
169
170         System.out.println( " => SUCCESS !!!" );
171     }
172
173     /**
174      * The default table as rows whose first column (its oid) is the row index...
175      * The difference between two following rows should always be one... If not,
176      * that means that the next are missing some rows :o(
177      */

178     public void testRemoteIteratorRetrieval() throws Exception JavaDoc {
179         System.out.print( "\nRunning testRemoteIteratorRetrieval" );
180
181         _riClient = getRemoteIteratorClient( SQL_STATEMENT );
182
183         int previousRowOID = -1;
184
185         while ( !_riClient.isLast() ) {
186             Iterator JavaDoc iterator = _riClient.next( 250 );
187
188             while ( iterator.hasNext() ) {
189                 List JavaDoc row = (List JavaDoc) iterator.next();
190
191                 if ( ( (Number JavaDoc) row.get( 0 ) ).intValue() - previousRowOID != 1 ) {
192                     fail( "\nError in interval retrieval..." );
193                 } else {
194                     previousRowOID = ( (Number JavaDoc) row.get( 0 ) ).intValue();
195                 }
196             }
197         }
198
199         System.out.println( " => SUCCESS !!!" );
200     }
201
202     /**
203      * The default table as rows whose first column (its oid) is the row index...
204      * Checks if the cursor is well moved.
205      */

206     public void testRemoteIteratorCursor() throws Exception JavaDoc {
207         System.out.print( "\nRunning testRemoteIteratorCursor" );
208
209         int nbRowsToMove = 0;
210         int expectedRowOID = 0;
211
212         _riClient = getRemoteIteratorClient( SQL_STATEMENT );
213
214         nbRowsToMove = 50;
215         List JavaDoc row = (List JavaDoc) _riClient.next( nbRowsToMove ).next();
216
217         if ( ( (Number JavaDoc) row.get( 0 ) ).intValue() != expectedRowOID ) {
218             fail( "\nError in next retrieval... Found: " + (Number JavaDoc) row.get( 0 ) + " expexted: " + expectedRowOID );
219         }
220
221         expectedRowOID += nbRowsToMove;
222         nbRowsToMove = 25;
223         row = (List JavaDoc) _riClient.next( nbRowsToMove ).next();
224
225         if ( ( (Number JavaDoc) row.get( 0 ) ).intValue() != expectedRowOID ) {
226             fail( "\nError in next retrieval... Found: " + (Number JavaDoc) row.get( 0 ) + " expexted: " + expectedRowOID );
227         }
228
229         expectedRowOID += nbRowsToMove;
230         nbRowsToMove = 5;
231         row = (List JavaDoc) _riClient.next( nbRowsToMove ).next();
232
233         if ( ( (Number JavaDoc) row.get( 0 ) ).intValue() != expectedRowOID ) {
234             fail( "\nError in next retrieval... Found: " + (Number JavaDoc) row.get( 0 ) + " expexted: " + expectedRowOID );
235         }
236
237         nbRowsToMove = 10;
238         expectedRowOID -= nbRowsToMove; // Begining of next block
239
row = (List JavaDoc) _riClient.previous( nbRowsToMove ).next();
240
241         if ( ( (Number JavaDoc) row.get( 0 ) ).intValue() != expectedRowOID ) {
242             fail( "\nError in previous retrieval... Found: " + (Number JavaDoc) row.get( 0 ) + " expexted: " + expectedRowOID );
243         }
244
245         nbRowsToMove = 1000; // Larger than the actual row
246
expectedRowOID = 0; // Begining of the cursor
247
row = (List JavaDoc) _riClient.previous( nbRowsToMove ).next();
248
249         if ( ( (Number JavaDoc) row.get( 0 ) ).intValue() != expectedRowOID ) {
250             fail( "\nError in previous retrieval... Found: " + (Number JavaDoc) row.get( 0 ) + " expexted: " + expectedRowOID );
251         }
252
253         expectedRowOID = 100;
254         row = (List JavaDoc) _riClient.absolute( 100 ).next();
255
256         if ( ( (Number JavaDoc) row.get( 0 ) ).intValue() != expectedRowOID ) {
257             fail( "\nError in absolute retrieval... Found: " + (Number JavaDoc) row.get( 0 ) + " expexted: " + expectedRowOID );
258         }
259
260         expectedRowOID = 2750;
261         row = (List JavaDoc) _riClient.absolute( 2750 ).next();
262
263         if ( ( (Number JavaDoc) row.get( 0 ) ).intValue() != expectedRowOID ) {
264             fail( "\nError in absolute retrieval... Found: " + (Number JavaDoc) row.get( 0 ) + " expexted: " + expectedRowOID );
265         }
266
267         System.out.println( " => SUCCESS !!!" );
268     }
269
270     // *************************************************************************
271
// ************************** Utility methods ******************************
272
// *************************************************************************
273

274     protected RemoteIteratorClient getRemoteIteratorClient( String JavaDoc p_statement ) {
275         RemoteIteratorClient riClient = null;
276
277         try {
278             riClient = new RemoteIteratorClient( _riService.getRemoteIterator( p_statement ) );
279         } catch( java.rmi.RemoteException JavaDoc e ) {
280             fail( "Check the SQL Statement... " + p_statement );
281         }
282
283         return riClient;
284     }
285
286     /**
287      * Called after each test... We remove the RI (if needed).
288      *
289      * @throws Exception
290      */

291     protected void tearDown() throws Exception JavaDoc {
292         super.tearDown();
293
294         if ( _riClient != null && _riClient.hasRemoteReference() ) {
295             _riClient.remove();
296         }
297     }
298 }
299
Popular Tags