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.deployment.service;
021:
022: import org.apache.geronimo.deployment.javabean.xbeans.BeanPropertyType;
023: import org.apache.geronimo.deployment.javabean.xbeans.JavabeanType;
024: import org.apache.geronimo.deployment.javabean.xbeans.PropertyType;
025: import org.apache.geronimo.crypto.Encryption;
026: import org.apache.geronimo.crypto.EncryptionManager;
027:
028: import com.agical.rmock.extension.junit.RMockTestCase;
029:
030: /**
031: *
032: * @version $Rev:$ $Date:$
033: */
034: public class JavaBeanXmlAttributeEditorTest extends RMockTestCase {
035:
036: private JavaBeanXmlAttributeEditor editor;
037:
038: @Override
039: protected void setUp() throws Exception {
040: editor = new JavaBeanXmlAttributeEditor(DummyJavaBean.class);
041: }
042:
043: public void testPrimitives() throws Exception {
044: DummyJavaBean bean = new DummyJavaBean();
045: bean.setBooleanValue(true);
046: bean.setByteValue((byte) 1);
047: bean.setCharValue('a');
048: bean.setDoubleValue(2);
049: bean.setFloatValue(3);
050: bean.setIntValue(4);
051: bean.setLongValue(5);
052: bean.setShortValue((short) 6);
053: bean.setString("string");
054:
055: editor.setValue(bean);
056: String result = editor.getAsText();
057:
058: JavabeanType javabeanType = JavabeanType.Factory.parse(result);
059: assertPrimitive(javabeanType, "booleanValue", "true");
060: assertPrimitive(javabeanType, "byteValue", "1");
061: assertPrimitive(javabeanType, "charValue", "a");
062: assertPrimitive(javabeanType, "doubleValue", "2.0");
063: assertPrimitive(javabeanType, "floatValue", "3.0");
064: assertPrimitive(javabeanType, "intValue", "4");
065: assertPrimitive(javabeanType, "longValue", "5");
066: assertPrimitive(javabeanType, "shortValue", "6");
067: assertPrimitive(javabeanType, "string", "string");
068: }
069:
070: private void assertPrimitive(JavabeanType javabeanType,
071: String propertyName, String value) {
072: for (PropertyType propertyType : javabeanType
073: .getPropertyArray()) {
074: if (propertyType.getName().equals(propertyName)) {
075: assertEquals(value, propertyType.getStringValue());
076: }
077: }
078: }
079:
080: /**
081: * I observed the resulting XML and it seems correct. It is weird that this test fails.
082: */
083: public void xtestNestedJavaBean() throws Exception {
084: DummyJavaBean bean = new DummyJavaBean();
085: DummyJavaBean nestedBean = new DummyJavaBean();
086: bean.setDummyJavaBean(nestedBean);
087:
088: editor.setValue(bean);
089: String result = editor.getAsText();
090:
091: JavabeanType javabeanType = JavabeanType.Factory.parse(result);
092: BeanPropertyType[] beanPropertyArray = javabeanType
093: .getBeanPropertyArray();
094: assertEquals(1, beanPropertyArray.length);
095: }
096:
097: public void testEncryption() throws Exception {
098: Encryption encryption = (Encryption) mock(Encryption.class);
099: encryption.encrypt("encryptOnPersist");
100: String encryptedValue = "encryptedOnPersist";
101: modify().returnValue(encryptedValue);
102:
103: startVerification();
104:
105: String prefix = "{Mock}";
106: EncryptionManager.setEncryptionPrefix(prefix, encryption);
107:
108: JavaBeanXmlAttributeEditor editor = new JavaBeanXmlAttributeEditor(
109: DummyJavaBean.class);
110:
111: DummyJavaBean bean = new DummyJavaBean();
112: bean.setEncryptOnPersist("encryptOnPersist");
113:
114: editor.setValue(bean);
115: String result = editor.getAsText();
116:
117: JavabeanType javabeanType = JavabeanType.Factory.parse(result);
118: assertPrimitive(javabeanType, "encryptOnPersist", prefix
119: + encryptedValue);
120: }
121:
122: }
|