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="connection_XAcceptor" 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.connection.XAcceptor
41'*************************************************************************
42On Error Goto ErrHndl
43
44    Dim bOK As Boolean
45    Dim oConnector As Object
46    Dim xConnection As Object
47    Dim sConnectionString As String
48    Dim args(1) As Variant
49
50    bOK = true
51
52    Test.startMethod("accept()")
53
54    oConnector = createUnoService("basichelper.Connector")
55
56    Dim iPos1 As Integer, iPos2 As Integer
57
58    iPos1 = inStr(1, CNCSTR, "socket")
59    iPos2 = inStr(iPos1, CNCSTR, "port=")
60
61    sConnectionString = mid(CNCSTR, iPos1, iPos2 - iPos1 + 5) &amp; "18888"
62
63    Out.Log("Connection string is '" &amp; sConnectionString &amp; "'")
64
65    args(0) = createUnoService("com.sun.star.connection.Connector")
66    args(1) = sConnectionString
67
68    ' When call initialize() method, a new thread is created and tries to
69    ' connect to Acceptor after some seconds.
70
71    oConnector.initialize(args())
72
73    ' Starting acception.
74    xConnection = oObj.accept(sConnectionString)
75    wait(500) 'Just to be sure that component had a chance to change it's state.
76
77    bOK = bOK AND hasUNOInterfaces(xConnection, "com.sun.star.connection.XConnection")
78    Out.Log("Connector's state is '" &amp; oConnector.getByName("State") &amp; "'")
79    bOK = bOK AND oConnector.getByName("State") = "connected" ' This means that connection was established.
80
81    if (bOK) then
82        Out.Log("Connection established! Trying to get data from stream.")
83
84        Dim aStr As String
85        Dim aByte As Integer
86        Dim readData() As Variant
87        aStr = ""
88        aByte = 1
89        while (aByte &lt;&gt; 0)
90            ReDim readData() As Variant
91            xConnection.read(readData, 1)
92            aByte = readData(0)
93            if (aByte &lt;&gt; 0) then
94                    aStr = aStr &amp; chr(aByte)
95            end if
96        wend
97
98        Out.Log("Returned data from stream is " &amp; aStr)
99        bOK = bOK AND aStr = sConnectionString
100    end if
101
102    Test.MethodTested("accept()", bOK)
103
104    Test.startMethod("stopAccepting()")
105    bOK = true
106    ' Now stopping acception and trying to connect again
107    ' (we can do this, if connection string is the same.)
108    oObj.stopAccepting()
109
110    Out.Log("Trying to connect after stopping accepting...")
111    oConnector.initialize(args())
112    Out.Log("Connector's state is '" &amp; oConnector.getByName("State") &amp; "'")
113
114    ' Now oConnector shouldn't connect. So, check it's state
115    ' after few seconds (= Connection's Timeout + 3 sec)
116    wait(3000 + oConnector.getByName("Timeout"))
117    Out.Log("Connector's state is '" &amp; oConnector.getByName("State") &amp; "'")
118    bOK = bOK AND oConnector.getByName("State") = "NoConnectException"
119
120    Test.MethodTested("stopAccepting()", bOK)
121
122Exit Sub
123ErrHndl:
124    Test.Exception()
125    resume next
126End Sub
127</script:module>
128