Changes between Initial Version and Version 1 of demo_08


Ignore:
Timestamp:
05/30/2012 09:56:06 AM (14 years ago)
Author:
qinhongyuan
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • demo_08

    v1 v1  
     1= 单元测试数据初始化--数组 = 
     2论坛权限方法测试,初始化数组数据。数据我们一般是存放到数据库的,但是单元测试并且是在测服务层,我们一般不连数据库的。[[BR]] 
     3自己简单的模拟数据出来又不是很适合,而且重用性低。这时我们可以把数据库的数据搬过来使用,以xml文件的格式[[BR]] 
     4{{{ 
     5public class UserRightServiceTest { 
     6    final static UserRightService userRightService = new UserRightService(); 
     7    private static Mockery context = new JUnit4Mockery() {{ 
     8        setImposteriser(ClassImposteriser.INSTANCE); 
     9    }}; 
     10    private static States state = context.states("test"); 
     11    static UserGroupRepository userGroupRepository; 
     12    static LockService lockService; 
     13    static UserGroup[] userGroups; 
     14 
     15        /** 
     16         * 初始化数组 
     17         * @throws DocumentException 
     18         */ 
     19    @BeforeClass 
     20    public static void setUp() throws DocumentException { 
     21                userGroupRepository = context.mock(UserGroupRepository.class); 
     22                lockService = context.mock(LockService.class); 
     23                //初始化用户组数据 
     24                userGroups = initUserGroups("bbs6_usergroup.xml", "bbs6_usergroup_item.xml");    
     25                userRightService.userGroupRepository = userGroupRepository; 
     26                userRightService.lockService = lockService; 
     27                userRightService.userGroups = userGroups; 
     28    } 
     29        public static UserGroup[] initUserGroups(String sourceFilePath, String sourceFileItemPath) throws DocumentException { 
     30 
     31                XMLParser items = new XMLParser(sourceFileItemPath); 
     32                Iterator iterItems = items.getIterator("row"); 
     33                Map<Long, Map<String, Long>> itemMap = new HashMap<Long, Map<String, Long>>(); 
     34                Element ele = null; 
     35 
     36                while (iterItems.hasNext()) { 
     37                        ele = (Element)iterItems.next(); 
     38                        long groupId = NumberUtils.toLong(ele.elementText("usergroupid")); 
     39                        if (itemMap.containsKey(groupId)) { 
     40                                itemMap.get(groupId).put(ele.elementText("name"), NumberUtils.toLong(ele.elementText("value"))); 
     41                        } else { 
     42                                Map<String, Long> tempMap = new HashMap<String, Long>(); 
     43                                tempMap.put(ele.elementText("name"), NumberUtils.toLong(ele.elementText("value"))); 
     44                                itemMap.put(groupId, tempMap); 
     45                        } 
     46                } 
     47 
     48                XMLParser iData = new XMLParser(sourceFilePath); 
     49                Iterator iter = iData.getIterator("row"); 
     50                List<UserGroup> userGroupsList = new ArrayList<UserGroup>(iData.getLength()); 
     51                 
     52                while (iter.hasNext()) { 
     53                        UserGroup ug = new UserGroup(); 
     54                        ele = (Element)iter.next(); 
     55                        long groupId = NumberUtils.toLong(ele.elementText("usergroupid")); 
     56                        ug.setUserGroupId(groupId); 
     57                        ug.setType(NumberUtils.toInt(ele.elementText("type"))); 
     58                        ug.setName(ele.elementText("name")); 
     59                        ug.setLimitMin(NumberUtils.toInt(ele.elementText("limitmin"))); 
     60                        ug.setItems(itemMap.get(groupId)); 
     61                        userGroupsList.add(ug); 
     62                } 
     63 
     64                return userGroupsList.toArray(new UserGroup[0]); 
     65        } 
     66 
     67 
     68         
     69} 
     70 
     71class XMLParser { 
     72        private Document doc = null; 
     73        public XMLParser() {} 
     74        public XMLParser(String sourceFilePath) throws DocumentException { 
     75                SAXReader reader = new SAXReader(); 
     76                doc = reader.read(UserRightServiceTest.class.getClassLoader().getResourceAsStream(sourceFilePath)); 
     77        } 
     78 
     79        public Iterator getIterator(String nodeText) { 
     80                return doc.getRootElement().elementIterator(nodeText); 
     81        } 
     82 
     83        public int getLength() { 
     84                return doc.getRootElement().elements().size(); 
     85        } 
     86} 
     87}}} 
     88- XMLParser是用来读去并解析xml文件的 
     89- initUserGroups方法是用来初始化我们需要的数据的 
     90多个测试方法都可以重用这个数组的了