You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/12/07 11:34:22 UTC

(camel) 01/01: CAMEL-20204: getLength on StringSource/StreamSourceCache to not use super.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch stringsource
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 2aa2d5757c2fcda1472214b33d4720e0981dced3
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Dec 7 12:34:07 2023 +0100

    CAMEL-20204: getLength on StringSource/StreamSourceCache to not use super.
---
 .../converter/stream/StreamSourceCacheTest.java    | 23 ++++++++++++++
 .../apache/camel/util/xml/StreamSourceCache.java   |  5 +++
 .../org/apache/camel/util/xml/StringSource.java    |  5 +++
 .../apache/camel/util/xml/StringSourceTest.java}   | 37 ++++++++++------------
 4 files changed, 50 insertions(+), 20 deletions(-)

diff --git a/core/camel-core/src/test/java/org/apache/camel/converter/stream/StreamSourceCacheTest.java b/core/camel-core/src/test/java/org/apache/camel/converter/stream/StreamSourceCacheTest.java
index 95b84db1104..fbc2a7a64ac 100644
--- a/core/camel-core/src/test/java/org/apache/camel/converter/stream/StreamSourceCacheTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/converter/stream/StreamSourceCacheTest.java
@@ -24,6 +24,7 @@ import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
 import org.apache.camel.support.DefaultExchange;
 import org.apache.camel.util.xml.StreamSourceCache;
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -39,8 +40,30 @@ public class StreamSourceCacheTest extends ContextTestSupport {
 
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         cache.writeTo(bos);
+        String s = context.getTypeConverter().convertTo(String.class, bos);
+        assertEquals("<foo>bar</foo>", s);
+    }
 
+    @Test
+    public void testStreamSourceCacheIsEmpty() throws Exception {
+        Exchange exchange = new DefaultExchange(context);
+
+        StreamSource source = context.getTypeConverter().convertTo(StreamSource.class, "");
+        StreamSourceCache cache = new StreamSourceCache(source, exchange);
+        Assertions.assertTrue(cache.isEmpty());
+
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        cache.writeTo(bos);
         String s = context.getTypeConverter().convertTo(String.class, bos);
+        assertEquals("", s);
+
+        source = context.getTypeConverter().convertTo(StreamSource.class, "<foo>bar</foo>");
+        cache = new StreamSourceCache(source, exchange);
+        Assertions.assertFalse(cache.isEmpty());
+
+        bos = new ByteArrayOutputStream();
+        cache.writeTo(bos);
+        s = context.getTypeConverter().convertTo(String.class, bos);
         assertEquals("<foo>bar</foo>", s);
     }
 
diff --git a/core/camel-xml-jaxp/src/main/java/org/apache/camel/util/xml/StreamSourceCache.java b/core/camel-xml-jaxp/src/main/java/org/apache/camel/util/xml/StreamSourceCache.java
index f38b84dccf8..542f60624d6 100644
--- a/core/camel-xml-jaxp/src/main/java/org/apache/camel/util/xml/StreamSourceCache.java
+++ b/core/camel-xml-jaxp/src/main/java/org/apache/camel/util/xml/StreamSourceCache.java
@@ -131,4 +131,9 @@ public final class StreamSourceCache extends StreamSource implements StreamCache
     public long position() {
         return -1;
     }
+
+    @Override
+    public boolean isEmpty() {
+        return length() == 0;
+    }
 }
diff --git a/core/camel-xml-jaxp/src/main/java/org/apache/camel/util/xml/StringSource.java b/core/camel-xml-jaxp/src/main/java/org/apache/camel/util/xml/StringSource.java
index 125d07314b0..6659032ce95 100644
--- a/core/camel-xml-jaxp/src/main/java/org/apache/camel/util/xml/StringSource.java
+++ b/core/camel-xml-jaxp/src/main/java/org/apache/camel/util/xml/StringSource.java
@@ -62,6 +62,11 @@ public class StringSource extends StreamSource implements Externalizable {
         this.encoding = encoding;
     }
 
+    @Override
+    public boolean isEmpty() {
+        return text.isEmpty();
+    }
+
     @Override
     public InputStream getInputStream() {
         try {
diff --git a/core/camel-core/src/test/java/org/apache/camel/converter/stream/StreamSourceCacheTest.java b/core/camel-xml-jaxp/src/test/java/org/apache/camel/util/xml/StringSourceTest.java
similarity index 53%
copy from core/camel-core/src/test/java/org/apache/camel/converter/stream/StreamSourceCacheTest.java
copy to core/camel-xml-jaxp/src/test/java/org/apache/camel/util/xml/StringSourceTest.java
index 95b84db1104..9e97887c92b 100644
--- a/core/camel-core/src/test/java/org/apache/camel/converter/stream/StreamSourceCacheTest.java
+++ b/core/camel-xml-jaxp/src/test/java/org/apache/camel/util/xml/StringSourceTest.java
@@ -14,34 +14,31 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.camel.converter.stream;
+package org.apache.camel.util.xml;
 
 import java.io.ByteArrayOutputStream;
 
-import javax.xml.transform.stream.StreamSource;
-
-import org.apache.camel.ContextTestSupport;
-import org.apache.camel.Exchange;
-import org.apache.camel.support.DefaultExchange;
-import org.apache.camel.util.xml.StreamSourceCache;
+import org.apache.camel.util.IOHelper;
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
-import static org.junit.jupiter.api.Assertions.assertEquals;
-
-public class StreamSourceCacheTest extends ContextTestSupport {
+public class StringSourceTest {
 
     @Test
-    public void testStreamSourceCache() throws Exception {
-        Exchange exchange = new DefaultExchange(context);
-
-        StreamSource source = context.getTypeConverter().convertTo(StreamSource.class, "<foo>bar</foo>");
-        StreamSourceCache cache = new StreamSourceCache(source, exchange);
-
+    public void testStringSourceIsEmpty() throws Exception {
+        StringSource source = new StringSource("");
+        Assertions.assertTrue(source.isEmpty());
+        Assertions.assertEquals("", source.getText());
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        cache.writeTo(bos);
-
-        String s = context.getTypeConverter().convertTo(String.class, bos);
-        assertEquals("<foo>bar</foo>", s);
+        IOHelper.copy(source.getInputStream(), bos);
+        Assertions.assertEquals("", bos.toString());
+
+        source = new StringSource("<foo>bar</foo>");
+        Assertions.assertFalse(source.isEmpty());
+        Assertions.assertEquals("<foo>bar</foo>", source.getText());
+        bos = new ByteArrayOutputStream();
+        IOHelper.copy(source.getInputStream(), bos);
+        Assertions.assertEquals("<foo>bar</foo>", bos.toString());
     }
 
 }