xref: /AOO41X/main/package/qa/storages/RegressionTest_125919.java (revision a740f2aac71e58ccad9369fb423cc251ef909663)
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 java.lang.Integer;
25 
26 import com.sun.star.uno.XInterface;
27 import com.sun.star.lang.XMultiServiceFactory;
28 import com.sun.star.lang.XSingleServiceFactory;
29 
30 import com.sun.star.bridge.XUnoUrlResolver;
31 import com.sun.star.uno.UnoRuntime;
32 import com.sun.star.uno.XInterface;
33 import com.sun.star.io.XStream;
34 import com.sun.star.io.XInputStream;
35 
36 import com.sun.star.embed.*;
37 
38 import share.LogWriter;
39 import complex.storages.TestHelper;
40 import complex.storages.StorageTest;
41 import complex.storages.BorderedStream;
42 
43 public class RegressionTest_125919 implements StorageTest {
44 
45     XMultiServiceFactory m_xMSF;
46     XSingleServiceFactory m_xStorageFactory;
47     TestHelper m_aTestHelper;
48 
49     int nMinTestLen = 0;
50     int nMaxTestLen = 60000;
51 
RegressionTest_125919( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )52     public RegressionTest_125919( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
53     {
54         m_xMSF = xMSF;
55         m_xStorageFactory = xStorageFactory;
56         m_aTestHelper = new TestHelper( aLogWriter, "RegressionTest_125919: " );
57     }
58 
test()59     public boolean test()
60     {
61         try
62         {
63             byte[] pBytes0 = new byte[0];
64             byte[] pBytes18 = new byte[18000];
65             byte[] pBytes36 = new byte[36000];
66 
67             for ( int nInitInd = 0; nInitInd < 36000; nInitInd++ )
68             {
69                 pBytes36[nInitInd] = ( new Integer( nInitInd >> ( ( nInitInd % 2 ) * 8 ) ) ).byteValue();
70                 if ( nInitInd < 18000 )
71                     pBytes18[nInitInd] = ( new Integer( 256  - pBytes36[nInitInd] ) ).byteValue();
72             }
73 
74             System.out.println( "This test can take up to some hours. The file size currently is about 50000." );
75             System.out.println( "Progress: " );
76             for ( int nAvailableBytes = nMinTestLen; nAvailableBytes < nMaxTestLen; nAvailableBytes++ )
77             {
78                 Object oBStream = new BorderedStream( nAvailableBytes );
79                 XStream xBorderedStream = (XStream)UnoRuntime.queryInterface( XStream.class, oBStream );
80                 if ( xBorderedStream == null )
81                 {
82                     m_aTestHelper.Error( "Can't create bordered stream!" );
83                     return false;
84                 }
85 
86                 // create storage based on the temporary stream
87                 Object pArgs[] = new Object[2];
88                 pArgs[0] = (Object) xBorderedStream;
89                 pArgs[1] = new Integer( ElementModes.WRITE );
90 
91                 Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
92                 XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
93                 if ( xTempStorage == null )
94                 {
95                     m_aTestHelper.Error( "Can't create temporary storage representation!" );
96                     return false;
97                 }
98 
99                 XTransactedObject xTransact = (XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xTempStorage );
100                 if ( xTransact == null )
101                 {
102                     m_aTestHelper.Error( "This test is designed for storages in transacted mode!" );
103                     return false;
104                 }
105 
106 
107                 if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "SubStream" + 0, "MediaType1", true, pBytes0 ) )
108                     return false;
109                 if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "SubStream" + 18, "MediaType2", true, pBytes18 ) )
110                     return false;
111                 if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "SubStream" + 36, "MediaType3", true, pBytes36 ) )
112                     return false;
113 
114                 if ( nAvailableBytes > 0 && nAvailableBytes % 100 == 0 )
115                     System.out.println( " " + nAvailableBytes );
116 
117                 if ( nAvailableBytes > 0 && nAvailableBytes % 2 == 1 )
118                     System.out.print( "#" );
119 
120                 try
121                 {
122                     xTransact.commit();
123 
124                     System.out.println( "" );
125                     if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
126                         return false;
127 
128                     // SUCCESS
129                     return true;
130                 }
131                 catch( UseBackupException aExc )
132                 {
133                     // when there is not enough place in the target location and the target file is empty
134                     // the direct writing will fail and must throw this exception with empty URL
135                     if ( aExc.TemporaryFileURL.length() != 0 )
136                         return false;
137                 }
138                 catch( Exception e )
139                 {
140                     System.out.println( "" );
141                     m_aTestHelper.Error( "Unexpected exception: " + e + "\nnAvailableBytes = " + nAvailableBytes );
142                     return false;
143                 }
144             }
145 
146             return false;
147         }
148         catch( Exception e )
149         {
150             m_aTestHelper.Error( "Exception: " + e );
151             return false;
152         }
153     }
154 }
155 
156