Author: jstrachan
Date: Mon Jun 9 09:28:43 2008
New Revision: 665748
URL: http://svn.apache.org/viewvc?rev=665748&view=rev
Log:
made StringSource externalizable along with a test case
Added:
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StringSourceTest.java
(with props)
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/StringSource.java
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/StringSource.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/StringSource.java?rev=665748&r1=665747&r2=665748&view=diff
==============================================================================
---
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/StringSource.java
(original)
+++
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/StringSource.java
Mon Jun 9 09:28:43 2008
@@ -16,25 +16,22 @@
*/
package org.apache.camel.converter.jaxp;
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
-import java.io.Reader;
-import java.io.Serializable;
-import java.io.StringReader;
-import java.io.UnsupportedEncodingException;
-
import javax.xml.transform.stream.StreamSource;
+import java.io.*;
/**
* A helper class which provides a JAXP [EMAIL PROTECTED]
javax.xml.transform.Source Source} from a String which can
* be read as many times as required.
- *
+ *
* @version $Revision$
*/
-public class StringSource extends StreamSource implements Serializable {
- private final String text;
+public class StringSource extends StreamSource implements Externalizable {
+ private String text;
private String encoding = "UTF-8";
+ public StringSource() {
+ }
+
public StringSource(String text) {
if (text == null) {
throw new NullPointerException("text can not be null");
@@ -73,4 +70,29 @@
return text;
}
+ public String getEncoding() {
+ return encoding;
+ }
+
+ public void setEncoding(String encoding) {
+ this.encoding = encoding;
+ }
+
+ public void setText(String text) {
+ this.text = text;
+ }
+
+ public void writeExternal(ObjectOutput out) throws IOException {
+ out.writeUTF(text);
+ out.writeUTF(encoding);
+ out.writeUTF(getPublicId());
+ out.writeUTF(getSystemId());
+ }
+
+ public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
+ text = in.readUTF();
+ encoding = in.readUTF();
+ setPublicId(in.readUTF());
+ setSystemId(in.readUTF());
+ }
}
Added:
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StringSourceTest.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StringSourceTest.java?rev=665748&view=auto
==============================================================================
---
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StringSourceTest.java
(added)
+++
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StringSourceTest.java
Mon Jun 9 09:28:43 2008
@@ -0,0 +1,61 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.camel.converter;
+
+import junit.framework.TestCase;
+import org.apache.camel.converter.jaxp.StringSource;
+import org.apache.camel.TypeConverter;
+import org.apache.camel.util.ReflectionInjector;
+import org.apache.camel.impl.converter.DefaultTypeConverter;
+
+import java.io.ObjectOutputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.*;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class StringSourceTest extends TestCase {
+ protected TypeConverter converter = new DefaultTypeConverter(new
ReflectionInjector());
+ protected String expectedBody = "<hello>world!</hello>";
+
+ public void testSerialization() throws Exception {
+ StringSource expected = new StringSource(expectedBody, "mySystemID",
"utf-8");
+ expected.setPublicId("myPublicId");
+
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ ObjectOutputStream output = new ObjectOutputStream(buffer);
+ output.writeObject(expected);
+ output.close();
+
+
+ ObjectInputStream in = new ObjectInputStream(new
ByteArrayInputStream(buffer.toByteArray()));
+ Object object = in.readObject();
+ assertTrue("is a StringSource", object instanceof StringSource);
+ StringSource actual = (StringSource) object;
+
+ assertEquals("source.text", expected.getPublicId(),
actual.getPublicId());
+ assertEquals("source.text", expected.getSystemId(),
actual.getSystemId());
+ assertEquals("source.text", expected.getEncoding(),
actual.getEncoding());
+ assertEquals("source.text", expected.getText(), actual.getText());
+
+ String value = converter.convertTo(String.class, actual);
+ assertEquals("text value of StringSource", expectedBody, value);
+ }
+
+}
Propchange:
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StringSourceTest.java
------------------------------------------------------------------------------
svn:eol-style = native