001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.geronimo.system.configuration;
021:
022: import java.beans.PropertyEditorSupport;
023: import java.util.Collection;
024: import java.util.Collections;
025:
026: import junit.framework.TestCase;
027:
028: import org.apache.geronimo.system.configuration.condition.JexlExpressionParser;
029: import org.apache.geronimo.system.plugin.model.AttributeType;
030: import org.apache.geronimo.system.plugin.model.GbeanType;
031:
032: /**
033: *
034: * @version $Rev:$ $Date:$
035: */
036: public class GBeanOverrideTest extends TestCase {
037:
038: private GbeanType gbeanType;
039: private String attributeName;
040:
041: @Override
042: protected void setUp() throws Exception {
043: gbeanType = new GbeanType();
044: gbeanType.setName("name");
045:
046: attributeName = "attName";
047: }
048:
049: public void testPropertyEditorIsCarriedByWriteXml()
050: throws Exception {
051: AttributeType attributeType = new AttributeType();
052: gbeanType.getAttributeOrReference().add(attributeType);
053: attributeType.setName(attributeName);
054: attributeType.getContent().add("value");
055: attributeType.setPropertyEditor("myPropertyEditor");
056:
057: GBeanOverride override = new GBeanOverride(gbeanType,
058: new JexlExpressionParser());
059: GbeanType copiedGBeanType = override.writeXml();
060: assertEquals(1, copiedGBeanType.getAttributeOrReference()
061: .size());
062: AttributeType copiedAttributeType = (AttributeType) copiedGBeanType
063: .getAttributeOrReference().get(0);
064: assertEquals(attributeType.getPropertyEditor(),
065: copiedAttributeType.getPropertyEditor());
066: }
067:
068: public void testPropertyEditorIsUsedToGetTextValue()
069: throws Exception {
070: GBeanOverride override = new GBeanOverride(gbeanType,
071: new JexlExpressionParser());
072: override.setAttribute(attributeName, new Bean(), Bean.class
073: .getName(), getClass().getClassLoader());
074:
075: assertEquals("bean", override.getAttribute(attributeName));
076:
077: GbeanType copiedGBeanType = override.writeXml();
078: assertEquals(1, copiedGBeanType.getAttributeOrReference()
079: .size());
080: AttributeType attributeType = (AttributeType) copiedGBeanType
081: .getAttributeOrReference().get(0);
082: assertEquals("bean", attributeType.getContent().get(0));
083: }
084:
085: public void testPropertyEditorIsDefinedWhenAttributeIsNotAPrimitiveAndItsTypeDoesNotEqualValueType()
086: throws Exception {
087: GBeanOverride override = new GBeanOverride(gbeanType,
088: new JexlExpressionParser());
089: override.setAttribute(attributeName, new Bean(), Service.class
090: .getName(), getClass().getClassLoader());
091:
092: GbeanType copiedGBeanType = override.writeXml();
093: assertEquals(1, copiedGBeanType.getAttributeOrReference()
094: .size());
095: AttributeType attributeType = (AttributeType) copiedGBeanType
096: .getAttributeOrReference().get(0);
097: assertEquals(BeanEditor.class.getName(), attributeType
098: .getPropertyEditor());
099: }
100:
101: public void testPropertyEditorIsNotDefinedWhenAttributeTypeEqualsValueType()
102: throws Exception {
103: GBeanOverride override = new GBeanOverride(gbeanType,
104: new JexlExpressionParser());
105: override.setAttribute(attributeName, new Bean(), Bean.class
106: .getName(), getClass().getClassLoader());
107:
108: GbeanType copiedGBeanType = override.writeXml();
109: assertEquals(1, copiedGBeanType.getAttributeOrReference()
110: .size());
111: AttributeType attributeType = (AttributeType) copiedGBeanType
112: .getAttributeOrReference().get(0);
113: assertNull(attributeType.getPropertyEditor());
114: }
115:
116: public void testPropertyEditorIsNotDefinedForPrimitives()
117: throws Exception {
118: GBeanOverride override = new GBeanOverride(gbeanType,
119: new JexlExpressionParser());
120: override.setAttribute(attributeName, new Integer(1), int.class
121: .getName(), getClass().getClassLoader());
122:
123: GbeanType copiedGBeanType = override.writeXml();
124: assertEquals(1, copiedGBeanType.getAttributeOrReference()
125: .size());
126: AttributeType attributeType = (AttributeType) copiedGBeanType
127: .getAttributeOrReference().get(0);
128: assertNull(attributeType.getPropertyEditor());
129: }
130:
131: public void testPropertyEditorIsNotDefinedForCollectionSubClasses()
132: throws Exception {
133: GBeanOverride override = new GBeanOverride(gbeanType,
134: new JexlExpressionParser());
135: override.setAttribute(attributeName, Collections
136: .singleton("test"), Collection.class.getName(),
137: getClass().getClassLoader());
138:
139: GbeanType copiedGBeanType = override.writeXml();
140: assertEquals(1, copiedGBeanType.getAttributeOrReference()
141: .size());
142: AttributeType attributeType = (AttributeType) copiedGBeanType
143: .getAttributeOrReference().get(0);
144: assertNull(attributeType.getPropertyEditor());
145: }
146:
147: public interface Service {
148: }
149:
150: public static class Bean implements Service {
151:
152: }
153:
154: public static class BeanEditor extends PropertyEditorSupport {
155:
156: @Override
157: public String getAsText() {
158: return "bean";
159: }
160:
161: @Override
162: public void setAsText(String text)
163: throws IllegalArgumentException {
164: assertEquals("bean", text);
165: }
166:
167: }
168:
169: }
|