001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.tests.shared;
034:
035: import com.flexive.shared.value.*;
036: import org.testng.annotations.DataProvider;
037: import org.testng.annotations.Test;
038: import org.apache.commons.lang.StringUtils;
039:
040: import java.util.Arrays;
041: import java.util.Collections;
042: import java.util.List;
043: import java.util.ArrayList;
044:
045: /**
046: * Generic FxValue tests
047: *
048: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
049: */
050: @Test(groups="shared")
051: public class FxValueTest {
052: private static Object[][] testInstances = new Object[][] {
053: { new ValueTestBean<String, FxString>(new FxString(false,
054: "Test string"), "Element",
055: new String[] { "String value" }, new String[0]) },
056: { new ValueTestBean<Integer, FxNumber>(new FxNumber(false,
057: 42), 1234, new String[] { "5678", "0", "-23124" },
058: new String[] { "6789x", "", "5.6", "-", "-0.1" }) },
059: { new ValueTestBean<Long, FxLargeNumber>(new FxLargeNumber(
060: false, (long) 42), (long) 1234, new String[] {
061: "5678", "0", "-23124" }, new String[] { "6789x",
062: "", "5.6", "-", "-0.1" }) },
063: { new ValueTestBean<Double, FxDouble>(new FxDouble(false,
064: 1979.1111), 1234.5678, new String[] { "5678", "0",
065: "-23124", "0.0", "-5.3", "213341.314231" },
066: new String[] { "6789x", "", "abc", "123,456" }) },
067: { new ValueTestBean<Float, FxFloat>(new FxFloat(false,
068: 1979.1111f), 1234.5678f, new String[] { "5678",
069: "0", "-23124", "0.0", "-5.3", "213341.314231" },
070: new String[] { "6789x", "", "abc", "123,456" }) },
071: { new ValueTestBean<Boolean, FxBoolean>(new FxBoolean(
072: false, true), false,
073: new String[] { "true", "false" }, new String[] {}) },
074: // TODO add test instances for FxDate, FxDateRange
075: };
076:
077: @Test(dataProvider="testInstances")
078: public <T, TDerived extends FxValue<T, TDerived>> void testIsValid(
079: ValueTestBean<T, TDerived> testBean) {
080: FxValue<T, TDerived> value = testBean.getValue().copy();
081: assert value.isValid() : "Test value should be valid: " + value;
082: assert !value.isMultiLanguage() : "Value must not be multilingual: "
083: + value;
084:
085: // test valid string values
086: for (String validValue : testBean.getValidStringValues()) {
087: //noinspection unchecked
088: ((FxValue) value).setValue(validValue);
089: assert value.isValid() : "String value should be valid: "
090: + validValue;
091: }
092:
093: // test invalid string values
094: for (String invalidValue : testBean.getInvalidStringValues()) {
095: //noinspection unchecked
096: ((FxValue) value).setValue(invalidValue);
097: assert !value.isValid() : "String value should not be valid: "
098: + invalidValue;
099: }
100: value.setValue(testBean.getValueElement());
101: assert value.isValid() : "Value must be valid: "
102: + testBean.getValueElement();
103: }
104:
105: @Test(dataProvider="testInstances")
106: public <T, TDerived extends FxValue<T, TDerived>> void testValidContract(
107: ValueTestBean<T, TDerived> testBean) {
108: // checks the contract of FxValue#isValid() and FxValue#getErrorValue():
109: FxValue<T, TDerived> value = testBean.getValue().copy();
110: // if !isValid(), then getErrorValue() must return an object.
111: for (String validValue : testBean.getValidStringValues()) {
112: //noinspection unchecked
113: ((FxValue) value).setValue(validValue);
114: assert value.isValid();
115: try {
116: value.getErrorValue();
117: assert false : "Valid values cannot return error values.";
118: } catch (IllegalStateException e) {
119: // pass
120: }
121: }
122: // check empty values
123: value.setEmpty();
124: if (value.isValid()) {
125: try {
126: value.getErrorValue();
127: assert false : "Valid values cannot return error values.";
128: } catch (IllegalStateException e) {
129: // pass
130: }
131: } else {
132: assert value.getErrorValue() != null : "Invalid values must return an error value.";
133: }
134: // if isValid(), then getErrorValue() must throw an IllegalStateException.
135: for (String invalidValue : testBean.getInvalidStringValues()) {
136: //noinspection unchecked
137: ((FxValue) value).setValue(invalidValue);
138: assert !value.isValid();
139: assert value.getErrorValue() != null : "Invalid value must return an error value.";
140: }
141: }
142:
143: /**
144: * Bug check: if a previously empty number property is updated with the same
145: * "empty" number, the isEmpty flag is not set correctly.
146: */
147: @Test
148: public void testUpdateEmptyValue() {
149: FxNumber number = new FxNumber(0).setEmpty();
150: assert number.isEmpty();
151: number.setValue(0);
152: assert !number.isEmpty() : "FxNumber still empty after explicit value update.";
153: }
154:
155: @DataProvider(name="testInstances")
156: private Object[][] getTestInstances() {
157: return testInstances;
158: }
159:
160: /**
161: * Test instance for a FxValue element.
162: */
163: private static class ValueTestBean<T, TDerived extends FxValue<T, TDerived>> {
164: private FxValue<T, TDerived> value;
165: private T valueElement;
166: private List<String> validStringValues;
167: private List<String> invalidStringValues;
168:
169: public ValueTestBean(FxValue<T, TDerived> value,
170: T valueElement, String[] validStringValues,
171: String[] invalidStringValues) {
172: super ();
173: this .value = value;
174: this .valueElement = valueElement;
175: this .validStringValues = Collections
176: .unmodifiableList(Arrays.asList(validStringValues));
177: this .invalidStringValues = Collections
178: .unmodifiableList(Arrays
179: .asList(invalidStringValues));
180: }
181:
182: public List<String> getInvalidStringValues() {
183: return invalidStringValues;
184: }
185:
186: public List<String> getValidStringValues() {
187: return validStringValues;
188: }
189:
190: public FxValue<T, TDerived> getValue() {
191: return value;
192: }
193:
194: public T getValueElement() {
195: return valueElement;
196: }
197: }
198: }
|