java虚拟路由器_Java实现模拟路由功能

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

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 routerInfoMap = new HashMap();

/**

* 载入路由表规则:

* 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 entry = (Entry)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\"?>"

+"router1";

assertEquals(false, routerService.loadRule(routerRule));

}

public void testcase2() {

RouterService routerService = new RouterService();

String routerRule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"

+"13567";

assertEquals(false, routerService.loadRule(routerRule));

}

public void testcase3() {

RouterService routerService = new RouterService();

String routerRule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"

+"13567router1"

+"13567router2";

assertEquals(false, routerService.loadRule(routerRule));

}

public void testcase4() {

RouterService routerService = new RouterService();

String routerRule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"

+"13562router1";

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\"?>"

+"13562router1"

+"135627router2";

assertEquals(true, routerService.loadRule(routerRule));

assertEquals("router2", routerService.selRouter("13562779908"));

}

}

测试结果:

0818b9ca8b590ca3270a3433284dd417.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值