xref: /aoo41x/main/package/qa/storages/Test16.java (revision a740f2aa)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package complex.storages;
23 
24 import com.sun.star.uno.XInterface;
25 import com.sun.star.lang.XMultiServiceFactory;
26 import com.sun.star.lang.XSingleServiceFactory;
27 
28 import com.sun.star.bridge.XUnoUrlResolver;
29 import com.sun.star.uno.UnoRuntime;
30 import com.sun.star.uno.XInterface;
31 
32 import com.sun.star.embed.*;
33 
34 import share.LogWriter;
35 import complex.storages.TestHelper;
36 import complex.storages.StorageTest;
37 
38 public class Test16 implements StorageTest {
39 
40 	XMultiServiceFactory m_xMSF;
41 	XSingleServiceFactory m_xStorageFactory;
42 	TestHelper m_aTestHelper;
43 
Test16( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )44 	public Test16( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
45 	{
46 		m_xMSF = xMSF;
47 		m_xStorageFactory = xStorageFactory;
48 		m_aTestHelper = new TestHelper( aLogWriter, "Test16: " );
49 	}
50 
test()51     public boolean test()
52 	{
53 		try
54 		{
55 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
56 			if ( sTempFileURL == null || sTempFileURL == "" )
57 			{
58 				m_aTestHelper.Error( "No valid temporary file was created!" );
59 				return false;
60 			}
61 
62 			// create temporary storage based on arbitrary medium
63 			// after such a storage is closed it is lost
64 			Object oTempStorage = m_xStorageFactory.createInstance();
65 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
66 			if ( xTempStorage == null )
67 			{
68 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
69 				return false;
70 			}
71 
72 			// open a new substorage
73 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
74 																		"SubStorage\u0442\u0435\u0441\u04421",
75 																		ElementModes.WRITE );
76 			if ( xTempSubStorage == null )
77 			{
78 				m_aTestHelper.Error( "Can't create substorage!" );
79 				return false;
80 			}
81 
82 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
83 
84 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
85 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream\u0442\u0435\u0441\u04421", "MediaType1", true, pBytes1 ) )
86 				return false;
87 
88 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
89 
90 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
91 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream\u0442\u0435\u0441\u04422", "MediaType2", false, pBytes2 ) )
92 				return false;
93 
94 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
95 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
96 															"MediaType3",
97 															true,
98 															ElementModes.WRITE ) )
99 				return false;
100 
101 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
102 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
103 															"MediaType4",
104 															false,
105 															ElementModes.WRITE ) )
106 				return false;
107 
108 			// create temporary storage based on a previously created temporary file
109 			Object pArgs[] = new Object[2];
110 			pArgs[0] = (Object) sTempFileURL;
111 			pArgs[1] = new Integer( ElementModes.WRITE );
112 
113 			Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
114 			XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
115 			if ( xTempFileStorage == null )
116 			{
117 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
118 				return false;
119 			}
120 
121 			// copy xTempStorage to xTempFileStorage
122 			// xTempFileStorage will be automatically commited
123 			if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
124 				return false;
125 
126 			// dispose used storages to free resources
127 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
128 				return false;
129 
130 			// ================================================
131 			// now check all the written and copied information
132 			// ================================================
133 
134 			// the temporary file must not be locked any more after storage disposing
135 			pArgs[1] = new Integer( ElementModes.WRITE );
136 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
137 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
138 			if ( xResultStorage == null )
139 			{
140 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
141 				return false;
142 			}
143 
144 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.WRITE ) )
145 				return false;
146 
147 			// open existing substorage
148 			XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
149 																		"SubStorage\u0442\u0435\u0441\u04421",
150 																		ElementModes.READ );
151 			if ( xResultSubStorage == null )
152 			{
153 				m_aTestHelper.Error( "Can't open existing substorage!" );
154 				return false;
155 			}
156 
157 			if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType4", false, ElementModes.READ ) )
158 				return false;
159 
160 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream\u0442\u0435\u0441\u04421", "MediaType1", true, pBytes1 ) )
161 				return false;
162 
163 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream\u0442\u0435\u0441\u04422", "MediaType2", false, pBytes2 ) )
164 				return false;
165 
166 			// dispose used storages to free resources
167 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
168 				return false;
169 
170 			return true;
171 		}
172 		catch( Exception e )
173 		{
174 			m_aTestHelper.Error( "Exception: " + e );
175 			return false;
176 		}
177     }
178 
179 }
180 
181