1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir package ifc.io; 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir import lib.MultiMethodTest; 31*cdf0e10cSrcweir import lib.Status; 32*cdf0e10cSrcweir import lib.StatusException; 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir import com.sun.star.io.XActiveDataControl; 35*cdf0e10cSrcweir import com.sun.star.io.XStreamListener; 36*cdf0e10cSrcweir import com.sun.star.lang.EventObject; 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir /** 39*cdf0e10cSrcweir * Testing <code>com.sun.star.io.XActiveDataControl</code> 40*cdf0e10cSrcweir * interface methods : 41*cdf0e10cSrcweir * <ul> 42*cdf0e10cSrcweir * <li><code> addListener()</code></li> 43*cdf0e10cSrcweir * <li><code> removeListener()</code></li> 44*cdf0e10cSrcweir * <li><code> start()</code></li> 45*cdf0e10cSrcweir * <li><code> terminate()</code></li> 46*cdf0e10cSrcweir * </ul> <p> 47*cdf0e10cSrcweir * 48*cdf0e10cSrcweir * Tests <code>XActiveDataControl</code> interface. First, it registers a listener 49*cdf0e10cSrcweir * and performs <code>start()</code> and <code>terminate()</code> calls. The 50*cdf0e10cSrcweir * events received in the listener are analyzed to verify the result.<p> 51*cdf0e10cSrcweir * 52*cdf0e10cSrcweir * @see com.sun.star.io.XActiveDataControl 53*cdf0e10cSrcweir */ 54*cdf0e10cSrcweir public class _XActiveDataControl extends MultiMethodTest { 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir /** 57*cdf0e10cSrcweir * Contains the object under test. 58*cdf0e10cSrcweir */ 59*cdf0e10cSrcweir public XActiveDataControl oObj = null; 60*cdf0e10cSrcweir 61*cdf0e10cSrcweir /** 62*cdf0e10cSrcweir * Indicates that the <code>XStreamListener.started()</code> method has 63*cdf0e10cSrcweir * been called. 64*cdf0e10cSrcweir */ 65*cdf0e10cSrcweir private boolean startCalled = false; 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir /** 68*cdf0e10cSrcweir * Indicates that the <code>XStreamListener.terminated()</code> method has 69*cdf0e10cSrcweir * been called. 70*cdf0e10cSrcweir */ 71*cdf0e10cSrcweir private boolean terminateCalled = false; 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir /** 74*cdf0e10cSrcweir * Indicates that the <code>XEventListener.closed()</code> method has 75*cdf0e10cSrcweir * been called. 76*cdf0e10cSrcweir */ 77*cdf0e10cSrcweir private boolean closeCalled = false; 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir /** 80*cdf0e10cSrcweir * Indicates that the <code>XStreamListener.error()</code> method has 81*cdf0e10cSrcweir * been called. 82*cdf0e10cSrcweir */ 83*cdf0e10cSrcweir private boolean errorCalled = false; 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir /** 86*cdf0e10cSrcweir * Contains the error, if <code>XStreamListener.error(Object error)</code> 87*cdf0e10cSrcweir * method was called. 88*cdf0e10cSrcweir */ 89*cdf0e10cSrcweir private Object error; 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir /** 92*cdf0e10cSrcweir * Indicates that the <code>XEventListener.disposing()</code> method has 93*cdf0e10cSrcweir * been called. 94*cdf0e10cSrcweir */ 95*cdf0e10cSrcweir private boolean smthngElseCalled = false; 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir /** 98*cdf0e10cSrcweir * The listener is used to verify results of the methods. 99*cdf0e10cSrcweir */ 100*cdf0e10cSrcweir private TestStreamListener listener = new TestStreamListener(); 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir /** 103*cdf0e10cSrcweir * XStreamListener implementation. Sets variables 104*cdf0e10cSrcweir * (<cod>estartedCalled</code>, <code>terminatedCalled</code>, etc.) to 105*cdf0e10cSrcweir * <tt>true</tt> if the appropriate method was called (for example, if 106*cdf0e10cSrcweir * <code>started()</code> was called, the <code>startedCalled</code> 107*cdf0e10cSrcweir * field is set). 108*cdf0e10cSrcweir */ 109*cdf0e10cSrcweir private class TestStreamListener implements XStreamListener { 110*cdf0e10cSrcweir public void started() { 111*cdf0e10cSrcweir startCalled = true ; 112*cdf0e10cSrcweir } 113*cdf0e10cSrcweir public void terminated() { 114*cdf0e10cSrcweir terminateCalled = true ; 115*cdf0e10cSrcweir } 116*cdf0e10cSrcweir public void error(Object e) { 117*cdf0e10cSrcweir error = e; 118*cdf0e10cSrcweir errorCalled = true ; 119*cdf0e10cSrcweir } 120*cdf0e10cSrcweir public void closed() { 121*cdf0e10cSrcweir closeCalled = true ; 122*cdf0e10cSrcweir } 123*cdf0e10cSrcweir public void disposing(EventObject e) { 124*cdf0e10cSrcweir smthngElseCalled = true ; 125*cdf0e10cSrcweir } 126*cdf0e10cSrcweir 127*cdf0e10cSrcweir } 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir /** 130*cdf0e10cSrcweir * Tests <code>addListener()</code>. The verification is performed later, in 131*cdf0e10cSrcweir * <code>_terminate()</code> method. 132*cdf0e10cSrcweir */ 133*cdf0e10cSrcweir public void _addListener() { 134*cdf0e10cSrcweir oObj.addListener(listener); 135*cdf0e10cSrcweir } 136*cdf0e10cSrcweir 137*cdf0e10cSrcweir /** 138*cdf0e10cSrcweir * Starts the data activity (e.g. data pump). Verifictation is performed 139*cdf0e10cSrcweir * later, in <code>_terminate()</code> method. 140*cdf0e10cSrcweir */ 141*cdf0e10cSrcweir public void _start() { 142*cdf0e10cSrcweir executeMethod("addListener()"); 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir oObj.start(); 145*cdf0e10cSrcweir 146*cdf0e10cSrcweir // waiting a little bit for data transfered 147*cdf0e10cSrcweir try { 148*cdf0e10cSrcweir Thread.sleep(200); 149*cdf0e10cSrcweir } catch (InterruptedException e) { 150*cdf0e10cSrcweir e.printStackTrace(log) ; 151*cdf0e10cSrcweir throw new StatusException(Status.failed(e.getMessage())); 152*cdf0e10cSrcweir } 153*cdf0e10cSrcweir } 154*cdf0e10cSrcweir 155*cdf0e10cSrcweir /** 156*cdf0e10cSrcweir * Tests <code>removeListener()</code>. Before, it ensures that other 157*cdf0e10cSrcweir * tests are perforemed and that <code>addListener()</code> is okay. Then, 158*cdf0e10cSrcweir * calls <code>XActiveDataControl.start()</code> and checks that no method 159*cdf0e10cSrcweir * of the listener was called. 160*cdf0e10cSrcweir */ 161*cdf0e10cSrcweir public void _removeListener() { 162*cdf0e10cSrcweir // performing other tests before, so, that don't break them 163*cdf0e10cSrcweir try { 164*cdf0e10cSrcweir executeMethod("terminate()"); 165*cdf0e10cSrcweir } catch (StatusException e) { 166*cdf0e10cSrcweir // the result doesn't matter 167*cdf0e10cSrcweir } 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir // check that addListener() is okay 170*cdf0e10cSrcweir requiredMethod("addListener()"); 171*cdf0e10cSrcweir 172*cdf0e10cSrcweir // clearing previous records 173*cdf0e10cSrcweir startCalled = false; 174*cdf0e10cSrcweir terminateCalled = false; 175*cdf0e10cSrcweir errorCalled = false; 176*cdf0e10cSrcweir error = null; 177*cdf0e10cSrcweir smthngElseCalled = false; 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir // removing the listener 180*cdf0e10cSrcweir oObj.removeListener(listener); 181*cdf0e10cSrcweir 182*cdf0e10cSrcweir // starting the activity 183*cdf0e10cSrcweir oObj.start(); 184*cdf0e10cSrcweir 185*cdf0e10cSrcweir // wait a little bit to allow for listeners to be called 186*cdf0e10cSrcweir try { 187*cdf0e10cSrcweir Thread.sleep(200); 188*cdf0e10cSrcweir } catch (InterruptedException e) { 189*cdf0e10cSrcweir e.printStackTrace(log) ; 190*cdf0e10cSrcweir throw new StatusException(Status.failed(e.getMessage())); 191*cdf0e10cSrcweir } 192*cdf0e10cSrcweir 193*cdf0e10cSrcweir // check that no removed listener's method was called 194*cdf0e10cSrcweir tRes.tested("removeListener()",!startCalled && 195*cdf0e10cSrcweir !terminateCalled && !errorCalled && !smthngElseCalled) ; 196*cdf0e10cSrcweir } 197*cdf0e10cSrcweir 198*cdf0e10cSrcweir /** 199*cdf0e10cSrcweir * Tests <code>terminate()</code>. First, ensures that <code>start()</code> 200*cdf0e10cSrcweir * has been called. Then, verifies <code>start()</code>, 201*cdf0e10cSrcweir * <code>addListener()</code> and <code>terminate()</code> results, by 202*cdf0e10cSrcweir * checking that the appropriate listener's methods have been called. 203*cdf0e10cSrcweir */ 204*cdf0e10cSrcweir public void _terminate() { 205*cdf0e10cSrcweir // ensuring that the activity has been started 206*cdf0e10cSrcweir executeMethod("start()"); 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir // terminating the activity 209*cdf0e10cSrcweir oObj.terminate(); 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir // waiting a little bit for listeners to be called 212*cdf0e10cSrcweir try { 213*cdf0e10cSrcweir Thread.sleep(200); 214*cdf0e10cSrcweir } catch (InterruptedException e) { 215*cdf0e10cSrcweir e.printStackTrace(log) ; 216*cdf0e10cSrcweir throw new StatusException(Status.failed(e.getMessage())); 217*cdf0e10cSrcweir } 218*cdf0e10cSrcweir 219*cdf0e10cSrcweir // check, if any error occured 220*cdf0e10cSrcweir if (errorCalled) { 221*cdf0e10cSrcweir Status.failed("Unexpected error"); 222*cdf0e10cSrcweir log.println("Unexpected error " + error); 223*cdf0e10cSrcweir ((Exception)error).printStackTrace(log); 224*cdf0e10cSrcweir } 225*cdf0e10cSrcweir 226*cdf0e10cSrcweir // verification of start() method - startedCalled method should be 227*cdf0e10cSrcweir // called 228*cdf0e10cSrcweir if (!tRes.tested("start()", startCalled)) { 229*cdf0e10cSrcweir log.println("XStreamListener.started() was not called()"); 230*cdf0e10cSrcweir } 231*cdf0e10cSrcweir 232*cdf0e10cSrcweir // check that any listener method is called 233*cdf0e10cSrcweir tRes.tested("addListener()", startCalled || 234*cdf0e10cSrcweir terminateCalled || errorCalled || smthngElseCalled); 235*cdf0e10cSrcweir 236*cdf0e10cSrcweir // checking that terminated() has been called or streams were closed 237*cdf0e10cSrcweir // before terminate() call, in this case termination has no sense. 238*cdf0e10cSrcweir tRes.tested("terminate()", terminateCalled || closeCalled); 239*cdf0e10cSrcweir } 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir /** 242*cdf0e10cSrcweir * Disposes the test environment, since it is used. 243*cdf0e10cSrcweir */ 244*cdf0e10cSrcweir public void after() { 245*cdf0e10cSrcweir this.disposeEnvironment(); 246*cdf0e10cSrcweir } 247*cdf0e10cSrcweir } 248*cdf0e10cSrcweir 249*cdf0e10cSrcweir 250