xref: /trunk/main/odk/examples/java/Storage/Test02.java (revision ae15d43a)
1*ae15d43aSAndrew Rist /**************************************************************
2*ae15d43aSAndrew Rist  *
3*ae15d43aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ae15d43aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ae15d43aSAndrew Rist  * distributed with this work for additional information
6*ae15d43aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ae15d43aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ae15d43aSAndrew Rist  * "License"); you may not use this file except in compliance
9*ae15d43aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ae15d43aSAndrew Rist  *
11*ae15d43aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ae15d43aSAndrew Rist  *
13*ae15d43aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ae15d43aSAndrew Rist  * software distributed under the License is distributed on an
15*ae15d43aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ae15d43aSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ae15d43aSAndrew Rist  * specific language governing permissions and limitations
18*ae15d43aSAndrew Rist  * under the License.
19*ae15d43aSAndrew Rist  *
20*ae15d43aSAndrew Rist  *************************************************************/
21*ae15d43aSAndrew Rist 
22cdf0e10cSrcweir package storagetesting;
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.uno.XInterface;
25cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
26cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
29cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
30cdf0e10cSrcweir import com.sun.star.uno.XInterface;
31cdf0e10cSrcweir import com.sun.star.io.XStream;
32cdf0e10cSrcweir import com.sun.star.io.XInputStream;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir import com.sun.star.embed.*;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir import storagetesting.TestHelper;
37cdf0e10cSrcweir import storagetesting.StorageTest;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir public class Test02 implements StorageTest {
40cdf0e10cSrcweir 
41cdf0e10cSrcweir 	XMultiServiceFactory m_xMSF;
42cdf0e10cSrcweir 	XSingleServiceFactory m_xStorageFactory;
43cdf0e10cSrcweir 	TestHelper m_aTestHelper;
44cdf0e10cSrcweir 
Test02( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )45cdf0e10cSrcweir 	public Test02( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
46cdf0e10cSrcweir 	{
47cdf0e10cSrcweir 		m_xMSF = xMSF;
48cdf0e10cSrcweir 		m_xStorageFactory = xStorageFactory;
49cdf0e10cSrcweir 		m_aTestHelper = new TestHelper( "Test02: " );
50cdf0e10cSrcweir 	}
51cdf0e10cSrcweir 
test()52cdf0e10cSrcweir     public boolean test()
53cdf0e10cSrcweir 	{
54cdf0e10cSrcweir 		try
55cdf0e10cSrcweir 		{
56cdf0e10cSrcweir 			XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
57cdf0e10cSrcweir 			if ( xTempFileStream == null )
58cdf0e10cSrcweir 				return false;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 			// create storage based on the temporary stream
61cdf0e10cSrcweir 			Object pArgs[] = new Object[2];
62cdf0e10cSrcweir 			pArgs[0] = (Object) xTempFileStream;
63cdf0e10cSrcweir 			pArgs[1] = new Integer( ElementModes.ELEMENT_WRITE );
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 			Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
66cdf0e10cSrcweir 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
67cdf0e10cSrcweir 			if ( xTempStorage == null )
68cdf0e10cSrcweir 			{
69cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
70cdf0e10cSrcweir 				return false;
71cdf0e10cSrcweir 			}
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 			// open a new substorage
74cdf0e10cSrcweir 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
75cdf0e10cSrcweir 																	"SubStorage1",
76cdf0e10cSrcweir 																	ElementModes.ELEMENT_WRITE );
77cdf0e10cSrcweir 			if ( xTempSubStorage == null )
78cdf0e10cSrcweir 			{
79cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
80cdf0e10cSrcweir 				return false;
81cdf0e10cSrcweir 			}
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
86cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
87cdf0e10cSrcweir 				return false;
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
90cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
91cdf0e10cSrcweir 															"MediaType2",
92cdf0e10cSrcweir 															true,
93cdf0e10cSrcweir 															ElementModes.ELEMENT_WRITE ) )
94cdf0e10cSrcweir 				return false;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
97cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
98cdf0e10cSrcweir 															"MediaType3",
99cdf0e10cSrcweir 															false,
100cdf0e10cSrcweir 															ElementModes.ELEMENT_WRITE ) )
101cdf0e10cSrcweir 				return false;
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 			// commit substorage first
104cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
105cdf0e10cSrcweir 				return false;
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 			// commit the root storage so the contents must be stored now
108cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
109cdf0e10cSrcweir 				return false;
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 			// dispose used storage to free resources
112cdf0e10cSrcweir 			// the substorage dispose will be triggered by this call
113cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
114cdf0e10cSrcweir 				return false;
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 			// ================================================
118cdf0e10cSrcweir 			// now check all the written information
119cdf0e10cSrcweir 			// ================================================
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 			// close the output part of the temporary stream
122cdf0e10cSrcweir 			// the output part must present since we already wrote to the stream
123cdf0e10cSrcweir 			if ( !m_aTestHelper.closeOutput( xTempFileStream ) )
124cdf0e10cSrcweir 				return false;
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 			XInputStream xTempInStream = m_aTestHelper.getInputStream( xTempFileStream );
127cdf0e10cSrcweir 			if ( xTempInStream == null )
128cdf0e10cSrcweir 				return false;
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 			// open input stream
132cdf0e10cSrcweir 			// since no mode is provided the result storage must be opened readonly
133cdf0e10cSrcweir 			Object pOneArg[] = new Object[1];
134cdf0e10cSrcweir 			pOneArg[0] = (Object) xTempInStream;
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pOneArg );
137cdf0e10cSrcweir 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
138cdf0e10cSrcweir 			if ( xResultStorage == null )
139cdf0e10cSrcweir 			{
140cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't open storage based on input stream!" );
141cdf0e10cSrcweir 				return false;
142cdf0e10cSrcweir 			}
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType2", true, ElementModes.ELEMENT_READ ) )
145cdf0e10cSrcweir 				return false;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 			// open existing substorage
148cdf0e10cSrcweir 			XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
149cdf0e10cSrcweir 																		"SubStorage1",
150cdf0e10cSrcweir 																		ElementModes.ELEMENT_READ );
151cdf0e10cSrcweir 			if ( xResultSubStorage == null )
152cdf0e10cSrcweir 			{
153cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't open existing substorage!" );
154cdf0e10cSrcweir 				return false;
155cdf0e10cSrcweir 			}
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType3", false, ElementModes.ELEMENT_READ ) )
158cdf0e10cSrcweir 				return false;
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream1", "MediaType1", pBytes1 ) )
161cdf0e10cSrcweir 				return false;
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 			return true;
164cdf0e10cSrcweir 		}
165cdf0e10cSrcweir 		catch( Exception e )
166cdf0e10cSrcweir 		{
167cdf0e10cSrcweir 			m_aTestHelper.Error( "Exception: " + e );
168cdf0e10cSrcweir 			return false;
169cdf0e10cSrcweir 		}
170cdf0e10cSrcweir     }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
174