码迷,mamicode.com
首页 > 编程语言 > 详细

Java实现模拟路由功能

时间:2015-08-02 13:43:30      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:java   junit   dom4j   

本文实现根据路由表适配最优的号段,选择路由器。选择路由的规则:

 1、号段num和网元都不能重复

  2、号段num不能重复

  3、选择号码适配长度最长的网元为路由目的地

RouterService.java

package com.discover.router;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 * 简易路由器
 * @author Administrator
 *
 */
public class RouterService {
    HashMap<String, String> routerInfoMap = new HashMap<String, String>();
    /**
     * 载入路由表规则:
     * 1、号段num和网元都不能重复
     * 2、号段num不能重复
     * @param routerRule 路由规则
     * @return
     */
    public boolean loadRule(String routerRule) {
        try {
            Document doc = DocumentHelper.parseText(routerRule);
            Element routertable = doc.getRootElement();
            Iterator routerIterator = routertable.elementIterator("router");
            while(routerIterator.hasNext())
            {
                Element router = (Element) routerIterator.next();
                String num = router.elementTextTrim("num");
                String net = router.elementTextTrim("net");
                if(num.isEmpty()||net.isEmpty())
                {
                    return false;
                }
                
                if(routerInfoMap.containsKey(num))
                {
                    return false;
                }
                else {
                    routerInfoMap.put(num, net);
                }
                
            }
        } catch (Exception e) {
            return false;
        }
        
        return true;
    }
    
    /**
     * 选择路由
     * 1、选择号码适配长度最长的网元为路由目的地
     * @param phone 电话号码
     * @return
     */
    public String selRouter(String phone) {
        Iterator iterator = routerInfoMap.entrySet().iterator();
        String num = "";
        String net = "";
        while (iterator.hasNext()) {
            Entry<String, String> entry = (Entry<String, String>)iterator.next();
            String key = entry.getKey();
            if(phone.indexOf(key)>-1)
            {
                if(key.length()>num.length())
                {
                    net = entry.getValue();
                }
            }
        }
        
        return net;
    }
}



采用JUnit对路由模拟器进行测试

TestRouterService.java

package com.discover.test;

import junit.framework.TestCase;

import com.discover.router.RouterService;

/**
 * @author Administrator
 *
 */
public class TestRouterService extends TestCase {
    public void testcase1() {
        RouterService routerService = new RouterService();
        String routerRule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            +"<routertable><router><num></num><net>router1</net></router></routertable>";
        assertEquals(false, routerService.loadRule(routerRule));
    }
    
    public void testcase2() {
        RouterService routerService = new RouterService();
        String routerRule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            +"<routertable><router><num>13567</num><net></net></router></routertable>";
        assertEquals(false, routerService.loadRule(routerRule));
    }
    
    public void testcase3() {
        RouterService routerService = new RouterService();
        String routerRule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            +"<routertable><router><num>13567</num><net>router1</net></router>"
        +"<router><num>13567</num><net>router2</net></router></routertable>";
        assertEquals(false, routerService.loadRule(routerRule));
    }
    
    public void testcase4() {
        RouterService routerService = new RouterService();
        String routerRule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            +"<routertable><router><num>13562</num><net>router1</net></router></routertable>";
        assertEquals(true, routerService.loadRule(routerRule));
        assertEquals("router1", routerService.selRouter("13562779908"));
    }
        
    public void testcase5() {
        RouterService routerService = new RouterService();
        String routerRule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            +"<routertable><router><num>13562</num><net>router1</net></router>"
            +"<router><num>135627</num><net>router2</net></router></routertable>";
        assertEquals(true, routerService.loadRule(routerRule));
        assertEquals("router2", routerService.selRouter("13562779908"));
    }
}


 测试结果:

技术分享

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

Java实现模拟路由功能

标签:java   junit   dom4j   

原文地址:http://blog.csdn.net/dotnetstudio/article/details/47206129

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!