diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/BeanUtils.java b/common/source/java/ch/systemsx/cisd/common/utilities/BeanUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..583c71a2b8758b72e3cc697df0e320f3631a4eea --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/utilities/BeanUtils.java @@ -0,0 +1,71 @@ +/* + * Copyright 2007 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.common.utilities; + +import java.beans.XMLDecoder; +import java.beans.XMLEncoder; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; + +/** + * Some utilities around <i>Java Bean</i>s. + * + * @author Christian Ribeaud + */ +public final class BeanUtils +{ + + private BeanUtils() + { + // Can not be instantiated. + } + + /** + * Encodes given object into a XML string. + * <p> + * To decode the returned XML string, you should use {@link #xmlDecode(String)}. + * </p> + */ + public final static String xmlEncode(Object object) + { + assert object != null; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(out)); + encoder.writeObject(object); + encoder.close(); + return out.toString(); + } + + /** + * This method decodes given XML string into an <code>Object</code>. + * <p> + * By using this method, we assume that the returned <code>Object</code> has been encoded using + * {@link #xmlEncode(Object)}. + * </p> + */ + public final static Object xmlDecode(String xmlString) + { + assert xmlString != null; + ByteArrayInputStream in = new ByteArrayInputStream(xmlString.getBytes()); + XMLDecoder encoder = new XMLDecoder(new BufferedInputStream(in)); + Object result = encoder.readObject(); + encoder.close(); + return result; + } +} diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/BeanUtilsTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/BeanUtilsTest.java new file mode 100644 index 0000000000000000000000000000000000000000..b2a2f7ee13c033abd5f0df5084bc83bd6ce8264d --- /dev/null +++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/BeanUtilsTest.java @@ -0,0 +1,86 @@ +/* + * Copyright 2007 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.common.utilities; + +import java.util.ArrayList; +import java.util.List; +import static org.testng.AssertJUnit.*; + +import org.testng.annotations.Test; + +/** + * Test cases for the {@link BeanUtils} class. + * + * @author Christian Ribeaud + */ +public final class BeanUtilsTest +{ + + /** + * Test method for {@link ch.systemsx.cisd.common.utilities.BeanUtils#xmlEncode(java.lang.Object)}. + */ + @Test + public final void testXmlEnDecode() + { + List<SimpleBean> list = new ArrayList<SimpleBean>(); + SimpleBean bean = new SimpleBean(); + bean.setFirstName("Tanja"); + bean.setLastName("Berg"); + list.add(bean); + bean = new SimpleBean(); + bean.setFirstName("Christian"); + bean.setLastName("Ribeaud"); + list.add(bean); + String xml = BeanUtils.xmlEncode(list); + List newList = (List) BeanUtils.xmlDecode(xml); + assert newList.size() == 2; + assertEquals(((SimpleBean)newList.get(0)).getFirstName(), "Tanja"); + assertEquals(((SimpleBean)newList.get(1)).getLastName(), "Ribeaud"); + } + + // This MUST be public. + public final static class SimpleBean + { + private String firstName; + + private String lastName; + + /** Returns <code>firstName</code>. */ + public final String getFirstName() + { + return firstName; + } + + /** Returns <code>lastName</code>. */ + public final String getLastName() + { + return lastName; + } + + /** Sets <code>firstName</code>. */ + public final void setFirstName(String firstName) + { + this.firstName = firstName; + } + + /** Sets <code>lastName</code>. */ + public final void setLastName(String lastName) + { + this.lastName = lastName; + } + } +}