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 import com.sun.star.io.XStream;
32 import com.sun.star.io.XInputStream;
33 
34 import com.sun.star.embed.*;
35 import com.sun.star.beans.StringPair;
36 
37 import share.LogWriter;
38 import complex.ofopxmlstorages.TestHelper;
39 import complex.ofopxmlstorages.StorageTest;
40 
41 public class Test02 implements StorageTest {
42 
43 	XMultiServiceFactory m_xMSF;
44 	XSingleServiceFactory m_xStorageFactory;
45 	TestHelper m_aTestHelper;
46 
Test02( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )47 	public Test02( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
48 	{
49 		m_xMSF = xMSF;
50 		m_xStorageFactory = xStorageFactory;
51 		m_aTestHelper = new TestHelper( aLogWriter, "Test02: " );
52 	}
53 
test()54     public boolean test()
55 	{
56 		try
57 		{
58 			StringPair[][] aRelations =
59 				{ { new StringPair( "Id", "Num1" ) },
60 				  { new StringPair( "Target", "TargetURLValue" ), new StringPair( "Id", "Num6" ) },
61 				  { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
62 				  { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) },
63 				  { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) },
64 				  { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) },
65 				  { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) }
66 				};
67 
68 
69 			XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
70 			if ( xTempFileStream == null )
71 				return false;
72 
73 			// create storage based on the temporary stream
74 			XStorage xTempStorage = m_aTestHelper.createStorageFromStream( m_xStorageFactory,
75 																			xTempFileStream,
76 																			ElementModes.WRITE );
77 			if ( xTempStorage == null )
78 			{
79 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
80 				return false;
81 			}
82 
83 			// open a new substorage
84 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
85 																	"SubStorage1",
86 																	ElementModes.WRITE );
87 			if ( xTempSubStorage == null )
88 			{
89 				m_aTestHelper.Error( "Can't create substorage!" );
90 				return false;
91 			}
92 
93 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
94 
95 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
96 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage,
97 														"SubStream1",
98 														"MediaType1",
99 														true,
100 														pBytes1,
101 														aRelations ) )
102 				return false;
103 
104 			// set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
105 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
106 															true,
107 															ElementModes.WRITE,
108 															aRelations ) )
109 				return false;
110 
111 			// set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
112 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
113 															false,
114 															ElementModes.WRITE,
115 															aRelations ) )
116 				return false;
117 
118 			// commit substorage first
119 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
120 				return false;
121 
122 			// commit the root storage so the contents must be stored now
123 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
124 				return false;
125 
126 			// dispose used storage to free resources
127 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
128 				return false;
129 
130 
131 			// ================================================
132 			// now check all the written information
133 			// ================================================
134 
135 			// close the output part of the temporary stream
136 			// the output part must present since we already wrote to the stream
137 			if ( !m_aTestHelper.closeOutput( xTempFileStream ) )
138 				return false;
139 
140 			XInputStream xTempInStream = m_aTestHelper.getInputStream( xTempFileStream );
141 			if ( xTempInStream == null )
142 				return false;
143 
144 
145 			// open input stream
146 			XStorage xResultStorage = m_aTestHelper.createStorageFromInputStream( m_xStorageFactory, xTempInStream );
147 			if ( xResultStorage == null )
148 			{
149 				m_aTestHelper.Error( "Can't open storage based on input stream!" );
150 				return false;
151 			}
152 
153 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage, true, ElementModes.READ, aRelations ) )
154 				return false;
155 
156 			// open existing substorage
157 			XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
158 																		"SubStorage1",
159 																		ElementModes.READ );
160 			if ( xResultSubStorage == null )
161 			{
162 				m_aTestHelper.Error( "Can't open existing substorage!" );
163 				return false;
164 			}
165 
166 			if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage,
167 														false,
168 														ElementModes.READ,
169 														aRelations ) )
170 				return false;
171 
172 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream1", "MediaType1", pBytes1, aRelations ) )
173 				return false;
174 
175 			return true;
176 		}
177 		catch( Exception e )
178 		{
179 			m_aTestHelper.Error( "Exception: " + e );
180 			return false;
181 		}
182     }
183 
184 }
185 
186