|
From: <rga...@us...> - 2002-11-21 21:57:05
|
Update of /cvsroot/csms/csms-core/src/test/src/org/fanfoot In directory sc8-pr-cvs1:/tmp/cvs-serv15493 Added Files: Data4Tests.java Log Message: Utioity class to create a test DB for Unit Tests --- NEW FILE: Data4Tests.java --- /* * ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Community Sports Management System. * * The Initial Developer of the Original Code is * We know What you Want.net * * Portions created by the Initial Developer are Copyright (C) 2002 * the Initial Developer. All Rights Reserved. * * Contributor(s): * * Ross Gardler <ross [at] saafe.org> * Mike Lever <mikel [at] fanfoot.com> * Stuart Gardler <stuart [at] wkwyw.net> * * ***** END LICENSE BLOCK ***** */ /* * Data4Tests.java * * Created on 20 September 2002, 21:59 */ package org.fanfoot; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.xindice.client.xmldb.services.CollectionManager; import org.apache.xindice.util.XindiceException; import org.apache.xindice.xml.dom.DOMParser; import org.fanfoot.db.DBException; import org.fanfoot.db.Players; import org.fanfoot.db.Players; import org.saafe.utils.XMLUtils; import org.w3c.dom.Document; import org.xmldb.api.DatabaseManager; import org.xmldb.api.base.Collection; import org.xmldb.api.base.Database; import org.xmldb.api.base.Resource; import org.xmldb.api.base.ResourceIterator; import org.xmldb.api.base.ResourceSet; import org.xmldb.api.base.XMLDBException; import org.xmldb.api.modules.XMLResource; import org.xmldb.api.modules.XPathQueryService; /** * Creates test data in a standalone database against which the * application test harnesses can be run. * *@author rgardler *@created 19 November 2002 */ public class Data4Tests { /** Players DB */ Players players; /** URI of the DB */ public static String DB_URI = "xmldb:xindice-embed:///db//"; /** Where to write log info */ final static Logger logger = Logger.getLogger( "org.fanfoot.db" ); public Data4Tests() throws DBException { super(); players = new Players( DB_URI ); } /** * Create the player collecton in the database. All previous data * will be removed. * *@exception DBException Description of the Exception */ public void createPlayerCollection() throws DBException { deletePlayerCollection(); Collection col = null; try { String driver = "org.apache.xindice.client.xmldb.DatabaseImpl"; Database database = null; try { Class c = Class.forName( driver ); database = (Database) c.newInstance(); } catch ( ClassNotFoundException cnfe ) { throw new DBException( "Cannot find class to connect to DB", cnfe ); } catch ( InstantiationException ie ) { throw new DBException( "Cannot instantiate class to connect to DB", ie ); } catch ( IllegalAccessException iae ) { throw new DBException( "Cannot access class to connect to DB", iae ); } DatabaseManager.registerDatabase( database ); col = DatabaseManager.getCollection( DB_URI ); if ( col == null ) { throw new DBException( "Cannot find the DB collection" ); } String strColName = Players.COLLECTION_NAME; CollectionManager service = (CollectionManager) col.getService( "CollectionManager", "1.0" ); String strColConf = "<collection compressed=\"true\" " + "name=\"" + strColName + "\">" + " <filer class=\"org.apache.xindice.core.filer.BTreeFiler\"" + " gzip=\"true\"/>" + "</collection>"; service.createCollection( strColName, DOMParser.toDocument( strColConf ) ); } catch ( XMLDBException e ) { throw new DBException( "XML:DB Exception occured " + e.errorCode, e ); } catch ( XindiceException xe ) { throw new DBException( "Xindice Exception occured", xe ); } finally { if ( col != null ) { try { col.close(); } catch ( XMLDBException e ) { throw new DBException( "XML:DB Exception occured " + e.errorCode, e ); } } } } /** * Delete the player collection data in the database. * *@exception DBException Description of the Exception */ public void deletePlayerCollection() throws DBException { Collection col = null; Database database = null; try { String driver = "org.apache.xindice.client.xmldb.DatabaseImpl"; try { Class c = Class.forName( driver ); database = (Database) c.newInstance(); } catch ( ClassNotFoundException cnfe ) { throw new DBException( "Cannot find class to connect to DB", cnfe ); } catch ( InstantiationException ie ) { throw new DBException( "Cannot instantiate class to connect to DB", ie ); } catch ( IllegalAccessException iae ) { throw new DBException( "Cannot access class to connect to DB", iae ); } DatabaseManager.registerDatabase( database ); col = DatabaseManager.getCollection( DB_URI ); if ( col == null ) { throw new DBException( "Cannot find the db collection" ); } CollectionManager service = (CollectionManager) col.getService( "CollectionManager", "1.0" ); service.dropCollection( Players.COLLECTION_NAME ); } catch ( XMLDBException e ) { if ( e.errorCode == 201 ) { // collection doesn;t exist to delete } else { throw new DBException( "XML:DB Exception occured " + e.errorCode, e ); } } finally { if ( col != null ) { try { col.close(); } catch ( XMLDBException e ) { throw new DBException( "XML:DB Exception occured " + e.errorCode, e ); } } } } private static String getPlayer100() { return "<player id=\"100\">" + "<firstName type=\"main\">David</firstName>" + "<firstName type=\"initial\">D.</firstName>" + "<surname>Seaman</surname>" + "<registeredTeam>ARS</registeredTeam>" + "<team>ARS</team>" + "<position>Goalkeeper</position>" + "<cost>4.0</cost>" + "<scoringEvents>" + "<game id=\"_Saturday, 9 November, 2002_Barclaycard Premiership_Arsenal_Newcastle United\">" + "<event>" + "<points>1</points>" + "<type>Appearance</type>" + "</event>" + "<event>" + "<points>4</points>" + "<type>Goalkeeper Clean Sheet</type>" + "</event>" + "</game>" + "</scoringEvents>" + "</player>"; } } |