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