/**
 * 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.activeblaze.impl.destination;

import junit.framework.TestCase;
import org.apache.activeblaze.wire.Buffer;

/**
 *
 *
 */
public class DestinationMatchTest extends TestCase {

    /**
     * Test method for DEstinationMatch
     */
    public void testIsMatch() {
        String base = "foo.fred.blah";
        byte[] data = base.getBytes();
        byte[] test = new byte[data.length + 4];
        System.arraycopy(data, 0, test, 4, data.length);
        doMatchTest(new Buffer(test, 4, data.length));
    }

    private void doMatchTest(Buffer base) {
        String match1 = "foo.fred.blah";
        String match2 = "foo.*.blah";
        String match3 = ">";
        String match4 = ">.*.blah";
        String match5 = ">.*.*";
        String match6 = "*.*.*";
        String match7 = "foo.fred.blah.>";

        String fail1 = "blah.fred.foo";
        String fail2 = "*.fred.foo";
        String fail3 = "foo.*.foo.>";

        assertTrue(DestinationMatch.isMatch(base, match1));
        assertTrue(DestinationMatch.isMatch(base, match2));
        assertTrue(DestinationMatch.isMatch(base, match3));
        assertTrue(DestinationMatch.isMatch(base, match4));
        assertTrue(DestinationMatch.isMatch(base, match5));
        assertTrue(DestinationMatch.isMatch(base, match6));
        assertTrue(DestinationMatch.isMatch(base, match7));

        assertTrue(DestinationMatch.isMatch(match1, base));
        assertTrue(DestinationMatch.isMatch(match2, base));
        assertTrue(DestinationMatch.isMatch(match3, base));
        assertTrue(DestinationMatch.isMatch(match4, base));
        assertTrue(DestinationMatch.isMatch(match5, base));
        assertTrue(DestinationMatch.isMatch(match6, base));
        assertTrue(DestinationMatch.isMatch(match7, base));

        assertFalse(DestinationMatch.isMatch(base, fail1));
        assertFalse(DestinationMatch.isMatch(base, fail2));
        assertFalse(DestinationMatch.isMatch(base, fail3));
    }
}
