001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.cocoon.forms.datatype;
019:
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.List;
023: import java.util.Locale;
024: import java.util.Map;
025:
026: import org.apache.avalon.framework.context.Context;
027: import org.apache.avalon.framework.context.DefaultContext;
028:
029: import org.apache.cocoon.components.ContextHelper;
030: import org.apache.cocoon.components.flow.FlowHelper;
031: import org.apache.cocoon.environment.ObjectModelHelper;
032: import org.apache.cocoon.environment.Request;
033: import org.apache.cocoon.environment.mock.MockRequest;
034: import org.apache.cocoon.forms.FormsConstants;
035: import org.apache.cocoon.xml.dom.DOMBuilder;
036:
037: import org.apache.excalibur.source.Source;
038: import org.apache.excalibur.source.impl.ResourceSource;
039:
040: import org.w3c.dom.Document;
041: import org.w3c.dom.Element;
042:
043: /**
044: * Test case for CForms's FlowModelSelectionList datatype.
045: * @version $Id: FlowJXPathSelectionListTestCase.java 518026 2007-03-14 05:25:54Z antonio $
046: */
047: public class FlowJXPathSelectionListTestCase extends
048: AbstractSelectionListTestCase {
049:
050: /**
051: * Test the generateSaxFragment method.
052: */
053: public void testGenerateSaxFragment() throws Exception {
054: List beans = new ArrayList(2);
055: beans.add(new TestBean("1", "One"));
056: beans.add(new TestBean("2", "Two"));
057: Map flowContextObject = new HashMap();
058: flowContextObject.put("beans", beans);
059: Request request = new MockRequest();
060: Map objectModel = new HashMap();
061: FlowHelper.setContextObject(objectModel, flowContextObject);
062: objectModel.put(ObjectModelHelper.REQUEST_OBJECT, request);
063: Map contextObjectModel = new HashMap();
064: contextObjectModel.put(ContextHelper.CONTEXT_OBJECT_MODEL,
065: objectModel);
066: Context context = new DefaultContext(contextObjectModel);
067: Source sampleSource = new ResourceSource(
068: "resource://org/apache/cocoon/forms/datatype/FlowJXPathSelectionListTestCase.source.xml");
069: Document sample = this .parser.parse(sampleSource
070: .getInputStream());
071: Element datatypeElement = (Element) sample
072: .getElementsByTagNameNS(FormsConstants.DEFINITION_NS,
073: "datatype").item(0);
074: Datatype datatype = this .datatypeManager.createDatatype(
075: datatypeElement, false);
076: FlowJXPathSelectionList list = new FlowJXPathSelectionList(
077: context, "beans", "key", "value", datatype, null,
078: false, null, false);
079: DOMBuilder dest = new DOMBuilder();
080: list.generateSaxFragment(dest, Locale.ENGLISH);
081: Source expectedSource = new ResourceSource(
082: "resource://org/apache/cocoon/forms/datatype/FlowJXPathSelectionListTestCase.dest.xml");
083: Document expected = this .parser.parse(expectedSource
084: .getInputStream());
085: Document destDocument = dest.getDocument();
086: assertEqual("Test if generated list matches expected",
087: expected, destDocument);
088: }
089:
090: /**
091: * Test the generateSaxFragment method with a list containing a null value.
092: */
093: public void testGenerateSaxFragmentWithNull() throws Exception {
094: List beans = new ArrayList(2);
095: beans.add(null);
096: beans.add(new TestBean("1", "One"));
097: beans.add(new TestBean("2", "Two"));
098: Map flowContextObject = new HashMap();
099: flowContextObject.put("beans", beans);
100: Request request = new MockRequest();
101: Map objectModel = new HashMap();
102: FlowHelper.setContextObject(objectModel, flowContextObject);
103: objectModel.put(ObjectModelHelper.REQUEST_OBJECT, request);
104: Map contextObjectModel = new HashMap();
105: contextObjectModel.put(ContextHelper.CONTEXT_OBJECT_MODEL,
106: objectModel);
107: Context context = new DefaultContext(contextObjectModel);
108: Source sampleSource = new ResourceSource(
109: "resource://org/apache/cocoon/forms/datatype/FlowJXPathSelectionListTestCase.source.xml");
110: Document sample = this .parser.parse(sampleSource
111: .getInputStream());
112: Element datatypeElement = (Element) sample
113: .getElementsByTagNameNS(FormsConstants.DEFINITION_NS,
114: "datatype").item(0);
115: Datatype datatype = this .datatypeManager.createDatatype(
116: datatypeElement, false);
117: FlowJXPathSelectionList list = new FlowJXPathSelectionList(
118: context, "beans", "key", "value", datatype, null,
119: false, null, false);
120: DOMBuilder dest = new DOMBuilder();
121: list.generateSaxFragment(dest, Locale.ENGLISH);
122: Source expectedSource = new ResourceSource(
123: "resource://org/apache/cocoon/forms/datatype/FlowJXPathSelectionListTestCaseWithNull.dest.xml");
124: Document expected = this .parser.parse(expectedSource
125: .getInputStream());
126: Document destDocument = dest.getDocument();
127: assertEqual("Test if generated list matches expected",
128: expected, destDocument);
129: }
130:
131: public static class TestBean {
132: private String key;
133: private String value;
134:
135: public TestBean(String key, String value) {
136: this .key = key;
137: this .value = value;
138: }
139:
140: public String getKey() {
141: return this .key;
142: }
143:
144: public String getValue() {
145: return this .value;
146: }
147:
148: public String toString() {
149: return "{ " + this .key + " : " + this .value + " }";
150: }
151: }
152: }
|