1 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 ; 57 import javax.naming.InitialContext ; 58 import javax.naming.NamingException ; 59 import java.util.Iterator ; 60 import java.util.List ; 61 import java.util.Properties ; 62 63 66 public class TestRemoteIterator extends TestCase { 67 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 SQL_STATEMENT = "select * from " + RITestDatabaseManager.TABLE_NAME; 79 80 public TestRemoteIterator( String s ) { 81 super( s ); 82 83 try { 84 if ( _mustUseRemoting ) { 85 _factory.setPort( 8080 ); 87 _factory.setHost( "localhost" ); 89 _factory.setServerContext( "testRI" ); 91 92 _riService = (RemoteIteratorService) _factory.getService( REMOTE_ITERATOR_SERVICE_DEFINITION ); 93 } else { 94 Context jndiContext = getInitialContext(); 95 _riService = ( (RemoteIteratorServiceHome) jndiContext.lookup( "ejb/RemoteIteratorService" ) ).create(); 96 } 97 } catch( Exception e ) { 98 e.printStackTrace(); 100 101 System.exit( 0 ); 103 } 104 } 105 106 private Context getInitialContext() throws NamingException { 107 Properties properties = new Properties (); 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 ( properties ); 113 } 114 115 119 122 public void testRemoteIteratorMetaData() throws Exception { 123 System.out.print( "\nRunning testRemoteIteratorMetaData" ); 124 125 _riClient = getRemoteIteratorClient( SQL_STATEMENT ); 126 127 List columnsMetaData = _riClient.getColumnMetatData(); 128 129 for ( int index = 0 ; index < columnsMetaData.size() ; index++ ) { 130 ColumnMetadata columnMetaData = (ColumnMetadata) columnsMetaData.get( index ); 131 132 String tableName = columnMetaData.getTableName(); 133 String columnName = columnMetaData.getColumnName(); 134 String columnClassName = columnMetaData.getColumnClassName(); 135 136 String expectedColumnName = ( index == 0 ) ? "OID" : "LABEL"; 137 String 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 159 public void testRemoteIteratorRecordsCount() throws Exception { 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 178 public void testRemoteIteratorRetrieval() throws Exception { 179 System.out.print( "\nRunning testRemoteIteratorRetrieval" ); 180 181 _riClient = getRemoteIteratorClient( SQL_STATEMENT ); 182 183 int previousRowOID = -1; 184 185 while ( !_riClient.isLast() ) { 186 Iterator iterator = _riClient.next( 250 ); 187 188 while ( iterator.hasNext() ) { 189 List row = (List ) iterator.next(); 190 191 if ( ( (Number ) row.get( 0 ) ).intValue() - previousRowOID != 1 ) { 192 fail( "\nError in interval retrieval..." ); 193 } else { 194 previousRowOID = ( (Number ) row.get( 0 ) ).intValue(); 195 } 196 } 197 } 198 199 System.out.println( " => SUCCESS !!!" ); 200 } 201 202 206 public void testRemoteIteratorCursor() throws Exception { 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 row = (List ) _riClient.next( nbRowsToMove ).next(); 216 217 if ( ( (Number ) row.get( 0 ) ).intValue() != expectedRowOID ) { 218 fail( "\nError in next retrieval... Found: " + (Number ) row.get( 0 ) + " expexted: " + expectedRowOID ); 219 } 220 221 expectedRowOID += nbRowsToMove; 222 nbRowsToMove = 25; 223 row = (List ) _riClient.next( nbRowsToMove ).next(); 224 225 if ( ( (Number ) row.get( 0 ) ).intValue() != expectedRowOID ) { 226 fail( "\nError in next retrieval... Found: " + (Number ) row.get( 0 ) + " expexted: " + expectedRowOID ); 227 } 228 229 expectedRowOID += nbRowsToMove; 230 nbRowsToMove = 5; 231 row = (List ) _riClient.next( nbRowsToMove ).next(); 232 233 if ( ( (Number ) row.get( 0 ) ).intValue() != expectedRowOID ) { 234 fail( "\nError in next retrieval... Found: " + (Number ) row.get( 0 ) + " expexted: " + expectedRowOID ); 235 } 236 237 nbRowsToMove = 10; 238 expectedRowOID -= nbRowsToMove; row = (List ) _riClient.previous( nbRowsToMove ).next(); 240 241 if ( ( (Number ) row.get( 0 ) ).intValue() != expectedRowOID ) { 242 fail( "\nError in previous retrieval... Found: " + (Number ) row.get( 0 ) + " expexted: " + expectedRowOID ); 243 } 244 245 nbRowsToMove = 1000; expectedRowOID = 0; row = (List ) _riClient.previous( nbRowsToMove ).next(); 248 249 if ( ( (Number ) row.get( 0 ) ).intValue() != expectedRowOID ) { 250 fail( "\nError in previous retrieval... Found: " + (Number ) row.get( 0 ) + " expexted: " + expectedRowOID ); 251 } 252 253 expectedRowOID = 100; 254 row = (List ) _riClient.absolute( 100 ).next(); 255 256 if ( ( (Number ) row.get( 0 ) ).intValue() != expectedRowOID ) { 257 fail( "\nError in absolute retrieval... Found: " + (Number ) row.get( 0 ) + " expexted: " + expectedRowOID ); 258 } 259 260 expectedRowOID = 2750; 261 row = (List ) _riClient.absolute( 2750 ).next(); 262 263 if ( ( (Number ) row.get( 0 ) ).intValue() != expectedRowOID ) { 264 fail( "\nError in absolute retrieval... Found: " + (Number ) row.get( 0 ) + " expexted: " + expectedRowOID ); 265 } 266 267 System.out.println( " => SUCCESS !!!" ); 268 } 269 270 274 protected RemoteIteratorClient getRemoteIteratorClient( String p_statement ) { 275 RemoteIteratorClient riClient = null; 276 277 try { 278 riClient = new RemoteIteratorClient( _riService.getRemoteIterator( p_statement ) ); 279 } catch( java.rmi.RemoteException e ) { 280 fail( "Check the SQL Statement... " + p_statement ); 281 } 282 283 return riClient; 284 } 285 286 291 protected void tearDown() throws Exception { 292 super.tearDown(); 293 294 if ( _riClient != null && _riClient.hasRemoteReference() ) { 295 _riClient.remove(); 296 } 297 } 298 } 299 | Popular Tags |