summaryrefslogtreecommitdiffstats
path: root/odk/examples/java/Storage
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 16:51:28 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 16:51:28 +0000
commit940b4d1848e8c70ab7642901a68594e8016caffc (patch)
treeeb72f344ee6c3d9b80a7ecc079ea79e9fba8676d /odk/examples/java/Storage
parentInitial commit. (diff)
downloadlibreoffice-upstream.tar.xz
libreoffice-upstream.zip
Adding upstream version 1:7.0.4.upstream/1%7.0.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'odk/examples/java/Storage')
-rw-r--r--odk/examples/java/Storage/Makefile75
-rw-r--r--odk/examples/java/Storage/StorageFunctionality.java156
-rw-r--r--odk/examples/java/Storage/StorageTest.java25
-rw-r--r--odk/examples/java/Storage/Test01.java169
-rw-r--r--odk/examples/java/Storage/Test02.java165
-rw-r--r--odk/examples/java/Storage/Test03.java220
-rw-r--r--odk/examples/java/Storage/Test04.java282
-rw-r--r--odk/examples/java/Storage/Test05.java278
-rw-r--r--odk/examples/java/Storage/Test06.java285
-rw-r--r--odk/examples/java/Storage/Test07.java148
-rw-r--r--odk/examples/java/Storage/Test08.java224
-rw-r--r--odk/examples/java/Storage/Test09.java134
-rw-r--r--odk/examples/java/Storage/TestHelper.java881
13 files changed, 3042 insertions, 0 deletions
diff --git a/odk/examples/java/Storage/Makefile b/odk/examples/java/Storage/Makefile
new file mode 100644
index 000000000..3bf990b5d
--- /dev/null
+++ b/odk/examples/java/Storage/Makefile
@@ -0,0 +1,75 @@
+#
+# This file is part of the LibreOffice project.
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+# This file incorporates work covered by the following license notice:
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed
+# with this work for additional information regarding copyright
+# ownership. The ASF licenses this file to you under the Apache
+# License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
+#
+
+# Builds the Java Storage test example of the SDK.
+
+PRJ=../../..
+SETTINGS=$(PRJ)/settings
+
+include $(SETTINGS)/settings.mk
+include $(SETTINGS)/std.mk
+
+# Define non-platform/compiler specific settings
+COMPONENT_NAME=JavaStorageTestExample
+OUT_COMP_CLASS = $(OUT_CLASS)/$(COMPONENT_NAME)
+
+JAVAFILES = \
+ StorageFunctionality.java \
+ TestHelper.java \
+ StorageTest.java \
+ Test01.java \
+ Test02.java \
+ Test03.java \
+ Test04.java \
+ Test05.java \
+ Test06.java \
+ Test07.java \
+ Test08.java \
+ Test09.java
+
+CLASSFILES = $(patsubst %.java,$(OUT_COMP_CLASS)/%.class,$(JAVAFILES))
+
+SDK_CLASSPATH = $(subst $(EMPTYSTRING) $(PATH_SEPARATOR),$(PATH_SEPARATOR),$(CLASSPATH)\
+ $(PATH_SEPARATOR)$(OUT_COMP_CLASS))
+
+
+# Targets
+.PHONY: ALL
+ALL : \
+ JavaStorageTestExample
+
+include $(SETTINGS)/stdtarget.mk
+
+$(CLASSFILES) : $(JAVAFILES)
+ -$(MKDIR) $(subst /,$(PS),$(@D))
+ $(SDK_JAVAC) $(JAVAC_FLAGS) -classpath "$(SDK_CLASSPATH)" -d $(OUT_COMP_CLASS) $(JAVAFILES)
+
+JavaStorageTestExample : $(CLASSFILES)
+ @echo --------------------------------------------------------------------------------
+ @echo Please use following command to execute the example!
+ @echo ------
+ @echo $(MAKE) StorageFunctionality.run
+ @echo --------------------------------------------------------------------------------
+
+%.run: $(OUT_COMP_CLASS)/%.class
+ $(SDK_JAVA) -classpath "$(SDK_CLASSPATH)" $(basename $@)
+
+.PHONY: clean
+clean :
+ -$(DELRECURSIVE) $(subst /,$(PS),$(OUT_COMP_CLASS))
+
diff --git a/odk/examples/java/Storage/StorageFunctionality.java b/odk/examples/java/Storage/StorageFunctionality.java
new file mode 100644
index 000000000..5b848019b
--- /dev/null
+++ b/odk/examples/java/Storage/StorageFunctionality.java
@@ -0,0 +1,156 @@
+/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
+ */
+
+import com.sun.star.lang.XMultiServiceFactory;
+import com.sun.star.lang.XMultiComponentFactory;
+import com.sun.star.bridge.XUnoUrlResolver;
+import com.sun.star.uno.UnoRuntime;
+import com.sun.star.uno.XNamingService;
+import com.sun.star.uno.XComponentContext;
+
+import com.sun.star.lang.*;
+
+public class StorageFunctionality {
+
+ public static void main( String args[] )
+ {
+ // connect to the office
+ String sConnectionString = "uno:socket,host=localhost,port=8100;urp;StarOffice.NamingService";
+
+ // It is possible to use a different connection string, passed as argument
+ if ( args.length == 1 ) {
+ sConnectionString = args[0];
+ }
+
+ XMultiServiceFactory xMSF = null;
+
+ // create connection(s) and get multiservicefactory
+ try {
+ xMSF = connect( sConnectionString );
+
+ if ( xMSF == null )
+ {
+ System.out.println( "Error: Couldn't get MSF!" );
+ return;
+ }
+ } catch( Exception e ) {
+ System.out.println( "Error: Couldn't get MSF, exception: " + e );
+ return;
+ }
+
+ XSingleServiceFactory xStorageFactory = null;
+ try
+ {
+ Object oStorageFactory = xMSF.createInstance( "com.sun.star.embed.StorageFactory" );
+ xStorageFactory = UnoRuntime.queryInterface( XSingleServiceFactory.class,
+ oStorageFactory );
+
+ if ( xStorageFactory == null )
+ {
+ System.out.println( "Error: Can not get storage factory!" );
+ return;
+ }
+ }
+ catch ( Exception e )
+ {
+ System.out.println( "Error: Can't get storage factory, exception: " + e + "!" );
+ return;
+ }
+
+ boolean bTestsPassed = true;
+
+ final int nNumTests = 9;
+ StorageTest pTests[] = new StorageTest[nNumTests];
+ pTests[0] = new Test01( xMSF, xStorageFactory );
+ pTests[1] = new Test02( xMSF, xStorageFactory );
+ pTests[2] = new Test03( xMSF, xStorageFactory );
+ pTests[3] = new Test04( xMSF, xStorageFactory );
+ pTests[4] = new Test05( xMSF, xStorageFactory );
+ pTests[5] = new Test06( xMSF, xStorageFactory );
+ pTests[6] = new Test07( xMSF, xStorageFactory );
+ pTests[7] = new Test08( xMSF, xStorageFactory );
+ pTests[8] = new Test09( xMSF, xStorageFactory );
+
+ System.out.println( "\nstart testing\n" );
+
+ for ( int nInd = 0; nInd < nNumTests; nInd++ )
+ {
+ String sTestName = "Test" + ( ( nInd < 9 ) ? "0" : "" ) + ( nInd + 1 );
+
+ System.out.println( "======= Storage test " + sTestName + " started!" );
+ if ( pTests[nInd].test() )
+ System.out.println( "======= Storage test " + sTestName + " passed!" );
+ else
+ {
+ System.out.println( "======= Storage test " + sTestName + " failed!" );
+ bTestsPassed = false;
+ }
+ }
+
+ if ( bTestsPassed )
+ System.out.println( "\ntesting passed" );
+ else
+ System.out.println( "\ntesting failed" );
+
+ System.out.println( "done" );
+
+ System.exit( 0 );
+ }
+
+
+ public static XMultiServiceFactory connect( String sConnectStr )
+ throws com.sun.star.uno.Exception,
+ com.sun.star.uno.RuntimeException,
+ Exception
+ {
+ // Get component context
+ XComponentContext xComponentContext =
+ com.sun.star.comp.helper.Bootstrap.createInitialComponentContext(
+ null );
+
+ // initial serviceManager
+ XMultiComponentFactory xLocalServiceManager =
+ xComponentContext.getServiceManager();
+
+ // create a connector, so that it can contact the office
+ Object oUrlResolver = xLocalServiceManager.createInstanceWithContext(
+ "com.sun.star.bridge.UnoUrlResolver", xComponentContext );
+ XUnoUrlResolver xUrlResolver = UnoRuntime.queryInterface(
+ XUnoUrlResolver.class, oUrlResolver );
+
+ Object oInitialObject = xUrlResolver.resolve( sConnectStr );
+ XNamingService xName = UnoRuntime.queryInterface(
+ XNamingService.class, oInitialObject );
+
+ XMultiServiceFactory xMSF = null;
+ if( xName != null ) {
+ System.err.println( "got the remote naming service !" );
+ Object oMSF = xName.getRegisteredObject("StarOffice.ServiceManager" );
+
+ xMSF = UnoRuntime.queryInterface( XMultiServiceFactory.class, oMSF );
+ }
+ else
+ System.out.println( "Error: Can't get XNamingService interface from url resolver!" );
+
+ return xMSF;
+ }
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/odk/examples/java/Storage/StorageTest.java b/odk/examples/java/Storage/StorageTest.java
new file mode 100644
index 000000000..b146d2229
--- /dev/null
+++ b/odk/examples/java/Storage/StorageTest.java
@@ -0,0 +1,25 @@
+/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
+ */
+
+public interface StorageTest
+{
+ boolean test();
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/odk/examples/java/Storage/Test01.java b/odk/examples/java/Storage/Test01.java
new file mode 100644
index 000000000..649f28440
--- /dev/null
+++ b/odk/examples/java/Storage/Test01.java
@@ -0,0 +1,169 @@
+/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
+ */
+
+import com.sun.star.lang.XMultiServiceFactory;
+import com.sun.star.lang.XSingleServiceFactory;
+
+import com.sun.star.uno.UnoRuntime;
+import com.sun.star.embed.*;
+
+public class Test01 implements StorageTest {
+
+ XMultiServiceFactory m_xMSF;
+ XSingleServiceFactory m_xStorageFactory;
+ TestHelper m_aTestHelper;
+
+ public Test01( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
+ {
+ m_xMSF = xMSF;
+ m_xStorageFactory = xStorageFactory;
+ m_aTestHelper = new TestHelper( "Test01: " );
+ }
+
+ public boolean test()
+ {
+ try
+ {
+ String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
+ if ( sTempFileURL == null || sTempFileURL.equals("") )
+ {
+ m_aTestHelper.Error( "No valid temporary file was created!" );
+ return false;
+ }
+
+ // create temporary storage based on arbitrary medium
+ // after such a storage is closed it is lost
+ Object oTempStorage = m_xStorageFactory.createInstance();
+ XStorage xTempStorage = UnoRuntime.queryInterface( XStorage.class, oTempStorage );
+ if ( xTempStorage == null )
+ {
+ m_aTestHelper.Error( "Can't create temporary storage representation!" );
+ return false;
+ }
+
+ // open a new substorage
+ XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
+ "SubStorage1",
+ ElementModes.WRITE );
+ if ( xTempSubStorage == null )
+ {
+ m_aTestHelper.Error( "Can't create substorage!" );
+ return false;
+ }
+
+ byte pBytes1[] = { 1, 1, 1, 1, 1 };
+
+ // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
+ if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
+ return false;
+
+ byte pBytes2[] = { 2, 2, 2, 2, 2 };
+
+ // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
+ if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
+ return false;
+
+ // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
+ if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
+ "MediaType3",
+ true,
+ ElementModes.READWRITE ) )
+ return false;
+
+ // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
+ if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
+ "MediaType4",
+ false,
+ ElementModes.WRITE ) )
+ return false;
+
+ // create temporary storage based on a previously created temporary file
+ Object pArgs[] = new Object[2];
+ pArgs[0] = sTempFileURL;
+ pArgs[1] = Integer.valueOf( ElementModes.WRITE );
+
+ Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
+ XStorage xTempFileStorage = UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
+ if ( xTempFileStorage == null )
+ {
+ m_aTestHelper.Error( "Can't create storage based on temporary file!" );
+ return false;
+ }
+
+ // copy xTempStorage to xTempFileStorage
+ // xTempFileStorage will be automatically committed
+ if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
+ return false;
+
+ // dispose used storages to free resources
+ if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
+ return false;
+
+
+ // now check all the written and copied information
+
+
+ // the temporary file must not be locked any more after storage disposing
+ pArgs[1] = Integer.valueOf( ElementModes.READWRITE );
+ Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
+ XStorage xResultStorage = UnoRuntime.queryInterface( XStorage.class, oResultStorage );
+ if ( xResultStorage == null )
+ {
+ m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
+ return false;
+ }
+
+ if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.READWRITE ) )
+ return false;
+
+ // open existing substorage
+ XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
+ "SubStorage1",
+ ElementModes.READ );
+ if ( xResultSubStorage == null )
+ {
+ m_aTestHelper.Error( "Can't open existing substorage!" );
+ return false;
+ }
+
+ if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType4", false, ElementModes.READ ) )
+ return false;
+
+ if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream1", "MediaType1", pBytes1 ) )
+ return false;
+
+ if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream2", "MediaType2", pBytes2 ) )
+ return false;
+
+ // dispose used storages to free resources
+ if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
+ return false;
+
+ return true;
+ }
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Exception: " + e );
+ return false;
+ }
+ }
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/odk/examples/java/Storage/Test02.java b/odk/examples/java/Storage/Test02.java
new file mode 100644
index 000000000..92f5a8c81
--- /dev/null
+++ b/odk/examples/java/Storage/Test02.java
@@ -0,0 +1,165 @@
+/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
+ */
+
+
+import com.sun.star.lang.XMultiServiceFactory;
+import com.sun.star.lang.XSingleServiceFactory;
+
+import com.sun.star.uno.UnoRuntime;
+import com.sun.star.io.XStream;
+import com.sun.star.io.XInputStream;
+
+import com.sun.star.embed.*;
+
+public class Test02 implements StorageTest {
+
+ XMultiServiceFactory m_xMSF;
+ XSingleServiceFactory m_xStorageFactory;
+ TestHelper m_aTestHelper;
+
+ public Test02( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
+ {
+ m_xMSF = xMSF;
+ m_xStorageFactory = xStorageFactory;
+ m_aTestHelper = new TestHelper( "Test02: " );
+ }
+
+ public boolean test()
+ {
+ try
+ {
+ XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
+ if ( xTempFileStream == null )
+ return false;
+
+ // create storage based on the temporary stream
+ Object pArgs[] = new Object[2];
+ pArgs[0] = xTempFileStream;
+ pArgs[1] = Integer.valueOf( ElementModes.WRITE );
+
+ Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
+ XStorage xTempStorage = UnoRuntime.queryInterface( XStorage.class, oTempStorage );
+ if ( xTempStorage == null )
+ {
+ m_aTestHelper.Error( "Can't create temporary storage representation!" );
+ return false;
+ }
+
+ // open a new substorage
+ XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
+ "SubStorage1",
+ ElementModes.WRITE );
+ if ( xTempSubStorage == null )
+ {
+ m_aTestHelper.Error( "Can't create substorage!" );
+ return false;
+ }
+
+ byte pBytes1[] = { 1, 1, 1, 1, 1 };
+
+ // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
+ if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
+ return false;
+
+ // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
+ if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
+ "MediaType2",
+ true,
+ ElementModes.WRITE ) )
+ return false;
+
+ // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
+ if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
+ "MediaType3",
+ false,
+ ElementModes.WRITE ) )
+ return false;
+
+ // commit substorage first
+ if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
+ return false;
+
+ // commit the root storage so the contents must be stored now
+ if ( !m_aTestHelper.commitStorage( xTempStorage ) )
+ return false;
+
+ // dispose used storage to free resources
+ // the substorage dispose will be triggered by this call
+ if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
+ return false;
+
+
+
+ // now check all the written information
+
+
+ // close the output part of the temporary stream
+ // the output part must present since we already wrote to the stream
+ if ( !m_aTestHelper.closeOutput( xTempFileStream ) )
+ return false;
+
+ XInputStream xTempInStream = m_aTestHelper.getInputStream( xTempFileStream );
+ if ( xTempInStream == null )
+ return false;
+
+
+ // open input stream
+ // since no mode is provided the result storage must be opened readonly
+ Object pOneArg[] = new Object[1];
+ pOneArg[0] = xTempInStream;
+
+ Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pOneArg );
+ XStorage xResultStorage = UnoRuntime.queryInterface( XStorage.class, oResultStorage );
+ if ( xResultStorage == null )
+ {
+ m_aTestHelper.Error( "Can't open storage based on input stream!" );
+ return false;
+ }
+
+ if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType2", true, ElementModes.READ ) )
+ return false;
+
+ // open existing substorage
+ XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
+ "SubStorage1",
+ ElementModes.READ );
+ if ( xResultSubStorage == null )
+ {
+ m_aTestHelper.Error( "Can't open existing substorage!" );
+ return false;
+ }
+
+ if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType3", false, ElementModes.READ ) )
+ return false;
+
+ if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream1", "MediaType1", pBytes1 ) )
+ return false;
+
+ return true;
+ }
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Exception: " + e );
+ return false;
+ }
+ }
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/odk/examples/java/Storage/Test03.java b/odk/examples/java/Storage/Test03.java
new file mode 100644
index 000000000..500d46b92
--- /dev/null
+++ b/odk/examples/java/Storage/Test03.java
@@ -0,0 +1,220 @@
+/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
+ */
+
+import com.sun.star.lang.XMultiServiceFactory;
+import com.sun.star.lang.XSingleServiceFactory;
+
+import com.sun.star.uno.UnoRuntime;
+import com.sun.star.embed.*;
+import com.sun.star.container.XNameAccess;
+
+public class Test03 implements StorageTest {
+
+ XMultiServiceFactory m_xMSF;
+ XSingleServiceFactory m_xStorageFactory;
+ TestHelper m_aTestHelper;
+
+ public Test03( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
+ {
+ m_xMSF = xMSF;
+ m_xStorageFactory = xStorageFactory;
+ m_aTestHelper = new TestHelper( "Test03: " );
+ }
+
+ public boolean test()
+ {
+ try
+ {
+ // create temporary storage based on arbitrary medium
+ // after such a storage is closed it is lost
+ Object oTempStorage = m_xStorageFactory.createInstance();
+ XStorage xTempStorage = UnoRuntime.queryInterface( XStorage.class, oTempStorage );
+ if ( xTempStorage == null )
+ {
+ m_aTestHelper.Error( "Can't create temporary storage representation!" );
+ return false;
+ }
+
+ // open a new substorage
+ XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
+ "SubStorage1",
+ ElementModes.WRITE );
+ if ( xTempSubStorage == null )
+ {
+ m_aTestHelper.Error( "Can't create substorage!" );
+ return false;
+ }
+
+ byte pBytes1[] = { 1, 1, 1, 1, 1 };
+
+ // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
+ if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
+ return false;
+
+ byte pBytes2[] = { 2, 2, 2, 2, 2 };
+
+ // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
+ if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
+ return false;
+
+ // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
+ if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
+ "MediaType3",
+ false,
+ ElementModes.WRITE ) )
+ return false;
+
+ if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
+ return false;
+
+ if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
+ return false;
+
+
+ // check storage hierarchy tree
+
+
+ // check that isStorageElement() and isStreamElement reacts to nonexistent object correctly
+ try {
+ xTempStorage.isStorageElement( "does not exist" );
+ m_aTestHelper.Error( "Nonexistent element doesn't detected by isStorageElement() call!" );
+ return false;
+ }
+ catch( com.sun.star.container.NoSuchElementException ne )
+ {
+ }
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Wrong exception is thrown by isStorageElement() call: " + e );
+ return false;
+ }
+
+ try {
+ xTempStorage.isStreamElement( "does not exist" );
+ m_aTestHelper.Error( "Nonexistent element doesn't detected by isStreamElement() call!" );
+ return false;
+ }
+ catch( com.sun.star.container.NoSuchElementException ne )
+ {
+ }
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Wrong exception is thrown by isStreamElement() call: " + e );
+ return false;
+ }
+
+ XNameAccess xRootNameAccess = UnoRuntime.queryInterface( XNameAccess.class, xTempStorage );
+ if ( xRootNameAccess == null )
+ {
+ m_aTestHelper.Error( "Root storage doesn't support XNameAccess!" );
+ return false;
+ }
+
+ try {
+ if ( !xTempStorage.isStorageElement( "SubStorage1" ) || xTempStorage.isStreamElement( "SubStorage1" ) )
+ {
+ m_aTestHelper.Error( "Child 'SubStorage1' can not be detected as storage!" );
+ return false;
+ }
+
+ if ( xTempStorage.isStorageElement( "SubStream1" ) || !xTempStorage.isStreamElement( "SubStream1" ) )
+ {
+ m_aTestHelper.Error( "Child 'SubStream1' can not be detected as stream!" );
+ return false;
+ }
+ }
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Child's type can not be detected, exception: " + e );
+ return false;
+ }
+
+
+ // check that root storage contents are represented correctly
+ String sRootCont[] = xRootNameAccess.getElementNames();
+
+ if ( sRootCont.length != 2 )
+ {
+ m_aTestHelper.Error( "Root storage contains wrong amount of children!" );
+ return false;
+ }
+
+ if ( !( sRootCont[0].equals( "SubStorage1" ) && sRootCont[1].equals( "SubStream1" )
+ || sRootCont[0].equals( "SubStream1" ) && sRootCont[1].equals( "SubStorage1" ) )
+ || !( xRootNameAccess.hasByName( "SubStream1" ) && xRootNameAccess.hasByName( "SubStorage1" ) ) )
+ {
+ m_aTestHelper.Error( "Root storage contains wrong list of children!" );
+ return false;
+ }
+
+ // get storage through XNameAccess
+ XStorage xResultSubStorage = getStorageFromNameAccess( xRootNameAccess, "SubStorage1" );
+ if ( xResultSubStorage == null )
+ return false;
+
+ if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType3", false, ElementModes.READ ) )
+ return false;
+
+ XNameAccess xChildAccess = UnoRuntime.queryInterface( XNameAccess.class, xResultSubStorage );
+ if ( xChildAccess == null )
+ {
+ m_aTestHelper.Error( "Child storage doesn't support XNameAccess!" );
+ return false;
+ }
+
+ if ( !xChildAccess.hasByName( "SubStream2" )
+ || !xResultSubStorage.isStreamElement( "SubStream2" )
+ || xResultSubStorage.isStorageElement( "SubStream2" ) )
+ {
+ m_aTestHelper.Error( "'SubStream2' can not be detected as child stream element of 'SubStorage1'!" );
+ return false;
+ }
+
+ return true;
+ }
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Exception: " + e );
+ return false;
+ }
+ }
+
+ public XStorage getStorageFromNameAccess( XNameAccess xAccess, String sName )
+ {
+ try
+ {
+ Object oStorage = xAccess.getByName( sName );
+ XStorage xResult = UnoRuntime.queryInterface( XStorage.class, oStorage );
+
+ if ( xResult != null )
+ return xResult;
+ else
+ m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess!" );
+ }
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess, exception: " + e );
+ }
+
+ return null;
+ }
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/odk/examples/java/Storage/Test04.java b/odk/examples/java/Storage/Test04.java
new file mode 100644
index 000000000..535253b19
--- /dev/null
+++ b/odk/examples/java/Storage/Test04.java
@@ -0,0 +1,282 @@
+/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
+ */
+
+import com.sun.star.lang.XMultiServiceFactory;
+import com.sun.star.lang.XSingleServiceFactory;
+import com.sun.star.uno.UnoRuntime;
+import com.sun.star.container.XNameAccess;
+
+import com.sun.star.embed.*;
+
+public class Test04 implements StorageTest {
+
+ XMultiServiceFactory m_xMSF;
+ XSingleServiceFactory m_xStorageFactory;
+ TestHelper m_aTestHelper;
+
+ public Test04( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
+ {
+ m_xMSF = xMSF;
+ m_xStorageFactory = xStorageFactory;
+ m_aTestHelper = new TestHelper( "Test04: " );
+ }
+
+ public boolean test()
+ {
+ try
+ {
+ String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
+ if ( sTempFileURL == null || sTempFileURL.equals("") )
+ {
+ m_aTestHelper.Error( "No valid temporary file was created!" );
+ return false;
+ }
+
+ // create temporary storage based on arbitrary medium
+ // after such a storage is closed it is lost
+ Object oTempStorage = m_xStorageFactory.createInstance();
+ XStorage xTempStorage = UnoRuntime.queryInterface( XStorage.class, oTempStorage );
+ if ( xTempStorage == null )
+ {
+ m_aTestHelper.Error( "Can't create temporary storage representation!" );
+ return false;
+ }
+
+ // open substorages and create streams there
+
+ // first substorage of the root storage
+ XStorage xTempSubStorage1 = m_aTestHelper.openSubStorage( xTempStorage,
+ "SubStorage1",
+ ElementModes.WRITE );
+ if ( xTempSubStorage1 == null )
+ {
+ m_aTestHelper.Error( "Can't create substorage!" );
+ return false;
+ }
+
+ byte pBytes1[] = { 1, 1, 1, 1, 1 };
+
+ // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
+ if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage1, "SubStream1", "MediaType1", true, pBytes1 ) )
+ return false;
+
+ // second substorage of the root storage
+ XStorage xTempSubStorage2 = m_aTestHelper.openSubStorage( xTempStorage,
+ "SubStorage2",
+ ElementModes.WRITE );
+ if ( xTempSubStorage2 == null )
+ {
+ m_aTestHelper.Error( "Can't create substorage!" );
+ return false;
+ }
+
+ byte pBytes2[] = { 2, 2, 2, 2, 2 };
+
+ // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
+ if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage2, "SubStream2", "MediaType2", false, pBytes2 ) )
+ return false;
+
+ // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
+ if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
+ "MediaType3",
+ true,
+ ElementModes.READWRITE ) )
+ return false;
+
+ // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
+ if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage1,
+ "MediaType4",
+ false,
+ ElementModes.WRITE ) )
+ return false;
+
+ // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
+ if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage2,
+ "MediaType5",
+ false,
+ ElementModes.WRITE ) )
+ return false;
+
+ // create temporary storage based on a previously created temporary file
+ Object pArgs[] = new Object[2];
+ pArgs[0] = sTempFileURL;
+ pArgs[1] = Integer.valueOf( ElementModes.WRITE );
+
+ Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
+ XStorage xTempFileStorage = UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
+ if ( xTempFileStorage == null )
+ {
+ m_aTestHelper.Error( "Can't create storage based on temporary file!" );
+ return false;
+ }
+
+ if ( !m_aTestHelper.copyElementTo( xTempStorage, "SubStorage1", xTempFileStorage ) )
+ return false;
+
+ // if storage is not committed before disposing all the changes will be lost
+ if ( !m_aTestHelper.commitStorage( xTempSubStorage2 ) )
+ return false;
+
+ // a storage must be disposed before moving/removing otherwise the access will be denied
+ if ( !m_aTestHelper.disposeStorage( xTempSubStorage2 ) )
+ return false;
+
+ if ( !m_aTestHelper.moveElementTo( xTempStorage, "SubStorage2", xTempFileStorage ) )
+ return false;
+
+ // SubStorage2 must be removed and disposed now
+ try
+ {
+ xTempSubStorage2.isStreamElement( "SubStream2" );
+ m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
+ return false;
+ }
+ catch( com.sun.star.lang.DisposedException de )
+ {
+ }
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
+ return false;
+ }
+
+ if ( !m_aTestHelper.copyElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
+ return false;
+
+ if ( !m_aTestHelper.renameElement( xTempFileStorage, "SubStream1", "SubStream1_copy" ) )
+ return false;
+
+ if ( !m_aTestHelper.moveElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
+ return false;
+
+ if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
+ return false;
+
+ // dispose used storages to free resources
+ if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
+ return false;
+
+
+ // now check all the written and copied information
+
+
+ // the temporary file must not be locked any more after storage disposing
+ pArgs[1] = Integer.valueOf( ElementModes.READWRITE );
+ Object oResStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
+ XStorage xResStorage = UnoRuntime.queryInterface( XStorage.class, oResStorage );
+ if ( xResStorage == null )
+ {
+ m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
+ return false;
+ }
+
+ // open and check SubStorage1
+ XStorage xResSubStorage1 = m_aTestHelper.openSubStorage( xResStorage,
+ "SubStorage1",
+ ElementModes.READ );
+ if ( xResSubStorage1 == null )
+ {
+ m_aTestHelper.Error( "Can't open existing substorage!" );
+ return false;
+ }
+
+ if ( !m_aTestHelper.checkStorageProperties( xResSubStorage1, "MediaType4", false, ElementModes.READ ) )
+ return false;
+
+
+ // open and check SubStorage2
+ XStorage xResSubStorage2 = m_aTestHelper.openSubStorage( xResStorage,
+ "SubStorage2",
+ ElementModes.READ );
+ if ( xResSubStorage2 == null )
+ {
+ m_aTestHelper.Error( "Can't open existing substorage!" );
+ return false;
+ }
+
+ if ( !m_aTestHelper.checkStorageProperties( xResSubStorage2, "MediaType5", false, ElementModes.READ ) )
+ return false;
+
+
+ // check all the result streams
+
+ if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1", "MediaType1", pBytes1 ) )
+ return false;
+
+ if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1_copy", "MediaType1", pBytes1 ) )
+ return false;
+
+ if ( !m_aTestHelper.checkStream( xResSubStorage1, "SubStream1", "MediaType1", pBytes1 ) )
+ return false;
+
+ if ( !m_aTestHelper.checkStream( xResSubStorage2, "SubStream2", "MediaType2", pBytes2 ) )
+ return false;
+
+ // the storage must be disposed before removing
+ if ( !m_aTestHelper.disposeStorage( xResSubStorage2 ) )
+ return false;
+
+ // remove element and check that it was removed completely
+ if ( !m_aTestHelper.removeElement( xResStorage, "SubStorage2" ) )
+ return false;
+
+ try
+ {
+ XNameAccess xResAccess = UnoRuntime.queryInterface( XNameAccess.class, xResStorage );
+ if ( xResAccess.hasByName( "SubStorage2" ) )
+ m_aTestHelper.Error( "SubStorage2 must be removed already!" );
+ }
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Can't get access to root storage, exception: " + e );
+ return false;
+ }
+
+ try
+ {
+ xResSubStorage2.isStreamElement( "SubStream2" );
+
+ m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
+ return false;
+ }
+ catch( com.sun.star.lang.DisposedException de )
+ {
+ }
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
+ return false;
+ }
+
+ // dispose used storages to free resources
+ if ( !m_aTestHelper.disposeStorage( xResStorage ) )
+ return false;
+
+ return true;
+ }
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Exception: " + e );
+ return false;
+ }
+ }
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/odk/examples/java/Storage/Test05.java b/odk/examples/java/Storage/Test05.java
new file mode 100644
index 000000000..29cf99d52
--- /dev/null
+++ b/odk/examples/java/Storage/Test05.java
@@ -0,0 +1,278 @@
+/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
+ */
+
+import com.sun.star.lang.XMultiServiceFactory;
+import com.sun.star.lang.XSingleServiceFactory;
+
+import com.sun.star.uno.UnoRuntime;
+import com.sun.star.io.XStream;
+
+import com.sun.star.embed.*;
+
+public class Test05 implements StorageTest {
+
+ XMultiServiceFactory m_xMSF;
+ XSingleServiceFactory m_xStorageFactory;
+ TestHelper m_aTestHelper;
+
+ public Test05( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
+ {
+ m_xMSF = xMSF;
+ m_xStorageFactory = xStorageFactory;
+ m_aTestHelper = new TestHelper( "Test05: " );
+ }
+
+ public boolean test()
+ {
+ try
+ {
+ String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
+ if ( sTempFileURL == null || sTempFileURL.equals("") )
+ {
+ m_aTestHelper.Error( "No valid temporary file was created!" );
+ return false;
+ }
+
+ // create temporary storage based on a previously created temporary file
+ Object pArgs[] = new Object[2];
+ pArgs[0] = sTempFileURL;
+ pArgs[1] = Integer.valueOf( ElementModes.WRITE );
+
+ Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
+ XStorage xTempFileStorage = UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
+ if ( xTempFileStorage == null )
+ {
+ m_aTestHelper.Error( "Can't create storage based on temporary file!" );
+ return false;
+ }
+
+ // open a new substorage
+ XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempFileStorage,
+ "SubStorage1",
+ ElementModes.WRITE );
+ if ( xTempSubStorage == null )
+ {
+ m_aTestHelper.Error( "Can't create substorage!" );
+ return false;
+ }
+
+ // open a new substorage
+ XStorage xSubSubStorage = m_aTestHelper.openSubStorage( xTempSubStorage,
+ "SubSubStorage1",
+ ElementModes.WRITE );
+ if ( xSubSubStorage == null )
+ {
+ m_aTestHelper.Error( "Can't create substorage!" );
+ return false;
+ }
+
+
+ byte pBytes1[] = { 1, 1, 1, 1, 1 };
+
+ // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
+ if ( !m_aTestHelper.WriteBytesToSubstream( xSubSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
+ return false;
+
+ byte pBytes2[] = { 2, 2, 2, 2, 2 };
+
+ // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
+ if ( !m_aTestHelper.WriteBytesToSubstream( xSubSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
+ return false;
+
+ // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
+ if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempFileStorage,
+ "MediaType3",
+ true,
+ ElementModes.WRITE ) )
+ return false;
+
+ // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
+ if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
+ "MediaType4",
+ false,
+ ElementModes.WRITE ) )
+ return false;
+
+ // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
+ if ( !m_aTestHelper.setStorageTypeAndCheckProps( xSubSubStorage,
+ "MediaType5",
+ false,
+ ElementModes.WRITE ) )
+ return false;
+
+
+ // commit all the storages
+ if ( !m_aTestHelper.commitStorage( xSubSubStorage ) )
+ return false;
+
+ if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
+ return false;
+
+ if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
+ return false;
+
+ // try to open an opened substorage, open call must fail
+ if ( !m_aTestHelper.cantOpenStorage( xTempFileStorage, "SubStorage1" ) )
+ return false;
+
+
+ // reopen created streams
+ XStream xSubStream1 = m_aTestHelper.OpenStream( xSubSubStorage,
+ "SubStream1",
+ ElementModes.READWRITE | ElementModes.NOCREATE );
+ XStream xSubStream2 = m_aTestHelper.OpenStream( xSubSubStorage,
+ "SubStream2",
+ ElementModes.READ | ElementModes.NOCREATE );
+ if ( xSubStream1 == null || xSubStream2 == null )
+ return false;
+
+ // it should be possible to have more than one copy of stream for reading
+ XStream xSubStream2clone = m_aTestHelper.OpenStream( xSubSubStorage,
+ "SubStream2",
+ ElementModes.READ | ElementModes.NOCREATE );
+
+
+ // so now the first stream can not be open neither for reading nor for writing
+ if ( !m_aTestHelper.cantOpenStream( xSubSubStorage, "SubStream1", ElementModes.WRITE )
+ || !m_aTestHelper.cantOpenStream( xSubSubStorage, "SubStream1", ElementModes.READ ) )
+ return false;
+
+ // the second stream can not be open for writing
+ if ( !m_aTestHelper.cantOpenStream( xSubSubStorage, "SubStream2", ElementModes.WRITE ) )
+ return false;
+
+
+ // dispose xTestSubStorage, all the subtree must be disposed
+ if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
+ return false;
+
+ // check that subtree was disposed correctly
+ try
+ {
+ xSubSubStorage.isStreamElement( "SubStream1" );
+ m_aTestHelper.Error( "Substorage was not disposed!" );
+ return false;
+ }
+ catch ( com.sun.star.lang.DisposedException de )
+ {}
+ catch ( Exception e )
+ {
+ m_aTestHelper.Error( "Wrong exception is thrown by disposed storage: " + e );
+ return false;
+ }
+
+ try
+ {
+ xSubStream1.getInputStream();
+ m_aTestHelper.Error( "Writeable substream was not disposed!" );
+ return false;
+ }
+ catch ( com.sun.star.lang.DisposedException de )
+ {}
+ catch ( Exception e )
+ {
+ m_aTestHelper.Error( "Wrong exception is thrown by disposed stream: " + e );
+ return false;
+ }
+
+ try
+ {
+ xSubStream2.getInputStream();
+ m_aTestHelper.Error( "Readonly substream was not disposed!" );
+ return false;
+ }
+ catch ( com.sun.star.lang.DisposedException de )
+ {}
+ catch ( Exception e )
+ {
+ m_aTestHelper.Error( "Wrong exception is thrown by disposed stream: " + e );
+ return false;
+ }
+
+
+ // dispose root storage
+ if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
+ return false;
+
+
+
+ // now check all the written and copied information
+
+
+ pArgs[1] = Integer.valueOf( ElementModes.READ );
+ Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
+ XStorage xResultStorage = UnoRuntime.queryInterface( XStorage.class, oResultStorage );
+ if ( xResultStorage == null )
+ {
+ m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
+ return false;
+ }
+
+ if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.READ ) )
+ return false;
+
+ // open existing substorage
+ XStorage xResSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
+ "SubStorage1",
+ ElementModes.READ );
+ if ( xResSubStorage == null )
+ {
+ m_aTestHelper.Error( "Can't open existing substorage 'SubSubStorage'!" );
+ return false;
+ }
+
+ if ( !m_aTestHelper.checkStorageProperties( xResSubStorage, "MediaType4", false, ElementModes.READ ) )
+ return false;
+
+ // open existing substorage
+ XStorage xResSubSubStorage = m_aTestHelper.openSubStorage( xResSubStorage,
+ "SubSubStorage1",
+ ElementModes.READ );
+ if ( xResSubSubStorage == null )
+ {
+ m_aTestHelper.Error( "Can't open existing substorage 'SubSubStorage'!" );
+ return false;
+ }
+
+ if ( !m_aTestHelper.checkStorageProperties( xResSubSubStorage, "MediaType5", false, ElementModes.READ ) )
+ return false;
+
+ // check substreams
+ if ( !m_aTestHelper.checkStream( xResSubSubStorage, "SubStream1", "MediaType1", pBytes1 ) )
+ return false;
+
+ if ( !m_aTestHelper.checkStream( xResSubSubStorage, "SubStream2", "MediaType2", pBytes2 ) )
+ return false;
+
+ // dispose used storages to free resources
+ if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
+ return false;
+
+ return true;
+ }
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Exception: " + e );
+ return false;
+ }
+ }
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/odk/examples/java/Storage/Test06.java b/odk/examples/java/Storage/Test06.java
new file mode 100644
index 000000000..027a4a2c1
--- /dev/null
+++ b/odk/examples/java/Storage/Test06.java
@@ -0,0 +1,285 @@
+/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
+ */
+
+import com.sun.star.lang.XMultiServiceFactory;
+import com.sun.star.lang.XSingleServiceFactory;
+
+import com.sun.star.uno.UnoRuntime;
+import com.sun.star.embed.*;
+
+public class Test06 implements StorageTest {
+
+ XMultiServiceFactory m_xMSF;
+ XSingleServiceFactory m_xStorageFactory;
+ TestHelper m_aTestHelper;
+
+ public Test06( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
+ {
+ m_xMSF = xMSF;
+ m_xStorageFactory = xStorageFactory;
+ m_aTestHelper = new TestHelper( "Test06: " );
+ }
+
+ public boolean test()
+ {
+ try
+ {
+ // create temporary storage based on arbitrary medium
+ // after such a storage is closed it is lost
+ Object oTempStorage = m_xStorageFactory.createInstance();
+ XStorage xTempStorage = UnoRuntime.queryInterface( XStorage.class, oTempStorage );
+ if ( xTempStorage == null )
+ {
+ m_aTestHelper.Error( "Can't create temporary storage representation!" );
+ return false;
+ }
+
+ try
+ {
+ xTempStorage.copyToStorage( null );
+ m_aTestHelper.Error( "The method must throw an exception because of illegal parameter!" );
+ return false;
+ }
+ catch( com.sun.star.lang.IllegalArgumentException iae )
+ {}
+ catch( com.sun.star.uno.Exception ue )
+ {}
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Unexpected exception because of illegal parameter : " + e );
+ return false;
+ }
+
+ // open new substorages
+ XStorage xTempSubStorage1 = m_aTestHelper.openSubStorage( xTempStorage,
+ "SubStorage1",
+ ElementModes.WRITE );
+ XStorage xTempSubStorage2 = m_aTestHelper.openSubStorage( xTempStorage,
+ "SubStorage2",
+ ElementModes.WRITE );
+ if ( xTempSubStorage1 == null || xTempSubStorage2 == null )
+ {
+ m_aTestHelper.Error( "Can't create substorage!" );
+ return false;
+ }
+
+ // in case stream is open for reading it must exist
+ try
+ {
+ xTempSubStorage1.openStreamElement( "NonExistingStream", ElementModes.READ );
+ m_aTestHelper.Error( "The method must throw an exception in case of try to open nonexistent stream for reading!" );
+ return false;
+ }
+ catch( com.sun.star.uno.Exception ue )
+ {}
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Unexpected exception in case of try to open nonexistent stream for reading : " + e );
+ return false;
+ }
+
+ // in case a storage is open for reading it must exist
+ try
+ {
+ xTempSubStorage1.openStreamElement( "NonExistingStorage", ElementModes.READ );
+ m_aTestHelper.Error( "The method must throw an exception in case of try to open nonexistent storage for reading!" );
+ return false;
+ }
+ catch( com.sun.star.uno.Exception ue )
+ {}
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Unexpected exception in case of try to open nonexistent storage for reading : " + e );
+ return false;
+ }
+
+ // in case of removing nonexistent element an exception must be thrown
+ try
+ {
+ xTempSubStorage1.removeElement( "NonExistingElement" );
+ m_aTestHelper.Error( "An exception must be thrown in case of removing nonexistent element!" );
+ return false;
+ }
+ catch( com.sun.star.container.NoSuchElementException ne )
+ {}
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Unexpected exception in case of try to remove nonexistent element : " + e );
+ return false;
+ }
+
+ // in case of renaming of nonexistent element an exception must be thrown
+ try
+ {
+ xTempSubStorage1.renameElement( "NonExistingElement", "NewName" );
+ m_aTestHelper.Error( "An exception must be thrown in case of renaming nonexistent element!" );
+ return false;
+ }
+ catch( com.sun.star.container.NoSuchElementException ne )
+ {}
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Unexpected exception in case of try to rename nonexistent element : " + e );
+ return false;
+ }
+
+ // in case of renaming to a name of existent element an exception must be thrown
+ try
+ {
+ xTempStorage.renameElement( "SubStorage1", "SubStorage2" );
+ m_aTestHelper.Error( "An exception must be thrown in case of renaming to the name of existent element!" );
+ return false;
+ }
+ catch( com.sun.star.container.ElementExistException ee )
+ {}
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Unexpected exception in case of try to rename to the name of existent element : " + e );
+ return false;
+ }
+
+ // in case of copying target storage must be provided
+ try
+ {
+ xTempStorage.copyElementTo( "SubStorage1", null, "SubStorage1" );
+ m_aTestHelper.Error( "An exception must be thrown in case empty reference is provided as target for copying!" );
+ return false;
+ }
+ catch( com.sun.star.lang.IllegalArgumentException iae )
+ {}
+ catch( com.sun.star.uno.Exception ue )
+ {}
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Unexpected exception in case empty reference is provided as target for copying : " + e );
+ return false;
+ }
+
+ // in case of moving target storage must be provided
+ try
+ {
+ xTempStorage.moveElementTo( "SubStorage1", null, "SubStorage1" );
+ m_aTestHelper.Error( "An exception must be thrown in case empty reference is provided as target for moving!" );
+ return false;
+ }
+ catch( com.sun.star.lang.IllegalArgumentException iae )
+ {}
+ catch( com.sun.star.uno.Exception ue )
+ {}
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Unexpected exception in case empty reference is provided as target for moving : " + e );
+ return false;
+ }
+
+
+ // prepare target for further testings
+
+ // create new temporary storage based on arbitrary medium
+ Object oTargetStorage = m_xStorageFactory.createInstance();
+ XStorage xTargetStorage = UnoRuntime.queryInterface( XStorage.class, oTargetStorage );
+ if ( xTargetStorage == null )
+ {
+ m_aTestHelper.Error( "Can't create temporary storage representation!" );
+ return false;
+ }
+
+ // open a new substorage
+ XStorage xTargetSubStorage = m_aTestHelper.openSubStorage( xTargetStorage,
+ "SubStorage1",
+ ElementModes.WRITE );
+ if ( xTargetSubStorage == null )
+ {
+ m_aTestHelper.Error( "Can't create substorage!" );
+ return false;
+ }
+
+ // in case of copying of nonexistent element an exception must be thrown
+ try
+ {
+ xTempStorage.copyElementTo( "Nonexistent element", xTargetStorage, "Target" );
+ m_aTestHelper.Error( "An exception must be thrown in case of copying of nonexistent element!" );
+ return false;
+ }
+ catch( com.sun.star.container.NoSuchElementException ne )
+ {}
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Unexpected exception in case of copying of nonexistent element: " + e );
+ return false;
+ }
+
+ // in case of moving of nonexistent element an exception must be thrown
+ try
+ {
+ xTempStorage.moveElementTo( "Nonexistent element", xTargetStorage, "Target" );
+ m_aTestHelper.Error( "An exception must be thrown in case of moving of nonexistent element!" );
+ return false;
+ }
+ catch( com.sun.star.container.NoSuchElementException ne )
+ {}
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Unexpected exception in case of moving of nonexistent element: " + e );
+ return false;
+ }
+
+ // in case target for copying already exists an exception must be thrown
+ try
+ {
+ xTempStorage.copyElementTo( "SubStorage1", xTargetStorage, "SubStorage1" );
+ m_aTestHelper.Error( "An exception must be thrown in case target for copying already exists!" );
+ return false;
+ }
+ catch( com.sun.star.container.ElementExistException ee )
+ {}
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Unexpected exception in case target for copying already exists: " + e );
+ return false;
+ }
+
+ // in case target for moving already exists an exception must be thrown
+ try
+ {
+ xTempStorage.moveElementTo( "SubStorage1", xTargetStorage, "SubStorage1" );
+ m_aTestHelper.Error( "An exception must be thrown in case target for moving already exists!" );
+ return false;
+ }
+ catch( com.sun.star.container.ElementExistException ee )
+ {}
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Unexpected exception in case target for moving already exists: " + e );
+ return false;
+ }
+
+
+ return true;
+ }
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Exception: " + e );
+ return false;
+ }
+ }
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/odk/examples/java/Storage/Test07.java b/odk/examples/java/Storage/Test07.java
new file mode 100644
index 000000000..d96b0e1eb
--- /dev/null
+++ b/odk/examples/java/Storage/Test07.java
@@ -0,0 +1,148 @@
+/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
+ */
+
+import com.sun.star.lang.XMultiServiceFactory;
+import com.sun.star.lang.XSingleServiceFactory;
+
+import com.sun.star.uno.UnoRuntime;
+import com.sun.star.embed.*;
+
+public class Test07 implements StorageTest {
+
+ XMultiServiceFactory m_xMSF;
+ XSingleServiceFactory m_xStorageFactory;
+ TestHelper m_aTestHelper;
+
+ public Test07( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
+ {
+ m_xMSF = xMSF;
+ m_xStorageFactory = xStorageFactory;
+ m_aTestHelper = new TestHelper( "Test07: " );
+ }
+
+ public boolean test()
+ {
+ try
+ {
+ String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
+ if ( sTempFileURL == null || sTempFileURL.equals("") )
+ {
+ m_aTestHelper.Error( "No valid temporary file was created!" );
+ return false;
+ }
+
+ // create temporary storage based on arbitrary medium
+ // after such a storage is closed it is lost
+ Object oTempStorage = m_xStorageFactory.createInstance();
+ XStorage xTempStorage = UnoRuntime.queryInterface( XStorage.class, oTempStorage );
+ if ( xTempStorage == null )
+ {
+ m_aTestHelper.Error( "Can't create temporary storage representation!" );
+ return false;
+ }
+
+ byte pBytes1[] = { 1, 1, 1, 1, 1 };
+ byte pPass1[] = { 1, 2, 3, 4, 5 };
+
+ // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
+ if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes1, pPass1 ) )
+ return false;
+
+ byte pBytes2[] = { 2, 2, 2, 2, 2 };
+ byte pPass2[] = { 5, 4, 3, 2, 1 };
+
+ // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
+ if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream2", "MediaType2", false, pBytes2, pPass2 ) )
+ return false;
+
+ // create temporary storage based on a previously created temporary file
+ Object pArgs[] = new Object[2];
+ pArgs[0] = sTempFileURL;
+ pArgs[1] = Integer.valueOf( ElementModes.WRITE );
+
+ Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
+ XStorage xTempFileStorage = UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
+ if ( xTempFileStorage == null )
+ {
+ m_aTestHelper.Error( "Can't create storage based on temporary file!" );
+ return false;
+ }
+
+ // copy xTempStorage to xTempFileStorage
+ // xTempFileStorage will be automatically committed
+ if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
+ return false;
+
+ // dispose used storages to free resources
+ if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
+ return false;
+
+
+ // now check all the written and copied information
+
+
+ // the temporary file must not be locked any more after storage disposing
+ pArgs[1] = Integer.valueOf( ElementModes.READWRITE );
+ Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
+ XStorage xResultStorage = UnoRuntime.queryInterface( XStorage.class, oResultStorage );
+ if ( xResultStorage == null )
+ {
+ m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
+ return false;
+ }
+
+ Object o2CopyStorage = m_xStorageFactory.createInstance();
+ XStorage x2CopyStorage = UnoRuntime.queryInterface( XStorage.class, o2CopyStorage );
+ if ( x2CopyStorage == null )
+ {
+ m_aTestHelper.Error( "Can't create temporary storage representation!" );
+ return false;
+ }
+
+ if ( !m_aTestHelper.copyStorage( xResultStorage, x2CopyStorage ) )
+ return false;
+
+ if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream1", "MediaType1", pBytes1, pPass1 ) )
+ return false;
+
+ if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream2", "MediaType2", pBytes2, pPass2 ) )
+ return false;
+
+ if ( !m_aTestHelper.checkEncrStream( x2CopyStorage, "SubStream1", "MediaType1", pBytes1, pPass1 ) )
+ return false;
+
+ if ( !m_aTestHelper.checkEncrStream( x2CopyStorage, "SubStream2", "MediaType2", pBytes2, pPass2 ) )
+ return false;
+
+ // dispose used storages to free resources
+ if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
+ return false;
+
+ return true;
+ }
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Exception: " + e );
+ return false;
+ }
+ }
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/odk/examples/java/Storage/Test08.java b/odk/examples/java/Storage/Test08.java
new file mode 100644
index 000000000..323e2d78c
--- /dev/null
+++ b/odk/examples/java/Storage/Test08.java
@@ -0,0 +1,224 @@
+/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
+ */
+
+import com.sun.star.lang.XMultiServiceFactory;
+import com.sun.star.lang.XSingleServiceFactory;
+
+import com.sun.star.uno.UnoRuntime;
+import com.sun.star.embed.*;
+
+public class Test08 implements StorageTest {
+
+ XMultiServiceFactory m_xMSF;
+ XSingleServiceFactory m_xStorageFactory;
+ TestHelper m_aTestHelper;
+
+ public Test08( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
+ {
+ m_xMSF = xMSF;
+ m_xStorageFactory = xStorageFactory;
+ m_aTestHelper = new TestHelper( "Test08: " );
+ }
+
+ public boolean test()
+ {
+ try
+ {
+
+ // create temporary storage based on arbitrary medium
+ // after such a storage is closed it is lost
+ Object oTempStorage = m_xStorageFactory.createInstance();
+ XStorage xTempStorage = UnoRuntime.queryInterface( XStorage.class, oTempStorage );
+ if ( xTempStorage == null )
+ {
+ m_aTestHelper.Error( "Can't create temporary storage representation!" );
+ return false;
+ }
+
+ // set the global password for the root storage
+ XEncryptionProtectedSource xTempStorageEncryption =
+ UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xTempStorage );
+
+ if ( xTempStorageEncryption == null )
+ {
+ m_aTestHelper.Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
+ return true;
+ }
+
+ byte pPass1[] = { 1, 2, 3 };
+ byte pPass2[] = { 3, 2, 1 };
+
+ try {
+ xTempStorageEncryption.setEncryptionPassword( new String(pPass1) );
+ }
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
+ return false;
+ }
+
+ // open a new substorage
+ XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
+ "SubStorage1",
+ ElementModes.WRITE );
+ if ( xTempSubStorage == null )
+ {
+ m_aTestHelper.Error( "Can't create substorage!" );
+ return false;
+ }
+
+ // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
+ // the stream will be encrypted with common password
+ byte pBytes1[] = { 1, 1, 1, 1, 1 };
+ if ( !m_aTestHelper.WBToSubstrOfEncr( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1, true ) )
+ return false;
+
+ // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
+ // the stream will not be encrypted
+ byte pBytes2[] = { 2, 2, 2, 2, 2 };
+ if ( !m_aTestHelper.WBToSubstrOfEncr( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes2, false ) )
+ return false;
+
+ // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
+ // the stream will be compressed with own password
+ byte pBytes3[] = { 3, 3, 3, 3, 3 };
+
+ // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
+ // the stream will not be encrypted
+ if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempSubStorage, "SubStream3", "MediaType3", false, pBytes3, pPass2 ) )
+ return false;
+
+ // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
+ if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
+ "MediaType4",
+ true,
+ ElementModes.READWRITE ) )
+ return false;
+
+ // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
+ if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
+ "MediaType5",
+ false,
+ ElementModes.WRITE ) )
+ return false;
+
+ // create temporary file
+ String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
+ if ( sTempFileURL == null || sTempFileURL.equals("") )
+ {
+ m_aTestHelper.Error( "No valid temporary file was created!" );
+ return false;
+ }
+
+ // create temporary storage based on a previously created temporary file
+ Object pArgs[] = new Object[2];
+ pArgs[0] = sTempFileURL;
+ pArgs[1] = Integer.valueOf( ElementModes.WRITE );
+
+ Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
+ XStorage xTempFileStorage = UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
+ if ( xTempFileStorage == null )
+ {
+ m_aTestHelper.Error( "Can't create storage based on temporary file!" );
+ return false;
+ }
+
+ // copy xTempStorage to xTempFileStorage
+ // xTempFileStorage will be automatically committed
+ if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
+ return false;
+
+ // dispose used storages to free resources
+ if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
+ return false;
+
+
+ // now check all the written and copied information
+
+
+ // the temporary file must not be locked any more after storage disposing
+ pArgs[1] = Integer.valueOf( ElementModes.READ );
+ Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
+ XStorage xResultStorage = UnoRuntime.queryInterface( XStorage.class, oResultStorage );
+ if ( xResultStorage == null )
+ {
+ m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
+ return false;
+ }
+
+ if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType4", true, ElementModes.READ ) )
+ return false;
+
+ // open existing substorage
+ XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
+ "SubStorage1",
+ ElementModes.READ );
+ if ( xResultSubStorage == null )
+ {
+ m_aTestHelper.Error( "Can't open existing substorage!" );
+ return false;
+ }
+
+ if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType5", false, ElementModes.READ ) )
+ return false;
+
+ // set the global password for the root storage
+ XEncryptionProtectedSource xResultStorageEncryption =
+ UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xResultStorage );
+
+ if ( xResultStorageEncryption == null )
+ {
+ m_aTestHelper.Error( "XEncryptionProtectedSource was successfully used already, so it must be supported!" );
+ return false;
+ }
+
+ try {
+ xResultStorageEncryption.setEncryptionPassword( new String(pPass2) );
+ }
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
+ return false;
+ }
+
+ if ( !m_aTestHelper.checkEncrStream( xResultSubStorage, "SubStream1", "MediaType1", pBytes1, pPass1 ) )
+ return false;
+
+ if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream2", "MediaType2", pBytes2 ) )
+ return false;
+
+ // the common root storage password should allow to open this stream
+ if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream3", "MediaType3", pBytes3 ) )
+ return false;
+
+ // dispose used storages to free resources
+ if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
+ return false;
+
+ return true;
+ }
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Exception: " + e );
+ return false;
+ }
+ }
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/odk/examples/java/Storage/Test09.java b/odk/examples/java/Storage/Test09.java
new file mode 100644
index 000000000..131ff06d8
--- /dev/null
+++ b/odk/examples/java/Storage/Test09.java
@@ -0,0 +1,134 @@
+/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
+ */
+
+import com.sun.star.lang.XMultiServiceFactory;
+import com.sun.star.lang.XSingleServiceFactory;
+
+import com.sun.star.uno.UnoRuntime;
+import com.sun.star.embed.*;
+
+public class Test09 implements StorageTest {
+
+ XMultiServiceFactory m_xMSF;
+ XSingleServiceFactory m_xStorageFactory;
+ TestHelper m_aTestHelper;
+
+ public Test09( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
+ {
+ m_xMSF = xMSF;
+ m_xStorageFactory = xStorageFactory;
+ m_aTestHelper = new TestHelper( "Test09: " );
+ }
+
+ public boolean test()
+ {
+ try
+ {
+
+ // create temporary storage based on arbitrary medium
+ // after such a storage is closed it is lost
+ Object oTempStorage = m_xStorageFactory.createInstance();
+ XStorage xTempStorage = UnoRuntime.queryInterface( XStorage.class, oTempStorage );
+ if ( xTempStorage == null )
+ {
+ m_aTestHelper.Error( "Can't create temporary storage representation!" );
+ return false;
+ }
+
+ byte pPass1[] = { 1, 2, 3 };
+ byte pPass2[] = { 3, 2, 1 };
+ byte pBytes[] = { 1, 1, 1, 1, 1 };
+
+ // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
+ // the stream will not be encrypted
+ if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream1", "MediaType1", false, pBytes, pPass1 ) )
+ return false;
+
+ // create temporary file
+ String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
+ if ( sTempFileURL == null || sTempFileURL.equals( "" ) )
+ {
+ m_aTestHelper.Error( "No valid temporary file was created!" );
+ return false;
+ }
+
+ // create temporary storage based on a previously created temporary file
+ Object pArgs[] = new Object[2];
+ pArgs[0] = sTempFileURL;
+ pArgs[1] = Integer.valueOf( ElementModes.WRITE );
+
+ Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
+ XStorage xTempFileStorage = UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
+ if ( xTempFileStorage == null )
+ {
+ m_aTestHelper.Error( "Can't create storage based on temporary file!" );
+ return false;
+ }
+
+ // copy xTempStorage to xTempFileStorage
+ // xTempFileStorage will be automatically committed
+ if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
+ return false;
+
+ // change password of the substream of new storage based on file
+ int nResult = m_aTestHelper.ChangeStreamPass( xTempFileStorage, "SubStream1", pPass1, pPass2 );
+ if ( nResult == 0 )
+ return false; // test failed
+ else if ( nResult == -1 )
+ return true; // tested optional feature is not supported
+
+ if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
+ return false;
+
+ // dispose used storages to free resources
+ if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
+ return false;
+
+
+ // now check all the written and copied information
+
+
+ // the temporary file must not be locked any more after storage disposing
+ pArgs[1] = Integer.valueOf( ElementModes.READ );
+ Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
+ XStorage xResultStorage = UnoRuntime.queryInterface( XStorage.class, oResultStorage );
+ if ( xResultStorage == null )
+ {
+ m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
+ return false;
+ }
+
+ if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream1", "MediaType1", pBytes, pPass2 ) )
+ return false;
+
+ // dispose used storages to free resources
+ if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
+ return false;
+
+ return true;
+ }
+ catch( Exception e )
+ {
+ m_aTestHelper.Error( "Exception: " + e );
+ return false;
+ }
+ }
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/odk/examples/java/Storage/TestHelper.java b/odk/examples/java/Storage/TestHelper.java
new file mode 100644
index 000000000..a486f1d53
--- /dev/null
+++ b/odk/examples/java/Storage/TestHelper.java
@@ -0,0 +1,881 @@
+/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
+ */
+
+import com.sun.star.uno.UnoRuntime;
+import com.sun.star.uno.AnyConverter;
+
+import com.sun.star.lang.*;
+import com.sun.star.embed.*;
+import com.sun.star.packages.*;
+import com.sun.star.io.*;
+import com.sun.star.beans.*;
+
+public class TestHelper {
+
+ String m_sTestPrefix;
+
+ public TestHelper( String sTestPrefix )
+ {
+ m_sTestPrefix = sTestPrefix;
+ }
+
+ public boolean WriteBytesToStream( XStream xStream,
+ String sStreamName,
+ String sMediaType,
+ boolean bCompressed,
+ byte[] pBytes )
+ {
+ // get output stream of substream
+ XOutputStream xOutput = xStream.getOutputStream();
+ if ( xOutput == null )
+ {
+ Error( "Can't get XOutputStream implementation from substream '" + sStreamName + "'!" );
+ return false;
+ }
+
+ // get XTruncate implementation from output stream
+ XTruncate xTruncate = UnoRuntime.queryInterface( XTruncate.class, xOutput );
+ if ( xTruncate == null )
+ {
+ Error( "Can't get XTruncate implementation from substream '" + sStreamName + "'!" );
+ return false;
+ }
+
+ // write requested byte sequence
+ try
+ {
+ xTruncate.truncate();
+ xOutput.writeBytes( pBytes );
+ }
+ catch( Exception e )
+ {
+ Error( "Can't write to stream '" + sStreamName + "', exception: " + e );
+ return false;
+ }
+
+ // get access to the XPropertySet interface
+ XPropertySet xPropSet = UnoRuntime.queryInterface( XPropertySet.class, xStream );
+ if ( xPropSet == null )
+ {
+ Error( "Can't get XPropertySet implementation from substream '" + sStreamName + "'!" );
+ return false;
+ }
+
+ // set properties to the stream
+ try
+ {
+ xPropSet.setPropertyValue( "MediaType", sMediaType );
+ xPropSet.setPropertyValue( "Compressed", Boolean.valueOf( bCompressed ) );
+ }
+ catch( Exception e )
+ {
+ Error( "Can't set properties to substream '" + sStreamName + "', exception: " + e );
+ return false;
+ }
+
+ // check size property of the stream
+ try
+ {
+ int nSize = AnyConverter.toInt( xPropSet.getPropertyValue( "Size" ) );
+ if ( nSize != pBytes.length )
+ {
+ Error( "The 'Size' property of substream '" + sStreamName + "' contains wrong value!" );
+ return false;
+ }
+ }
+ catch( Exception e )
+ {
+ Error( "Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e );
+ return false;
+ }
+
+ // free the stream resources, garbage collector may remove the object too late
+ XComponent xComponent = UnoRuntime.queryInterface( XComponent.class, xStream );
+ if ( xComponent == null )
+ {
+ Error( "Can't get XComponent implementation from substream '" + sStreamName + "'!" );
+ return false;
+ }
+ xComponent.dispose();
+
+ return true;
+ }
+
+
+ public boolean WriteBytesToSubstream( XStorage xStorage,
+ String sStreamName,
+ String sMediaType,
+ boolean bCompressed,
+ byte[] pBytes )
+ {
+ // open substream element
+ XStream xSubStream = null;
+ try
+ {
+ Object oSubStream = xStorage.openStreamElement( sStreamName, ElementModes.WRITE );
+ xSubStream = UnoRuntime.queryInterface( XStream.class, oSubStream );
+ if ( xSubStream == null )
+ {
+ Error( "Can't create substream '" + sStreamName + "'!" );
+ return false;
+ }
+ }
+ catch( Exception e )
+ {
+ Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
+ return false;
+ }
+
+ return WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes );
+ }
+
+ public boolean WriteBytesToEncrSubstream( XStorage xStorage,
+ String sStreamName,
+ String sMediaType,
+ boolean bCompressed,
+ byte[] pBytes,
+ byte[] pPass )
+ {
+ // open substream element
+ XStream xSubStream = null;
+ try
+ {
+ Object oSubStream = xStorage.openEncryptedStreamElement( sStreamName, ElementModes.WRITE, new String(pPass) );
+ xSubStream = UnoRuntime.queryInterface( XStream.class, oSubStream );
+ if ( xSubStream == null )
+ {
+ Error( "Can't create substream '" + sStreamName + "'!" );
+ return false;
+ }
+ }
+ catch( Exception e )
+ {
+ Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
+ return false;
+ }
+
+ return WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes );
+ }
+
+ public boolean WBToSubstrOfEncr( XStorage xStorage,
+ String sStreamName,
+ String sMediaType,
+ boolean bCompressed,
+ byte[] pBytes,
+ boolean bEncrypted )
+ {
+ // open substream element
+ XStream xSubStream = null;
+ try
+ {
+ Object oSubStream = xStorage.openStreamElement( sStreamName, ElementModes.WRITE );
+ xSubStream = UnoRuntime.queryInterface( XStream.class, oSubStream );
+ if ( xSubStream == null )
+ {
+ Error( "Can't create substream '" + sStreamName + "'!" );
+ return false;
+ }
+ }
+ catch( Exception e )
+ {
+ Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
+ return false;
+ }
+
+ // get access to the XPropertySet interface
+ XPropertySet xPropSet = UnoRuntime.queryInterface( XPropertySet.class, xSubStream );
+ if ( xPropSet == null )
+ {
+ Error( "Can't get XPropertySet implementation from substream '" + sStreamName + "'!" );
+ return false;
+ }
+
+ // set properties to the stream
+ try
+ {
+ xPropSet.setPropertyValue( "Encrypted", Boolean.valueOf( bEncrypted ) );
+ }
+ catch( Exception e )
+ {
+ Error( "Can't set 'Encrypted' property to substream '" + sStreamName + "', exception: " + e );
+ return false;
+ }
+
+ return WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes );
+ }
+
+ public int ChangeStreamPass( XStorage xStorage,
+ String sStreamName,
+ byte[] pOldPass,
+ byte[] pNewPass )
+ {
+ // open substream element
+ XStream xSubStream = null;
+ try
+ {
+ Object oSubStream = xStorage.openEncryptedStreamElement( sStreamName, ElementModes.WRITE, new String(pOldPass) );
+ xSubStream = UnoRuntime.queryInterface( XStream.class, oSubStream );
+ if ( xSubStream == null )
+ {
+ Error( "Can't open substream '" + sStreamName + "'!" );
+ return 0;
+ }
+ }
+ catch( Exception e )
+ {
+ Error( "Can't open substream '" + sStreamName + "', exception : " + e + "!" );
+ return 0;
+ }
+
+
+ // change the password for the stream
+ XEncryptionProtectedSource xStreamEncryption =
+ UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xSubStream );
+
+ if ( xStreamEncryption == null )
+ {
+ Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
+ return -1;
+ }
+
+ try {
+ xStreamEncryption.setEncryptionPassword( new String(pNewPass) );
+ }
+ catch( Exception e )
+ {
+ Error( "Can't change encryption key of the substream '" + sStreamName + "', exception:" + e );
+ return 0;
+ }
+
+ // free the stream resources, garbage collector may remove the object too late
+ XComponent xComponent = UnoRuntime.queryInterface( XComponent.class, xSubStream );
+ if ( xComponent == null )
+ {
+ Error( "Can't get XComponent implementation from substream '" + sStreamName + "'!" );
+ return 0;
+ }
+ xComponent.dispose();
+
+ return 1;
+ }
+
+ public boolean setStorageTypeAndCheckProps( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )
+ {
+ boolean bOk = false;
+
+ // get access to the XPropertySet interface
+ XPropertySet xPropSet = UnoRuntime.queryInterface( XPropertySet.class, xStorage );
+ if ( xPropSet != null )
+ {
+ try
+ {
+ // set "MediaType" property to the stream
+ xPropSet.setPropertyValue( "MediaType", sMediaType );
+
+ // get "IsRoot" and "OpenMode" properties and control there values
+ boolean bPropIsRoot = AnyConverter.toBoolean( xPropSet.getPropertyValue( "IsRoot" ) );
+ int nPropMode = AnyConverter.toInt( xPropSet.getPropertyValue( "OpenMode" ) );
+
+ bOk = true;
+ if ( bPropIsRoot != bIsRoot )
+ {
+ Error( "'IsRoot' property contains wrong value!" );
+ bOk = false;
+ }
+
+ if ( ( bIsRoot && ( nPropMode | ElementModes.READ ) != ( nMode | ElementModes.READ ) )
+ || ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
+ {
+ Error( "'OpenMode' property contains wrong value!" );
+ bOk = false;
+ }
+ }
+ catch( Exception e )
+ {
+ Error( "Can't control properties of substorage, exception: " + e );
+ }
+ }
+ else
+ {
+ Error( "Can't get XPropertySet implementation from storage!" );
+ }
+
+ return bOk;
+ }
+
+ public boolean checkStorageProperties( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )
+ {
+ boolean bOk = false;
+
+ // get access to the XPropertySet interface
+ XPropertySet xPropSet = UnoRuntime.queryInterface( XPropertySet.class, xStorage );
+ if ( xPropSet != null )
+ {
+ try
+ {
+ // get "MediaType", "IsRoot" and "OpenMode" properties and control there values
+ String sPropMediaType = AnyConverter.toString( xPropSet.getPropertyValue( "MediaType" ) );
+ boolean bPropIsRoot = AnyConverter.toBoolean( xPropSet.getPropertyValue( "IsRoot" ) );
+ int nPropMode = AnyConverter.toInt( xPropSet.getPropertyValue( "OpenMode" ) );
+
+ bOk = true;
+ if ( !sPropMediaType.equals( sMediaType ) )
+ {
+ Error( "'MediaType' property contains wrong value, expected '"
+ + sMediaType + "', set '" + sPropMediaType + "' !" );
+ bOk = false;
+ }
+
+ if ( bPropIsRoot != bIsRoot )
+ {
+ Error( "'IsRoot' property contains wrong value!" );
+ bOk = false;
+ }
+
+ if ( ( bIsRoot && ( nPropMode | ElementModes.READ ) != ( nMode | ElementModes.READ ) )
+ || ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
+ {
+ Error( "'OpenMode' property contains wrong value!" );
+ bOk = false;
+ }
+ }
+ catch( Exception e )
+ {
+ Error( "Can't get properties of substorage, exception: " + e );
+ }
+ }
+ else
+ {
+ Error( "Can't get XPropertySet implementation from storage!" );
+ }
+
+ return bOk;
+ }
+
+ public boolean InternalCheckStream( XStream xStream,
+ String sName,
+ String sMediaType,
+ byte[] pBytes )
+ {
+ // get input stream of substream
+ XInputStream xInput = xStream.getInputStream();
+ if ( xInput == null )
+ {
+ Error( "Can't get XInputStream implementation from substream '" + sName + "'!" );
+ return false;
+ }
+
+ byte pContents[][] = new byte[1][]; // ???
+
+ // read contents
+ try
+ {
+ xInput.readBytes( pContents, pBytes.length + 1 );
+ }
+ catch( Exception e )
+ {
+ Error( "Can't read from stream '" + sName + "', exception: " + e );
+ return false;
+ }
+
+ // check size of stream data
+ if ( pContents.length == 0 )
+ {
+ Error( "SubStream '" + sName + "' reading produced disaster!" );
+ return false;
+ }
+
+ if ( pBytes.length != pContents[0].length )
+ {
+ Error( "SubStream '" + sName + "' contains wrong amount of data! (" + pContents[0].length + "/" + pBytes.length + ")" );
+ return false;
+ }
+
+ // check stream data
+ for ( int ind = 0; ind < pBytes.length; ind++ )
+ {
+ if ( pBytes[ind] != pContents[0][ind] )
+ {
+ Error( "SubStream '" + sName + "' contains wrong data!" );
+ return false;
+ }
+ }
+
+
+ // check properties
+ boolean bOk = false;
+
+ // get access to the XPropertySet interface
+ XPropertySet xPropSet = UnoRuntime.queryInterface( XPropertySet.class, xStream );
+ if ( xPropSet != null )
+ {
+ try
+ {
+ // get "MediaType" and "Size" properties and control there values
+ String sPropMediaType = AnyConverter.toString( xPropSet.getPropertyValue( "MediaType" ) );
+ long nPropSize = AnyConverter.toLong( xPropSet.getPropertyValue( "Size" ) );
+
+ bOk = true;
+ if ( !sPropMediaType.equals( sMediaType ) )
+ {
+ Error( "'MediaType' property contains wrong value for stream '" + sName + "',\nexpected: '"
+ + sMediaType + "', set: '" + sPropMediaType + "'!" );
+ bOk = false;
+ }
+
+ if ( nPropSize != pBytes.length )
+ {
+ Error( "'Size' property contains wrong value for stream'" + sName + "'!" );
+ bOk = false;
+ }
+ }
+ catch( Exception e )
+ {
+ Error( "Can't get properties of substream '" + sName + "', exception: " + e );
+ }
+ }
+ else
+ {
+ Error( "Can't get XPropertySet implementation from stream '" + sName + "'!" );
+ }
+
+ return bOk;
+ }
+
+ public boolean checkStream( XStorage xParentStorage,
+ String sName,
+ String sMediaType,
+ byte[] pBytes )
+ {
+ // open substream element first
+ XStream xSubStream = null;
+ try
+ {
+ Object oSubStream = xParentStorage.openStreamElement( sName, ElementModes.READ );
+ xSubStream = UnoRuntime.queryInterface( XStream.class, oSubStream );
+ if ( xSubStream == null )
+ {
+ Error( "Can't open substream '" + sName + "'!" );
+ return false;
+ }
+ }
+ catch( Exception e )
+ {
+ Error( "Can't open substream '" + sName + "', exception : " + e + "!" );
+ return false;
+ }
+
+ return InternalCheckStream( xSubStream, sName, sMediaType, pBytes );
+ }
+
+ public boolean checkEncrStream( XStorage xParentStorage,
+ String sName,
+ String sMediaType,
+ byte[] pBytes,
+ byte[] pPass )
+ {
+ // Important: a common password for any of parent storage should not be set or
+ // should be different from pPass
+
+ if ( pPass.length == 0 )
+ {
+ Error( "Wrong password is used in the test!" );
+ return false;
+ }
+
+ try
+ {
+ xParentStorage.openStreamElement( sName, ElementModes.READ );
+ Error( "Encrypted stream '" + sName + "' was opened without password!" );
+ return false;
+ }
+ catch( WrongPasswordException wpe )
+ {}
+ catch( Exception e )
+ {
+ Error( "Unexpected exception in case of opening of encrypted stream '" + sName + "' without password: " + e + "!" );
+ return false;
+ }
+
+ byte pWrongPass[] = { 1, 1 };
+ pWrongPass[0] += pPass[0];
+ try
+ {
+ xParentStorage.openEncryptedStreamElement( sName, ElementModes.READ, new String(pWrongPass) );
+ Error( "Encrypted stream '" + sName + "' was opened with wrong password!" );
+ return false;
+ }
+ catch( WrongPasswordException wpe )
+ {}
+ catch( Exception e )
+ {
+ Error( "Unexpected exception in case of opening of encrypted stream '" + sName + "' with wrong password: " + e + "!" );
+ return false;
+ }
+
+ XStream xSubStream = null;
+ try
+ {
+ Object oSubStream = xParentStorage.openEncryptedStreamElement( sName, ElementModes.READ, new String(pPass) );
+ xSubStream = UnoRuntime.queryInterface( XStream.class, oSubStream );
+ if ( xSubStream == null )
+ {
+ Error( "Can't open encrypted substream '" + sName + "'!" );
+ return false;
+ }
+ }
+ catch( Exception e )
+ {
+ Error( "Can't open encrypted substream '" + sName + "', exception : " + e + "!" );
+ return false;
+ }
+
+ return InternalCheckStream( xSubStream, sName, sMediaType, pBytes );
+ }
+
+ public boolean copyStorage( XStorage xSourceStorage, XStorage xDestStorage )
+ {
+ // copy xSourceStorage to xDestStorage
+ try
+ {
+ xSourceStorage.copyToStorage( xDestStorage );
+ }
+ catch( Exception e )
+ {
+ Error( "Storage copying failed, exception: " + e );
+ return false;
+ }
+
+ return true;
+ }
+
+ public boolean commitStorage( XStorage xStorage )
+ {
+ // XTransactedObject must be supported by storages
+ XTransactedObject xTransact = UnoRuntime.queryInterface( XTransactedObject.class, xStorage );
+ if ( xTransact == null )
+ {
+ Error( "Storage doesn't implement transacted access!" );
+ return false;
+ }
+
+ try
+ {
+ xTransact.commit();
+ }
+ catch( Exception e )
+ {
+ Error( "Storage commit failed, exception:" + e );
+ return false;
+ }
+
+ return true;
+ }
+
+ public boolean disposeStorage( XStorage xStorage )
+ {
+ // dispose the storage
+ XComponent xComponent = UnoRuntime.queryInterface( XComponent.class, xStorage );
+ if ( xComponent == null )
+ {
+ Error( "Can't retrieve XComponent implementation from storage!" );
+ return false;
+ }
+
+ try
+ {
+ xComponent.dispose();
+ }
+ catch( Exception e )
+ {
+ Error( "Storage disposing failed!" );
+ return false;
+ }
+
+ return true;
+ }
+
+ public XInputStream getInputStream( XStream xStream )
+ {
+ XInputStream xInTemp = null;
+ try
+ {
+ xInTemp = xStream.getInputStream();
+ if ( xInTemp == null )
+ Error( "Can't get the input part of a stream!" );
+ }
+ catch ( Exception e )
+ {
+ Error( "Can't get the input part of a stream, exception :" + e );
+ }
+
+ return xInTemp;
+ }
+
+ public boolean closeOutput( XStream xStream )
+ {
+ XOutputStream xOutTemp = null;
+ try
+ {
+ xOutTemp = xStream.getOutputStream();
+ if ( xOutTemp == null )
+ {
+ Error( "Can't get the output part of a stream!" );
+ return false;
+ }
+ }
+ catch ( Exception e )
+ {
+ Error( "Can't get the output part of a stream, exception :" + e );
+ return false;
+ }
+
+ try
+ {
+ xOutTemp.closeOutput();
+ }
+ catch ( Exception e )
+ {
+ Error( "Can't close output part of a stream, exception :" + e );
+ return false;
+ }
+
+ return true;
+ }
+
+ public XStorage openSubStorage( XStorage xStorage, String sName, int nMode )
+ {
+ // open existing substorage
+ try
+ {
+ Object oSubStorage = xStorage.openStorageElement( sName, nMode );
+ XStorage xSubStorage = UnoRuntime.queryInterface( XStorage.class, oSubStorage );
+ return xSubStorage;
+ }
+ catch( Exception e )
+ {
+ Error( "Can't open substorage '" + sName + "', exception: " + e );
+ }
+
+ return null;
+ }
+
+ public XStream CreateTempFileStream( XMultiServiceFactory xMSF )
+ {
+ // try to get temporary file representation
+ XStream xTempFileStream = null;
+ try
+ {
+ Object oTempFile = xMSF.createInstance( "com.sun.star.io.TempFile" );
+ xTempFileStream = UnoRuntime.queryInterface( XStream.class, oTempFile );
+ }
+ catch( Exception e )
+ {}
+
+ if ( xTempFileStream == null )
+ Error( "Can't create temporary file!" );
+
+ return xTempFileStream;
+ }
+
+ public String CreateTempFile( XMultiServiceFactory xMSF )
+ {
+ String sResult = null;
+
+ // try to get temporary file representation
+ XPropertySet xTempFileProps = null;
+ try
+ {
+ Object oTempFile = xMSF.createInstance( "com.sun.star.io.TempFile" );
+ xTempFileProps = UnoRuntime.queryInterface( XPropertySet.class, oTempFile );
+ }
+ catch( Exception e )
+ {}
+
+ if ( xTempFileProps != null )
+ {
+ try
+ {
+ xTempFileProps.setPropertyValue( "RemoveFile", Boolean.FALSE );
+ sResult = AnyConverter.toString( xTempFileProps.getPropertyValue( "Uri" ) );
+ }
+ catch( Exception e )
+ {
+ Error( "Can't control TempFile properties, exception: " + e );
+ }
+ }
+ else
+ {
+ Error( "Can't create temporary file representation!" );
+ }
+
+ // close temporary file explicitly
+ try
+ {
+ XStream xStream = UnoRuntime.queryInterface( XStream.class, xTempFileProps );
+ if ( xStream != null )
+ {
+ XOutputStream xOut = xStream.getOutputStream();
+ if ( xOut != null )
+ xOut.closeOutput();
+
+ XInputStream xIn = xStream.getInputStream();
+ if ( xIn != null )
+ xIn.closeInput();
+ }
+ else
+ Error( "Can't close TempFile!" );
+ }
+ catch( Exception e )
+ {
+ Error( "Can't close TempFile, exception: " + e );
+ }
+
+ return sResult;
+ }
+
+ public boolean copyElementTo( XStorage xSource, String sName, XStorage xDest )
+ {
+ // copy element with name sName from xSource to xDest
+ try
+ {
+ xSource.copyElementTo( sName, xDest, sName );
+ }
+ catch( Exception e )
+ {
+ Error( "Element copying failed, exception: " + e );
+ return false;
+ }
+
+ return true;
+ }
+
+ public boolean moveElementTo( XStorage xSource, String sName, XStorage xDest )
+ {
+ // move element with name sName from xSource to xDest
+ try
+ {
+ xSource.moveElementTo( sName, xDest, sName );
+ }
+ catch( Exception e )
+ {
+ Error( "Element moving failed, exception: " + e );
+ return false;
+ }
+
+ return true;
+ }
+
+ public boolean renameElement( XStorage xStorage, String sOldName, String sNewName )
+ {
+ // rename element with name sOldName to sNewName
+ try
+ {
+ xStorage.renameElement( sOldName, sNewName );
+ }
+ catch( Exception e )
+ {
+ Error( "Element renaming failed, exception: " + e );
+ return false;
+ }
+
+ return true;
+ }
+
+ public boolean removeElement( XStorage xStorage, String sName )
+ {
+ // remove element with name sName
+ try
+ {
+ xStorage.removeElement( sName );
+ }
+ catch( Exception e )
+ {
+ Error( "Element removing failed, exception: " + e );
+ return false;
+ }
+
+ return true;
+ }
+
+ public XStream OpenStream( XStorage xStorage,
+ String sStreamName,
+ int nMode )
+ {
+ // open substream element
+ XStream xSubStream = null;
+ try
+ {
+ Object oSubStream = xStorage.openStreamElement( sStreamName, nMode );
+ xSubStream = UnoRuntime.queryInterface( XStream.class, oSubStream );
+ if ( xSubStream == null )
+ Error( "Can't create substream '" + sStreamName + "'!" );
+ }
+ catch( Exception e )
+ {
+ Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
+ }
+
+ return xSubStream;
+ }
+
+ public boolean cantOpenStorage( XStorage xStorage, String sName )
+ {
+ // try to open an opened substorage, open call must fail
+ try
+ {
+ Object oDummyStorage = xStorage.openStorageElement( sName, ElementModes.READ );
+ Error( "The trying to reopen opened substorage '" + sName + "' must fail!" );
+ }
+ catch( Exception e )
+ {
+ return true;
+ }
+
+ return false;
+ }
+
+ public boolean cantOpenStream( XStorage xStorage, String sName, int nMode )
+ {
+ // try to open the substream with specified mode must fail
+ try
+ {
+ Object oDummyStream = xStorage.openStreamElement( sName, nMode );
+ Error( "The trying to open substoream '" + sName + "' must fail!" );
+ }
+ catch( Exception e )
+ {
+ return true;
+ }
+
+ return false;
+ }
+
+ public void Error( String sError )
+ {
+ System.out.println( m_sTestPrefix + "Error: " + sError );
+ }
+
+ public void Message( String sError )
+ {
+ System.out.println( m_sTestPrefix + sError );
+ }
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */