2020import static org .junit .jupiter .api .Assertions .assertNotNull ;
2121import static org .junit .jupiter .api .Assertions .assertNull ;
2222
23- import javax .jms .Connection ;
24- import javax .jms .JMSException ;
25- import javax .jms .Message ;
26- import javax .jms .MessageConsumer ;
27- import javax .jms .MessageProducer ;
28- import javax .jms .Queue ;
29- import javax .jms .Session ;
30- import javax .jms .TextMessage ;
23+ import javax .jms .*;
24+ import javax .xml .parsers .DocumentBuilder ;
25+ import javax .xml .xpath .XPath ;
26+ import javax .xml .xpath .XPathConstants ;
27+ import javax .xml .xpath .XPathFactory ;
28+ import java .io .ByteArrayInputStream ;
29+ import java .io .StringReader ;
3130import java .net .URI ;
3231
3332import org .apache .activemq .artemis .api .core .QueueConfiguration ;
3635import org .apache .activemq .artemis .core .server .ActiveMQServer ;
3736import org .apache .activemq .artemis .core .server .impl .AddressInfo ;
3837import org .apache .activemq .artemis .core .settings .impl .AddressSettings ;
38+ import org .apache .activemq .filter .XPathExpression ;
3939import org .junit .jupiter .api .Test ;
40+ import org .w3c .dom .Document ;
41+ import org .xml .sax .InputSource ;
4042
4143public class JMSXPathSelectorTest extends MultiprotocolJMSClientTestSupport {
4244
@@ -55,6 +57,9 @@ protected URI getBrokerQpidJMSConnectionURI() {
5557 protected void addConfiguration (ActiveMQServer server ) {
5658 server .getConfiguration ().setPersistenceEnabled (false );
5759 server .getAddressSettingsRepository ().addMatch (NORMAL_QUEUE_NAME , new AddressSettings ());
60+
61+ //<!-- need this for org.apache.activemq.filter.JAXPXPathEvaluator for Xpath selectors with OpenWire JMS clients-->
62+ System .setProperty ("org.apache.activemq.XPathEvaluatorClassName" , "org.apache.activemq.artemis.tests.integration.jms.multiprotocol.JMSXPathSelectorTest$JAXPXPathEvaluator" );
5863 }
5964
6065 @ Override
@@ -154,4 +159,60 @@ private void sendMessage(ConnectionSupplier producerConnectionSupplier, String q
154159 p .send (queue1 , message2 , deliveryMode , priority , timeToLive );
155160 }
156161 }
162+
163+ public static class JAXPXPathEvaluator implements XPathExpression .XPathEvaluator {
164+
165+ private static final XPathFactory FACTORY = XPathFactory .newInstance ();
166+ private final String xpathExpression ;
167+ private final DocumentBuilder builder ;
168+ private final XPath xpath = FACTORY .newXPath ();
169+
170+ public JAXPXPathEvaluator (String xpathExpression , DocumentBuilder builder ) throws Exception {
171+ this .xpathExpression = xpathExpression ;
172+ if (builder != null ) {
173+ this .builder = builder ;
174+ } else {
175+ throw new RuntimeException ("No document builder available" );
176+ }
177+ }
178+
179+ @ Override
180+ public boolean evaluate (org .apache .activemq .command .Message message ) throws JMSException {
181+ if (message instanceof TextMessage ) {
182+ String text = ((TextMessage )message ).getText ();
183+ return evaluate (text );
184+ } else if (message instanceof BytesMessage ) {
185+ BytesMessage bm = (BytesMessage )message ;
186+ byte data [] = new byte [(int )bm .getBodyLength ()];
187+ bm .readBytes (data );
188+ return evaluate (data );
189+ }
190+ return false ;
191+ }
192+
193+ private boolean evaluate (byte [] data ) {
194+ try {
195+ InputSource inputSource = new InputSource (new ByteArrayInputStream (data ));
196+ Document inputDocument = builder .parse (inputSource );
197+ return (Boolean ) xpath .evaluate (xpathExpression , inputDocument , XPathConstants .BOOLEAN );
198+ } catch (Exception e ) {
199+ return false ;
200+ }
201+ }
202+
203+ private boolean evaluate (String text ) {
204+ try {
205+ InputSource inputSource = new InputSource (new StringReader (text ));
206+ Document inputDocument = builder .parse (inputSource );
207+ return (Boolean ) xpath .evaluate (xpathExpression , inputDocument , XPathConstants .BOOLEAN );
208+ } catch (Exception e ) {
209+ return false ;
210+ }
211+ }
212+
213+ @ Override
214+ public String toString () {
215+ return xpathExpression ;
216+ }
217+ }
157218}
0 commit comments