標籤: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實現類比路由功能