Source code: org/finj/test/ObserverTest.java
1 package org.finj.test;
2
3 import junit.framework.Test;
4 import junit.framework.TestSuite;
5
6 import org.finj.FTPClientAdapter;
7 import org.finj.FTPClientObserver;
8 import org.finj.FTPResponse;
9 import org.finj.test.FTPClientTest;
10
11 import org.finj.test.FinjTestCase;
12
13 /**
14 * Tests the features of the <code>org.finj.FTPClientObserver</code>.
15 *
16 *
17 *
18 *
19 * Copyright (C)
20 *
21 * This library is free software; you can redistribute it and/or
22 * modify it under the terms of the GNU Lesser General Public
23 * License as published by the Free Software Foundation; either
24 * version 2.1 of the License, or (at your option) any later version.
25 *
26 * This library is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 * Lesser General Public License for more details.
30 *
31 * You should have received a copy of the GNU Lesser General Public
32 * License along with this library; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 *
35 * @author Javier Iglesias -- jiglesias@users.sourceforge.net
36 * @version $Id: ObserverTest.java,v 1.3 2003/10/22 08:26:27 jiglesia Exp $
37 * @since v1.0.2
38 */
39 public class ObserverTest extends FinjTestCase {
40
41 /**
42 *
43 *
44 * @since v1.0.2
45 */
46 public ObserverTest ( String name ) {
47 super(name);
48 }
49
50 /**
51 *
52 *
53 * @since v1.0.2
54 */
55 protected void tearDown ( ) {
56 client = null;
57 }
58
59 /**
60 *
61 *
62 * @since v1.0.2
63 */
64 public static Test suite ( ) {
65 TestSuite suite = new TestSuite("tests concerning org.finj.Observers");
66 suite.addTestSuite(ObserverTest.class);
67 return suite;
68 }
69
70 /**
71 *
72 *
73 * @since v1.0.2
74 */
75 public void testNotification ( ) throws Exception {
76 NotifiedObserver observer = new NotifiedObserver();
77
78 // try adding observer
79 client.setObserver(observer);
80 assertTrue(observer.observNotif);
81
82 // open a connection to the server
83 client.open();
84 assertTrue(observer.openCtrlNotif);
85
86 // send a command
87 // FIXME : how to check for command sent AND response received ?
88 client.checkConnection();
89 assertTrue(observer.commandNotif);
90
91 // put a file
92
93
94 // get a file
95
96
97 // close connection
98 client.close();
99 assertTrue(observer.closedCtrlNotif);
100
101 // try removing observer
102 observer.observNotif = false;
103 client.removeObserver();
104 assertTrue(observer.observNotif);
105 }
106
107 /**
108 *
109 *
110 * @since v1.0.2
111 */
112 public void testManipulation ( ) {
113 // there should be no observer just after build up
114 assertTrue(!client.hasObserver());
115
116 // set an Observer
117 FTPClientObserver observer = new FTPClientAdapter();
118 client.setObserver(observer);
119
120 // now, there should be an observer
121 assertTrue(client.hasObserver());
122
123 // and it should be the one we've just created
124 assertTrue(client.getObserver() == observer);
125
126 // now, remove observer and check
127 client.removeObserver();
128 assertTrue(!client.hasObserver());
129 }
130
131 /**
132 *
133 *
134 * @since v1.0.2
135 */
136 private class NotifiedObserver extends Object implements FTPClientObserver {
137 public boolean observNotif = false;
138 public boolean openCtrlNotif = false;
139 public boolean closedCtrlNotif = false;
140 public boolean commandNotif = false;
141 public boolean responseNotif = false;
142 public boolean dataInNotif = false;
143 public boolean dataOutNotif = false;
144
145 public void isObserving ( boolean isIt ) {observNotif = true;}
146 public void controlConnectionOpened ( ) {openCtrlNotif = true;}
147 public void controlConnectionClosed ( ) {closedCtrlNotif = true;}
148 public void commandSent ( String command ) {commandNotif = true;}
149 public void responseReceived ( FTPResponse response ) {responseNotif = true;}
150 public void dataReceived ( int bytes ) {dataInNotif = true;}
151 public void dataSent ( int bytes ) {dataOutNotif = true;}
152 }
153
154
155
156 /**
157 *
158 *
159 * @since v1.0.2
160 */
161 public static void main ( String[] args ) {
162 new ObserverTest("").runTestInterface(args);
163 }
164 }