1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3<script:module xmlns:script="http://openoffice.org/2000/script" script:name="io_XMarkableStream" script:language="StarBasic">
4
5
6'*************************************************************************
7'
8' DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9'
10' Copyright 2000, 2010 Oracle and/or its affiliates.
11'
12' OpenOffice.org - a multi-platform office productivity suite
13'
14' This file is part of OpenOffice.org.
15'
16' OpenOffice.org is free software: you can redistribute it and/or modify
17' it under the terms of the GNU Lesser General Public License version 3
18' only, as published by the Free Software Foundation.
19'
20' OpenOffice.org is distributed in the hope that it will be useful,
21' but WITHOUT ANY WARRANTY; without even the implied warranty of
22' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23' GNU Lesser General Public License version 3 for more details
24' (a copy is included in the LICENSE file that accompanied this code).
25'
26' You should have received a copy of the GNU Lesser General Public License
27' version 3 along with OpenOffice.org.  If not, see
28' <http://www.openoffice.org/license.html>
29' for a copy of the LGPLv3 License.
30'
31'*************************************************************************
32'*************************************************************************
33
34
35
36' Be sure that all variables are dimensioned:
37option explicit
38
39
40
41Sub RunTest()
42
43'*************************************************************************
44' INTERFACE:
45' com.sun.star.io.XMarkableStream
46'*************************************************************************
47On Error Goto ErrHndl
48    Dim bOK As Boolean
49    Dim Bytes(20) As Integer
50    Dim rBytes(0)
51    Dim lastRByte As Integer
52    Dim i As Integer
53    Dim oOutStream As Object
54    Dim oInStream As Object
55    Dim iMark1 As Integer
56    Dim iMark2 As Integer
57    Dim iByte As Integer
58    Dim iBytes As Integer
59    Dim iByteAfterMark As Integer
60    Dim iByteAfterMark1 As Integer
61    Dim iByteAfterMark2 As Integer
62    Dim iOffset As Integer
63    Dim sFileStr As String
64
65    if (cObjectName = "stm.MarkableInputStream" OR cObjectName = "stm.ObjectInputStream") then
66
67        for i = 0 to ubound(Bytes())
68            Bytes(i) = i * 2
69        next i
70
71        Out.Log("First reset streams and write some bytes...")
72        oOutStream = getOutStream()
73        oOutStream.writeBytes(Bytes())
74        ResetStreams()
75
76        Test.StartMethod("createMark()")
77        bOK = true
78        Out.Log("Skip 3 bytes.")
79        oObj.skipBytes(3)
80        iMark1 = oObj.createMark()
81        Out.Log("Mark" + iMark1 + " was created.")
82
83        oObj.readBytes(rBytes(), 1)
84        iByteAfterMark1 = rBytes(0)
85        Out.Log("Byte after Mark" + iMark1 + " is " + int(iByteAfterMark1))
86
87        Out.Log("Skip 5 bytes.")
88        oObj.skipBytes(5)
89        iMark2 = oObj.createMark()
90        Out.Log("Mark" + iMark2 + " was created.")
91
92        oObj.readBytes(rBytes(), 1)
93        iByteAfterMark2 = rBytes(0)
94        Out.Log("Byte after Mark" + iMark2 + " is " + int(iByteAfterMark2))
95
96        Out.Log("Skip 7 bytes.")
97        oObj.skipBytes(7)
98        oObj.readBytes(rBytes(), 1)
99        lastRByte = rBytes(0)
100
101        Out.Log("Jump to Mark" + iMark2)
102        oObj.jumpToMark(iMark2)
103        oObj.readBytes(rBytes(), 1)
104        iByteAfterMark = rBytes(0)
105        Out.Log("Byte after Mark" + iMark2 + " is " + int(iByteAfterMark) + ", expected " + int(iByteAfterMark2))
106        bOK = bOK AND iByteAfterMark = iByteAfterMark2
107
108        Out.Log("Jump to Mark" + iMark1)
109        oObj.jumpToMark(iMark1)
110        oObj.readBytes(rBytes(), 1)
111        iByteAfterMark = rBytes(0)
112        Out.Log("Byte after Mark" + iMark1 + " is " + int(iByteAfterMark) + ", expected " + int(iByteAfterMark1))
113        bOK = bOK AND iByteAfterMark = iByteAfterMark1
114
115        Test.MethodTested("createMark()", bOK)
116        Test.MethodTested("jumpToMark()", bOK)
117
118        Test.StartMethod("offsetToMark()")
119        bOK = true
120        iOffset = oObj.offsetToMark(iMark2)
121        Out.Log("Offset from current position to Mark" + iMark2 + " is " + iOffset)
122        bOK = bOK AND iOffset = -5
123        Test.MethodTested("offsetToMark()", bOK)
124
125        Test.StartMethod("deleteMark()")
126        bOK = true
127        Out.Log("Delete Mark" + iMark1)
128        oObj.deleteMark(iMark1)
129        On Error goto ErrHndl1
130            Out.Log("Trying to jump to deleted mark")
131            oObj.jumpToMark(iMark1)
132            Out.Log("No exception occured. FAILED")
133            bOK = false
134            goto Cont1
135        ErrHndl1:
136            Out.Log("Expected exception: " + error)
137        Cont1:
138        Test.MethodTested("deleteMark()", bOK)
139
140        Test.StartMethod("jumpToFurthest()")
141        bOK = true
142        oObj.readBytes(rBytes(), 1)
143        iByte = rBytes(0)
144        Out.Log("Perform a reading operation from the current position. Byte " + int(iByte) + " was read.")
145        Out.Log("Changing position.")
146        oObj.jumpToMark(iMark2)
147        Out.Log("Changing position with jumpToFurthest()")
148        oObj.jumpToFurthest()
149        oObj.readBytes(rBytes(), 1)
150        Out.Log("From the current position byte " + int(rBytes(0)) + " was read. Expected byte is " + int(lastRByte) + 2)
151        bOK = bOK AND lastRByte + 2 = rBytes(0)
152        Test.MethodTested("jumpToFurthest()", bOK)
153    else
154        bOK = true
155        Out.Log("Write 3 bytes to stream")
156        ReDim Bytes(2) As Integer
157        for i = 0 to ubound(Bytes())
158            Bytes(i) = i
159        next i
160        oObj.writeBytes(Bytes())
161        Out.Log("Creating a Mark.")
162        iMark1 = oObj.createMark()
163        Out.Log("Write 4 bytes to stream")
164        ReDim Bytes(3) As Integer
165        for i = 0 to ubound(Bytes())
166            Bytes(i) = i + 3
167        next i
168        oObj.writeBytes(Bytes())
169        Out.Log("Creating a Mark.")
170        iMark2 = oObj.createMark()
171
172        iOffset = oObj.offsetToMark(iMark1)
173        Out.Log("Offset from current position to Mark" + iMark1 + " is " + iOffset)
174        bOK = bOK AND iOffset = 4
175        Test.MethodTested("offsetToMark()", bOK)
176
177        Out.Log("Write 5 bytes to stream")
178        ReDim Bytes(4) As Integer
179        for i = 0 to ubound(Bytes())
180            Bytes(i) = i + 7
181        next i
182        oObj.writeBytes(Bytes())
183
184        Out.Log("Testing jumpToMark()")
185        Out.Log("Testing deleteMark()")
186        bOK = true
187        Out.Log("Deleting Mark1")
188        oObj.deleteMark(iMark2)
189        On Error goto ErrHndl2
190            Out.Log("Trying to jump to Mark1")
191            oObj.jumpToMark(iMark2)
192            Out.Log("No exception occured - FAILED")
193            bOK = false
194            goto Cont2
195        ErrHndl2:
196            Out.Log("Expected exception: " + error)
197        Cont2:
198
199        Test.MethodTested("deleteMark()", bOK)
200
201        bOK = true
202
203        Out.Log("Jump to Mark0")
204        oObj.jumpToMark(iMark1)
205        Test.MethodTested("jumpToMark()", bOK)
206        Test.MethodTested("createMark()", bOK)
207
208        bOK = true
209        Out.Log("Write 2 bytes to stream")
210        ReDim Bytes(1) As Integer
211
212        for i = 0 to ubound(Bytes())
213            Bytes(i) = i + 12
214        next i
215
216        oObj.writeBytes(Bytes())
217        Out.Log("Changing position")
218        oObj.jumpToMark(iMark1)
219        Out.Log("Changing position with jumpToFurthest()")
220        oObj.jumpToFurthest()
221
222        Out.Log("Write 2 bytes to stream")
223        ReDim Bytes(1) As Integer
224        for i = 0 to ubound(Bytes())
225            Bytes(i) = i + 14
226        next i
227        oObj.writeBytes(Bytes())
228
229        Out.Log("Comparing file with expected {0, 1, 2, 12, 13, 5, 6, 7, 8, 9, 10, 11, 14, 15}")
230        oInStream = getInStream()
231        iBytes = oInStream.readBytes(rBytes(), 20)
232        Out.Log("There are " + iBytes + " in stream:")
233        sFileStr = "" + int(rBytes(0))
234        for i = 1 to ubound(rBytes())
235            sFileStr = sFileStr + ", " + int(rBytes(i))
236        next i
237        Out.Log("They are {" + sFileStr + "}")
238        bOK = bOK AND sFileStr = "0, 1, 2, 12, 13, 5, 6, 7, 8, 9, 10, 11, 14, 15"
239
240        Test.MethodTested("jumpToFurthest()", bOK)
241    end if
242
243    ResetStreams()
244    DisposeObj()
245    CreateObj()
246
247Exit Sub
248ErrHndl:
249    Test.Exception()
250    bOK = false
251    resume next
252End Sub
253</script:module>
254