Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 66282200BCC for ; Tue, 29 Nov 2016 19:15:53 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 654A1160B15; Tue, 29 Nov 2016 18:15:53 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 89E9F160AFC for ; Tue, 29 Nov 2016 19:15:52 +0100 (CET) Received: (qmail 27379 invoked by uid 500); 29 Nov 2016 18:15:51 -0000 Mailing-List: contact commits-help@activemq.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@activemq.apache.org Delivered-To: mailing list commits@activemq.apache.org Received: (qmail 27369 invoked by uid 99); 29 Nov 2016 18:15:51 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 29 Nov 2016 18:15:51 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id E6D3DE01F4; Tue, 29 Nov 2016 18:15:50 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: davsclaus@apache.org To: commits@activemq.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: activemq git commit: fix for: --msgsel for the purge command not working as described. This fixes #108. Thanks to Frank Dietrich for the patch. Date: Tue, 29 Nov 2016 18:15:50 +0000 (UTC) archived-at: Tue, 29 Nov 2016 18:15:53 -0000 Repository: activemq Updated Branches: refs/heads/master 78492febc -> a5c8bcb10 fix for: --msgsel for the purge command not working as described. This fixes #108. Thanks to Frank Dietrich for the patch. Project: http://git-wip-us.apache.org/repos/asf/activemq/repo Commit: http://git-wip-us.apache.org/repos/asf/activemq/commit/a5c8bcb1 Tree: http://git-wip-us.apache.org/repos/asf/activemq/tree/a5c8bcb1 Diff: http://git-wip-us.apache.org/repos/asf/activemq/diff/a5c8bcb1 Branch: refs/heads/master Commit: a5c8bcb10973cd944f6a1fb938b119a863c51170 Parents: 78492fe Author: Claus Ibsen Authored: Tue Nov 29 19:15:43 2016 +0100 Committer: Claus Ibsen Committed: Tue Nov 29 19:15:43 2016 +0100 ---------------------------------------------------------------------- .../activemq/console/command/PurgeCommand.java | 38 +++--- .../console/command/PurgeCommandTest.java | 124 +++++++++++++++++++ 2 files changed, 143 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/activemq/blob/a5c8bcb1/activemq-console/src/main/java/org/apache/activemq/console/command/PurgeCommand.java ---------------------------------------------------------------------- diff --git a/activemq-console/src/main/java/org/apache/activemq/console/command/PurgeCommand.java b/activemq-console/src/main/java/org/apache/activemq/console/command/PurgeCommand.java index dd841ae..4974925 100644 --- a/activemq-console/src/main/java/org/apache/activemq/console/command/PurgeCommand.java +++ b/activemq-console/src/main/java/org/apache/activemq/console/command/PurgeCommand.java @@ -20,7 +20,6 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.StringTokenizer; - import javax.management.MBeanServerInvocationHandler; import javax.management.ObjectInstance; import javax.management.ObjectName; @@ -118,15 +117,10 @@ public class PurgeCommand extends AbstractJmxCommand { // We then need to construct the SQL-92 query out of // this list. - String sqlQuery = null; - if (queryAddObjects.size() > 1) { - sqlQuery = convertToSQL92(queryAddObjects); - } else { - sqlQuery = queryAddObjects.get(0); - } + String sqlQuery = convertToSQL92(queryAddObjects); removed = proxy.removeMatchingMessages(sqlQuery); context.printInfo("Removed: " + removed - + " messages for message selector " + sqlQuery.toString()); + + " messages for message selector " + sqlQuery); if (resetStatistics) { proxy.resetStatistics(); @@ -208,22 +202,28 @@ public class PurgeCommand extends AbstractJmxCommand { * @return SQL-92 string of that query. */ public String convertToSQL92(List tokens) { - String selector = ""; + StringBuilder selector = new StringBuilder(); - // Convert to message selector + boolean isFirstToken = true; for (Iterator i = tokens.iterator(); i.hasNext(); ) { - selector = selector + "(" + i.next().toString() + ") AND "; - } - - // Remove last AND and replace '*' with '%' - if (!selector.equals("")) { - selector = selector.substring(0, selector.length() - 5); - selector = selector.replace('*', '%'); + String token = i.next().toString(); + if (token.matches("^[^=]*='.*[\\*\\?].*'$")) { + token = token.replace('?', '_') + .replace('*', '%') + .replaceFirst("=", " LIKE "); + } + if (isFirstToken) { + isFirstToken = false; + } else { + selector.append(" AND "); + } + selector.append('(') + .append(token) + .append(')'); } - return selector; + return selector.toString(); } - /** * Print the help messages for the browse command */ http://git-wip-us.apache.org/repos/asf/activemq/blob/a5c8bcb1/activemq-console/src/test/java/org/apache/activemq/console/command/PurgeCommandTest.java ---------------------------------------------------------------------- diff --git a/activemq-console/src/test/java/org/apache/activemq/console/command/PurgeCommandTest.java b/activemq-console/src/test/java/org/apache/activemq/console/command/PurgeCommandTest.java new file mode 100644 index 0000000..1779cdb --- /dev/null +++ b/activemq-console/src/test/java/org/apache/activemq/console/command/PurgeCommandTest.java @@ -0,0 +1,124 @@ +/** + * 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.activemq.console.command; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +@RunWith(Parameterized.class) +public class PurgeCommandTest { + + private final List datum; + private final String expected; + + /** + * Produces the data for the test. + * + * @return + */ + @Parameters(name = "{index}: convertToSQL92({0})={1}") + public static Collection produceTestData() { + List params = new ArrayList<>(); + // wildcard query enclosed by single quotes must be converted into + // SQL92 LIKE-statement + params.add(toParameterArray( + "(JMSMessageId LIKE '%:10_')", + "JMSMessageId='*:10?'") + ); + + // query parameter containing wildcard characters but not enclosed by + // single quotes must be taken as literal + params.add(toParameterArray( + "(JMSMessageId=*:10?)", + "JMSMessageId=*:10?") + ); + params.add(toParameterArray( + "(JMSMessageId=%:10_)", + "JMSMessageId=%:10_") + ); + + // query parameter not enclosed by single quotes must be taken as literal + params.add(toParameterArray( + "(JMSMessageId=SOME_ID)", + "JMSMessageId=SOME_ID") + ); + + // query parameter not containing wildcard characters but enclosed by + // single quotes must not be converted into a SQL92 LIKE-statement + params.add(toParameterArray( + "(JMSMessageId='SOME_ID')", + "JMSMessageId='SOME_ID'") + ); + params.add(toParameterArray( + "(JMSMessageId='%:10_')", + "JMSMessageId='%:10_'") + ); + + // multiple query parameter must be concatenated by 'AND' + params.add(toParameterArray( + "(JMSMessageId LIKE '%:10_') AND (JMSPriority>5)", + "JMSMessageId='*:10?'", "JMSPriority>5") + ); + params.add(toParameterArray( + "(JMSPriority>5) AND (JMSMessageId LIKE '%:10_')", + "JMSPriority>5", "JMSMessageId='*:10?'") + ); + + // a query which is already in SQL92 syntax should not be altered + params.add(toParameterArray( + "((JMSPriority>5) AND (JMSMessageId LIKE '%:10_'))", + "(JMSPriority>5) AND (JMSMessageId LIKE '%:10_')") + ); + return params; + } + + /** + * Test if the wildcard queries correctly converted into a valid SQL92 + * statement. + */ + @Test + public void testConvertToSQL92() { + System.out.print("testTokens = " + datum); + System.out.println(" output = " + expected); + PurgeCommand pc = new PurgeCommand(); + Assert.assertEquals(expected, pc.convertToSQL92(datum)); + } + + /** + * Convert the passed parameter into an object array which is used for + * the unit tests of method convertToSQL92. + * + * @param datum the tokens which are passed as list to the method + * @param expected the expected value returned by the method + * @return object array with the values used for the unit test + */ + static Object[] toParameterArray(String expected, String... tokens) { + return new Object[]{Arrays.asList(tokens), expected}; + } + + public PurgeCommandTest(List datum, String expected) { + this.datum = datum; + this.expected = expected; + } +}