bump product version to 7.6.3.2-android
[LibreOffice.git] / sot / qa / complex / olesimplestorage / Test01.java
blob18d220e05488cd16cafa2bd0b293c804f4654a19
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package complex.olesimplestorage;
21 import com.sun.star.lang.XMultiServiceFactory;
22 import com.sun.star.io.XTempFile;
23 import com.sun.star.embed.XOLESimpleStorage;
24 import com.sun.star.uno.UnoRuntime;
26 import java.util.Random;
29 public class Test01 implements OLESimpleStorageTest
31 private XMultiServiceFactory m_xMSF = null;
32 private TestHelper m_aTestHelper = null;
33 private static final int pStreamCnt = 5;
34 private static final int pBytesCnt = 10;
36 public Test01 ( XMultiServiceFactory xMSF )
38 m_xMSF = xMSF;
39 m_aTestHelper = new TestHelper ("Test01: ");
42 public boolean test ()
44 try
46 //create a new temporary stream
47 Object oTempFile = m_xMSF.createInstance ( "com.sun.star.io.TempFile" );
48 XTempFile xTempFile = UnoRuntime.queryInterface(XTempFile.class, oTempFile);
49 m_aTestHelper.Message ( "A new temporary stream created." );
51 //create OLESimpleStorage based on it
52 Object pArgs[] = new Object[2];
53 pArgs[0] = xTempFile;
54 pArgs[1] = Boolean.TRUE;
55 Object oOLESimpleStorage = m_xMSF.createInstanceWithArguments ( "com.sun.star.embed.OLESimpleStorage", pArgs );
56 XOLESimpleStorage xOLESimpleStorage = UnoRuntime.queryInterface(XOLESimpleStorage.class, oOLESimpleStorage);
57 m_aTestHelper.Message ( "OLESimpleStorage based on XStream created." );
59 //fill it with some streams
60 Object oStream[] = new Object[pStreamCnt];
61 byte pBytesIn[][][] = new byte [pStreamCnt][1][pBytesCnt];
62 byte pBytesOut[][] = new byte [pStreamCnt][pBytesCnt];
63 XTempFile xTempStream[] = new XTempFile[pStreamCnt];
64 Random oRandom = new Random ();
65 final String sSubStreamPrefix = "SubStream";
66 for ( int i = 0; i < pStreamCnt; i++ )
68 oRandom.nextBytes (pBytesOut[i]);
69 oStream[i] = m_xMSF.createInstance ( "com.sun.star.io.TempFile" );
70 xTempStream[i] = UnoRuntime.queryInterface(XTempFile.class, oStream[i]);
71 xTempStream[i].getOutputStream ().writeBytes (pBytesOut[i]);
72 xTempStream[i].seek (0);
73 m_aTestHelper.Message ( "Substream " + i + " initialized." );
74 if (xOLESimpleStorage.hasByName (sSubStreamPrefix + i))
76 xOLESimpleStorage.replaceByName ( sSubStreamPrefix + i, xTempStream[i] );
78 else
80 xOLESimpleStorage.insertByName ( sSubStreamPrefix + i, xTempStream[i] );
81 m_aTestHelper.Message ( "Substream " + i + " inserted." );
85 //commit the storage and close it
86 xOLESimpleStorage.commit ();
87 m_aTestHelper.Message ( "Storage committed." );
88 xOLESimpleStorage.dispose ();
89 for ( int i = 0; i < pStreamCnt; ++i )
91 xTempStream[i].setRemoveFile ( true );
92 xTempStream[i].getInputStream ().closeInput ();
93 xTempStream[i].getOutputStream ().closeOutput ();
95 m_aTestHelper.Message ( "Storage closed." );
97 //open the same stream with the constructor for inputstream
98 pArgs[0] = xTempFile.getInputStream ();
99 oOLESimpleStorage = m_xMSF.createInstanceWithArguments ( "com.sun.star.embed.OLESimpleStorage", pArgs );
100 xOLESimpleStorage = UnoRuntime.queryInterface(XOLESimpleStorage.class, oOLESimpleStorage);
101 m_aTestHelper.Message ( "Storage reopened, based on XInputStream." );
103 //check that all the streams contain correct information
104 m_aTestHelper.Message ( "Checking data contained in all the substreams..." );
105 for ( int i = 0; i < pStreamCnt; ++i )
107 if ( xOLESimpleStorage.hasByName (sSubStreamPrefix + i) )
109 xTempStream[i] = UnoRuntime.queryInterface(XTempFile.class, xOLESimpleStorage.getByName(sSubStreamPrefix + i));
110 xTempStream[i].seek (0);
111 xTempStream[i].getInputStream ().readBytes (pBytesIn[i], pBytesIn[i][0].length + 1 );
112 for ( int j = 0; j < pBytesCnt; ++j )
114 if ( pBytesIn[i][0][j] != pBytesOut[i][j] )
116 m_aTestHelper.Error ( "Stream " + i + " byte " + j + ": INCORRECT DATA!");
117 return false;
119 else
121 m_aTestHelper.Message ( "Stream " + i + " byte " + j + ": CORRECT." );
125 else
127 m_aTestHelper.Error( "Stream " + i + " is lost!");
128 return false;
131 m_aTestHelper.Message ( "All substreams contain correct data. SUCCESS." );
133 catch ( Exception e )
135 m_aTestHelper.Error ( "Exception: " + e );
136 return false;
138 return true;