xref: /aoo4110/main/package/qa/storages/Test18.java (revision b1cdbd2c)
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 Test18 implements StorageTest {
39 
40 	XMultiServiceFactory m_xMSF;
41 	XSingleServiceFactory m_xStorageFactory;
42 	TestHelper m_aTestHelper;
43 
Test18( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )44 	public Test18( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
45 	{
46 		m_xMSF = xMSF;
47 		m_xStorageFactory = xStorageFactory;
48 		m_aTestHelper = new TestHelper( aLogWriter, "Test18: " );
49 	}
50 
test()51     public boolean test()
52 	{
53 		try
54 		{
55             // test the default value of Compressed property
56 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
57 			if ( sTempFileURL == null || sTempFileURL == "" )
58 			{
59 				m_aTestHelper.Error( "No valid temporary file was created!" );
60 				return false;
61 			}
62 
63 			// create temporary storage based on arbitrary medium
64 			// after such a storage is closed it is lost
65 			Object oTempStorage = m_xStorageFactory.createInstance();
66 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
67 			if ( xTempStorage == null )
68 			{
69 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
70 				return false;
71 			}
72 
73 			// open a new substorage
74 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
75 																		"SubStorage1",
76 																		ElementModes.WRITE );
77 			if ( xTempSubStorage == null )
78 			{
79 				m_aTestHelper.Error( "Can't create substorage!" );
80 				return false;
81 			}
82 
83 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
84 
85 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
86 			if ( !m_aTestHelper.WriteBytesToSubstreamDefaultCompressed( xTempSubStorage, "SubStream1", "image/jpeg", pBytes1 ) )
87 				return false;
88 
89 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
90 			if ( !m_aTestHelper.WriteBytesToSubstreamDefaultCompressed( xTempSubStorage, "SubStream2", "image/png", pBytes1 ) )
91 				return false;
92 
93 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
94 			if ( !m_aTestHelper.WriteBytesToSubstreamDefaultCompressed( xTempSubStorage, "SubStream3", "image/gif", pBytes1 ) )
95 				return false;
96 
97 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
98 			if ( !m_aTestHelper.WriteBytesToSubstreamDefaultCompressed( xTempSubStorage, "SubStream4", "MediaType1", pBytes1 ) )
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( xTempStorage,
103 															"MediaType3",
104 															true,
105 															ElementModes.WRITE ) )
106 				return false;
107 
108 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
109 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
110 															"MediaType4",
111 															false,
112 															ElementModes.WRITE ) )
113 				return false;
114 
115 			// create temporary storage based on a previously created temporary file
116 			Object pArgs[] = new Object[2];
117 			pArgs[0] = (Object) sTempFileURL;
118 			pArgs[1] = new Integer( ElementModes.WRITE );
119 
120 			Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
121 			XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
122 			if ( xTempFileStorage == null )
123 			{
124 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
125 				return false;
126 			}
127 
128 			// copy xTempStorage to xTempFileStorage
129 			// xTempFileStorage will be automatically commited
130 			if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
131 				return false;
132 
133 			// dispose used storages to free resources
134 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
135 				return false;
136 
137 			// ================================================
138 			// now check all the written and copied information
139 			// ================================================
140 
141 			// the temporary file must not be locked any more after storage disposing
142 			pArgs[1] = new Integer( ElementModes.WRITE );
143 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
144 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
145 			if ( xResultStorage == null )
146 			{
147 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
148 				return false;
149 			}
150 
151 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.WRITE ) )
152 				return false;
153 
154 			// open existing substorage
155 			XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
156 																		"SubStorage1",
157 																		ElementModes.READ );
158 			if ( xResultSubStorage == null )
159 			{
160 				m_aTestHelper.Error( "Can't open existing substorage!" );
161 				return false;
162 			}
163 
164 			if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType4", false, ElementModes.READ ) )
165 				return false;
166 
167 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream1", "image/jpeg", false, pBytes1 ) )
168 				return false;
169 
170 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream2", "image/png", false, pBytes1 ) )
171 				return false;
172 
173 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream3", "image/gif", false, pBytes1 ) )
174 				return false;
175 
176 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream4", "MediaType1", true, pBytes1 ) )
177 				return false;
178 
179 			// dispose used storages to free resources
180 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
181 				return false;
182 
183 			return true;
184 		}
185 		catch( Exception e )
186 		{
187 			m_aTestHelper.Error( "Exception: " + e );
188 			return false;
189 		}
190     }
191 
192 }
193 
194