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