xref: /trunk/main/odk/examples/java/Storage/Test07.java (revision a893be29)
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 Test07 implements StorageTest {
38 
39 	XMultiServiceFactory m_xMSF;
40 	XSingleServiceFactory m_xStorageFactory;
41 	TestHelper m_aTestHelper;
42 
Test07( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )43 	public Test07( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
44 	{
45 		m_xMSF = xMSF;
46 		m_xStorageFactory = xStorageFactory;
47 		m_aTestHelper = new TestHelper( "Test07: " );
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 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
72 			byte pPass1[] = { 1, 2, 3, 4, 5 };
73 
74 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
75 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes1, pPass1 ) )
76 				return false;
77 
78 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
79 			byte pPass2[] = { 5, 4, 3, 2, 1 };
80 
81 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
82 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream2", "MediaType2", false, pBytes2, pPass2 ) )
83 				return false;
84 
85 			// create temporary storage based on a previously created temporary file
86 			Object pArgs[] = new Object[2];
87 			pArgs[0] = (Object) sTempFileURL;
88 			pArgs[1] = new Integer( ElementModes.ELEMENT_WRITE );
89 
90 			Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
91 			XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
92 			if ( xTempFileStorage == null )
93 			{
94 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
95 				return false;
96 			}
97 
98 			// copy xTempStorage to xTempFileStorage
99 			// xTempFileStorage will be automatically committed
100 			if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
101 				return false;
102 
103 			// dispose used storages to free resources
104 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
105 				return false;
106 
107 			// ================================================
108 			// now check all the written and copied information
109 			// ================================================
110 
111 			// the temporary file must not be locked any more after storage disposing
112 			pArgs[1] = new Integer( ElementModes.ELEMENT_READWRITE );
113 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
114 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
115 			if ( xResultStorage == null )
116 			{
117 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
118 				return false;
119 			}
120 
121 			Object o2CopyStorage = m_xStorageFactory.createInstance();
122 			XStorage x2CopyStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, o2CopyStorage );
123 			if ( x2CopyStorage == null )
124 			{
125 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
126 				return false;
127 			}
128 
129 			if ( !m_aTestHelper.copyStorage( xResultStorage, x2CopyStorage ) )
130 				return false;
131 
132 			if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream1", "MediaType1", pBytes1, pPass1 ) )
133 				return false;
134 
135 			if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream2", "MediaType2", pBytes2, pPass2 ) )
136 				return false;
137 
138 			if ( !m_aTestHelper.checkEncrStream( x2CopyStorage, "SubStream1", "MediaType1", pBytes1, pPass1 ) )
139 				return false;
140 
141 			if ( !m_aTestHelper.checkEncrStream( x2CopyStorage, "SubStream2", "MediaType2", pBytes2, pPass2 ) )
142 				return false;
143 
144 			// dispose used storages to free resources
145 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
146 				return false;
147 
148 			return true;
149 		}
150 		catch( Exception e )
151 		{
152 			m_aTestHelper.Error( "Exception: " + e );
153 			return false;
154 		}
155     }
156 
157 }
158 
159