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