You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shale.apache.org by cr...@apache.org on 2006/10/09 02:23:00 UTC
svn commit: r454245 - in /shale/framework/trunk/shale-test/src:
main/java/org/apache/shale/test/el/MockVariableValueExpression.java
test/java/org/apache/shale/test/el/
test/java/org/apache/shale/test/el/MockExpressionFactoryTestCase.java
Author: craigmcc
Date: Sun Oct 8 17:22:59 2006
New Revision: 454245
URL: http://svn.apache.org/viewvc?view=rev&rev=454245
Log:
MockExpressionFactory unit tests for type coercion and "literal"
value expressions (those that simply wrap a literal value).
SHALE-304
Added:
shale/framework/trunk/shale-test/src/test/java/org/apache/shale/test/el/
shale/framework/trunk/shale-test/src/test/java/org/apache/shale/test/el/MockExpressionFactoryTestCase.java (with props)
Modified:
shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockVariableValueExpression.java
Modified: shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockVariableValueExpression.java
URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockVariableValueExpression.java?view=diff&rev=454245&r1=454244&r2=454245
==============================================================================
--- shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockVariableValueExpression.java (original)
+++ shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockVariableValueExpression.java Sun Oct 8 17:22:59 2006
@@ -109,7 +109,7 @@
*/
public boolean isLiteralText() {
- return false;
+ return true;
}
Added: shale/framework/trunk/shale-test/src/test/java/org/apache/shale/test/el/MockExpressionFactoryTestCase.java
URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-test/src/test/java/org/apache/shale/test/el/MockExpressionFactoryTestCase.java?view=auto&rev=454245
==============================================================================
--- shale/framework/trunk/shale-test/src/test/java/org/apache/shale/test/el/MockExpressionFactoryTestCase.java (added)
+++ shale/framework/trunk/shale-test/src/test/java/org/apache/shale/test/el/MockExpressionFactoryTestCase.java Sun Oct 8 17:22:59 2006
@@ -0,0 +1,330 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shale.test.el;
+
+import javax.el.ELContext;
+import javax.el.PropertyNotWritableException;
+import javax.el.ValueExpression;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import org.apache.shale.test.base.AbstractJsfTestCase;
+import org.apache.shale.test.mock.MockApplication12;
+
+/**
+ * <p>Test case for <code>MockExpressionFactory.</p>
+ */
+public class MockExpressionFactoryTestCase extends AbstractJsfTestCase {
+
+
+ // ------------------------------------------------------------ Constructors
+
+
+ // Construct a new instance of this test case.
+ public MockExpressionFactoryTestCase(String name) {
+ super(name);
+ }
+
+
+ // ---------------------------------------------------- Overall Test Methods
+
+
+ // Set up instance variables required by this test case.
+ protected void setUp() throws Exception {
+
+ super.setUp();
+ factory = (MockExpressionFactory)
+ ((MockApplication12) application).getExpressionFactory();
+
+ }
+
+
+ // Return the tests included in this test case.
+ public static Test suite() {
+
+ return (new TestSuite(MockExpressionFactoryTestCase.class));
+
+ }
+
+
+ // Tear down instance variables required by this test case.
+ protected void tearDown() throws Exception {
+
+ factory = null;
+ super.tearDown();
+
+ }
+
+
+ // -------------------------------------------------------- Static Variables
+
+
+ /**
+ * <p>Input values to be converted, of all the interesting types.</p>
+ */
+ private static final Object[] INPUT_VALUES = {
+ (String) null,
+ "",
+ "1234",
+ Boolean.TRUE,
+ Boolean.FALSE,
+ new Byte((byte) 123),
+ new Double(234),
+ new Float(345),
+ new Integer(456),
+ new Long(567),
+ new Short((short) 678),
+ };
+
+
+ /**
+ * <p>Output values when converted to Boolean.</p>
+ */
+ private static final Boolean[] OUTPUT_BOOLEAN = {
+ Boolean.FALSE,
+ Boolean.FALSE,
+ Boolean.FALSE,
+ Boolean.TRUE,
+ Boolean.FALSE,
+ null,
+ null,
+ null,
+ null,
+ null,
+ null
+ };
+
+
+ /**
+ * <p>Output values when converted to Integer.</p>
+ */
+ private static final Integer[] OUTPUT_INTEGER = {
+ new Integer(0),
+ new Integer(0),
+ new Integer(1234),
+ new Integer(1),
+ new Integer(0),
+ new Integer(123),
+ new Integer(234),
+ new Integer(345),
+ new Integer(456),
+ new Integer(567),
+ new Integer(678)
+ };
+
+
+ /**
+ * <p>Output values when converted to String.</p>
+ */
+ private static final String[] OUTPUT_STRING = {
+ "",
+ "",
+ "1234",
+ "true",
+ "false",
+ "123",
+ "234.0",
+ "345.0",
+ "456",
+ "567",
+ "678"
+ };
+
+
+ // ------------------------------------------------------ Instance Variables
+
+
+ /**
+ * <p>The expression factory to be tested.</p>
+ */
+ private MockExpressionFactory factory = null;
+
+
+ // ------------------------------------------------- Individual Test Methods
+
+
+ // Test coercion when expectedType is passed as Boolean.
+ public void testCoerceToBoolean() {
+
+ Object result = null;
+ for (int i = 0; i < INPUT_VALUES.length; i++) {
+ try {
+ result = factory.coerceToType(INPUT_VALUES[i], Boolean.class);
+ if ((INPUT_VALUES[i] == null)
+ || (INPUT_VALUES[i] instanceof String)
+ || (INPUT_VALUES[i] instanceof Boolean)) {
+ assertEquals("[" + i + "]", OUTPUT_BOOLEAN[i], result);
+ } else {
+ fail("[" + i + "] should have thrown IllegalArgumentException");
+ }
+ } catch (IllegalArgumentException e) {
+ if ((INPUT_VALUES[i] == null)
+ || (INPUT_VALUES[i] instanceof String)
+ || (INPUT_VALUES[i] instanceof Boolean)) {
+ fail("[" + i + "] threw IllegalArgumentException");
+ } else {
+ ; // Expected result
+ }
+ }
+ }
+
+ }
+
+
+ // Test coercion when expectedType is passed as null. We should
+ // get the original object back
+ public void testCoerceToNull() {
+
+ Object result = null;
+ for (int i = 0; i < INPUT_VALUES.length; i++) {
+ result = factory.coerceToType(INPUT_VALUES[i], null);
+ if (INPUT_VALUES[i] == null) {
+ assertNull(result);
+ } else {
+ assertTrue("[" + i + "]", result == INPUT_VALUES[i]);
+ }
+ }
+
+ }
+
+
+ // Test coercion when expectedType is Object. We should
+ // get the original object back.
+ public void testCoerceToObject() {
+
+ Object result = null;
+ for (int i = 0; i < INPUT_VALUES.length; i++) {
+ result = factory.coerceToType(INPUT_VALUES[i], Object.class);
+ if (INPUT_VALUES[i] == null) {
+ assertNull(result);
+ } else {
+ assertTrue("[" + i + "]", result == INPUT_VALUES[i]);
+ }
+ }
+
+ }
+
+
+ // Test coercion when expectedType is Integer
+ public void testCoerceToInteger() {
+
+ Object result = null;
+ for (int i = 0; i < INPUT_VALUES.length; i++) {
+ try {
+ result = factory.coerceToType(INPUT_VALUES[i], Integer.class);
+ if ((INPUT_VALUES[i] != null)
+ && (INPUT_VALUES[i] instanceof Boolean)) {
+ fail("[" + i + "] should have thrown IllegalArgumentException");
+ } else {
+ assertEquals("[" + i + "]", OUTPUT_INTEGER[i], result);
+ }
+ } catch (IllegalArgumentException e) {
+ if ((INPUT_VALUES[i] != null)
+ && (INPUT_VALUES[i] instanceof Boolean)) {
+ ; // Expected result
+ } else {
+ fail("[" + i + "] should have thrown IllegalArgumentException");
+ }
+ }
+ }
+
+ }
+
+
+ // Test coercion when expectedType is String.
+ public void testCoerceToString() {
+
+ Object result = null;
+ for (int i = 0; i < INPUT_VALUES.length; i++) {
+ result = factory.coerceToType(INPUT_VALUES[i], String.class);
+ assertEquals("[" + i + "]", OUTPUT_STRING[i], result);
+ }
+
+ }
+
+
+ // Test ValueExpression that wraps a literal String object and conversion to Integer
+ public void testLiteralValueExpressionInteger() {
+
+ ELContext context = facesContext.getELContext();
+
+ ValueExpression expr = factory.createValueExpression("123", Integer.class);
+ assertEquals(Integer.class, expr.getExpectedType());
+ assertEquals(String.class, expr.getType(context));
+ assertEquals(new Integer(123), expr.getValue(context));
+ assertTrue(expr.isLiteralText());
+ assertTrue(expr.isReadOnly(context));
+ try {
+ expr.setValue(context, "234");
+ fail("Should have thrown PropertyNotWritableException");
+ } catch (PropertyNotWritableException e) {
+ ; // Expected result
+ }
+
+ }
+
+
+ // Test ValueExpression that wraps a literal String object and no conversion
+ public void testLiteralValueExpressionNone() {
+
+ ELContext context = facesContext.getELContext();
+
+ ValueExpression expr = factory.createValueExpression("abc", String.class);
+ assertEquals(String.class, expr.getExpectedType());
+ assertEquals(String.class, expr.getType(context));
+ assertEquals("abc", expr.getValue(context));
+ assertTrue(expr.isLiteralText());
+ assertTrue(expr.isReadOnly(context));
+ try {
+ expr.setValue(context, "def");
+ fail("Should have thrown PropertyNotWritableException");
+ } catch (PropertyNotWritableException e) {
+ ; // Expected result
+ }
+
+ }
+
+
+ // Test ValueExpression that wraps a literal Integer object and conversion to String
+ public void testLiteralValueExpressionString() {
+
+ ELContext context = facesContext.getELContext();
+
+ ValueExpression expr = factory.createValueExpression(new Integer(123), String.class);
+ assertEquals(String.class, expr.getExpectedType());
+ assertEquals(Integer.class, expr.getType(context));
+ assertEquals("123", expr.getValue(context));
+ assertTrue(expr.isLiteralText());
+ assertTrue(expr.isReadOnly(context));
+ try {
+ expr.setValue(context, new Integer(234));
+ fail("Should have thrown PropertyNotWritableException");
+ } catch (PropertyNotWritableException e) {
+ ; // Expected result
+ }
+
+ }
+
+
+ public void testPristine() {
+
+ assertNotNull(factory);
+
+ }
+
+
+
+}
Propchange: shale/framework/trunk/shale-test/src/test/java/org/apache/shale/test/el/MockExpressionFactoryTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: shale/framework/trunk/shale-test/src/test/java/org/apache/shale/test/el/MockExpressionFactoryTestCase.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL