RootQNameMessageSelector extends AbstractMessageSelector { @Override public boolean accept(Message message) { Document doc; try { doc = XMLUtils.parseMessagePayload(getPayloadAsString(message)); } catch (LSException e) { log.warn("Root QName message selector ignoring not well-formed XML message payload", e); return false; } if (StringUtils.hasText(rootQName.getNamespaceURI())) { return rootQName.equals(QNameUtils.getQNameForNode(doc.getFirstChild())); } else { return rootQName.getLocalPart().equals(doc.getFirstChild().getLocalName()); } } RootQNameMessageSelector(String name, String value, TestContext context); @Override boolean accept(Message message); static final String SELECTOR_ID; }
@Test public void testQNameSelector() { RootQNameMessageSelector messageSelector = new RootQNameMessageSelector(RootQNameMessageSelector.SELECTOR_ID, "Foo", context); Assert.assertTrue(messageSelector.accept(new DefaultMessage("<Foo><text>foobar</text></Foo>"))); Assert.assertTrue(messageSelector.accept(new DefaultMessage("<Foo xmlns=\"http: Assert.assertFalse(messageSelector.accept(new DefaultMessage("<Bar><text>foobar</text></Bar>"))); messageSelector = new RootQNameMessageSelector(RootQNameMessageSelector.SELECTOR_ID,"{}Foo", context); Assert.assertTrue(messageSelector.accept(new DefaultMessage("<Foo><text>foobar</text></Foo>"))); Assert.assertTrue(messageSelector.accept(new DefaultMessage("<Foo xmlns=\"http: Assert.assertFalse(messageSelector.accept(new DefaultMessage("<Bar><text>foobar</text></Bar>"))); }
@Test public void testQNameSelectorWithMessageObjectPayload() { RootQNameMessageSelector messageSelector = new RootQNameMessageSelector(RootQNameMessageSelector.SELECTOR_ID,"Foo", context); Assert.assertTrue(messageSelector.accept(new DefaultMessage(new DefaultMessage("<Foo><text>foobar</text></Foo>")))); Assert.assertTrue(messageSelector.accept(new DefaultMessage(new DefaultMessage("<Foo xmlns=\"http: Assert.assertFalse(messageSelector.accept(new DefaultMessage(new DefaultMessage("<Bar><text>foobar</text></Bar>")))); }
@Test public void testQNameWithNamespaceSelector() { RootQNameMessageSelector messageSelector = new RootQNameMessageSelector(RootQNameMessageSelector.SELECTOR_ID,"{http: Assert.assertTrue(messageSelector.accept(new DefaultMessage("<Foo xmlns=\"http: Assert.assertFalse(messageSelector.accept(new DefaultMessage("<Foo><text>foobar</text></Foo>"))); Assert.assertFalse(messageSelector.accept(new DefaultMessage("<Foo xmlns=\"http: Assert.assertFalse(messageSelector.accept(new DefaultMessage("<Bar xmlns=\"http: }
@Test public void testNonXmlPayload() { RootQNameMessageSelector messageSelector = new RootQNameMessageSelector(RootQNameMessageSelector.SELECTOR_ID,"{http: Assert.assertFalse(messageSelector.accept(new DefaultMessage("PLAINTEXT"))); }