Repository: camel Updated Branches: refs/heads/master 3af718bce -> 4c58572e6
a minor clean up and add more tests for xml tokenizer Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/4c58572e Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/4c58572e Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/4c58572e Branch: refs/heads/master Commit: 4c58572e61c5423043adadb1268f4866d66ce815 Parents: 3af718b Author: Akitoshi Yoshida <[email protected]> Authored: Thu Jun 5 13:06:29 2014 +0200 Committer: Akitoshi Yoshida <[email protected]> Committed: Thu Jun 5 13:06:29 2014 +0200 ---------------------------------------------------------------------- .../camel/support/RecordableInputStream.java | 4 + .../support/XMLTokenExpressionIterator.java | 20 +++- .../camel/support/AttributedQNameTest.java | 75 +++++++++++++ .../support/RecordableInputStreamTest.java | 105 +++++++++++++++++++ 4 files changed, 203 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/4c58572e/camel-core/src/main/java/org/apache/camel/support/RecordableInputStream.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/support/RecordableInputStream.java b/camel-core/src/main/java/org/apache/camel/support/RecordableInputStream.java index f0288fe..8ae29e6 100644 --- a/camel-core/src/main/java/org/apache/camel/support/RecordableInputStream.java +++ b/camel-core/src/main/java/org/apache/camel/support/RecordableInputStream.java @@ -84,6 +84,10 @@ class RecordableInputStream extends FilterInputStream { recording = true; } + int size() { + return buf.size(); + } + private static class TrimmableByteArrayOutputStream extends ByteArrayOutputStream { public void trim(int head, int tail) { System.arraycopy(buf, head, buf, 0, count - head - tail); http://git-wip-us.apache.org/repos/asf/camel/blob/4c58572e/camel-core/src/main/java/org/apache/camel/support/XMLTokenExpressionIterator.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/support/XMLTokenExpressionIterator.java b/camel-core/src/main/java/org/apache/camel/support/XMLTokenExpressionIterator.java index 2273e85..498fde3 100644 --- a/camel-core/src/main/java/org/apache/camel/support/XMLTokenExpressionIterator.java +++ b/camel-core/src/main/java/org/apache/camel/support/XMLTokenExpressionIterator.java @@ -520,7 +520,25 @@ public class XMLTokenExpressionIterator extends ExpressionAdapter implements Nam } } if (wc) { - lcpattern = Pattern.compile(lcp.replace(".", "\\.").replace("*", ".*").replace("?", ".")); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < lcp.length(); i++) { + char c = lcp.charAt(i); + switch (c) { + case '.': + sb.append("\\."); + break; + case '*': + sb.append(".*"); + break; + case '?': + sb.append('.'); + break; + default: + sb.append(c); + break; + } + } + lcpattern = Pattern.compile(sb.toString()); } } } http://git-wip-us.apache.org/repos/asf/camel/blob/4c58572e/camel-core/src/test/java/org/apache/camel/support/AttributedQNameTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/support/AttributedQNameTest.java b/camel-core/src/test/java/org/apache/camel/support/AttributedQNameTest.java new file mode 100644 index 0000000..49833b1 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/support/AttributedQNameTest.java @@ -0,0 +1,75 @@ +/** + * 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.support; + +import javax.xml.namespace.QName; + +import org.apache.camel.support.XMLTokenExpressionIterator.AttributedQName; +import org.junit.Assert; +import org.junit.Test; + +/** + * + */ +public class AttributedQNameTest extends Assert { + + @Test + public void testMatches() { + AttributedQName aqname = new AttributedQName("urn:foo", "petra"); + assertTrue(aqname.matches(new QName("urn:foo", "petra"))); + assertFalse(aqname.matches(new QName("urn:bar", "petra"))); + assertFalse(aqname.matches(new QName("urn:foo", "petira"))); + + aqname = new AttributedQName("urn:foo", "*tra"); + assertTrue(aqname.matches(new QName("urn:foo", "petra"))); + assertTrue(aqname.matches(new QName("urn:foo", "astra"))); + assertFalse(aqname.matches(new QName("urn:foo", "sandra"))); + + aqname = new AttributedQName("urn:foo", "pe*"); + assertTrue(aqname.matches(new QName("urn:foo", "petra"))); + assertFalse(aqname.matches(new QName("urn:foo", "astra"))); + assertTrue(aqname.matches(new QName("urn:foo", "peach"))); + assertTrue(aqname.matches(new QName("urn:foo", "peteria"))); + + aqname = new AttributedQName("urn:foo", "p*t*a"); + assertTrue(aqname.matches(new QName("urn:foo", "petra"))); + assertFalse(aqname.matches(new QName("urn:foo", "astra"))); + assertFalse(aqname.matches(new QName("urn:foo", "pesandra"))); + assertTrue(aqname.matches(new QName("urn:foo", "patricia"))); + + aqname = new AttributedQName("urn:foo", "p?t?a"); + assertTrue(aqname.matches(new QName("urn:foo", "petra"))); + assertTrue(aqname.matches(new QName("urn:foo", "patia"))); + assertFalse(aqname.matches(new QName("urn:foo", "patricia"))); + + aqname = new AttributedQName("urn:foo", "de.petra"); + assertTrue(aqname.matches(new QName("urn:foo", "de.petra"))); + + aqname = new AttributedQName("urn:foo", "de.pe*"); + assertTrue(aqname.matches(new QName("urn:foo", "de.petra"))); + assertTrue(aqname.matches(new QName("urn:foo", "de.peach"))); + assertFalse(aqname.matches(new QName("urn:foo", "delpeach"))); + + // matches any namespaces qualified and unqualified + aqname = new AttributedQName("*", "p*a"); + assertTrue(aqname.matches(new QName("urn:foo", "petra"))); + assertTrue(aqname.matches(new QName("urn:bar", "patia"))); + assertTrue(aqname.matches(new QName("", "patricia"))); + assertFalse(aqname.matches(new QName("urn:bar", "peach"))); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/4c58572e/camel-core/src/test/java/org/apache/camel/support/RecordableInputStreamTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/support/RecordableInputStreamTest.java b/camel-core/src/test/java/org/apache/camel/support/RecordableInputStreamTest.java new file mode 100644 index 0000000..9599dc1 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/support/RecordableInputStreamTest.java @@ -0,0 +1,105 @@ +/** + * 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.support; + +import java.io.ByteArrayInputStream; + +import org.junit.Assert; +import org.junit.Test; + +/** + * + */ +public class RecordableInputStreamTest extends Assert { + private static final byte[] DATA; + + static { + DATA = new byte[512]; + final int radix = 0x7f - 0x20; + for (int i = 0; i < 512; i++) { + DATA[i] = (byte) (i % radix + 0x20); + } + } + @Test + public void testReadAndGetTextsBufferPurge() throws Exception { + RecordableInputStream ris = new RecordableInputStream(new ByteArrayInputStream(DATA), "utf-8"); + assertEquals(0, ris.size()); + byte[] buf = new byte[64]; + + // 8 * 64 = 512 + for (int i = 0; i < 8; i++) { + // read in 64 bytes + int n = ris.read(buf, 0, buf.length); + assertEquals(64, n); + assertEquals(64, ris.size()); + + int offset = i * 64; + // consume the first 32 bytes + String text = ris.getText(32); + assertEquals(new String(DATA, offset, 32, "utf-8"), text); + assertEquals(32, ris.size()); + + // consume the other 32 bytes + text = ris.getText(32); + assertEquals(new String(DATA, offset + 32, 32, "utf-8"), text); + assertEquals(0, ris.size()); + + ris.record(); + } + + ris.close(); + } + @Test + public void testReadAndGetTextsAutoStopRecord() throws Exception { + RecordableInputStream ris = new RecordableInputStream(new ByteArrayInputStream(DATA), "utf-8"); + assertEquals(0, ris.size()); + byte[] buf = new byte[64]; + + // read 64 bytes + int n = ris.read(buf, 0, buf.length); + assertEquals(64, n); + assertEquals(64, ris.size()); + + // consume the 64 bytes + String text = ris.getText(64); + + assertEquals(new String(DATA, 0, 64, "utf-8"), text); + assertEquals(0, ris.size()); + + // read the next 64 bytes + n = ris.read(buf, 0, buf.length); + assertEquals(64, n); + assertEquals(0, ris.size()); + + // turn back on the recording and read the next 64 bytes + ris.record(); + n = ris.read(buf, 0, buf.length); + assertEquals(64, n); + assertEquals(64, ris.size()); + + // consume the 64 bytes + text = ris.getText(64); + + // 64 * 2 = 128 + assertEquals(new String(DATA, 128, 64, "utf-8"), text); + assertEquals(0, ris.size()); + + ris.close(); + } + +}
