    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.filter;
    19
    20  import org.codehaus.activemq.TestSupport;
    21  import org.codehaus.activemq.message.ActiveMQMessage;
    22
    23  import javax.jms.Destination;
    24  import javax.jms.JMSException;
    25
    26
    27  /***
    28   * @version $Revision: 1.2 $
    29   */
    30  public class DestinationFilterTest extends TestSupport {
    31
    32      public void testNonWildcard() throws JMSException {
    33          Filter filter = createFilter("FOO.BAR");
    34
    35          assertFilterMatches(filter, "FOO.BAR", true);
    36          assertFilterMatches(filter, "FOO.BA", false);
    37          assertFilterMatches(filter, "FOO.BAR.", false);
    38          assertFilterMatches(filter, "FOO.BAR.Y", false);
    39          assertFilterMatches(filter, "FOO", false);
    40      }
    41
    42      public void testPrefixWildcard() throws JMSException {
    43          Filter filter = createFilter("FOO.>");
    44
    45          assertFilterMatches(filter, "FOO.BAR", true);
    46          assertFilterMatches(filter, "FOO.BA", true);
    47          assertFilterMatches(filter, "FOO.BAR.", true);
    48          assertFilterMatches(filter, "FOO.BAR.Y", true);
    49          assertFilterMatches(filter, "FOO", false);
    50          assertFilterMatches(filter, "BAR.BAR", false);
    51      }
    52
    53      public void testTwoLevelPrefixWildcard() throws JMSException {
    54          Filter filter = createFilter("FOO.BAR.>");
    55
    56          assertFilterMatches(filter, "FOO.BAR", false);
    57          assertFilterMatches(filter, "FOO.BAR.FOO", true);
    58          assertFilterMatches(filter, "FOO.BAR.FOO.WHATNOT", true);
    59          assertFilterMatches(filter, "FOO.XAR.FOO", false);
    60      }
    61
    62      public void testMatchAnythingWildcard() throws JMSException {
    63          Filter filter = createFilter(">");
    64
    65          assertFilterMatches(filter, "FOO.BAR", true);
    66          assertFilterMatches(filter, "FOO.BA", true);
    67          assertFilterMatches(filter, "FOO.BAR.", true);
    68          assertFilterMatches(filter, "FOO.BAR.Y", true);
    69          assertFilterMatches(filter, "", true);
    70          assertFilterMatches(filter, "FOO", true);
    71          assertFilterMatches(filter, "BAR.BAR", true);
    72      }
    73
    74      public void testWildcard() throws JMSException {
    75          Filter filter = createFilter("FOO.*");
    76
    77          assertFilterMatches(filter, "FOO.BAR", true);
    78          assertFilterMatches(filter, "FOO.BA", true);
    79          assertFilterMatches(filter, "FOO.BAR.", false);
    80          assertFilterMatches(filter, "FOO.BAR.Y", false);
    81          assertFilterMatches(filter, "FOO", false);
    82          assertFilterMatches(filter, "BAR.BAR", false);
    83      }
    84
    85      public void testDoubleWildcard() throws JMSException {
    86          Filter filter = createFilter("FOO.*.*");
    87
    88          assertFilterMatches(filter, "FOO.BAR", false);
    89          assertFilterMatches(filter, "FOO.BAR.XYZ", true);
    90          assertFilterMatches(filter, "FOO.BAR.", true);
    91          assertFilterMatches(filter, "FOO.BAR.XYZ.", false);
    92          assertFilterMatches(filter, "FOO.BAR.XYZ.ZZZ", false);
    93          assertFilterMatches(filter, "FOO", false);
    94          assertFilterMatches(filter, "BAR.BAR", false);
    95      }
    96
    97      public void testIntermixedWildcards() throws JMSException {
    98          Filter filter = createFilter("FOO.*.ABC.*");
    99
    100         assertFilterMatches(filter, "FOO.BAR", false);
    101         assertFilterMatches(filter, "FOO.BAR.ABC", false);
    102         assertFilterMatches(filter, "FOO.BAR.ABC.DEF", true);
    103         assertFilterMatches(filter, "FOO..ABC.DEF", true);
    104         assertFilterMatches(filter, "FOO..ABC.", true);
    105         assertFilterMatches(filter, "FOO.BAR.ABC.DEF.", false);
    106         assertFilterMatches(filter, "FOO.BAR.ABC.DEF.X", false);
    107         assertFilterMatches(filter, "FOO.BAR.ABC", false);
    108         assertFilterMatches(filter, "FOO.BAR.XYZ.", false);
    109         assertFilterMatches(filter, "FOO.BAR.XYZ.ZZZ", false);
    110         assertFilterMatches(filter, "FOO", false);
    111         assertFilterMatches(filter, "BAR.BAR", false);
    112     }
    113
    114     public void testCompositeFilter() throws JMSException {
    115         Filter filter = createFilter("FOO.BAR.ABC.DEF,FOO.XYZ");
    116
    117         assertFilterMatches(filter, "FOO.XYZ", true);
    118         assertFilterMatches(filter, "FOO.BAR", false);
    119         assertFilterMatches(filter, "FOO.BAR.ABC", false);
    120         assertFilterMatches(filter, "FOO.BAR.ABC.DEF", true);
    121         assertFilterMatches(filter, "FOO.BAR.ABC.DEF.", false);
    122         assertFilterMatches(filter, "FOO.BAR.ABC.DEF.X", false);
    123         assertFilterMatches(filter, "FOO.BAR.ABC", false);
    124         assertFilterMatches(filter, "FOO.BAR.XYZ.", false);
    125         assertFilterMatches(filter, "FOO.BAR.XYZ.ZZZ", false);
    126         assertFilterMatches(filter, "FOO", false);
    127         assertFilterMatches(filter, "BAR.BAR", false);
    128     }
    129
    130     protected void assertFilterMatches(Filter filter, String subject, boolean expected) throws JMSException {
    131         Destination destination = createDestination(subject);
    132
    133         // lets create a Message now
    134         ActiveMQMessage message = createMessage();
    135         message.setJMSDestination(destination);
    136
    137         boolean actual = filter.matches(message);
    138         assertEquals("Matching " + filter + " to: " + destination, expected, actual);
    139     }
    140
    141     protected Filter createFilter(String subject) {
    142         return DestinationFilter.parseFilter(createDestination(subject));
    143     }
    144 }