View Javadoc

1   // HylaFAXClientTest.java - gnu.hylafax test application for the client
2   // $Id: HylaFAXClientTest.java,v 1.3 2007/05/07 18:26:53 sjardine Exp $
3   //
4   // Copyright 1999, 2000 Joe Phillips <jaiger@net-foundry.com>
5   // Copyright 2001, Innovation Software Group, LLC - http://www.innovationsw.com
6   // Copyright 2006, John Yeary <jyeary@javanetwork.net>
7   //
8   // For information on the HylaFAX FAX server see
9   // http://www.hylafax.org/
10  //
11  // This library is free software; you can redistribute it and/or
12  // modify it under the terms of the GNU Library General Public
13  // License as published by the Free Software Foundation; either
14  // version 2 of the License, or (at your option) any later version.
15  //
16  // This library is distributed in the hope that it will be useful,
17  // but WITHOUT ANY WARRANTY; without even the implied warranty of
18  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  // Library General Public License for more details.
20  //
21  // You should have received a copy of the GNU Library General Public
22  // License along with this library; if not, write to the Free
23  // Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  //
25  
26  package gnu.hylafax.util;
27  
28  import gnu.hylafax.Client;
29  import gnu.hylafax.HylaFAXClient;
30  
31  import java.io.FileInputStream;
32  import java.io.FileNotFoundException;
33  import java.io.FileOutputStream;
34  import java.io.RandomAccessFile;
35  import java.util.Vector;
36  
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  
40  /***
41   * @author John Yeary <jyeary@javanetwork.net>
42   * @version 1.0
43   * 
44   * <p>
45   * This class was created to move the test out of the HylaFAXClient class.
46   * </p>
47   * <p>
48   * TODO A better test framework needs to be put into place.
49   * </p>
50   */
51  public class HylaFAXClientTest extends HylaFAXClient {
52  
53      private final static Log log = LogFactory.getLog(Client.class);
54  
55      /*** Creates a new instance of HylaFAXClientTest */
56      public HylaFAXClientTest() {
57  	// Do nothing.
58      }
59  
60      /***
61       * Run some basic tests.
62       * 
63       * @param Arguments
64       *                an array of command-line-argument Strings
65       */
66      public static void main(String Arguments[]) {
67  	HylaFAXClient c = new HylaFAXClient();
68  
69  	try {
70  	    c.open("localhost");
71  	    c.noop();
72  	    c.setPassive(true); // use passive transfers
73  	    c.user("fax");
74  
75  	    /*
76  	     * Using this type of file allows us to visually verify that the
77  	     * files have not changed after being transferred back and forth.
78  	     */
79  	    c.type(TYPE_IMAGE); // should cause files to remain same size after
80  
81  	    System.out.println("current directory is: " + c.pwd());
82  
83  	    c.cwd("docq");
84  	    // c.cwd("bad-directory-name");
85  	    System.out.println("current directory is: " + c.pwd());
86  
87  	    c.cdup();
88  	    System.out.println("current directory is: " + c.pwd());
89  
90  	    // c.admin("MyPassword");
91  	    System.out.println("idle timer set to " + c.idle() + " seconds.");
92  	    c.idle(1800);
93  	    System.out.println("idle timer set to " + c.idle() + " seconds.");
94  
95  	    System.out.println("job format: " + c.jobfmt());
96  	    c.jobfmt("%-4j");
97  	    System.out.println("job format: " + c.jobfmt());
98  
99  	    // set file structure
100 	    c.stru(STRU_FILE);
101 	    // c.stru(STRU_RECORD);
102 	    c.stru(STRU_TIFF);
103 	    // c.stru(STRU_PAGE);
104 	    c.stru(STRU_FILE);
105 
106 	    // send temp file (stot)
107 	    {
108 		String filename = "test.ps";
109 		FileInputStream file = new FileInputStream(filename);
110 
111 		String f = c.putTemporary(file);
112 		System.out.println("filename= " + f);
113 
114 		// test size command
115 		long local_size, remote_size;
116 		local_size = (new RandomAccessFile(filename, "r").length());
117 		remote_size = c.size(f);
118 		System.out.println(filename + " local size is " + local_size);
119 		System.out.println(f + " remote size is " + remote_size);
120 
121 		// retrieve the temp file now
122 		FileOutputStream out_file = new FileOutputStream(filename
123 			+ ".retr");
124 		c.get(f, out_file);
125 		local_size = (new RandomAccessFile(filename + ".retr", "r")
126 			.length());
127 		System.out.println(filename + ".retr size is " + local_size);
128 
129 		// retrieve the temp file now (using ZLIB mode)
130 		FileOutputStream zip_file = new FileOutputStream(filename
131 			+ ".gz");
132 		c.mode(MODE_ZLIB);
133 		c.get(f, zip_file);
134 		local_size = (new RandomAccessFile(filename + ".gz", "r")
135 			.length());
136 		System.out.println(filename + ".gz size is " + local_size);
137 		c.mode(MODE_STREAM);
138 
139 	    }
140 	    // end stot/retr test
141 
142 	    // test list command
143 	    {
144 		Vector files;
145 		int counter;
146 
147 		// list current directory
148 		files = c.getList();
149 		for (counter = 0; counter < files.size(); counter++) {
150 		    System.out.println((String) files.elementAt(counter));
151 		}
152 
153 		// list /tmp directory
154 		files = c.getList("/tmp");
155 		for (counter = 0; counter < files.size(); counter++) {
156 		    System.out.println((String) files.elementAt(counter));
157 		}
158 
159 		// list /tmp directory (with mode ZLIB)
160 		c.mode(MODE_ZLIB);
161 		files = c.getList("/tmp");
162 		for (counter = 0; counter < files.size(); counter++) {
163 		    System.out.println((String) files.elementAt(counter));
164 		}
165 		c.mode(MODE_STREAM);
166 
167 		try {
168 		    // attempt to list file that doesn't exist
169 		    c.getList("/joey-joe-joe-jr.shabba-do"); // that's the
170 								// worst name
171 								// I've ever
172 								// heard.
173 		    System.out.println("ERROR: file not found was expected");
174 		} catch (FileNotFoundException fnfe) {
175 		    // expected this, continue
176 		    System.out.println("GOOD: file not found, as expected");
177 		}
178 
179 		// list current directory, should be the same as above
180 		files = c.getList();
181 		for (counter = 0; counter < files.size(); counter++) {
182 		    System.out.println((String) files.elementAt(counter));
183 		}
184 	    }
185 	    // end list test
186 
187 	    // test nlst command
188 	    {
189 		Vector files;
190 		int counter;
191 
192 		// list /tmp directory
193 		files = c.getNameList("/tmp");
194 
195 		for (counter = 0; counter < files.size(); counter++) {
196 		    System.out.println((String) files.elementAt(counter));
197 		}
198 
199 		// list /tmp directory
200 		files = c.getNameList("/tmp");
201 
202 		for (counter = 0; counter < files.size(); counter++) {
203 		    System.out.println((String) files.elementAt(counter));
204 		}
205 
206 		// list /tmp directory (using mode ZLIB)
207 		c.mode(MODE_ZLIB);
208 		files = c.getNameList("/tmp");
209 
210 		for (counter = 0; counter < files.size(); counter++) {
211 		    System.out.println((String) files.elementAt(counter));
212 		}
213 
214 		c.mode(MODE_STREAM);
215 
216 		// list current directory
217 		files = c.getNameList();
218 
219 		for (counter = 0; counter < files.size(); counter++) {
220 		    System.out.println((String) files.elementAt(counter));
221 		}
222 
223 	    }
224 	    // end nlst test
225 
226 	    // get system type string
227 	    String system = c.syst();
228 	    System.out.println("system type: " + system + ".");
229 
230 	    c.noop();
231 
232 	    // stat tests
233 	    {
234 		// test normal server status message
235 		Vector status = c.stat();
236 		int counter;
237 		for (counter = 0; counter < status.size(); counter++) {
238 		    System.out.println(status.elementAt(counter));
239 		}
240 
241 		// test directory status
242 		status = c.stat("docq");
243 		for (counter = 0; counter < status.size(); counter++) {
244 		    System.out.println(status.elementAt(counter));
245 		}
246 
247 		// test non-existing directory status
248 		try {
249 		    status = c.stat("joey-joe-joe-junior-shabba-do");
250 		    for (counter = 0; counter < status.size(); counter++) {
251 			System.out.println(status.elementAt(counter));
252 		    }
253 		} catch (FileNotFoundException fnfe) {
254 		    System.out
255 			    .println("GOOD: file not found.  this is what we expected");
256 		}
257 	    }
258 
259 	    c.noop();
260 
261 	    c.quit();
262 
263 	} catch (Exception e) {
264 	    log.error(e.getMessage(), e);
265 	}
266 
267 	System.out.println("main: end");
268     }
269 
270 }