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'  Licensed to the Apache Software Foundation (ASF) under one
9'  or more contributor license agreements.  See the NOTICE file
10'  distributed with this work for additional information
11'  regarding copyright ownership.  The ASF licenses this file
12'  to you under the Apache License, Version 2.0 (the
13'  "License"); you may not use this file except in compliance
14'  with the License.  You may obtain a copy of the License at
15'
16'    http://www.apache.org/licenses/LICENSE-2.0
17'
18'  Unless required by applicable law or agreed to in writing,
19'  software distributed under the License is distributed on an
20'  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21'  KIND, either express or implied.  See the License for the
22'  specific language governing permissions and limitations
23'  under the License.
24'
25'*************************************************************************
26
27
28
29
30
31' Be sure that all variables are dimensioned:
32option explicit
33
34
35
36Sub RunTest()
37
38'*************************************************************************
39' INTERFACE:
40' com.sun.star.io.XMarkableStream
41'*************************************************************************
42On Error Goto ErrHndl
43    Dim bOK As Boolean
44    Dim Bytes(20) As Integer
45    Dim rBytes(0)
46    Dim lastRByte As Integer
47    Dim i As Integer
48    Dim oOutStream As Object
49    Dim oInStream As Object
50    Dim iMark1 As Integer
51    Dim iMark2 As Integer
52    Dim iByte As Integer
53    Dim iBytes As Integer
54    Dim iByteAfterMark As Integer
55    Dim iByteAfterMark1 As Integer
56    Dim iByteAfterMark2 As Integer
57    Dim iOffset As Integer
58    Dim sFileStr As String
59
60    if (cObjectName = "stm.MarkableInputStream" OR cObjectName = "stm.ObjectInputStream") then
61
62        for i = 0 to ubound(Bytes())
63            Bytes(i) = i * 2
64        next i
65
66        Out.Log("First reset streams and write some bytes...")
67        oOutStream = getOutStream()
68        oOutStream.writeBytes(Bytes())
69        ResetStreams()
70
71        Test.StartMethod("createMark()")
72        bOK = true
73        Out.Log("Skip 3 bytes.")
74        oObj.skipBytes(3)
75        iMark1 = oObj.createMark()
76        Out.Log("Mark" + iMark1 + " was created.")
77
78        oObj.readBytes(rBytes(), 1)
79        iByteAfterMark1 = rBytes(0)
80        Out.Log("Byte after Mark" + iMark1 + " is " + int(iByteAfterMark1))
81
82        Out.Log("Skip 5 bytes.")
83        oObj.skipBytes(5)
84        iMark2 = oObj.createMark()
85        Out.Log("Mark" + iMark2 + " was created.")
86
87        oObj.readBytes(rBytes(), 1)
88        iByteAfterMark2 = rBytes(0)
89        Out.Log("Byte after Mark" + iMark2 + " is " + int(iByteAfterMark2))
90
91        Out.Log("Skip 7 bytes.")
92        oObj.skipBytes(7)
93        oObj.readBytes(rBytes(), 1)
94        lastRByte = rBytes(0)
95
96        Out.Log("Jump to Mark" + iMark2)
97        oObj.jumpToMark(iMark2)
98        oObj.readBytes(rBytes(), 1)
99        iByteAfterMark = rBytes(0)
100        Out.Log("Byte after Mark" + iMark2 + " is " + int(iByteAfterMark) + ", expected " + int(iByteAfterMark2))
101        bOK = bOK AND iByteAfterMark = iByteAfterMark2
102
103        Out.Log("Jump to Mark" + iMark1)
104        oObj.jumpToMark(iMark1)
105        oObj.readBytes(rBytes(), 1)
106        iByteAfterMark = rBytes(0)
107        Out.Log("Byte after Mark" + iMark1 + " is " + int(iByteAfterMark) + ", expected " + int(iByteAfterMark1))
108        bOK = bOK AND iByteAfterMark = iByteAfterMark1
109
110        Test.MethodTested("createMark()", bOK)
111        Test.MethodTested("jumpToMark()", bOK)
112
113        Test.StartMethod("offsetToMark()")
114        bOK = true
115        iOffset = oObj.offsetToMark(iMark2)
116        Out.Log("Offset from current position to Mark" + iMark2 + " is " + iOffset)
117        bOK = bOK AND iOffset = -5
118        Test.MethodTested("offsetToMark()", bOK)
119
120        Test.StartMethod("deleteMark()")
121        bOK = true
122        Out.Log("Delete Mark" + iMark1)
123        oObj.deleteMark(iMark1)
124        On Error goto ErrHndl1
125            Out.Log("Trying to jump to deleted mark")
126            oObj.jumpToMark(iMark1)
127            Out.Log("No exception occurred. FAILED")
128            bOK = false
129            goto Cont1
130        ErrHndl1:
131            Out.Log("Expected exception: " + error)
132        Cont1:
133        Test.MethodTested("deleteMark()", bOK)
134
135        Test.StartMethod("jumpToFurthest()")
136        bOK = true
137        oObj.readBytes(rBytes(), 1)
138        iByte = rBytes(0)
139        Out.Log("Perform a reading operation from the current position. Byte " + int(iByte) + " was read.")
140        Out.Log("Changing position.")
141        oObj.jumpToMark(iMark2)
142        Out.Log("Changing position with jumpToFurthest()")
143        oObj.jumpToFurthest()
144        oObj.readBytes(rBytes(), 1)
145        Out.Log("From the current position byte " + int(rBytes(0)) + " was read. Expected byte is " + int(lastRByte) + 2)
146        bOK = bOK AND lastRByte + 2 = rBytes(0)
147        Test.MethodTested("jumpToFurthest()", bOK)
148    else
149        bOK = true
150        Out.Log("Write 3 bytes to stream")
151        ReDim Bytes(2) As Integer
152        for i = 0 to ubound(Bytes())
153            Bytes(i) = i
154        next i
155        oObj.writeBytes(Bytes())
156        Out.Log("Creating a Mark.")
157        iMark1 = oObj.createMark()
158        Out.Log("Write 4 bytes to stream")
159        ReDim Bytes(3) As Integer
160        for i = 0 to ubound(Bytes())
161            Bytes(i) = i + 3
162        next i
163        oObj.writeBytes(Bytes())
164        Out.Log("Creating a Mark.")
165        iMark2 = oObj.createMark()
166
167        iOffset = oObj.offsetToMark(iMark1)
168        Out.Log("Offset from current position to Mark" + iMark1 + " is " + iOffset)
169        bOK = bOK AND iOffset = 4
170        Test.MethodTested("offsetToMark()", bOK)
171
172        Out.Log("Write 5 bytes to stream")
173        ReDim Bytes(4) As Integer
174        for i = 0 to ubound(Bytes())
175            Bytes(i) = i + 7
176        next i
177        oObj.writeBytes(Bytes())
178
179        Out.Log("Testing jumpToMark()")
180        Out.Log("Testing deleteMark()")
181        bOK = true
182        Out.Log("Deleting Mark1")
183        oObj.deleteMark(iMark2)
184        On Error goto ErrHndl2
185            Out.Log("Trying to jump to Mark1")
186            oObj.jumpToMark(iMark2)
187            Out.Log("No exception occurred - FAILED")
188            bOK = false
189            goto Cont2
190        ErrHndl2:
191            Out.Log("Expected exception: " + error)
192        Cont2:
193
194        Test.MethodTested("deleteMark()", bOK)
195
196        bOK = true
197
198        Out.Log("Jump to Mark0")
199        oObj.jumpToMark(iMark1)
200        Test.MethodTested("jumpToMark()", bOK)
201        Test.MethodTested("createMark()", bOK)
202
203        bOK = true
204        Out.Log("Write 2 bytes to stream")
205        ReDim Bytes(1) As Integer
206
207        for i = 0 to ubound(Bytes())
208            Bytes(i) = i + 12
209        next i
210
211        oObj.writeBytes(Bytes())
212        Out.Log("Changing position")
213        oObj.jumpToMark(iMark1)
214        Out.Log("Changing position with jumpToFurthest()")
215        oObj.jumpToFurthest()
216
217        Out.Log("Write 2 bytes to stream")
218        ReDim Bytes(1) As Integer
219        for i = 0 to ubound(Bytes())
220            Bytes(i) = i + 14
221        next i
222        oObj.writeBytes(Bytes())
223
224        Out.Log("Comparing file with expected {0, 1, 2, 12, 13, 5, 6, 7, 8, 9, 10, 11, 14, 15}")
225        oInStream = getInStream()
226        iBytes = oInStream.readBytes(rBytes(), 20)
227        Out.Log("There are " + iBytes + " in stream:")
228        sFileStr = "" + int(rBytes(0))
229        for i = 1 to ubound(rBytes())
230            sFileStr = sFileStr + ", " + int(rBytes(i))
231        next i
232        Out.Log("They are {" + sFileStr + "}")
233        bOK = bOK AND sFileStr = "0, 1, 2, 12, 13, 5, 6, 7, 8, 9, 10, 11, 14, 15"
234
235        Test.MethodTested("jumpToFurthest()", bOK)
236    end if
237
238    ResetStreams()
239    DisposeObj()
240    CreateObj()
241
242Exit Sub
243ErrHndl:
244    Test.Exception()
245    bOK = false
246    resume next
247End Sub
248</script:module>
249