
/*
 * dbXML License, Version 1.0
 *
 *
 * Copyright (c) 1999-2001 The dbXML Group, L.L.C.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by
 *        The dbXML Group (http://www.dbxml.com/)."
 *    Alternately, this acknowledgment may appear in the software
 *    itself, if and wherever such third-party acknowledgments normally
 *    appear.
 *
 * 4. The names "dbXML" and "The dbXML Group" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    info@dbxml.com.
 *
 * 5. Products derived from this software may not be called "dbXML",
 *    nor may "dbXML" appear in their name, without prior written
 *    permission of The dbXML Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE DBXML GROUP OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id: CorbaAPITestCase.java,v 1.5 2001/08/15 20:06:49 tom Exp $
 */

import java.io.*;

import junit.framework.*;

import org.xmldb.api.*;
import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;

import org.dbxml.core.corba.*;
import org.dbxml.core.corba.db.Collection;
import org.dbxml.core.corba.db.Database;
import org.dbxml.core.corba.db.DatabaseManager;
import org.dbxml.core.corba.db.*;
import org.dbxml.xml.TextWriter;

import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;

import org.w3c.dom.*;

import java.util.*;
import java.io.*;
import java.net.*;


public class CorbaAPITestCase extends TestCase {

   // Property name to use to set the naming service up for resolve_initial_refs
   public final static String DEFAULT_ORB_NAMING_PROP_VALUE = "InitRef.NameService";
   //  Default location where the Name service IOR can be located.
   public final static String DEFAULT_NAMING_URI = "http://localhost:4080/NamingService";
   // Default ORB class. This is used to override the JDKs built in CORBA ORB.
   public final static String DEFAULT_ORB_CLASS = "jacorb.orb.ORB";
   // Default ORBSingleton class. This is used to override the JDKs built in CORBA ORB
   public final static String DEFAULT_ORB_SINGLETON_CLASS = "jacorb.orb.ORBSingleton";
   
   // XML prefix for prepending to all documents used in testing
   public final static String XML_PREFIX = "<?xml version=\"1.0\"?>" + "\12";
   
   static public final String TEST_COLLECTION_NAME = "tests";
   static public final String TEST_COLLECTION_CHILD1_NAME = "child1";
   static public final String TEST_COLLECTION_CHILD2_NAME = "child2";
   static public final String INSTANCE_NAME = "db";

   static final byte[] EmptyBytes = new byte[0];
   
   // Collection instance used for testing
   protected Collection col = null;
   // Collection manager instance used for testing
   protected CollectionManager colman = null;
   // Database instance used for testing
   protected Database db = null;
   // DatabaseManager instance for testing
   protected DatabaseManager dbman = null;
   
   public CorbaAPITestCase(String name) {
      super(name);
   }

   public void setUp() throws Exception {
      try {

         // Setup connection to the ORB
         String namingURI = DEFAULT_NAMING_URI;
         String orbNamingProp = DEFAULT_ORB_NAMING_PROP_VALUE;

         // create and initialize the ORB
         Properties orbConf = new Properties();
         // Set the default naming location which is for OpenORB by default.
         orbConf.put(orbNamingProp, namingURI);

         // Also set it up for JacORB 1.1 to make switching easy.
         orbConf.put("jacorb.NameServerURL", namingURI);

         // Also set it up for JacORB 1.3 to make switching easy.
         orbConf.put("ORBInitRef.NameService", namingURI);

         orbConf.put("org.omg.CORBA.ORBClass", DEFAULT_ORB_CLASS);
         orbConf.put("org.omg.CORBA.ORBSingletonClass", DEFAULT_ORB_SINGLETON_CLASS);

         ORB orb = ORB.init(new String[0], orbConf);

         NamingContextExt ncRef = NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));
         if (ncRef == null) {
            throw new XMLDBException(ErrorCodes.VENDOR_ERROR, 1,
               "A naming service could not be located at the uri "
               + namingURI + ". Make sure your instance is running");
         }

         // Get the database instance from naming, the path is dbxml/db
         NameComponent root = new NameComponent("dbxml", "");
         NameComponent instance = new NameComponent(INSTANCE_NAME, "");
         NameComponent path[] = {root, instance};

         db = (org.dbxml.core.corba.db.Database)DatabaseHelper.narrow(ncRef.resolve(path));

         // Get a DatabaseManager instance
         dbman = db.getDatabaseManager();
         
         // Get a collection instance to use for the testing
         col = db.getCollection(TEST_COLLECTION_NAME);
         // Get a collection manager instance to use for testing
         colman = col.getCollectionManager();
         
      } catch (Exception e) {
         System.out.println("ERROR : " + e) ;
         e.printStackTrace();
      }

   }

   public void tearDown() throws org.dbxml.core.corba.db.APIException {
      
      try {
         // Loop over all documents in the collection and delete
         String[] coldocs = col.listDocuments();
         for ( int i=0 ; i < coldocs.length ; i++ ) {
            col.removeDocument( coldocs[i] );
         }
         
         // Loop over all indexex in the collection and remove
         String[] colindexes = colman.listIndexers();
         for ( int i=0 ; i < colindexes.length ; i++ ) {
            colman.dropIndexer( colindexes[i] );
         }
         
         // Loop over all XMLObjects in the collection and remove
         String[] colxmlobjects = colman.listXMLObjects();
         for ( int i=0 ; i < colxmlobjects.length ; i++ ) {
            colman.dropXMLObject( colxmlobjects[i] );
         }
      } catch( Exception e ) {
         System.out.println("ERROR : " + e);
         e.printStackTrace();
      } finally {
         // Release the resources taken by col
         col.remove();
         // Release the resource taken by colman
         colman.remove();
         // Release all resources taken by db
         db.remove();
      }
   }
   
   // Loop over two EncodedBuffers testing them for equality
   public boolean testEquality(EncodedBuffer buf1, EncodedBuffer buf2) {
      boolean equal = true;
      
      /*
      for ( int i=0 ; i < buf1.buf.length ; i++ ) {
         System.out.print( (char)buf1.buf[i] );
      }
      System.out.println();
      for ( int i=0 ; i < buf2.buf.length ; i++ ) {
         System.out.print( (char)buf2.buf[i] );
      }
      System.out.println();
      */
      
      
      // If the two buffers are of different lengths return false, otherwise check all characters
      if ( buf1.buf.length != buf2.buf.length ) {
         equal = false;
      }
      else {
         for ( int i = 0 ; i < buf1.buf.length ; i++ ) {
            if ( buf1.buf[i] != buf2.buf[i] ) {
               equal=false;
            }
         }
      }
      
      return equal;
   }
}





