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="ucb_XSimpleFileAccess" 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.ucb.XSimpleFileAccess
41'*************************************************************************
42On Error Goto ErrHndl
43    Dim bOK As Boolean
44    Dim testDir As String
45    Dim cSubFolder As String
46    Dim cFile1 As String
47    Dim cFile2 As String
48    Dim cFile3 As String
49    Dim oFC As Variant
50    Dim oDT As Object
51    Dim oOS As Object
52    Dim oIS As Object
53    Dim cFileName As String
54    Dim cCrDate As String
55    Dim cType As String
56    Dim i As Integer
57    Dim k As Integer
58    Dim oIH As Object
59
60    Dim Bytes(5) As Integer
61    for i = 0 to 5
62        Bytes(i) = i
63    next i
64
65    testDir = utils.Path2URL(cTestDocsDir + "XSimpleFileAccessBASIC")
66    Out.Log("Test directory is '" + testDir + "'")
67    cSubFolder = utils.getTempFileURL( + "/SubFolder")
68    cFile1 = utils.Path2URL(testDir + "/File1.txt")
69    cFile2 = utils.Path2URL(testDir + "/File2.txt")
70    cFile3 = utils.Path2URL(cSubFolder + "/File3.txt")
71    if FileExists(cFile3) then kill(cFile3)
72    if (FileExists(cSubFolder)) then rmDir(cSubFolder)
73
74    Out.Log("Test that files are in place...")
75    bOK = true
76    bOK = bOK AND FileExists(cFile1)
77    bOK = bOK AND FileExists(cFile2)
78    if (NOT bOK) then
79        Out.Log("Can't find '" + cFile1 + "' and/or '" + cFile2 + "'")
80        Exit Sub
81    end if
82
83    Test.StartMethod("createFolder()")
84    bOK = true
85    Out.Log("Creating subfolder '" + cSubFolder + "'")
86    oObj.createFolder(cSubFolder)
87    bOK = bOK AND FileExists(cSubFolder)
88    Test.MethodTested("createFolder()", bOK)
89
90    Test.StartMethod("isFolder()")
91    bOK = true
92    bOK = bOK AND oObj.isFolder(cSubFolder)
93    bOK = bOK AND NOT oObj.isFolder(cFile1)
94    Test.MethodTested("isFolder()", bOK)
95
96    Test.StartMethod("getFolderContents()")
97    bOK = true
98    Out.Log("Getting content of folder '" + utils.Path2URL(cTestDocsDir) + "'")
99    oFC = oObj.getFolderContents(utils.Path2URL(cTestDocsDir), False)
100    ' Getting files amount (without folders)
101    cFileName = Dir(utils.Path2URL(cTestDocsDir))
102    i = 0
103    Out.Log("File list :")
104    while (cFileName &lt;&gt; "")
105        Out.Log("   " + cFileName)
106        i = i + 1
107        cFileName = Dir()
108    wend
109
110    bOK = bOK AND ubound(oFC) = i - 1
111    if (bOK) then
112        for k = 0 to i - 1
113            bOK = bOK AND FileExists(oFC(k))
114        next k
115    else
116        Out.Log("Amount of files in list is wrong: " + (ubound(oFC) + 1) + "," + i)
117
118    end if
119
120    Test.MethodTested("getFolderContents()", bOK)
121
122    Test.StartMethod("move()")
123    bOK = true
124    oObj.move(cFile2, cFile3)
125    bOK = bOK AND FileExists(cFile3) AND NOT FileExists(cFile2)
126    Test.MethodTested("move()", bOK)
127
128    Test.StartMethod("copy()")
129    bOK = true
130    oObj.copy(cFile3, cFile2)
131    bOK = bOK AND FileExists(cFile3) AND FileExists(cFile2)
132    Test.MethodTested("copy()", bOK)
133
134    Test.StartMethod("openFileWrite()")
135    bOK = true
136
137    Test.StartMethod("kill()")
138    bOK = true
139    oObj.kill(cFile3)
140    bOK = bOK AND NOT FileExists(cFile3)
141    Test.MethodTested("kill()", bOK)
142
143    Test.StartMethod("exists()")
144    bOK = true
145    bOK = bOK AND oObj.exists(cFile1) = FileExists(cFile1) AND oObj.exists(cFile3) = FileExists(cFile3)
146    Test.MethodTested("exists()", bOK)
147
148    Out.Log("creating a new file '" + cFile3 + "'")
149    oOS = oObj.openFileWrite(cFile3)
150    bOK = bOK AND NOT isNULL(oOS)
151    bOK = bOK AND FileExists(cFile3)
152    if (bOK) then
153        oOS.writeBytes(Bytes())
154        oOS.closeOutput()
155    end if
156    Test.MethodTested("openFileWrite()", bOK)
157
158    Test.StartMethod("getSize()")
159    bOK = true
160    Out.Log("Actual: " + oObj.getSize(cFile3) + " Expected: " + (ubound(Bytes()) + 1))
161    bOK = bOK AND oObj.getSize(cFile3) = (ubound(Bytes()) + 1)
162    Test.MethodTested("getSize()", bOK)
163
164    Test.StartMethod("setReadOnly()")
165    Test.StartMethod("isReadOnly()")
166    bOK = true
167    oObj.setReadOnly(cSubFolder, true)
168    bOK = bOK AND oObj.isReadOnly(cSubFolder)
169    oObj.setReadOnly(cSubFolder, false)
170    bOK = bOK AND NOT oObj.isReadOnly(cSubFolder)
171    Test.MethodTested("isReadOnly()", bOK)
172    Test.MethodTested("setReadOnly()", bOK)
173
174    Test.StartMethod("getContentType()")
175    bOK = true
176    cType = oObj.getContentType(cFile3)
177    Out.Log("Content Type is '" + cType + "'")
178    Test.MethodTested("getContentType()", bOK)
179
180    Test.StartMethod("getDateTimeModified()")
181    bOK = true
182    oDT = oObj.getDateTimeModified(cFile3)
183    cCrDate = Date()
184
185
186    bOK = bOK AND Day(cCrDate) = oDT.Day
187    bOK = bOK AND Month(cCrDate) = oDT.Month
188    bOK = bOK AND Year(cCrDate) = oDT.Year
189
190    if (NOT bOK) then
191        Out.Log("FileDateTime returned '" + cCrDate + "'")
192        Out.Log("getDateTimeModified returned '" + oDT.Day + "/" _
193                                                + oDT.Month + "/" _
194                                                + oDT.Year + " " _
195                                                + oDT.Hours + ":" _
196                                                + oDT.Minutes + ":" _
197                                                + oDT.Seconds + "'")
198    end if
199    Test.MethodTested("getDateTimeModified()", bOK)
200
201    Test.StartMethod("openFileRead()")
202    bOK = true
203    oIS = oObj.openFileRead(cFile3)
204    bOK = bOK AND NOT isNULL(oIS)
205    Dim rData(10) As Integer
206    Dim nRb As Integer
207    nRb = oIS.readBytes(rData(), 100)
208    bOK = bOK AND nRb = ubound(Bytes()) + 1
209    if (NOT bOK) then
210        Out.Log("Amount of read files is wrong")
211    else
212        for i = 0 to nRb - 1
213            bOK = bOK AND Bytes(i) = rData(i)
214        next i
215    end if
216    oIS.closeInput()
217    Test.MethodTested("openFileRead()", bOK)
218
219    Test.StartMethod("openFileReadWrite()")
220    bOK = true
221    oIS = oObj.openFileReadWrite(cFile3)
222    bOK = bOK AND hasUnoInterfaces(oIS, "com.sun.star.io.XStream")
223    Test.MethodTested("openFileReadWrite()", bOK)
224
225    Test.StartMethod("setInteractionHandler()")
226    bOK = true
227    oIH = createUNOInterface("com.sun.star.sdb.InteractionHandler")
228    oObj.setInteractionHandler(oIH)
229    Test.MethodTested("setInteractionHandler()", bOK)
230
231Exit Sub
232ErrHndl:
233    Test.Exception()
234    bOK = false
235    resume next
236End Sub
237</script:module>
238