001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestConstrainedBean.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.site;
009:
010: import com.uwyn.rife.site.TextualIdentifierGenerator;
011: import java.util.ArrayList;
012: import java.util.List;
013: import junit.framework.TestCase;
014:
015: public class TestConstrainedBean extends TestCase {
016: public TestConstrainedBean(String name) {
017: super (name);
018: }
019:
020: public void testInstantiation() {
021: ConstrainedBean bean = new ConstrainedBean();
022: assertNull(bean.getAssociations());
023: assertFalse(bean.hasAssociations());
024: assertNull(bean.getUniques());
025: assertFalse(bean.hasUniques());
026: assertNull(bean.getTextualIdentifier());
027: assertFalse(bean.hasTextualIdentifier());
028: assertNull(bean.getDefaultOrdering());
029: assertFalse(bean.hasDefaultOrdering());
030: }
031:
032: public void testAssociations() {
033: Class[] associations = new Class[] { RegularBeanImpl.class,
034: InitializedBeanImpl.class };
035:
036: ConstrainedBean bean = new ConstrainedBean();
037: assertSame(bean, bean.associations(associations));
038: assertTrue(bean.hasAssociations());
039: assertSame(associations, bean.getAssociations());
040:
041: assertSame(bean, bean.associations((Class[]) null));
042: assertFalse(bean.hasAssociations());
043: assertNull(bean.getAssociations());
044:
045: associations = new Class[0];
046: assertSame(bean, bean.associations(associations));
047: assertFalse(bean.hasAssociations());
048: assertSame(associations, bean.getAssociations());
049: }
050:
051: public void testUniques() {
052: ConstrainedBean bean = new ConstrainedBean();
053: assertSame(bean, bean.unique());
054: assertTrue(bean.hasUniques());
055: assertNotNull(bean.getUniques());
056: assertEquals(1, bean.getUniques().size());
057: assertEquals(0, ((String[]) bean.getUniques().get(0)).length);
058:
059: assertSame(bean, bean.uniques(null));
060: assertFalse(bean.hasUniques());
061: assertNull(bean.getUniques());
062:
063: final String[] first_unique = new String[] { "one", "two",
064: "three" };
065: final String[] second_unique = new String[] { "one2", "two2",
066: "three2", "four2" };
067: final String[] third_unique = new String[] { "one3", "two3" };
068:
069: assertSame(bean, bean.unique(first_unique));
070: assertTrue(bean.hasUniques());
071: assertNotNull(bean.getUniques());
072: assertEquals(1, bean.getUniques().size());
073: assertSame(first_unique, bean.getUniques().get(0));
074:
075: assertSame(bean, bean.unique(second_unique));
076: assertTrue(bean.hasUniques());
077: assertEquals(2, bean.getUniques().size());
078: assertSame(first_unique, bean.getUniques().get(0));
079: assertSame(second_unique, bean.getUniques().get(1));
080:
081: assertSame(bean, bean.unique(third_unique));
082: assertTrue(bean.hasUniques());
083: assertEquals(3, bean.getUniques().size());
084: assertSame(first_unique, bean.getUniques().get(0));
085: assertSame(second_unique, bean.getUniques().get(1));
086: assertSame(third_unique, bean.getUniques().get(2));
087:
088: assertSame(bean, bean.uniques(null));
089: assertFalse(bean.hasUniques());
090: assertNull(bean.getUniques());
091:
092: assertSame(bean, bean.uniques(new ArrayList<String[]>() {
093: {
094: add(first_unique);
095: add(second_unique);
096: add(third_unique);
097: }
098: }));
099: assertTrue(bean.hasUniques());
100: assertEquals(3, bean.getUniques().size());
101: assertSame(first_unique, bean.getUniques().get(0));
102: assertSame(second_unique, bean.getUniques().get(1));
103: assertSame(third_unique, bean.getUniques().get(2));
104: }
105:
106: public void testTextualIdentifier() {
107: TextualIdentifierGenerator<InitializedBeanImpl> identifier = new TextualIdentifierGenerator<InitializedBeanImpl>() {
108: public void setBean(InitializedBeanImpl bean) {
109: }
110:
111: public String generateIdentifier() {
112: return null;
113: }
114: };
115:
116: ConstrainedBean bean = new ConstrainedBean();
117: assertSame(bean, bean.textualIdentifier(identifier));
118: assertTrue(bean.hasTextualIdentifier());
119: assertSame(identifier, bean.getTextualIdentifier());
120:
121: assertSame(bean, bean.textualIdentifier(null));
122: assertFalse(bean.hasTextualIdentifier());
123: assertNull(bean.getTextualIdentifier());
124: }
125:
126: public void testDefaultOrdering() {
127: ConstrainedBean bean = new ConstrainedBean();
128: assertSame(bean, bean.defaultOrder("col1"));
129: assertTrue(bean.hasDefaultOrdering());
130: assertEquals(1, bean.getDefaultOrdering().size());
131: assertEquals("col1", ((ConstrainedBean.Order) bean
132: .getDefaultOrdering().get(0)).getPropertyName());
133: assertSame(ConstrainedBean.ASC, ((ConstrainedBean.Order) bean
134: .getDefaultOrdering().get(0)).getDirection());
135: assertSame(bean, bean
136: .defaultOrder("col2", ConstrainedBean.DESC));
137: assertTrue(bean.hasDefaultOrdering());
138: assertEquals(2, bean.getDefaultOrdering().size());
139: assertEquals("col2", ((ConstrainedBean.Order) bean
140: .getDefaultOrdering().get(1)).getPropertyName());
141: assertSame(ConstrainedBean.DESC, ((ConstrainedBean.Order) bean
142: .getDefaultOrdering().get(1)).getDirection());
143:
144: assertSame(bean, bean.defaultOrdering(null));
145: assertFalse(bean.hasDefaultOrdering());
146: assertNull(bean.getDefaultOrdering());
147:
148: List<ConstrainedBean.Order> ordering = new ArrayList<ConstrainedBean.Order>();
149: ordering.add(new ConstrainedBean.Order("col3",
150: ConstrainedBean.DESC));
151: ordering.add(new ConstrainedBean.Order("col4",
152: ConstrainedBean.ASC));
153: ordering.add(new ConstrainedBean.Order("col5",
154: ConstrainedBean.DESC));
155: assertSame(bean, bean.defaultOrdering(ordering));
156: assertTrue(bean.hasDefaultOrdering());
157: assertEquals(3, bean.getDefaultOrdering().size());
158: assertEquals("col3", ((ConstrainedBean.Order) bean
159: .getDefaultOrdering().get(0)).getPropertyName());
160: assertSame(ConstrainedBean.DESC, ((ConstrainedBean.Order) bean
161: .getDefaultOrdering().get(0)).getDirection());
162: assertEquals("col4", ((ConstrainedBean.Order) bean
163: .getDefaultOrdering().get(1)).getPropertyName());
164: assertSame(ConstrainedBean.ASC, ((ConstrainedBean.Order) bean
165: .getDefaultOrdering().get(1)).getDirection());
166: assertEquals("col5", ((ConstrainedBean.Order) bean
167: .getDefaultOrdering().get(2)).getPropertyName());
168: assertSame(ConstrainedBean.DESC, ((ConstrainedBean.Order) bean
169: .getDefaultOrdering().get(2)).getDirection());
170: }
171: }
|