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.container.XNameAccess;
34 import com.sun.star.beans.StringPair;
35 
36 import share.LogWriter;
37 import complex.ofopxmlstorages.TestHelper;
38 import complex.ofopxmlstorages.StorageTest;
39 
40 public class Test03 implements StorageTest {
41 
42 	XMultiServiceFactory m_xMSF;
43 	XSingleServiceFactory m_xStorageFactory;
44 	TestHelper m_aTestHelper;
45 
Test03( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )46 	public Test03( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
47 	{
48 		m_xMSF = xMSF;
49 		m_xStorageFactory = xStorageFactory;
50 		m_aTestHelper = new TestHelper( aLogWriter, "Test03: " );
51 	}
52 
test()53     public boolean test()
54 	{
55 		try
56 		{
57 			StringPair[][] aRelations =
58 				{ { new StringPair( "Id", "Num1" ) },
59 				  { new StringPair( "Target", "TargetURLValue" ), new StringPair( "Id", "Num6" ) },
60 				  { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
61 				  { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) },
62 				  { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) },
63 				  { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) },
64 				  { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) }
65 				};
66 
67 			// create temporary storage based on arbitrary medium
68 			// after such a storage is closed it is lost
69 			XStorage xTempStorage = m_aTestHelper.createTempStorage( m_xMSF, m_xStorageFactory );
70 			if ( xTempStorage == null )
71 			{
72 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
73 				return false;
74 			}
75 
76 			// open a new substorage
77 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
78 																		"SubStorage1",
79 																		ElementModes.WRITE );
80 			if ( xTempSubStorage == null )
81 			{
82 				m_aTestHelper.Error( "Can't create substorage!" );
83 				return false;
84 			}
85 
86 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
87 
88 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
89 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage,
90 														"SubStream1",
91 														"MediaType1",
92 														true,
93 														pBytes1,
94 														aRelations ) )
95 				return false;
96 
97 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
98 
99 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
100 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage,
101 														"SubStream2",
102 														"MediaType2",
103 														false,
104 														pBytes2,
105 														aRelations ) )
106 				return false;
107 
108 			// set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
109 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
110 															false,
111 															ElementModes.WRITE,
112 															aRelations ) )
113 				return false;
114 
115 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
116 				return false;
117 
118 			if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
119 				return false;
120 
121 			// ================================================
122 			// check storage hyerarchy tree
123 			// ================================================
124 
125 			// check that isStorageElement() and isStreamElement reacts to nonexisting object correctly
126 			try {
127 				xTempStorage.isStorageElement( "does not exist" );
128 				m_aTestHelper.Error( "Nonexisting element doesn't detected by isStorageElement() call!" );
129 				return false;
130 			}
131 			catch( com.sun.star.container.NoSuchElementException ne )
132 			{
133 			}
134 			catch( Exception e )
135 			{
136 				m_aTestHelper.Error( "Wrong exception is thrown by isStorageElement() call: " + e );
137 				return false;
138 			}
139 
140 			try {
141 				xTempStorage.isStreamElement( "does not exist" );
142 				m_aTestHelper.Error( "Nonexisting element doesn't detected by isStreamElement() call!" );
143 				return false;
144 			}
145 			catch( com.sun.star.container.NoSuchElementException ne )
146 			{
147 			}
148 			catch( Exception e )
149 			{
150 				m_aTestHelper.Error( "Wrong exception is thrown by isStreamElement() call: " + e );
151 				return false;
152 			}
153 
154 			XNameAccess xRootNameAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xTempStorage );
155 			if ( xRootNameAccess == null )
156 			{
157 				m_aTestHelper.Error( "Root storage doesn't support XNameAccess!" );
158 				return false;
159 			}
160 
161 			try {
162 				if ( !xTempStorage.isStorageElement( "SubStorage1" ) || xTempStorage.isStreamElement( "SubStorage1" ) )
163 				{
164 					m_aTestHelper.Error( "Child 'SubStorage1' can not be detected as storage!" );
165 					return false;
166 				}
167 
168 				if ( xTempStorage.isStorageElement( "SubStream1" ) || !xTempStorage.isStreamElement( "SubStream1" ) )
169 				{
170 					m_aTestHelper.Error( "Child 'SubStream1' can not be detected as stream!" );
171 					return false;
172 				}
173 			}
174 			catch( Exception e )
175 			{
176 				m_aTestHelper.Error( "Child's type can not be detected, exception: " + e );
177 				return false;
178 			}
179 
180 
181 			// check that root storage contents are represented correctly
182 			String sRootCont[] = xRootNameAccess.getElementNames();
183 
184 			if ( sRootCont.length != 2 )
185 			{
186 				m_aTestHelper.Error( "Root storage contains wrong amount of children!" );
187 				return false;
188 			}
189 
190 			if ( !( sRootCont[0].equals( "SubStorage1" ) && sRootCont[1].equals( "SubStream1" )
191 			     || sRootCont[0].equals( "SubStream1" ) && sRootCont[1].equals( "SubStorage1" ) )
192 			  || !( xRootNameAccess.hasByName( "SubStream1" ) && xRootNameAccess.hasByName( "SubStorage1" ) ) )
193 			{
194 				m_aTestHelper.Error( "Root storage contains wrong list of children!" );
195 				return false;
196 			}
197 
198 			// get storage through XNameAccess
199 			XStorage xResultSubStorage = getStorageFromNameAccess( xRootNameAccess, "SubStorage1" );
200 			if ( xResultSubStorage == null )
201 				return false;
202 
203 			if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage,
204 														false,
205 														ElementModes.READ,
206 														aRelations ) )
207 				return false;
208 
209 			XNameAccess xChildAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResultSubStorage );
210 			if ( xChildAccess == null )
211 			{
212 				m_aTestHelper.Error( "Child storage doesn't support XNameAccess!" );
213 				return false;
214 			}
215 
216 			if ( !xChildAccess.hasByName( "SubStream2" )
217 			  || !xResultSubStorage.isStreamElement( "SubStream2" )
218 			  || xResultSubStorage.isStorageElement( "SubStream2" ) )
219 			{
220 				m_aTestHelper.Error( "'SubStream2' can not be detected as child stream element of 'SubStorage1'!" );
221 				return false;
222 			}
223 
224 			return true;
225 		}
226 		catch( Exception e )
227 		{
228 			m_aTestHelper.Error( "Exception: " + e );
229 			return false;
230 		}
231     }
232 
getStorageFromNameAccess( XNameAccess xAccess, String sName )233 	public XStorage getStorageFromNameAccess( XNameAccess xAccess, String sName )
234 	{
235 		try
236 		{
237 			Object oStorage = xAccess.getByName( sName );
238 			XStorage xResult = (XStorage) UnoRuntime.queryInterface( XStorage.class, oStorage );
239 
240 			if ( xResult != null )
241 				return xResult;
242 			else
243 				m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess!" );
244 		}
245 		catch( Exception e )
246 		{
247 			m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess, exception: " + e );
248 		}
249 
250 		return null;
251 	}
252 
253 }
254 
255