com.tera.common.rule.PriorityRuleManagerTest.java Source code

Java tutorial

Introduction

Here is the source code for com.tera.common.rule.PriorityRuleManagerTest.java

Source

/**
 * This file is part of tera-api.
 *
 * tera-api is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
    
 * tera-api is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
    
 * You should have received a copy of the GNU General Public License
 * along with tera-api.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.tera.common.rule;

import static org.junit.Assert.fail;

import org.apache.commons.lang.StringUtils;
import org.junit.Assert;
import org.junit.Test;

/**
 * @author ATracer
 * @since 0.0.6
 */
public class PriorityRuleManagerTest {
    @Test
    public void testAddRemoveRule() {
        Rule<String, String> rule1 = createRule(RulePriority.MEDIUM);
        Rule<String, String> rule2 = createRule(RulePriority.LOW);
        RuleManagerTests.testAddRemoveRule(new PriorityRuleManager<String, String>(), rule1, rule2);
    }

    @Test
    public void testFirstLastPriorities() {
        PriorityRuleManager<String, String> rm = new PriorityRuleManager<String, String>();
        Rule<String, String> rule1 = createRule(RulePriority.FIRST);
        rm.addRule(rule1);
        try {
            rm.addRule(rule1);
            fail("Duplicate first rule");
        } catch (Exception ex) {
        }

        Rule<String, String> rule2 = createRule(RulePriority.LAST);
        rm.addRule(rule2);
        try {
            rm.addRule(rule2);
            fail("Duplicate last rule");
        } catch (Exception ex) {
        }
    }

    @Test
    public void testOrdertPriorities() {

        PriorityRuleManager<String, String> rm = new PriorityRuleManager<String, String>();
        Rule<String, String> rule1 = createRule("first", RulePriority.FIRST);
        Rule<String, String> rule2 = createRule(RulePriority.HIGH);
        Rule<String, String> rule3 = createRule(RulePriority.MEDIUM);
        Rule<String, String> rule4 = createRule(RulePriority.LOW);
        Rule<String, String> rule5 = createRule(RulePriority.LAST);

        rm.addRule(rule4);
        rm.addRule(rule2);
        rm.addRule(rule5);
        rm.addRule(rule3);
        rm.addRule(rule1);

        Assert.assertNotNull(rm.calculate(StringUtils.EMPTY));

        PriorityRuleManager<String, String> rm2 = new PriorityRuleManager<String, String>();
        rule1 = createRule(RulePriority.FIRST);
        rule2 = createRule("high", RulePriority.HIGH);
        rule3 = createRule(RulePriority.MEDIUM);
        rule4 = createRule(RulePriority.LOW);
        rule5 = createRule(RulePriority.LAST);

        rm2.addRule(rule4);
        rm2.addRule(rule2);
        rm2.addRule(rule5);
        rm2.addRule(rule3);
        rm2.addRule(rule1);

        Assert.assertNotNull(rm2.calculate(StringUtils.EMPTY));
        Assert.assertEquals("high", rm2.calculate(StringUtils.EMPTY));

        PriorityRuleManager<String, String> rm3 = new PriorityRuleManager<String, String>();
        rule1 = createRule(RulePriority.FIRST);
        rule2 = createRule("high", RulePriority.HIGH);
        rule3 = createRule("medium", RulePriority.MEDIUM);
        rule4 = createRule(RulePriority.LOW);
        rule5 = createRule(RulePriority.LAST);

        rm3.addRule(rule4);
        rm3.addRule(rule5);
        rm3.addRule(rule3);// added first medium
        rm3.addRule(rule2);// added second high
        rm3.addRule(rule1);

        Assert.assertNotNull(rm3.calculate(StringUtils.EMPTY));
        Assert.assertEquals("high", rm3.calculate(StringUtils.EMPTY));
    }

    /**
     * @param priority
     * @return
     */
    private Rule<String, String> createRule(final RulePriority priority) {
        return new PrioritizedRule<String, String>() {
            @Override
            public boolean validate(String param) {
                return true;
            }

            @Override
            public String getOutput() {
                return null;
            }

            @Override
            public RulePriority getPriority() {
                return priority;
            }
        };
    }

    /**
     * Create rules that always return output
     * 
     * @param output
     * @param priority
     * @return
     */
    private Rule<String, String> createRule(final String output, final RulePriority priority) {
        return new PrioritizedRule<String, String>() {
            @Override
            public boolean validate(String param) {
                return false;
            }

            @Override
            public String getOutput() {
                return output;
            }

            @Override
            public RulePriority getPriority() {
                return priority;
            }
        };
    }
}