Tag: cws_dev300_odbmacros3
User: fs      
Date: 2008-07-30 08:50:58+0000
Modified:
   dba/dbaccess/qa/complex/dbaccess/DatabaseDocument.java

Log:
 #i76128# more test cases for XLoadable

File Changes:

Directory: /dba/dbaccess/qa/complex/dbaccess/
=============================================

File [changed]: DatabaseDocument.java
Url: 
http://dba.openoffice.org/source/browse/dba/dbaccess/qa/complex/dbaccess/DatabaseDocument.java?r1=1.1.2.3&r2=1.1.2.4
Delta lines:  +220 -46
----------------------
--- DatabaseDocument.java       2008-07-28 06:30:21+0000        1.1.2.3
+++ DatabaseDocument.java       2008-07-30 08:50:55+0000        1.1.2.4
@@ -7,7 +7,7 @@
  * OpenOffice.org - a multi-platform office productivity suite
  *
  * $RCSfile: DatabaseDocument.java,v $
- * $Revision: 1.1.2.3 $
+ * $Revision: 1.1.2.4 $
  *
  * This file is part of OpenOffice.org.
  *
@@ -29,6 +29,9 @@
  ************************************************************************/
 package complex.dbaccess;
 
+import com.sun.star.beans.PropertyState;
+import com.sun.star.document.DocumentEvent;
+import com.sun.star.lang.XEventListener;
 import com.sun.star.lang.XMultiServiceFactory;
 import com.sun.star.uno.Exception;
 import com.sun.star.uno.UnoRuntime;
@@ -36,25 +39,119 @@
 import com.sun.star.frame.FrameSearchFlag;
 import com.sun.star.beans.PropertyValue;
 import com.sun.star.beans.XPropertySet;
+import com.sun.star.container.XSet;
+import com.sun.star.document.XDocumentEventListener;
 import com.sun.star.document.XEventsSupplier;
 import com.sun.star.document.XScriptInvocationContext;
 import com.sun.star.lang.XComponent;
 import com.sun.star.frame.XComponentLoader;
+import com.sun.star.frame.XLoadable;
 import com.sun.star.frame.XModel;
 import com.sun.star.frame.XModel2;
 import com.sun.star.frame.XTitle;
+import com.sun.star.lang.EventObject;
 import com.sun.star.lang.NotInitializedException;
+import com.sun.star.lang.XServiceInfo;
+import com.sun.star.lang.XSingleComponentFactory;
 import com.sun.star.script.provider.XScriptProviderSupplier;
 import com.sun.star.sdb.XFormDocumentsSupplier;
 import com.sun.star.sdb.XOfficeDatabaseDocument;
 import com.sun.star.sdb.XReportDocumentsSupplier;
+import com.sun.star.uno.XComponentContext;
+import com.sun.star.util.CloseVetoException;
 import com.sun.star.util.XCloseable;
 import connectivity.tools.*;
