xref: /aoo42x/main/package/qa/storages/Test17.java (revision a740f2aa)
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.storages;
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 share.LogWriter;
37 import complex.storages.TestHelper;
38 import complex.storages.StorageTest;
39 
40 public class Test17 implements StorageTest {
41 
42 	XMultiServiceFactory m_xMSF;
43 	XSingleServiceFactory m_xStorageFactory;
44 	TestHelper m_aTestHelper;
45 
Test17( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )46 	public Test17( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
47 	{
48 		m_xMSF = xMSF;
49 		m_xStorageFactory = xStorageFactory;
50 		m_aTestHelper = new TestHelper( aLogWriter, "Test17: " );
51 	}
52 
test()53     public boolean test()
54 	{
55 		try
56 		{
57 			XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
58 			if ( xTempFileStream == null )
59 				return false;
60 
61 			// create storage based on the temporary stream
62 			Object pArgs[] = new Object[2];
63 			pArgs[0] = (Object) xTempFileStream;
64 			pArgs[1] = new Integer( ElementModes.WRITE );
65 
66 			Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
67 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
68 			if ( xTempStorage == null )
69 			{
70 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
71 				return false;
72 			}
73 
74 
75 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
76             String pNames[] = { "SubStream1", "SubStream2", "SubStream3", "SubStream4", "SubStream5", "SubStream6", "SubStream7" };
77 
78             for ( int nInd = 0; nInd < pNames.length; nInd++ )
79             {
80 			    // open a new substorage
81 			    XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
82 																	    "SubStorage1",
83 																	    ElementModes.WRITE );
84 			    if ( xTempSubStorage == null )
85 			    {
86 				    m_aTestHelper.Error( "Can't create substorage!" );
87 				    return false;
88 			    }
89 
90 			    // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
91 			    if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, pNames[nInd], "MediaType1", true, pBytes1 ) )
92 				    return false;
93 
94                 // commit substorage first
95                 if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
96                     return false;
97 
98                 // dispose used storage to free resources
99                 if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
100                     return false;
101             }
102 
103 			// commit the root storage so the contents must be stored now
104 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
105 				return false;
106 
107 			// dispose used storage to free resources
108 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
109 				return false;
110 
111 
112 			// ================================================
113 			// now check all the written information
114 			// ================================================
115 
116 			// close the output part of the temporary stream
117 			// the output part must present since we already wrote to the stream
118 			if ( !m_aTestHelper.closeOutput( xTempFileStream ) )
119 				return false;
120 
121 			XInputStream xTempInStream = m_aTestHelper.getInputStream( xTempFileStream );
122 			if ( xTempInStream == null )
123 				return false;
124 
125 
126 			// open input stream
127 			// since no mode is provided the result storage must be opened readonly
128 			Object pOneArg[] = new Object[1];
129 			pOneArg[0] = (Object) xTempInStream;
130 
131 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pOneArg );
132 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
133 			if ( xResultStorage == null )
134 			{
135 				m_aTestHelper.Error( "Can't open storage based on input stream!" );
136 				return false;
137 			}
138 
139 			// open existing substorage
140 			XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
141 																		"SubStorage1",
142 																		ElementModes.READ );
143 			if ( xResultSubStorage == null )
144 			{
145 				m_aTestHelper.Error( "Can't open existing substorage!" );
146 				return false;
147 			}
148 
149             for ( int nInd = 0; nInd < pNames.length; nInd++ )
150 			    if ( !m_aTestHelper.checkStream( xResultSubStorage, pNames[nInd], "MediaType1", true, pBytes1 ) )
151 				    return false;
152 
153 			return true;
154 		}
155 		catch( Exception e )
156 		{
157 			m_aTestHelper.Error( "Exception: " + e );
158 			return false;
159 		}
160     }
161 
162 }
163 
164