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