+import helper.FileTools;
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Iterator;
+import java.util.Vector;
 
 public class DatabaseDocument extends CRMBasedTestCase
 {
+    /** a helper class which can be used by the Basic scripts in our test 
documents
+     *  to notify us of events in this document
+     */
+    private class CallbackComponent implements XDocumentEventListener
+    {
+        public void documentEventOccured( DocumentEvent _event )
+        {
+            onEventCallback( _event );
+        }
+
+        public void disposing( com.sun.star.lang.EventObject _Event )
+        {
+            // not interested in
+        }
+    };
+
+    private static String getCallbackComponentServiceName()
+    {
+        return "org.openoffice.complex.dbaccess.EventCallback";
+    }
+
+    /** a factory for a CallbackComponent
+     */
+    private class CallbackComponentFactory implements XSingleComponentFactory, 
XServiceInfo, XComponent
+    {
+        private Vector  m_eventListeners = new Vector();
+
+        public Object createInstanceWithContext( XComponentContext _context ) 
throws Exception
+        {
+            return new CallbackComponent();
+        }
+
+        public Object createInstanceWithArgumentsAndContext( Object[] arg0, 
XComponentContext _context ) throws Exception
+        {
+            return createInstanceWithContext( _context );
+        }
+        
+        public String getImplementationName()
+        {
+            return "org.openoffice.complex.dbaccess.CallbackComponent";
+        }
+
+        public boolean supportsService( String _service )
+        {
+            return _service.equals( getCallbackComponentServiceName() );
+        }
+
+        public String[] getSupportedServiceNames()
+        {
+            return new String[] { getCallbackComponentServiceName() };
+        }
+
+        public void dispose()
+        {
+            EventObject event = new EventObject( this );
+
+            Vector eventListenersCopy = (Vector)m_eventListeners.clone();
+            Iterator iter = eventListenersCopy.iterator();
+            while ( iter.hasNext() )
+            {
+                ((XEventListener)iter.next()).disposing( event );
+            }
+        }
+
+        public void addEventListener( XEventListener _listener )
+        {
+            if ( _listener != null )
+                m_eventListeners.add( _listener );
+        }
+
+        public void removeEventListener( XEventListener _listener )
+        {
+            m_eventListeners.remove( _listener );
+        }
+    };
+
+    private XComponent m_callbackFactory = null;
+
     // 
--------------------------------------------------------------------------------------------------------
-    public String[] getTestMethodNames() {
+    public String[] getTestMethodNames()
+    {
         return new String[]
         {
             "testLoadable",
@@ -63,23 +160,47 @@
     }
 
     // 
--------------------------------------------------------------------------------------------------------
-    public String getTestObjectName() {
+    public String getTestObjectName()
+    {
         return "DatabaseDocument";
     }
 
+    public void before()
+    {
+        super.before();
+
+        try
+        {
+            // at our service factory, insert a new factory for our 
CallbackComponent
+            // this will allow the Basic code in our test documents to call 
back into this test case
+            // here, by just instantiating this service
+            XSet globalFactory = (XSet)UnoRuntime.queryInterface(
+                XSet.class, getORB() );
+            m_callbackFactory = new CallbackComponentFactory();
+            globalFactory.insert( m_callbackFactory );
+        }
+        catch( Exception e )
+        {
+            System.err.println( "could not create the test case, error 
message:\n" + e.getMessage() );
+            e.printStackTrace( System.err );
+            failed( "failed to created the test case" );
+        }
+    }
+
+    // 
--------------------------------------------------------------------------------------------------------
+    public void after()
+    {
+        super.after();
+
+        // dispose our callback factory. This will automatically remove it 
from our service
+        // factory
+        m_callbackFactory.dispose();
+    }
+
     // 
--------------------------------------------------------------------------------------------------------
     protected void createTestCase()
     {
-//        try
-//        {
             super.createTestCase();
-//        }
-//        catch( Exception e )
-//        {
-//            System.err.println( "could not create the test case, error 
message:\n" + e.getMessage() );
-//            e.printStackTrace( System.err );
-//            assure( "failed to created the test case", false );
-//        }
     }
 
     // 
--------------------------------------------------------------------------------------------------------
@@ -117,13 +238,8 @@
     }
 
     // 
--------------------------------------------------------------------------------------------------------
-    public void testLoadable() throws Exception
+    private void impl_checkDocumentInitState( Object _document, boolean 
_isInitialized )
     {
-        XModel databaseDoc = (XModel)UnoRuntime.queryInterface( XModel.class,
-            getORB().createInstance( "com.sun.star.sdb.OfficeDatabaseDocument" 
) );
-        XStorable storeDoc = (XStorable)UnoRuntime.queryInterface( 
XStorable.class,
-            databaseDoc );
-
         // things you cannot do with an uninitialized document:
         UnoMethodDescriptor[] unsupportedMethods = new UnoMethodDescriptor[] {
             new UnoMethodDescriptor( XStorable.class, "store" ),
@@ -139,23 +255,66 @@
 
         for ( int i=0; i<unsupportedMethods.length; ++i)
         {
-            verifyExpectedException( databaseDoc, 
unsupportedMethods[i].unoInterfaceClass,
-                unsupportedMethods[i].methodName, new Object[]{}, 
NotInitializedException.class );
+            verifyExpectedException( _document, 
unsupportedMethods[i].unoInterfaceClass,
+                unsupportedMethods[i].methodName, new Object[]{}, 
_isInitialized ? null : NotInitializedException.class );
+        }
+    }
+
+    // 
--------------------------------------------------------------------------------------------------------
+    private String impl_copyTempFile( String sourceURL ) throws IOException, 
URISyntaxException
+    {
+        File targetFile = File.createTempFile( getTestObjectName(), ".odb" );
+        FileTools.copyFile( new File( new URI( sourceURL ) ), targetFile );
+        targetFile.deleteOnExit();
+        return targetFile.getAbsoluteFile().toURL().toString();
+    }
+
+    // 
--------------------------------------------------------------------------------------------------------
+    private XModel impl_cloneDocument( XModel _databaseDoc ) throws 
CloseVetoException, IOException, URISyntaxException, Exception
+    {
+        // close the document after rememberinbg its URL
+        String documentURL = _databaseDoc.getURL();
+        XCloseable closeDoc = (XCloseable)UnoRuntime.queryInterface( 
XCloseable.class,
+            _databaseDoc );
+        closeDoc.close( true );
+
+        // clone the file
+        documentURL = impl_copyTempFile( documentURL );
+
+        XModel databaseDoc = (XModel)UnoRuntime.queryInterface( XModel.class,
+            getORB().createInstance( "com.sun.star.sdb.OfficeDatabaseDocument" 
) );
+        impl_checkDocumentInitState( databaseDoc, false );
+
+        return databaseDoc;
         }
 
-        // however, you can ask the document for its URL and location
+    // 
--------------------------------------------------------------------------------------------------------
+    public void testLoadable() throws Exception, IOException, 
URISyntaxException
+    {
+        XModel databaseDoc = (XModel)UnoRuntime.queryInterface( XModel.class,
+            getORB().createInstance( "com.sun.star.sdb.OfficeDatabaseDocument" 
) );
+        XStorable storeDoc = (XStorable)UnoRuntime.queryInterface( 
XStorable.class, databaseDoc );
+
+        // verify the document rejects API calls which require it to be 
initialized
+        impl_checkDocumentInitState( databaseDoc, false );
+
+        // though the document is not initialized, you can ask for the 
location, the URL, and the args
         String location = storeDoc.getLocation();
         String url = databaseDoc.getURL();
+        PropertyValue[] args = databaseDoc.getArgs();
         // they should be all empty at this time
-        assureEquals( "location is expected to be empty here", location, "" );
-        assureEquals( "URL is expected to be empty here", url, "" );
+        assureEquals( "location is expected to be empty here", "", location );
+        assureEquals( "URL is expected to be empty here", "", url );
+        assureEquals( "Args are expected to be empty here", 0, args.length );
 
         // and, you should be able to set properties at the data source
+        {
         XOfficeDatabaseDocument dataSourceAccess = 
(XOfficeDatabaseDocument)UnoRuntime.queryInterface(
             XOfficeDatabaseDocument.class, databaseDoc );
         XPropertySet dsProperties = (XPropertySet)UnoRuntime.queryInterface(
             XPropertySet.class, dataSourceAccess.getDataSource() );
         dsProperties.setPropertyValue( "URL", "sdbc:embedded:hsqldb" );
+        }
 
         // there's three methods how you can initialize a database document
         // 1. XLoadable::initNew
@@ -163,26 +322,41 @@
         // 3. XStorable::storeAsURL
         //      (this is for compatibility reasons, to not break existing code)
 
+        // ....................................................................
         // Let's check 3. first
-        String documentURL = null;
-        try
-        {
-            documentURL = java.io.File.createTempFile(
-                getTestObjectName(), ".oxs" 
).getAbsoluteFile().toURL().toString();
+        File documentFile = java.io.File.createTempFile( getTestObjectName(), 
".odb" );
+        documentFile.deleteOnExit();
+        String documentURL = documentFile.getAbsoluteFile().toURL().toString();
             storeDoc.storeAsURL( documentURL, new PropertyValue[] {} );
-        }
-        catch( java.lang.Exception e )
-        {
-            assure( "could not store the document", false );
-        }
+
         // now that the document is stored, ...
         // ... its URL should be correct
         assureEquals( "wrong URL after storing the document", documentURL, 
databaseDoc.getURL() );
-        // ... it should be initialized, i.e. we should be able to call all 
the methods from above
-        for ( int i=0; i<unsupportedMethods.length; ++i)
-        {
-            verifyExpectedException( databaseDoc, 
unsupportedMethods[i].unoInterfaceClass,
-                unsupportedMethods[i].methodName, new Object[]{}, null );
+        // ... it should be initialized
+        impl_checkDocumentInitState( databaseDoc, true );
+
+        // ....................................................................
+        // then 2.
+        databaseDoc = impl_cloneDocument( databaseDoc );
+
+        // load the doc, and verify it's initialized then, and has the proper 
URL
+        XLoadable loadDoc = (XLoadable)UnoRuntime.queryInterface( 
XLoadable.class, databaseDoc );
+        loadDoc.load( new PropertyValue[] { new PropertyValue( "URL", 0, 
documentURL, PropertyState.DIRECT_VALUE ) } );
+        databaseDoc.attachResource( documentURL, new PropertyValue[0] );
+
+        assureEquals( "wrong URL after loading the document", documentURL, 
databaseDoc.getURL() );
+        impl_checkDocumentInitState( databaseDoc, true );
+
+        // ....................................................................
+        // then 1.
+        databaseDoc = impl_cloneDocument( databaseDoc );
+        loadDoc = (XLoadable)UnoRuntime.queryInterface( XLoadable.class, 
databaseDoc );
+        loadDoc.initNew();
+        assureEquals( "wrong URL after initializing the document", "", 
databaseDoc.getURL() );
+        impl_checkDocumentInitState( databaseDoc, true );
         }
+
+    void onEventCallback( DocumentEvent _Event )
+    {
     }
 }




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to