001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit.javascript.configuration;
039:
040: import com.gargoylesoftware.htmlunit.WebTestCase;
041:
042: /**
043: * Tests for {@link JavaScriptConfiguration}.
044: *
045: * @version $Revision: 2132 $
046: * @author Chris Erskine
047: * @author Ahmed Ashour
048: */
049: public class ClassConfigurationTest extends WebTestCase {
050: /**
051: * Create an instance
052: * @param name The name of the test
053: */
054: public ClassConfigurationTest(final String name) {
055: super (name);
056: }
057:
058: /**
059: * Reset the JavaScriptConfiguration file for each test to it's inital clean state.
060: *
061: * @throws Exception if the test fails
062: */
063: protected void setUp() throws Exception {
064: super .setUp();
065: JavaScriptConfiguration.resetClassForTesting();
066: }
067:
068: /**
069: * Test equality on a class configuration
070: * @throws Exception - Exception on error
071: */
072: public void testConfigurationSimplePropertyEquality()
073: throws Exception {
074: final ClassConfiguration config1 = new ClassConfiguration("c1",
075: ConfigTestClass.class.getName(), null, null, null, true);
076: final ClassConfiguration config2 = new ClassConfiguration("c2",
077: ConfigTestClass.class.getName(), null, null, null, true);
078:
079: config1.addProperty("test", true, true);
080: assertFalse("Configs should not be equal", config1
081: .equals(config2));
082: config2.addProperty("test", true, true);
083: assertTrue("Configs should now be equal", config1
084: .equals(config2));
085: }
086:
087: /**
088: * Test equality on a class configuration for function
089: * @throws Exception - Exception on error
090: */
091: public void testConfigurationSimpleFunctionEquality()
092: throws Exception {
093: final ClassConfiguration config1 = new ClassConfiguration("c1",
094: ConfigTestClass.class.getName(), null, null, null, true);
095: final ClassConfiguration config2 = new ClassConfiguration("c2",
096: ConfigTestClass.class.getName(), null, null, null, true);
097:
098: config1.addFunction("testFunction");
099: assertFalse("Configs should not be equal", config1
100: .equals(config2));
101: config2.addFunction("testFunction");
102: assertTrue("Configs should now be equal", config1
103: .equals(config2));
104: }
105:
106: /**
107: * Test equality on a class configuration
108: *
109: * @throws Exception - Exception on error
110: */
111: public void testConfigurationSimpleUnequalProperties()
112: throws Exception {
113: final ClassConfiguration config1 = new ClassConfiguration("c1",
114: ConfigTestClass.class.getName(), null, null, null, true);
115: final ClassConfiguration config2 = new ClassConfiguration("c2",
116: ConfigTestClass.class.getName(), null, null, null, true);
117:
118: config1.addProperty("test", true, true);
119: assertFalse("Configs should not be equal", config1
120: .equals(config2));
121: config2.addProperty("test", true, false);
122: assertFalse(
123: "Configs should not be equal due to different property values",
124: config1.equals(config2));
125: }
126:
127: /**
128: * @throws Exception on error
129: */
130: public void testForJSFlagTrue() throws Exception {
131: final ClassConfiguration config1 = new ClassConfiguration("c1",
132: ConfigTestClass.class.getName(), null, null, null, true);
133: assertTrue("JSObject Flag should have been set", config1
134: .isJsObject());
135: }
136:
137: /**
138: * @throws Exception on error
139: */
140: public void testForJSFlagFalse() throws Exception {
141: final ClassConfiguration config1 = new ClassConfiguration("c1",
142: ConfigTestClass.class.getName(), null, null, null,
143: false);
144: assertFalse("JSObject Flag should not have been set", config1
145: .isJsObject());
146: }
147:
148: /**
149: * Test equality on a class configuration
150: *
151: * @throws Exception - Exception on error
152: */
153: public void testConfigurationPropertyEqualityWithBrowser()
154: throws Exception {
155: final ClassConfiguration config1 = new ClassConfiguration("c1",
156: ConfigTestClass.class.getName(), null, null, null, true);
157: final ClassConfiguration config2 = new ClassConfiguration("c2",
158: ConfigTestClass.class.getName(), null, null, null, true);
159:
160: config1.addProperty("test", true, true);
161: config2.addProperty("test", true, true);
162: config1.setBrowser("test", "Netscape");
163: assertFalse("Should not be equal with browser added", config1
164: .equals(config2));
165: config2.setBrowser("test", "Netscape");
166: assertTrue("Should be equal with browser added", config1
167: .equals(config2));
168: }
169:
170: /**
171: * Test equality on a class configuration mis-matched browsers
172: *
173: * @throws Exception - Exception on error
174: */
175: public void testConfigurationPropertyEqualityWithDifferentBrowsers()
176: throws Exception {
177: final ClassConfiguration config1 = new ClassConfiguration("c1",
178: ConfigTestClass.class.getName(), null, null, null, true);
179: final ClassConfiguration config2 = new ClassConfiguration("c2",
180: ConfigTestClass.class.getName(), null, null, null, true);
181:
182: config1.addProperty("test", true, true);
183: config2.addProperty("test", true, true);
184: config1.setBrowser("test", "Netscape");
185: assertFalse("Should not be equal with browser added", config1
186: .equals(config2));
187: config2.setBrowser("test", "Microsoft Internet Explorer");
188: assertFalse("Should be equal with different browser added",
189: config1.equals(config2));
190: }
191:
192: /**
193: * Test for throwing exception when setter method is not defined
194: *
195: * @throws Exception - Exception on error
196: */
197: public void testNoSetterMethod() throws Exception {
198: final ClassConfiguration config1 = new ClassConfiguration("c1",
199: ConfigTestClass.class.getName(), null, null, null, true);
200: try {
201: config1.addProperty("getterOnly", true, true);
202: fail("Should produce an exception due to not finding the methods");
203: } catch (final IllegalStateException e) {
204: assertTrue(true);
205: }
206: }
207:
208: /**
209: * Test for throwing exception when setter method is not defined
210: *
211: * @throws Exception - Exception on error
212: */
213: public void testNoFunctionMethod() throws Exception {
214: final ClassConfiguration config1 = new ClassConfiguration("c1",
215: ConfigTestClass.class.getName(), null, null, null, true);
216: try {
217: config1.addFunction("noTestFunction");
218: fail("Should produce an exception due to not finding the methods");
219: } catch (final IllegalStateException e) {
220: assertTrue(true);
221: }
222: }
223:
224: /**
225: * test class
226: */
227: protected class ConfigTestClass {
228: private boolean test_ = false;
229:
230: /**
231: * Dummy function
232: */
233: public void jsxFunction_testFunction() {
234: }
235:
236: /**
237: * @return boolean
238: */
239: public boolean jsxGet_test() {
240: return test_;
241: }
242:
243: /**
244: * @return boolean
245: */
246: public boolean jsxGet_getterOnly() {
247: return test_;
248: }
249:
250: /**
251: * @param testFlag - test value
252: */
253: public void jsxSet_test(final boolean testFlag) {
254: test_ = testFlag;
255: }
256: }
257: }
|