View Javadoc
1   //
2   // $Id$
3   //
4   // jupload - A file upload applet.
5   //
6   // Copyright 2010 The JUpload Team
7   //
8   // Created: 2 avr. 2010
9   // Creator: etienne_sf
10  // Last modified: $Date$
11  //
12  // This program is free software; you can redistribute it and/or modify
13  // it under the terms of the GNU General Public License as published by
14  // the Free Software Foundation; either version 2 of the License, or
15  // (at your option) any later version.
16  //
17  // This program is distributed in the hope that it will be useful,
18  // but WITHOUT ANY WARRANTY; without even the implied warranty of
19  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  // GNU General Public License for more details.
21  //
22  // You should have received a copy of the GNU General Public License
23  // along with this program; if not, write to the Free Software
24  // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  
26  package wjhk.jupload2.context;
27  
28  import java.net.URL;
29  
30  import javax.swing.JApplet;
31  
32  import org.junit.Assert;
33  import org.junit.Before;
34  import org.junit.Test;
35  
36  import wjhk.jupload2.JUploadApplet;
37  import wjhk.jupload2.policies.UploadPolicy;
38  import wjhk.jupload2.policies.UploadPolicyFactory;
39  import wjhk.jupload2.testhelpers.UploadPolicyTestHelper;
40  import wjhk.jupload2.upload.AbstractJUploadTestHelper;
41  
42  /** */
43  @SuppressWarnings("serial")
44  class JUploadAppletTestHelper extends JUploadApplet {
45  	URL documentBase = null;
46  
47  	@Override
48  	public String getParameter(String prop) {
49  		if (prop.equals(UploadPolicy.PROP_UPLOAD_POLICY)) {
50  			return UploadPolicyTestHelper.class.getName();
51  		} else {
52  			// For test cases, we use the System properties, as we don't have
53  			// any applet parameter.
54  			return System.getProperty(prop);
55  		}
56  	}
57  
58  	/** Simulate calls to getDocumentBase() */
59  	@Override
60  	public URL getDocumentBase() {
61  		return this.documentBase;
62  	}
63  }
64  
65  /**
66   * This class is just used to catch the getUploadPolicy method. This is
67   * necessary (for JUnit tests) to prevent UploadPolicyFactory to create a new
68   * UploadPolicy.
69   */
70  class JUploadContextAppletTestHelper extends JUploadContextApplet {
71  
72  	static UploadPolicy uploadPolicyTestCase = null;
73  
74  	public JUploadContextAppletTestHelper(JApplet theApplet,
75  			UploadPolicy uploadPolicy) {
76  		super(theApplet);
77  		this.uploadPolicy = uploadPolicy;
78  	}
79  
80  	/**
81  	 * Retrieves the current upload policy used in the unit testing. Override
82  	 * the default method, which would call the
83  	 * {@link UploadPolicyFactory#getUploadPolicy(JUploadContext)} method.
84  	 * 
85  	 * @return the current upload policy of the running test.
86  	 */
87  	@Override
88  	public UploadPolicy getUploadPolicy() {
89  		if (this.uploadPolicy == null) {
90  			this.uploadPolicy = uploadPolicyTestCase;
91  		}
92  		return this.uploadPolicy;
93  	}
94  
95  	/** Does nothing, avoir real 'sign' test */
96  	@Override
97  	void checkAppletIsSigned() {
98  		// Nothing. Just to avoid the original call to checkAppletIsSigned(),
99  		// which would throw an error.
100 	}
101 }
102 
103 /**
104  * @author etienne_sf
105  */
106 public class JUploadContextAppletTest extends AbstractJUploadTestHelper {
107 
108 	JUploadAppletTestHelper applet;
109 
110 	JUploadContextApplet juploadContextApplet;
111 
112 	String paramName = "A non existing parameter";
113 
114 	/** */
115 	@Before
116 	public void setUp() {
117 		JUploadContextAppletTestHelper.uploadPolicyTestCase = this.uploadPolicy;
118 		this.applet = new JUploadAppletTestHelper();
119 		this.juploadContextApplet = new JUploadContextAppletTestHelper(
120 				this.applet, this.uploadPolicy);
121 	}
122 
123 	/** */
124 	@Test
125 	public void testInit() {
126 		Assert.assertNotNull("Check the applet", this.applet);
127 		Assert.assertEquals("Check the applet getter", this.applet,
128 				this.juploadContextApplet.theApplet);
129 		Assert.assertNull("Check the rootPane: the applet",
130 				this.juploadContextApplet.frame);
131 	}
132 
133 	/** */
134 	@Test
135 	public void testGetParameter_String() {
136 		String defaultParamValue = "A test default value";
137 
138 		System.clearProperty(this.paramName);
139 		Assert.assertEquals("A dummy parameter name: should use default",
140 				defaultParamValue, this.juploadContextApplet.getParameter(
141 						this.paramName, defaultParamValue));
142 
143 		String paramValue = "A real value";
144 		System.setProperty(this.paramName, paramValue);
145 		Assert.assertEquals("A dummy parameter name: should use given value",
146 				paramValue, this.juploadContextApplet.getParameter(
147 						this.paramName, defaultParamValue));
148 	}
149 
150 	/** */
151 	@Test
152 	public void testGetParameter_Int() {
153 		int defaultParamValue = -23465;
154 
155 		System.clearProperty(this.paramName);
156 		Assert.assertEquals("A dummy parameter name: should use default",
157 				defaultParamValue, this.juploadContextApplet.getParameter(
158 						this.paramName, defaultParamValue));
159 
160 		int paramValue = -7342;
161 		System.setProperty(this.paramName, Integer.toString(paramValue));
162 		Assert.assertEquals("A dummy parameter name: should use given value",
163 				paramValue, this.juploadContextApplet.getParameter(
164 						this.paramName, defaultParamValue));
165 	}
166 
167 	/** */
168 	@Test
169 	public void testGetParameter_Float() {
170 		float defaultParamValue = (float) -23465.2143;
171 
172 		System.clearProperty(this.paramName);
173 		Assert.assertEquals("A dummy parameter name: should use default",
174 				defaultParamValue, this.juploadContextApplet.getParameter(
175 						this.paramName, defaultParamValue), 0);
176 
177 		float paramValue = (float) -7342.532;
178 		System.setProperty(this.paramName, Float.toString(paramValue));
179 		Assert.assertEquals("A dummy parameter name: should use given value",
180 				paramValue, this.juploadContextApplet.getParameter(
181 						this.paramName, defaultParamValue), 0);
182 	}
183 
184 	/** */
185 	@Test
186 	public void testGetParameter_Long() {
187 		long defaultParamValue = -23465;
188 
189 		System.clearProperty(this.paramName);
190 		Assert.assertEquals("A dummy parameter name: should use default",
191 				defaultParamValue, this.juploadContextApplet.getParameter(
192 						this.paramName, defaultParamValue));
193 
194 		long paramValue = -7342;
195 		System.setProperty(this.paramName, Long.toString(paramValue));
196 		Assert.assertEquals("A dummy parameter name: should use given value",
197 				paramValue, this.juploadContextApplet.getParameter(
198 						this.paramName, defaultParamValue));
199 	}
200 
201 	/** */
202 	@Test
203 	public void testGetParameter_Boolean() {
204 		boolean defaultParamValue = false;
205 
206 		System.clearProperty(this.paramName);
207 		Assert.assertEquals("A dummy parameter name: should use default",
208 				defaultParamValue, this.juploadContextApplet.getParameter(
209 						this.paramName, defaultParamValue));
210 		defaultParamValue = true;
211 		Assert.assertEquals("A dummy parameter name: should use default",
212 				defaultParamValue, this.juploadContextApplet.getParameter(
213 						this.paramName, defaultParamValue));
214 
215 		System.setProperty(this.paramName, "true");
216 		Assert.assertTrue("A dummy parameter name: should use given value",
217 				this.juploadContextApplet.getParameter(this.paramName,
218 						defaultParamValue));
219 	}
220 
221 	/** */
222 	@Test
223 	public void testJsString() {
224 		Assert.assertEquals("jsString()", "\\$\\'\\'\\n",
225 				this.juploadContextApplet.jsString("$\"'\r\n"));
226 	}
227 }