1 package complex
.olesimplestorage
;
3 import complexlib
.ComplexTestCase
;
5 import com
.sun
.star
.lang
.XMultiServiceFactory
;
6 import com
.sun
.star
.io
.XInputStream
;
7 import com
.sun
.star
.io
.XOutputStream
;
8 import com
.sun
.star
.io
.XTempFile
;
9 import com
.sun
.star
.embed
.XOLESimpleStorage
;
10 import com
.sun
.star
.uno
.UnoRuntime
;
12 import java
.util
.Random
;
13 import share
.LogWriter
;
15 public class Test01
implements OLESimpleStorageTest
17 XMultiServiceFactory m_xMSF
= null;
18 TestHelper m_aTestHelper
= null;
19 final int pStreamCnt
= 5;
20 final int pBytesCnt
= 10;
22 public Test01 ( XMultiServiceFactory xMSF
, LogWriter aLogWriter
)
25 m_aTestHelper
= new TestHelper (aLogWriter
, "Test01: ");
28 public boolean test ()
32 //create a new temporary stream
33 Object oTempFile
= m_xMSF
.createInstance ( "com.sun.star.io.TempFile" );
34 XTempFile xTempFile
= (XTempFile
) UnoRuntime
.queryInterface ( XTempFile
.class, oTempFile
);
35 m_aTestHelper
.Message ( "A new temporary stream created." );
37 //create OLESimpleStorage based on it
38 Object pArgs
[] = new Object
[2];
39 pArgs
[0] = (Object
) xTempFile
;
40 pArgs
[1] = new Boolean( true );
41 Object oOLESimpleStorage
= m_xMSF
.createInstanceWithArguments ( "com.sun.star.embed.OLESimpleStorage", pArgs
);
42 XOLESimpleStorage xOLESimpleStorage
= (XOLESimpleStorage
) UnoRuntime
.queryInterface ( XOLESimpleStorage
.class, oOLESimpleStorage
);
43 m_aTestHelper
.Message ( "OLESimpleStorage based on XStream created." );
45 //fill it with some streams
46 Object oStream
[] = new Object
[pStreamCnt
];
47 byte pBytesIn
[][][] = new byte [pStreamCnt
][1][pBytesCnt
];
48 byte pBytesOut
[][] = new byte [pStreamCnt
][pBytesCnt
];
49 XTempFile xTempStream
[] = new XTempFile
[pStreamCnt
];
50 Random oRandom
= new Random ();
51 final String sSubStreamPrefix
= "SubStream";
52 for ( int i
= 0; i
< pStreamCnt
; i
++ )
54 oRandom
.nextBytes (pBytesOut
[i
]);
55 oStream
[i
] = m_xMSF
.createInstance ( "com.sun.star.io.TempFile" );
56 xTempStream
[i
] = (XTempFile
) UnoRuntime
.queryInterface ( XTempFile
.class, oStream
[i
] );
57 xTempStream
[i
].getOutputStream ().writeBytes (pBytesOut
[i
]);
58 xTempStream
[i
].seek (0);
59 m_aTestHelper
.Message ( "Substream " + i
+ " initialized." );
60 if (xOLESimpleStorage
.hasByName (sSubStreamPrefix
+ i
))
62 xOLESimpleStorage
.replaceByName ( sSubStreamPrefix
+ i
, xTempStream
[i
] );
66 xOLESimpleStorage
.insertByName ( sSubStreamPrefix
+ i
, xTempStream
[i
] );
67 m_aTestHelper
.Message ( "Substream " + i
+ " inserted." );
71 //commit the storage and close it
72 xOLESimpleStorage
.commit ();
73 m_aTestHelper
.Message ( "Storage commited." );
74 xOLESimpleStorage
.dispose ();
75 for ( int i
= 0; i
< pStreamCnt
; ++i
)
77 xTempStream
[i
].setRemoveFile ( true );
78 xTempStream
[i
].getInputStream ().closeInput ();
79 xTempStream
[i
].getOutputStream ().closeOutput ();
81 m_aTestHelper
.Message ( "Storage closed." );
83 //open the same stream with the constructor for inputstream
84 pArgs
[0] = (Object
)xTempFile
.getInputStream ();
85 oOLESimpleStorage
= m_xMSF
.createInstanceWithArguments ( "com.sun.star.embed.OLESimpleStorage", pArgs
);
86 xOLESimpleStorage
= (XOLESimpleStorage
)UnoRuntime
.queryInterface ( XOLESimpleStorage
.class, oOLESimpleStorage
);
87 m_aTestHelper
.Message ( "Storage reopened, based on XInputStream." );
89 //check that all the streams contain correct information
90 m_aTestHelper
.Message ( "Checking data contained in all the substreams..." );
91 for ( int i
= 0; i
< pStreamCnt
; ++i
)
93 if ( xOLESimpleStorage
.hasByName (sSubStreamPrefix
+ i
) )
95 xTempStream
[i
] = (XTempFile
)UnoRuntime
.queryInterface (
96 XTempFile
.class, xOLESimpleStorage
.getByName (sSubStreamPrefix
+ i
) );
97 xTempStream
[i
].seek (0);
98 xTempStream
[i
].getInputStream ().readBytes (pBytesIn
[i
], pBytesIn
[i
][0].length
+ 1 );
99 for ( int j
= 0; j
< pBytesCnt
; ++j
)
101 if ( pBytesIn
[i
][0][j
] != pBytesOut
[i
][j
] )
103 m_aTestHelper
.Error ( "Stream " + i
+ " byte " + j
+ ": INCORRECT DATA!");
108 m_aTestHelper
.Message ( "Stream " + i
+ " byte " + j
+ ": CORRECT." );
114 m_aTestHelper
.Error( "Stream " + i
+ " is lost!");
118 m_aTestHelper
.Message ( "All substreams contain correct data. SUCCESS." );
120 catch ( Exception e
)
122 m_aTestHelper
.Error ( "Exception: " + e
);