View Javadoc
1   package wjhk.jupload2.testhelpers;
2   
3   import java.awt.Cursor;
4   import java.awt.Frame;
5   import java.io.FileInputStream;
6   import java.io.IOException;
7   import java.io.InputStream;
8   import java.util.Properties;
9   import java.util.Vector;
10  
11  import javax.swing.JApplet;
12  
13  import org.apache.log4j.Logger;
14  
15  import wjhk.jupload2.context.JUploadContext;
16  import wjhk.jupload2.exception.JUploadException;
17  import wjhk.jupload2.gui.JUploadPanel;
18  import wjhk.jupload2.gui.JUploadTextArea;
19  import wjhk.jupload2.gui.filepanel.FilePanel.FileListViewMode;
20  import wjhk.jupload2.policies.UploadPolicy;
21  
22  /**
23   * This class is a used as a context, out of real JUpload application. It is used to build contexts for JUnit tests in
24   * the JUpload package.
25   * 
26   * @author etienne_sf
27   */
28  public class JUploadContextTestHelper implements JUploadContext {
29  
30      /** We mask real mime types here. */
31      public static String TEST_CASE_MIME_TYPE = "X-jupload/noFileTypeIn_JUploadContextTestCase";
32  
33      /** Logger for this class */
34      protected final Logger logger = Logger.getLogger(getClass());
35  
36      /** */
37      public UploadPolicy uploadPolicy = null;
38  
39      /** */
40      public JUploadPanel juploadPanel = null;
41  
42      /** */
43      public JUploadTextArea logWindow = null;
44  
45      /**
46       * Root folder for policies configuration files, that are used in JUnit tests. The current user dir is the root of
47       * the eclipse project.
48       */
49      public final static String TEST_ROOT_FOLDER = "tests/";
50  
51      /**
52       * Root folder for policies configuration files, that are used in JUnit tests.
53       */
54      public final static String TEST_PROPERTIES_FOLDER = TEST_ROOT_FOLDER + "policies/";
55  
56      /**
57       * Root folder for policies configuration files, that are used in JUnit tests.
58       */
59      public final static String TEST_FILES_FOLDER = TEST_ROOT_FOLDER + "files/";
60  
61      /**
62       * The policy default configuration file. It contains default values that are shared by all policy configuration
63       * files.
64       */
65      public final static String TEST_DEFAULT_PROPERTIES_FILE = TEST_PROPERTIES_FOLDER
66              + "default_uploadPolicy.properties";
67  
68      /**
69       * Used to control the last call to {@link #registerUnload(Object, String)}
70       */
71      public Object lastRegisterUnloadObject = null;
72  
73      /**
74       * Used to control the last call to {@link #registerUnload(Object, String)}
75       */
76      public String lastRegisterUnloadMethod = null;
77  
78      /**
79       * @param uploadPolicy
80       * @param juploadPanel
81       */
82      public JUploadContextTestHelper(UploadPolicy uploadPolicy, JUploadPanel juploadPanel) {
83          this.uploadPolicy = uploadPolicy;
84          this.juploadPanel = juploadPanel;
85          this.logWindow = new JUploadTextArea(100, 100, uploadPolicy);
86      }
87  
88      /**
89       * If using this constructor, you should set afteward: uploadPolicy, logWindow.
90       * 
91       * @param juploadPanel
92       */
93      public JUploadContextTestHelper(JUploadPanel juploadPanel) {
94          this.juploadPanel = juploadPanel;
95          this.logWindow = new JUploadTextArea(100, 100, uploadPolicy);
96      }
97  
98      /**
99       * Creates and loads a property file, and return the loaded result.
100      * 
101      * @param filename The name of the file, which contains the properties to load
102      * @param defaultProperties The default properties value. Put null if no default Properties should be used.
103      * @return The loaded properties. It's empty if an error occurs.
104      */
105     Properties loadPropertiesFromTestFile(String filename, Properties defaultProperties) {
106         Properties properties = new Properties(defaultProperties);
107         try {
108             InputStream isProperties = new FileInputStream(filename);
109             properties.load(isProperties);
110             isProperties.close();
111         } catch (IOException e1) {
112             System.out.println("Error while loading " + filename + " (" + e1.getClass().getName() + ")");
113             e1.printStackTrace();
114         }
115 
116         return properties;
117     }
118 
119     boolean getBooleanProperty(String property, boolean def) {
120         if (property.startsWith("xxx")) {
121             // Hum, if we go here, there are some unchanged "xxx" in the code.
122             // To be changed.
123             throw new UnsupportedOperationException(this.getClass() + ".getBooleanProperty() unknown property: '"
124                     + property + "'");
125         } else {
126             return parseBoolean(property, def);
127         }
128     }
129 
130     /*********************************************************************************
131      * Methods of the JUploadContext interface
132      *********************************************************************************/
133     /**
134      * @param url
135      * @param success
136      */
137     public void displayURL(String url, boolean success) {
138         throw new UnsupportedOperationException(this.getClass() + ".displayURL() is not implemented in tests cases");
139     }
140 
141     /**
142      * @see wjhk.jupload2.context.JUploadContext#getApplet()
143      */
144     public JApplet getApplet() {
145         throw new UnsupportedOperationException(this.getClass() + ".getApplet() is not implemented in tests cases");
146     }
147 
148     /**
149      * @see wjhk.jupload2.context.JUploadContext#getBuildDate()
150      */
151     public String getBuildDate() {
152         return "no build date for TestCase";
153     }
154 
155     /**
156      * @see wjhk.jupload2.context.JUploadContext#getBuildNumber()
157      */
158     public int getBuildNumber() {
159         throw new UnsupportedOperationException(this.getClass() + ".getBuildNumber() is not implemented in tests cases");
160     }
161 
162     /**
163      * @see wjhk.jupload2.context.JUploadContext#getCursor()
164      */
165     public Cursor getCursor() {
166         throw new UnsupportedOperationException(this.getClass() + ".getCursor() is not implemented in tests cases");
167     }
168 
169     /**
170      * @see wjhk.jupload2.context.JUploadContext#getLastModified()
171      */
172     public String getLastModified() {
173         throw new UnsupportedOperationException(this.getClass()
174                 + ".getLastModified() is not implemented in tests cases");
175     }
176 
177     /**
178      * @see wjhk.jupload2.context.JUploadContext#getLogWindow()
179      */
180     public JUploadTextArea getLogWindow() {
181         return this.logWindow;
182     }
183 
184     /**
185      * @see wjhk.jupload2.context.JUploadContext#getMimeType(java.lang.String)
186      */
187     public String getMimeType(final String fileExtension) {
188         return TEST_CASE_MIME_TYPE;
189     }
190 
191     /**
192      * Get a String parameter value from applet properties or System properties.
193      * 
194      * @param key The name of the parameter to fetch.
195      * @param def A default value which is used, when the specified parameter is not set.
196      * @return The value of the applet parameter (resp. system property). If the parameter was not specified or no such
197      *         system property exists, returns the given default value.
198      */
199 
200     public String getParameter(String key, String def) {
201         String paramStr = (System.getProperty(key) != null ? System.getProperty(key) : def);
202         return paramStr;
203     }
204 
205     /**
206      * @param key
207      * @param def
208      * @return The value for this parameter
209      */
210     public int getParameter(String key, int def) {
211         String paramDef = Integer.toString(def);
212         String paramStr = System.getProperty(key) != null ? System.getProperty(key) : paramDef;
213         return parseInt(paramStr, def);
214     }
215 
216     /**
217      * @param key
218      * @param def
219      * @return The value for this parameter
220      */
221     public float getParameter(String key, float def) {
222         String paramDef = Float.toString(def);
223         String paramStr = System.getProperty(key) != null ? System.getProperty(key) : paramDef;
224         return parseFloat(paramStr, def);
225     }
226 
227     /**
228      * @param key
229      * @param def
230      * @return The value for this parameter
231      */
232     public long getParameter(String key, long def) {
233         String paramDef = Long.toString(def);
234         String paramStr = System.getProperty(key) != null ? System.getProperty(key) : paramDef;
235         return parseLong(paramStr, def);
236     }
237 
238     /**
239      * @param key
240      * @param def
241      * @return The value for this parameter
242      */
243 
244     public boolean getParameter(String key, boolean def) {
245         String paramDef = (def ? "true" : "false");
246         String paramStr = System.getProperty(key) != null ? System.getProperty(key) : paramDef;
247         return parseBoolean(paramStr, def);
248     }// getParameter(boolean)
249 
250     /** {@inheritDoc} */
251     public FileListViewMode getParameter(String key, FileListViewMode def) {
252         String paramDef = def.toString();
253         String paramStr = System.getProperty(key) != null ? System.getProperty(key) : paramDef;
254         return parseFileListViewMode(paramStr, def);
255     }// getParameter(FileListViewMode)
256 
257     /**
258      * @see wjhk.jupload2.context.JUploadContext#getUploadPanel()
259      */
260     public JUploadPanel getUploadPanel() {
261         return this.juploadPanel;
262     }
263 
264     /**
265      * @see wjhk.jupload2.context.JUploadContext#getUploadPolicy()
266      */
267     public UploadPolicy getUploadPolicy() {
268         return this.uploadPolicy;
269     }
270 
271     /**
272      * @see wjhk.jupload2.context.JUploadContext#getDetailedVersionMessage()
273      */
274     public String getDetailedVersionMessage() {
275         return "TestCase (detailed message)";
276     }
277 
278     /**
279      * @see wjhk.jupload2.context.JUploadContext#getVersion()
280      */
281     public String getVersion() {
282         return "TestCase version";
283     }
284 
285     /**
286      * @see wjhk.jupload2.context.JUploadContext#getSvnRevision()
287      */
288     public String getSvnRevision() {
289         return "TestCase svn revision";
290     }
291 
292     /**
293      * @see wjhk.jupload2.context.JUploadContext#normalizeURL(java.lang.String)
294      */
295     public String normalizeURL(String url) throws JUploadException {
296         return url;
297     }
298 
299     /** {@inheritDoc} */
300     public int parseInt(String value, int def) {
301         int ret = def;
302         // Then, parse it as an integer.
303         try {
304             ret = Integer.parseInt(value);
305         } catch (NumberFormatException e) {
306             ret = def;
307             this.logger.warn("Invalid int value: " + value + ", using default value: " + def);
308 
309         }
310         return ret;
311     }
312 
313     /** {@inheritDoc} */
314     public float parseFloat(String value, float def) {
315         float ret = def;
316         // Then, parse it as an integer.
317         try {
318             ret = Float.parseFloat(value);
319         } catch (NumberFormatException e) {
320             ret = def;
321             this.logger.warn("Invalid float value: " + value + ", using default value: " + def);
322         }
323         return ret;
324     }
325 
326     /** {@inheritDoc} */
327     public long parseLong(String value, long def) {
328         long ret = def;
329         // Then, parse it as an integer.
330         try {
331             ret = Long.parseLong(value);
332         } catch (NumberFormatException e) {
333             ret = def;
334             this.logger.warn("Invalid long value: " + value + ", using default value: " + def);
335         }
336         return ret;
337     }
338 
339     /** {@inheritDoc} */
340     public boolean parseBoolean(String value, boolean def) {
341         // Then, parse it as a boolean.
342         if (value.toUpperCase().equals("FALSE")) {
343             return false;
344         } else if (value.toUpperCase().equals("TRUE")) {
345             return true;
346         } else {
347             this.logger.warn("Invalid boolean value: " + value + ", using default value: " + def);
348         }
349         return def;
350     }
351 
352     /** {@inheritDoc} */
353     public FileListViewMode parseFileListViewMode(String value, FileListViewMode def) {
354         FileListViewMode ret = def;
355         try {
356             ret = FileListViewMode.valueOf(value);
357         } catch (IllegalArgumentException e) {
358             this.logger.warn("Invalid FileListViewMode value: " + value + ", using default value: " + def);
359         }
360         return ret;
361     }
362 
363     /**
364      * @see wjhk.jupload2.context.JUploadContext#readCookieFromNavigator(java.util.Vector)
365      */
366     public void readCookieFromNavigator(Vector<String> headers) {
367         this.logger.warn(this.getClass() + ".readCookieFromNavigator() is not implemented in tests cases");
368     }
369 
370     /**
371      * @see wjhk.jupload2.context.JUploadContext#readUserAgentFromNavigator(java.util.Vector)
372      */
373     public void readUserAgentFromNavigator(Vector<String> headers) {
374         this.logger.warn(this.getClass() + ".readUserAgentFromNavigator() is not implemented in tests cases");
375     }
376 
377     /**
378      * @see wjhk.jupload2.context.JUploadContext#registerUnload(java.lang.Object, java.lang.String)
379      */
380     public void registerUnload(Object object, String method) {
381         this.lastRegisterUnloadObject = object;
382         this.lastRegisterUnloadMethod = method;
383         this.logger.warn(this.getClass() + ".registerUnload() is not implemented in tests cases");
384     }
385 
386     /**
387      * @see wjhk.jupload2.context.JUploadContext#runUnload()
388      */
389     public void runUnload() {
390         throw new UnsupportedOperationException(this.getClass() + ".runUnload() is not implemented in tests cases");
391     }
392 
393     /**
394      * @see wjhk.jupload2.context.JUploadContext#setCursor(java.awt.Cursor)
395      */
396     public Cursor setCursor(Cursor cursor) {
397         throw new UnsupportedOperationException(this.getClass() + ".setCursor() is not implemented in tests cases");
398     }
399 
400     /**
401      * @see wjhk.jupload2.context.JUploadContext#setProperty(java.lang.String, java.lang.String)
402      */
403     public void setProperty(String prop, String value) {
404         throw new UnsupportedOperationException(this.getClass() + ".setProperty() is not implemented in tests cases");
405     }
406 
407     /**
408      * @see wjhk.jupload2.context.JUploadContext#setWaitCursor()
409      */
410     public Cursor setWaitCursor() {
411         throw new UnsupportedOperationException(this.getClass() + ".setWaitCursor() is not implemented in tests cases");
412     }
413 
414     /**
415      * @see wjhk.jupload2.context.JUploadContext#showStatus(java.lang.String)
416      */
417     public void showStatus(String status) {
418         this.logger.warn(this.getClass() + ".showStatus() is not implemented in tests cases");
419     }
420 
421     /**
422      * @see wjhk.jupload2.context.JUploadContext#startUpload()
423      */
424     public String startUpload() {
425         throw new UnsupportedOperationException(this.getClass() + ".startUpload() is not implemented in tests cases");
426     }
427 
428     /** @see JUploadContext#getFrame() */
429     public Frame getFrame() {
430         throw new UnsupportedOperationException(this.getClass() + ".getFrame() is not implemented in tests cases");
431     }
432 
433 }