| | 18 | |
| | 19 | 使用Autowired注解,注入GameDAO |
| | 20 | {{{ #!java |
| | 21 | @Autowired |
| | 22 | private GameDAO gameDAO; |
| | 23 | }}} |
| | 24 | |
| | 25 | 测试代码示例如下: |
| | 26 | {{{ #!java |
| | 27 | @Autowired |
| | 28 | @Autowired |
| | 29 | private GameDAO gameDAO; |
| | 30 | |
| | 31 | private Game newGame(){ |
| | 32 | Game game = new Game(); |
| | 33 | Date now = null; |
| | 34 | try { |
| | 35 | now = DateUtils.parseDate("2011-11-16 23:23:23", new String[]{"yyyy-MM-dd HH:mm:ss"}); |
| | 36 | } catch (ParseException ex) { |
| | 37 | Logger.getLogger(GameDAOImplTest.class.getName()).log(Level.SEVERE, null, ex); |
| | 38 | } |
| | 39 | game.setCreateAt(now); |
| | 40 | game.setUpdateAt(now); |
| | 41 | game.setName("三十六计"); |
| | 42 | return game; |
| | 43 | } |
| | 44 | |
| | 45 | @Test |
| | 46 | public void testAddGame(){ |
| | 47 | long gameId = gameDAO.createGame(newGame()); |
| | 48 | Assert.assertTrue(gameId > 0); |
| | 49 | } |
| | 50 | |
| | 51 | @Test |
| | 52 | public void testFindGame(){ |
| | 53 | long gameId = gameDAO.createGame(newGame()); |
| | 54 | Game game = gameDAO.findGame(gameId); |
| | 55 | Assert.assertNotNull(game); |
| | 56 | } |
| | 57 | |
| | 58 | @Test |
| | 59 | public void testUpdateGame(){ |
| | 60 | long gameId = gameDAO.createGame(newGame()); |
| | 61 | Game game = gameDAO.findGame(gameId); |
| | 62 | game.setName("三十七计"); |
| | 63 | int result = gameDAO.updateGame(game, gameId); |
| | 64 | Assert.assertEquals(1, result); |
| | 65 | } |
| | 66 | |
| | 67 | @Test |
| | 68 | public void testDelGame(){ |
| | 69 | long gameId = gameDAO.createGame(newGame()); |
| | 70 | int result = gameDAO.deleteGame(gameId); |
| | 71 | Assert.assertEquals(1, result); |
| | 72 | } |
| | 73 | |
| | 74 | @Test |
| | 75 | public void testListGameInPagination(){ |
| | 76 | |
| | 77 | int count = gameDAO.countGames(); |
| | 78 | int size = count >= 20 ? 20 : count; |
| | 79 | |
| | 80 | List<Game> list = gameDAO.listGamesInPagination(1, 20, "gameId"); |
| | 81 | Assert.assertEquals(size, list.size()); |
| | 82 | |
| | 83 | } |
| | 84 | |
| | 85 | |
| | 86 | @Test |
| | 87 | public void testEncode(){ |
| | 88 | |
| | 89 | @SuppressWarnings("unchecked") |
| | 90 | AbstractRepository<Game> repository = (AbstractRepository<Game>)gameDAO; |
| | 91 | String value = repository.encode(newGame()); |
| | 92 | |
| | 93 | String expected = "gameId:0,name:{4}三十六计,createAt:1321457003000,updateAt:1321457003000,rate:0.0,scale:0.0,pageWidth:0,pageHeight:0,fcm:0"; |
| | 94 | Assert.assertEquals(expected, value); |
| | 95 | } |
| | 96 | |
| | 97 | @Test |
| | 98 | public void testDecode(){ |
| | 99 | @SuppressWarnings("unchecked") |
| | 100 | AbstractRepository<Game> repository = (AbstractRepository<Game>)gameDAO; |
| | 101 | String value = "gameId:0,name:{4}三十六计,createAt:1321457003000,updateAt:1321457003000,rate:0.0,scale:0.0,pageWidth:0,pageHeight:0,fcm:0"; |
| | 102 | Game game = repository.decode(value); |
| | 103 | |
| | 104 | Assert.assertEquals("三十六计", game.getName()); |
| | 105 | |
| | 106 | DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | 107 | Assert.assertEquals("2011-11-16 23:23:23", f.format(game.getCreateAt())); |
| | 108 | } |
| | 109 | |
| | 110 | @Test |
| | 111 | public void countGames(){ |
| | 112 | |
| | 113 | gameDAO.createGame(newGame()); |
| | 114 | gameDAO.createGame(newGame()); |
| | 115 | gameDAO.createGame(newGame()); |
| | 116 | |
| | 117 | int count = gameDAO.countGames(); |
| | 118 | |
| | 119 | Assert.assertTrue(count >= 3); |
| | 120 | } |
| | 121 | }}} |