/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * Licensed 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.codehaus.activemq.filter;

import org.codehaus.activemq.message.ActiveMQMessage;
import org.codehaus.activemq.test.TestSupport;

import javax.jms.Destination;
import javax.jms.JMSException;


/**
 * @version $Revision: 1.3 $
 */
public class DestinationFilterTest extends TestSupport {

    public void testNonWildcard() throws JMSException {
        Filter filter = createFilter("FOO.BAR");

        assertFilterMatches(filter, "FOO.BAR", true);
        assertFilterMatches(filter, "FOO.BA", false);
        assertFilterMatches(filter, "FOO.BAR.", false);
        assertFilterMatches(filter, "FOO.BAR.Y", false);
        assertFilterMatches(filter, "FOO", false);
    }

    public void testPrefixWildcard() throws JMSException {
        Filter filter = createFilter("FOO.>");

        assertFilterMatches(filter, "FOO.BAR", true);
        assertFilterMatches(filter, "FOO.BA", true);
        assertFilterMatches(filter, "FOO.BAR.", true);
        assertFilterMatches(filter, "FOO.BAR.Y", true);
        assertFilterMatches(filter, "FOO", false);
        assertFilterMatches(filter, "BAR.BAR", false);
    }

    public void testTwoLevelPrefixWildcard() throws JMSException {
        Filter filter = createFilter("FOO.BAR.>");

        assertFilterMatches(filter, "FOO.BAR", false);
        assertFilterMatches(filter, "FOO.BAR.FOO", true);
        assertFilterMatches(filter, "FOO.BAR.FOO.WHATNOT", true);
        assertFilterMatches(filter, "FOO.XAR.FOO", false);
    }

    public void testMatchAnythingWildcard() throws JMSException {
        Filter filter = createFilter(">");

        assertFilterMatches(filter, "FOO.BAR", true);
        assertFilterMatches(filter, "FOO.BA", true);
        assertFilterMatches(filter, "FOO.BAR.", true);
        assertFilterMatches(filter, "FOO.BAR.Y", true);
        assertFilterMatches(filter, "", true);
        assertFilterMatches(filter, "FOO", true);
        assertFilterMatches(filter, "BAR.BAR", true);
    }

    public void testWildcard() throws JMSException {
        Filter filter = createFilter("FOO.*");

        assertFilterMatches(filter, "FOO.BAR", true);
        assertFilterMatches(filter, "FOO.BA", true);
        assertFilterMatches(filter, "FOO.BAR.", false);
        assertFilterMatches(filter, "FOO.BAR.Y", false);
        assertFilterMatches(filter, "FOO", false);
        assertFilterMatches(filter, "BAR.BAR", false);
    }

    public void testDoubleWildcard() throws JMSException {
        Filter filter = createFilter("FOO.*.*");

        assertFilterMatches(filter, "FOO.BAR", false);
        assertFilterMatches(filter, "FOO.BAR.XYZ", true);
        assertFilterMatches(filter, "FOO.BAR.", true);
        assertFilterMatches(filter, "FOO.BAR.XYZ.", false);
        assertFilterMatches(filter, "FOO.BAR.XYZ.ZZZ", false);
        assertFilterMatches(filter, "FOO", false);
        assertFilterMatches(filter, "BAR.BAR", false);
    }

    public void testWildcardInMiddle() throws JMSException {
        Filter filter = createFilter("FOO.*.XYZ");

        assertFilterMatches(filter, "FOO.BAR", false);
        assertFilterMatches(filter, "FOO.BAR.XYZ", true);
        assertFilterMatches(filter, "FOO..XYZ", true);
        assertFilterMatches(filter, "FOO.WHATEVER.XYZ", true);
        assertFilterMatches(filter, "FOO.BAR.XYZ.", false);
        assertFilterMatches(filter, "FOO.BAR.XYZ.ZZZ", false);
        assertFilterMatches(filter, "FOO", false);
        assertFilterMatches(filter, "BAR.BAR", false);
    }

    public void testIntermixedWildcards() throws JMSException {
        Filter filter = createFilter("FOO.*.ABC.*");

        assertFilterMatches(filter, "FOO.BAR", false);
        assertFilterMatches(filter, "FOO.BAR.ABC", false);
        assertFilterMatches(filter, "FOO.BAR.ABC.DEF", true);
        assertFilterMatches(filter, "FOO..ABC.DEF", true);
        assertFilterMatches(filter, "FOO..ABC.", true);
        assertFilterMatches(filter, "FOO.BAR.ABC.DEF.", false);
        assertFilterMatches(filter, "FOO.BAR.ABC.DEF.X", false);
        assertFilterMatches(filter, "FOO.BAR.ABC", false);
        assertFilterMatches(filter, "FOO.BAR.XYZ.", false);
        assertFilterMatches(filter, "FOO.BAR.XYZ.ZZZ", false);
        assertFilterMatches(filter, "FOO", false);
        assertFilterMatches(filter, "BAR.BAR", false);
    }

    public void testCompositeFilter() throws JMSException {
        Filter filter = createFilter("FOO.BAR.ABC.DEF,FOO.XYZ");

        assertFilterMatches(filter, "FOO.XYZ", true);
        assertFilterMatches(filter, "FOO.BAR", false);
        assertFilterMatches(filter, "FOO.BAR.ABC", false);
        assertFilterMatches(filter, "FOO.BAR.ABC.DEF", true);
        assertFilterMatches(filter, "FOO.BAR.ABC.DEF.", false);
        assertFilterMatches(filter, "FOO.BAR.ABC.DEF.X", false);
        assertFilterMatches(filter, "FOO.BAR.ABC", false);
        assertFilterMatches(filter, "FOO.BAR.XYZ.", false);
        assertFilterMatches(filter, "FOO.BAR.XYZ.ZZZ", false);
        assertFilterMatches(filter, "FOO", false);
        assertFilterMatches(filter, "BAR.BAR", false);
    }

    protected void assertFilterMatches(Filter filter, String subject, boolean expected) throws JMSException {
        Destination destination = createDestination(subject);
        
        // lets create a Message now
        ActiveMQMessage message = createMessage();
        message.setJMSDestination(destination);

        boolean actual = filter.matches(message);
        assertEquals("Matching " + filter + " to: " + destination, expected, actual);
    }

    protected Filter createFilter(String subject) {
        return DestinationFilter.parseFilter(createDestination(subject));
    }
}
