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 import com.sun.star.io.XStream;
32 import com.sun.star.io.XInputStream;
33 
34 import com.sun.star.embed.*;
35 
36 import storagetesting.TestHelper;
37 import storagetesting.StorageTest;
38 
39 public class Test02 implements StorageTest {
40 
41 	XMultiServiceFactory m_xMSF;
42 	XSingleServiceFactory m_xStorageFactory;
43 	TestHelper m_aTestHelper;
44 
Test02( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )45 	public Test02( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
46 	{
47 		m_xMSF = xMSF;
48 		m_xStorageFactory = xStorageFactory;
49 		m_aTestHelper = new TestHelper( "Test02: " );
50 	}
51 
test()52     public boolean test()
53 	{
54 		try
55 		{
56 			XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
57 			if ( xTempFileStream == null )
58 				return false;
59 
60 			// create storage based on the temporary stream
61 			Object pArgs[] = new Object[2];
62 			pArgs[0] = (Object) xTempFileStream;
63 			pArgs[1] = new Integer( ElementModes.ELEMENT_WRITE );
64 
65 			Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
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.ELEMENT_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.WriteBytesToSubstream( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
87 				return false;
88 
89 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
90 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
91 															"MediaType2",
92 															true,
93 															ElementModes.ELEMENT_WRITE ) )
94 				return false;
95 
96 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
97 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
98 															"MediaType3",
99 															false,
100 															ElementModes.ELEMENT_WRITE ) )
101 				return false;
102 
103 			// commit substorage first
104 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
105 				return false;
106 
107 			// commit the root storage so the contents must be stored now
108 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
109 				return false;
110 
111 			// dispose used storage to free resources
112 			// the substorage dispose will be triggered by this call
113 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
114 				return false;
115 
116 
117 			// ================================================
118 			// now check all the written information
119 			// ================================================
120 
121 			// close the output part of the temporary stream
122 			// the output part must present since we already wrote to the stream
123 			if ( !m_aTestHelper.closeOutput( xTempFileStream ) )
124 				return false;
125 
126 			XInputStream xTempInStream = m_aTestHelper.getInputStream( xTempFileStream );
127 			if ( xTempInStream == null )
128 				return false;
129 
130 
131 			// open input stream
132 			// since no mode is provided the result storage must be opened readonly
133 			Object pOneArg[] = new Object[1];
134 			pOneArg[0] = (Object) xTempInStream;
135 
136 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pOneArg );
137 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
138 			if ( xResultStorage == null )
139 			{
140 				m_aTestHelper.Error( "Can't open storage based on input stream!" );
141 				return false;
142 			}
143 
144 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType2", true, ElementModes.ELEMENT_READ ) )
145 				return false;
146 
147 			// open existing substorage
148 			XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
149 																		"SubStorage1",
150 																		ElementModes.ELEMENT_READ );
151 			if ( xResultSubStorage == null )
152 			{
153 				m_aTestHelper.Error( "Can't open existing substorage!" );
154 				return false;
155 			}
156 
157 			if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType3", false, ElementModes.ELEMENT_READ ) )
158 				return false;
159 
160 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream1", "MediaType1", pBytes1 ) )
161 				return false;
162 
163 			return true;
164 		}
165 		catch( Exception e )
166 		{
167 			m_aTestHelper.Error( "Exception: " + e );
168 			return false;
169 		}
170     }
171 
172 }
173 
174