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.ofopxmlstorages;
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 import com.sun.star.beans.StringPair;
34 
35 import share.LogWriter;
36 import complex.ofopxmlstorages.TestHelper;
37 import complex.ofopxmlstorages.StorageTest;
38 
39 public class Test01 implements StorageTest {
40 
41 	XMultiServiceFactory m_xMSF;
42 	XSingleServiceFactory m_xStorageFactory;
43 	TestHelper m_aTestHelper;
44 
Test01( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )45 	public Test01( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
46 	{
47 		m_xMSF = xMSF;
48 		m_xStorageFactory = xStorageFactory;
49 		m_aTestHelper = new TestHelper( aLogWriter, "Test01: " );
50 	}
51 
test()52     public boolean test()
53 	{
54 		StringPair[][] aRelations1 =
55 			{ { new StringPair( "Id", "Num1" ) },
56 			  { new StringPair( "Target", "TargetURLValue1" ), new StringPair( "Id", "Num6" ) },
57 			  { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
58 			  { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
59 			  { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
60 			  { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
61 			  { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value1" ) }
62 			};
63 
64 		StringPair[][] aRelations2 =
65 			{ { new StringPair( "Id", "Num1" ) },
66 			  { new StringPair( "Target", "TargetURLValue2" ), new StringPair( "Id", "Num6" ) },
67 			  { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
68 			  { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) },
69 			  { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) },
70 			  { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) },
71 			  { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) }
72 			};
73 
74 		try
75 		{
76 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
77 			if ( sTempFileURL == null || sTempFileURL == "" )
78 			{
79 				m_aTestHelper.Error( "No valid temporary file was created!" );
80 				return false;
81 			}
82 
83 			// create temporary storage based on arbitrary medium
84 			// after such a storage is closed it is lost
85 			XStorage xTempStorage = m_aTestHelper.createTempStorage( m_xMSF, m_xStorageFactory );
86 			if ( xTempStorage == null )
87 			{
88 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
89 				return false;
90 			}
91 
92 			// open a new substorage
93 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
94 																		"SubStorage1",
95 																		ElementModes.WRITE );
96 			if ( xTempSubStorage == null )
97 			{
98 				m_aTestHelper.Error( "Can't create substorage!" );
99 				return false;
100 			}
101 
102 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
103 
104 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
105 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage,
106 														"SubStream1",
107 														"MediaType1",
108 														true,
109 														pBytes1,
110 														aRelations1 ) )
111 				return false;
112 
113 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
114 
115 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
116 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage,
117 														"SubStream2",
118 														"MediaType2",
119 														false,
120 														pBytes2,
121 														aRelations2 ) )
122 				return false;
123 
124 			// set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
125 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
126 															true,
127 															ElementModes.WRITE,
128 															aRelations1 ) )
129 				return false;
130 
131 			// set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
132 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
133 															false,
134 															ElementModes.WRITE,
135 															aRelations1 ) )
136 				return false;
137 
138 			// create temporary storage based on a previously created temporary file
139 			XStorage xTempFileStorage = m_aTestHelper.createStorageFromURL( m_xStorageFactory,
140 																			sTempFileURL,
141 																			ElementModes.WRITE );
142 			if ( xTempFileStorage == null )
143 			{
144 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
145 				return false;
146 			}
147 
148 			// copy xTempStorage to xTempFileStorage
149 			// xTempFileStorage will be automatically committed
150 			if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
151 				return false;
152 
153 			// dispose used storages to free resources
154 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
155 				return false;
156 
157 			// ================================================
158 			// now check all the written and copied information
159 			// ================================================
160 
161 			// the temporary file must not be locked any more after storage disposing
162 			XStorage xResultStorage = m_aTestHelper.createStorageFromURL( m_xStorageFactory,
163 																			sTempFileURL,
164 																			ElementModes.WRITE );
165 			if ( xResultStorage == null )
166 			{
167 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
168 				return false;
169 			}
170 
171 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage,
172 														true,
173 														ElementModes.WRITE,
174 														aRelations1 ) )
175 				return false;
176 
177 			// open existing substorage
178 			XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
179 																		"SubStorage1",
180 																		ElementModes.READ );
181 			if ( xResultSubStorage == null )
182 			{
183 				m_aTestHelper.Error( "Can't open existing substorage!" );
184 				return false;
185 			}
186 
187 			if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage,
188 														false,
189 														ElementModes.READ,
190 														aRelations1 ) )
191 				return false;
192 
193 			if ( !m_aTestHelper.checkStream( xResultSubStorage,
194 											"SubStream1",
195 											"MediaType1",
196 											pBytes1,
197 											aRelations1 ) )
198 				return false;
199 
200 			if ( !m_aTestHelper.checkStream( xResultSubStorage,
201 											"SubStream2",
202 											"MediaType2",
203 											pBytes2,
204 											aRelations2 ) )
205 				return false;
206 
207 			// dispose used storages to free resources
208 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
209 				return false;
210 
211 			return true;
212 		}
213 		catch( Exception e )
214 		{
215 			m_aTestHelper.Error( "Exception: " + e );
216 			return false;
217 		}
218     }
219 
220 }
221 
222