2020import static org .junit .jupiter .api .Assertions .assertNotNull ;
2121import static org .junit .jupiter .api .Assertions .assertNull ;
2222
23+ import javax .jms .BytesMessage ;
2324import javax .jms .Connection ;
2425import javax .jms .JMSException ;
2526import javax .jms .Message ;
2829import javax .jms .Queue ;
2930import javax .jms .Session ;
3031import javax .jms .TextMessage ;
32+ import javax .xml .parsers .DocumentBuilder ;
33+ import javax .xml .xpath .XPath ;
34+ import javax .xml .xpath .XPathConstants ;
35+ import javax .xml .xpath .XPathFactory ;
36+ import java .io .ByteArrayInputStream ;
37+ import java .io .StringReader ;
3138import java .net .URI ;
3239
3340import org .apache .activemq .artemis .api .core .QueueConfiguration ;
3643import org .apache .activemq .artemis .core .server .ActiveMQServer ;
3744import org .apache .activemq .artemis .core .server .impl .AddressInfo ;
3845import org .apache .activemq .artemis .core .settings .impl .AddressSettings ;
46+ import org .apache .activemq .filter .XPathExpression ;
3947import org .junit .jupiter .api .Test ;
48+ import org .w3c .dom .Document ;
49+ import org .xml .sax .InputSource ;
4050
4151public class JMSXPathSelectorTest extends MultiprotocolJMSClientTestSupport {
4252
@@ -55,6 +65,9 @@ protected URI getBrokerQpidJMSConnectionURI() {
5565 protected void addConfiguration (ActiveMQServer server ) {
5666 server .getConfiguration ().setPersistenceEnabled (false );
5767 server .getAddressSettingsRepository ().addMatch (NORMAL_QUEUE_NAME , new AddressSettings ());
68+
69+ //<!-- need this for org.apache.activemq.filter.JAXPXPathEvaluator for Xpath selectors with OpenWire JMS clients-->
70+ System .setProperty ("org.apache.activemq.XPathEvaluatorClassName" , "org.apache.activemq.artemis.tests.integration.jms.multiprotocol.JMSXPathSelectorTest$JAXPXPathEvaluator" );
5871 }
5972
6073 @ Override
@@ -154,4 +167,60 @@ private void sendMessage(ConnectionSupplier producerConnectionSupplier, String q
154167 p .send (queue1 , message2 , deliveryMode , priority , timeToLive );
155168 }
156169 }
170+
171+ public static class JAXPXPathEvaluator implements XPathExpression .XPathEvaluator {
172+
173+ private static final XPathFactory FACTORY = XPathFactory .newInstance ();
174+ private final String xpathExpression ;
175+ private final DocumentBuilder builder ;
176+ private final XPath xpath = FACTORY .newXPath ();
177+
178+ public JAXPXPathEvaluator (String xpathExpression , DocumentBuilder builder ) throws Exception {
179+ this .xpathExpression = xpathExpression ;
180+ if (builder != null ) {
181+ this .builder = builder ;
182+ } else {
183+ throw new RuntimeException ("No document builder available" );
184+ }
185+ }
186+
187+ @ Override
188+ public boolean evaluate (org .apache .activemq .command .Message message ) throws JMSException {
189+ if (message instanceof TextMessage ) {
190+ String text = ((TextMessage )message ).getText ();
191+ return evaluate (text );
192+ } else if (message instanceof BytesMessage ) {
193+ BytesMessage bm = (BytesMessage )message ;
194+ byte [] data = new byte [(int )bm .getBodyLength ()];
195+ bm .readBytes (data );
196+ return evaluate (data );
197+ }
198+ return false ;
199+ }
200+
201+ private boolean evaluate (byte [] data ) {
202+ try {
203+ InputSource inputSource = new InputSource (new ByteArrayInputStream (data ));
204+ Document inputDocument = builder .parse (inputSource );
205+ return (Boolean ) xpath .evaluate (xpathExpression , inputDocument , XPathConstants .BOOLEAN );
206+ } catch (Exception e ) {
207+ return false ;
208+ }
209+ }
210+
211+ private boolean evaluate (String text ) {
212+ try {
213+ InputSource inputSource = new InputSource (new StringReader (text ));
214+ Document inputDocument = builder .parse (inputSource );
215+ return (Boolean ) xpath .evaluate (xpathExpression , inputDocument , XPathConstants .BOOLEAN );
216+ } catch (Exception e ) {
217+ return false ;
218+ }
219+ }
220+
221+ @ Override
222+ public String toString () {
223+ return xpathExpression ;
224+ }
225+ }
157226}
0 commit comments