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
37' Be sure that all variables are dimensioned:
38option explicit
39
40
41
42Sub RunTest()
43
44'*************************************************************************
45' INTERFACE:
46' com.sun.star.io.XMarkableStream
47'*************************************************************************
48On Error Goto ErrHndl
49    Dim bOK As Boolean
50    Dim Bytes(20) As Integer
51    Dim rBytes(0)
52    Dim lastRByte As Integer
53    Dim i As Integer
54    Dim oOutStream As Object
55    Dim oInStream As Object
56    Dim iMark1 As Integer
57    Dim iMark2 As Integer
58    Dim iByte As Integer
59    Dim iBytes As Integer
60    Dim iByteAfterMark As Integer
61    Dim iByteAfterMark1 As Integer
62    Dim iByteAfterMark2 As Integer
63    Dim iOffset As Integer
64    Dim sFileStr As String
65
66    if (cObjectName = "stm.MarkableInputStream" OR cObjectName = "stm.ObjectInputStream") then
67
68        for i = 0 to ubound(Bytes())
69            Bytes(i) = i * 2
70        next i
71
72        Out.Log("First reset streams and write some bytes...")
73        oOutStream = getOutStream()
74        oOutStream.writeBytes(Bytes())
75        ResetStreams()
76
77        Test.StartMethod("createMark()")
78        bOK = true
79        Out.Log("Skip 3 bytes.")
80        oObj.skipBytes(3)
81        iMark1 = oObj.createMark()
82        Out.Log("Mark" + iMark1 + " was created.")
83
84        oObj.readBytes(rBytes(), 1)
85        iByteAfterMark1 = rBytes(0)
86        Out.Log("Byte after Mark" + iMark1 + " is " + int(iByteAfterMark1))
87
88        Out.Log("Skip 5 bytes.")
89        oObj.skipBytes(5)
90        iMark2 = oObj.createMark()
91        Out.Log("Mark" + iMark2 + " was created.")
92
93        oObj.readBytes(rBytes(), 1)
94        iByteAfterMark2 = rBytes(0)
95        Out.Log("Byte after Mark" + iMark2 + " is " + int(iByteAfterMark2))
96
97        Out.Log("Skip 7 bytes.")
98        oObj.skipBytes(7)
99        oObj.readBytes(rBytes(), 1)
100        lastRByte = rBytes(0)
101
102        Out.Log("Jump to Mark" + iMark2)
103        oObj.jumpToMark(iMark2)
104        oObj.readBytes(rBytes(), 1)
105        iByteAfterMark = rBytes(0)
106        Out.Log("Byte after Mark" + iMark2 + " is " + int(iByteAfterMark) + ", expected " + int(iByteAfterMark2))
107        bOK = bOK AND iByteAfterMark = iByteAfterMark2
108
109        Out.Log("Jump to Mark" + iMark1)
110        oObj.jumpToMark(iMark1)
111        oObj.readBytes(rBytes(), 1)
112        iByteAfterMark = rBytes(0)
113        Out.Log("Byte after Mark" + iMark1 + " is " + int(iByteAfterMark) + ", expected " + int(iByteAfterMark1))
114        bOK = bOK AND iByteAfterMark = iByteAfterMark1
115
116        Test.MethodTested("createMark()", bOK)
117        Test.MethodTested("jumpToMark()", bOK)
118
119        Test.StartMethod("offsetToMark()")
120        bOK = true
121        iOffset = oObj.offsetToMark(iMark2)
122        Out.Log("Offset from current position to Mark" + iMark2 + " is " + iOffset)
123        bOK = bOK AND iOffset = -5
124        Test.MethodTested("offsetToMark()", bOK)
125
126        Test.StartMethod("deleteMark()")
127        bOK = true
128        Out.Log("Delete Mark" + iMark1)
129        oObj.deleteMark(iMark1)
130        On Error goto ErrHndl1
131            Out.Log("Trying to jump to deleted mark")
132            oObj.jumpToMark(iMark1)
133            Out.Log("No exception occured. FAILED")
134            bOK = false
135            goto Cont1
136        ErrHndl1:
137            Out.Log("Expected exception: " + error)
138        Cont1:
139        Test.MethodTested("deleteMark()", bOK)
140
141        Test.StartMethod("jumpToFurthest()")
142        bOK = true
143        oObj.readBytes(rBytes(), 1)
144        iByte = rBytes(0)
145        Out.Log("Perform a reading operation from the current position. Byte " + int(iByte) + " was read.")
146        Out.Log("Changing position.")
147        oObj.jumpToMark(iMark2)
148        Out.Log("Changing position with jumpToFurthest()")
149        oObj.jumpToFurthest()
150        oObj.readBytes(rBytes(), 1)
151        Out.Log("From the current position byte " + int(rBytes(0)) + " was read. Expected byte is " + int(lastRByte) + 2)
152        bOK = bOK AND lastRByte + 2 = rBytes(0)
153        Test.MethodTested("jumpToFurthest()", bOK)
154    else
155        bOK = true
156        Out.Log("Write 3 bytes to stream")
157        ReDim Bytes(2) As Integer
158        for i = 0 to ubound(Bytes())
159            Bytes(i) = i
160        next i
161        oObj.writeBytes(Bytes())
162        Out.Log("Creating a Mark.")
163        iMark1 = oObj.createMark()
164        Out.Log("Write 4 bytes to stream")
165        ReDim Bytes(3) As Integer
166        for i = 0 to ubound(Bytes())
167            Bytes(i) = i + 3
168        next i
169        oObj.writeBytes(Bytes())
170        Out.Log("Creating a Mark.")
171        iMark2 = oObj.createMark()
172
173        iOffset = oObj.offsetToMark(iMark1)
174        Out.Log("Offset from current position to Mark" + iMark1 + " is " + iOffset)
175        bOK = bOK AND iOffset = 4
176        Test.MethodTested("offsetToMark()", bOK)
177
178        Out.Log("Write 5 bytes to stream")
179        ReDim Bytes(4) As Integer
180        for i = 0 to ubound(Bytes())
181            Bytes(i) = i + 7
182        next i
183        oObj.writeBytes(Bytes())
184
185        Out.Log("Testing jumpToMark()")
186        Out.Log("Testing deleteMark()")
187        bOK = true
188        Out.Log("Deleting Mark1")
189        oObj.deleteMark(iMark2)
190        On Error goto ErrHndl2
191            Out.Log("Trying to jump to Mark1")
192            oObj.jumpToMark(iMark2)
193            Out.Log("No exception occured - FAILED")
194            bOK = false
195            goto Cont2
196        ErrHndl2:
197            Out.Log("Expected exception: " + error)
198        Cont2:
199
200        Test.MethodTested("deleteMark()", bOK)
201
202        bOK = true
203
204        Out.Log("Jump to Mark0")
205        oObj.jumpToMark(iMark1)
206        Test.MethodTested("jumpToMark()", bOK)
207        Test.MethodTested("createMark()", bOK)
208
209        bOK = true
210        Out.Log("Write 2 bytes to stream")
211        ReDim Bytes(1) As Integer
212
213        for i = 0 to ubound(Bytes())
214            Bytes(i) = i + 12
215        next i
216
217        oObj.writeBytes(Bytes())
218        Out.Log("Changing position")
219        oObj.jumpToMark(iMark1)
220        Out.Log("Changing position with jumpToFurthest()")
221        oObj.jumpToFurthest()
222
223        Out.Log("Write 2 bytes to stream")
224        ReDim Bytes(1) As Integer
225        for i = 0 to ubound(Bytes())
226            Bytes(i) = i + 14
227        next i
228        oObj.writeBytes(Bytes())
229
230        Out.Log("Comparing file with expected {0, 1, 2, 12, 13, 5, 6, 7, 8, 9, 10, 11, 14, 15}")
231        oInStream = getInStream()
232        iBytes = oInStream.readBytes(rBytes(), 20)
233        Out.Log("There are " + iBytes + " in stream:")
234        sFileStr = "" + int(rBytes(0))
235        for i = 1 to ubound(rBytes())
236            sFileStr = sFileStr + ", " + int(rBytes(i))
237        next i
238        Out.Log("They are {" + sFileStr + "}")
239        bOK = bOK AND sFileStr = "0, 1, 2, 12, 13, 5, 6, 7, 8, 9, 10, 11, 14, 15"
240
241        Test.MethodTested("jumpToFurthest()", bOK)
242    end if
243
244    ResetStreams()
245    DisposeObj()
246    CreateObj()
247
248Exit Sub
249ErrHndl:
250    Test.Exception()
251    bOK = false
252    resume next
253End Sub
254</script:module>
255