How to use createStrictMock method of org.powermock.api.easymock.PowerMock class

Best Powermock code snippet using org.powermock.api.easymock.PowerMock.createStrictMock

Source:ServerToClientTest.java Github

copy

Full Screen

...111 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");112 field1.setAccessible(true);113 field1.set(client, vo);114 // ftp115 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);116 Field field2 = ServerToClient.class.getDeclaredField("ftp");117 field2.setAccessible(true);118 field2.set(client, ftp);119 // mock step 1120 PowerMock.expectPrivate(client, "connect", ftp, vo).andReturn(true).times(1);121 PowerMock.replay(ServerToClient.class, client);122 // =================== Input ===================123 // =================== Process ===================124 Method method = ServerToClient.class.getDeclaredMethod("connect");125 boolean result = (Boolean) method.invoke(client);126 // =================== Output ===================127 Assert.assertEquals(true, result);128 // =================== After ===================129 PowerMock.verify(ServerToClient.class, client);130 }131 /**132 * Connection failure.133 *134 * @throws Exception135 */136 @Test137 public void testConnect002() throws Exception {138 // =================== Before ===================139 ServerToClient client = PowerMock.createPartialMock(ServerToClient.class, "connect", FTPClient.class, FTPFileInfoVO.class);140 // ftpVO141 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");142 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");143 field1.setAccessible(true);144 field1.set(client, vo);145 // ftp146 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);147 Field field2 = ServerToClient.class.getDeclaredField("ftp");148 field2.setAccessible(true);149 field2.set(client, ftp);150 // mock step 1151 PowerMock.expectPrivate(client, "connect", ftp, vo).andReturn(false).times(1);152 PowerMock.replay(ServerToClient.class, client);153 // =================== Input ===================154 // =================== Process ===================155 Method method = ServerToClient.class.getDeclaredMethod("connect");156 boolean result = (Boolean) method.invoke(client);157 // =================== Output ===================158 Assert.assertEquals(false, result);159 // =================== After ===================160 PowerMock.verify(ServerToClient.class, client);161 }162 /**163 * Login success.164 *165 * @throws Exception166 */167 @Test168 public void testLogin001() throws Exception {169 // =================== Before ===================170 ServerToClient client = new ServerToClient();171 // ftpVO172 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");173 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");174 field1.setAccessible(true);175 field1.set(client, vo);176 // ftp177 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);178 Field field2 = ServerToClient.class.getDeclaredField("ftp");179 field2.setAccessible(true);180 field2.set(client, ftp);181 // mock step 1182 EasyMock.expect(ftp.login("username", "password")).andReturn(true).times(1);183 EasyMock.replay(ftp);184 // =================== Input ===================185 // =================== Process ===================186 Method method = ServerToClient.class.getDeclaredMethod("login");187 boolean result = (Boolean) method.invoke(client);188 // =================== Output ===================189 Assert.assertEquals(true, result);190 // =================== After ===================191 EasyMock.verify(ftp);192 }193 /**194 * Login failure.195 *196 * @throws Exception197 */198 @Test199 public void testLogin002() throws Exception {200 // =================== Before ===================201 ServerToClient client = new ServerToClient();202 // ftpVO203 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");204 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");205 field1.setAccessible(true);206 field1.set(client, vo);207 // ftp208 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);209 Field field2 = ServerToClient.class.getDeclaredField("ftp");210 field2.setAccessible(true);211 field2.set(client, ftp);212 // mock step 1213 EasyMock.expect(ftp.login("username", "password")).andReturn(false).times(1);214 EasyMock.replay(ftp);215 // =================== Input ===================216 // =================== Process ===================217 Method method = ServerToClient.class.getDeclaredMethod("login");218 boolean result = (Boolean) method.invoke(client);219 // =================== Output ===================220 Assert.assertEquals(false, result);221 // =================== After ===================222 EasyMock.verify(ftp);223 }224 /**225 * Download process, when remote file not exists, failure is expected.226 *227 * @throws Exception228 */229 @Test230 public void testDoTransferDownload001() throws Exception {231 // =================== Before ===================232 ServerToClient client = new ServerToClient();233 // ftpVO234 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");235 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");236 field1.setAccessible(true);237 field1.set(client, vo);238 // localFile239 File localFile = PowerMock.createStrictMock(File.class);240 Field field2 = ServerToClient.class.getDeclaredField("localFile");241 field2.setAccessible(true);242 field2.set(client, localFile);243 // ftp244 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);245 Field field3 = ServerToClient.class.getDeclaredField("ftp");246 field3.setAccessible(true);247 field3.set(client, ftp);248 // mock step 1249 EasyMock.expect(ftp.setFileType(FTP.BINARY_FILE_TYPE)).andReturn(true).times(1);250 // mock step 2251 ftp.enterLocalPassiveMode();252 EasyMock.expectLastCall().times(1);253 // mock step 3254 FTPFile[] ftpFiles = {};255 EasyMock.expect(ftp.listFiles("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml")).andReturn(ftpFiles).times(1);256 EasyMock.replay(localFile, ftp);257 // =================== Input ===================258 // =================== Process ===================259 Method method = ServerToClient.class.getDeclaredMethod("doTransfer");260 boolean result = (Boolean) method.invoke(client);261 // =================== Output ===================262 Assert.assertEquals(false, result);263 // =================== After ===================264 EasyMock.verify(localFile, ftp);265 }266 /**267 * Download process, when remote file exists, and local file not exists, but fail to create parent directory,268 * failure is expected.269 *270 * @throws Exception271 */272 @Test273 public void testDoTransferDownload002() throws Exception {274 // =================== Before ===================275 ServerToClient client = new ServerToClient();276 // ftpVO277 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");278 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");279 field1.setAccessible(true);280 field1.set(client, vo);281 // localFile282 File localFile = PowerMock.createStrictMock(File.class);283 Field field2 = ServerToClient.class.getDeclaredField("localFile");284 field2.setAccessible(true);285 field2.set(client, localFile);286 // ftp287 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);288 Field field3 = ServerToClient.class.getDeclaredField("ftp");289 field3.setAccessible(true);290 field3.set(client, ftp);291 // mock step 1292 EasyMock.expect(ftp.setFileType(FTP.BINARY_FILE_TYPE)).andReturn(true).times(1);293 // mock step 2294 ftp.enterLocalPassiveMode();295 EasyMock.expectLastCall().times(1);296 // mock step 3297 FTPFile ftpFile = PowerMock.createStrictMock(FTPFile.class);298 FTPFile[] ftpFiles = {ftpFile};299 EasyMock.expect(ftp.listFiles("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml")).andReturn(ftpFiles).times(1);300 // mock step 4301 EasyMock.expect(localFile.exists()).andReturn(false).times(1);302 // mock step 5303 File localFileParentPath = PowerMock.createStrictMock(File.class);304 EasyMock.expect(localFile.getParentFile()).andReturn(localFileParentPath).times(1);305 EasyMock.expect(localFileParentPath.exists()).andReturn(false).times(1);306 EasyMock.expect(localFile.getParentFile()).andReturn(localFileParentPath).times(1);307 EasyMock.expect(localFileParentPath.mkdirs()).andReturn(false).times(1);308 EasyMock.replay(ftp, ftpFile, localFile, localFileParentPath);309 // =================== Input ===================310 // =================== Process ===================311 Method method = ServerToClient.class.getDeclaredMethod("doTransfer");312 boolean result = (Boolean) method.invoke(client);313 // =================== Output ===================314 Assert.assertEquals(false, result);315 // =================== After ===================316 EasyMock.verify(ftp, ftpFile, localFile, localFileParentPath);317 }318 /**319 * Download process, when remote file exists, and local file not exists, and local file parent directory exists,320 * but fail to create local file, failure is expected.321 *322 * @throws Exception323 */324 @Test325 public void testDoTransferDownload003() throws Exception {326 // =================== Before ===================327 ServerToClient client = new ServerToClient();328 // ftpVO329 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");330 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");331 field1.setAccessible(true);332 field1.set(client, vo);333 // localFile334 File localFile = PowerMock.createStrictMock(File.class);335 Field field2 = ServerToClient.class.getDeclaredField("localFile");336 field2.setAccessible(true);337 field2.set(client, localFile);338 // ftp339 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);340 Field field3 = ServerToClient.class.getDeclaredField("ftp");341 field3.setAccessible(true);342 field3.set(client, ftp);343 // mock step 1344 EasyMock.expect(ftp.setFileType(FTP.BINARY_FILE_TYPE)).andReturn(true).times(1);345 // mock step 2346 ftp.enterLocalPassiveMode();347 EasyMock.expectLastCall().times(1);348 // mock step 3349 FTPFile ftpFile = PowerMock.createStrictMock(FTPFile.class);350 FTPFile[] ftpFiles = {ftpFile};351 EasyMock.expect(ftp.listFiles("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml")).andReturn(ftpFiles).times(1);352 // mock step 4353 EasyMock.expect(localFile.exists()).andReturn(false).times(1);354 // mock step 5355 File localFileParentPath = PowerMock.createStrictMock(File.class);356 EasyMock.expect(localFile.getParentFile()).andReturn(localFileParentPath).times(1);357 EasyMock.expect(localFileParentPath.exists()).andReturn(true).times(1);358 // mock step 6359 EasyMock.expect(localFile.createNewFile()).andReturn(false).times(1);360 EasyMock.replay(ftp, ftpFile, localFile, localFileParentPath);361 // =================== Input ===================362 // =================== Process ===================363 Method method = ServerToClient.class.getDeclaredMethod("doTransfer");364 boolean result = (Boolean) method.invoke(client);365 // =================== Output ===================366 Assert.assertEquals(false, result);367 // =================== After ===================368 EasyMock.verify(ftp, ftpFile, localFile, localFileParentPath);369 }370 /**371 * Download process, when remote file exists, and local file not exists, and create local file parent directory success,372 * but fail to create local file, failure is expected.373 *374 * @throws Exception375 */376 @Test377 public void testDoTransferDownload004() throws Exception {378 // =================== Before ===================379 ServerToClient client = new ServerToClient();380 // ftpVO381 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");382 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");383 field1.setAccessible(true);384 field1.set(client, vo);385 // localFile386 File localFile = PowerMock.createStrictMock(File.class);387 Field field2 = ServerToClient.class.getDeclaredField("localFile");388 field2.setAccessible(true);389 field2.set(client, localFile);390 // ftp391 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);392 Field field3 = ServerToClient.class.getDeclaredField("ftp");393 field3.setAccessible(true);394 field3.set(client, ftp);395 // mock step 1396 EasyMock.expect(ftp.setFileType(FTP.BINARY_FILE_TYPE)).andReturn(true).times(1);397 // mock step 2398 ftp.enterLocalPassiveMode();399 EasyMock.expectLastCall().times(1);400 // mock step 3401 FTPFile ftpFile = PowerMock.createStrictMock(FTPFile.class);402 FTPFile[] ftpFiles = {ftpFile};403 EasyMock.expect(ftp.listFiles("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml")).andReturn(ftpFiles).times(1);404 // mock step 4405 EasyMock.expect(localFile.exists()).andReturn(false).times(1);406 // mock step 5407 File localFileParentPath = PowerMock.createStrictMock(File.class);408 EasyMock.expect(localFile.getParentFile()).andReturn(localFileParentPath).times(1);409 EasyMock.expect(localFileParentPath.exists()).andReturn(false).times(1);410 EasyMock.expect(localFile.getParentFile()).andReturn(localFileParentPath).times(1);411 EasyMock.expect(localFileParentPath.mkdirs()).andReturn(true).times(1);412 // mock step 6413 EasyMock.expect(localFile.createNewFile()).andReturn(false).times(1);414 EasyMock.replay(ftp, ftpFile, localFile, localFileParentPath);415 // =================== Input ===================416 // =================== Process ===================417 Method method = ServerToClient.class.getDeclaredMethod("doTransfer");418 boolean result = (Boolean) method.invoke(client);419 // =================== Output ===================420 Assert.assertEquals(false, result);421 // =================== After ===================422 EasyMock.verify(ftp, ftpFile, localFile, localFileParentPath);423 }424 /**425 * Download process, when remote file exists, and local file not exists, and create local file success,426 * but fail to retrieve remote file, failure is expected.427 *428 * @throws Exception429 */430 @Test431 @PrepareForTest({ ServerToClient.class })432 public void testDoTransferDownload005() throws Exception {433 // =================== Before ===================434 ServerToClient client = new ServerToClient();435 // ftpVO436 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");437 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");438 field1.setAccessible(true);439 field1.set(client, vo);440 // localFile441 File localFile = PowerMock.createStrictMock(File.class);442 Field field2 = ServerToClient.class.getDeclaredField("localFile");443 field2.setAccessible(true);444 field2.set(client, localFile);445 // ftp446 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);447 Field field3 = ServerToClient.class.getDeclaredField("ftp");448 field3.setAccessible(true);449 field3.set(client, ftp);450 // mock step 1451 EasyMock.expect(ftp.setFileType(FTP.BINARY_FILE_TYPE)).andReturn(true).times(1);452 // mock step 2453 ftp.enterLocalPassiveMode();454 EasyMock.expectLastCall().times(1);455 // mock step 3456 FTPFile ftpFile = PowerMock.createStrictMock(FTPFile.class);457 FTPFile[] ftpFiles = {ftpFile};458 EasyMock.expect(ftp.listFiles("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml")).andReturn(ftpFiles).times(1);459 // mock step 4460 EasyMock.expect(localFile.exists()).andReturn(false).times(1);461 // mock step 5462 File localFileParentPath = PowerMock.createStrictMock(File.class);463 EasyMock.expect(localFile.getParentFile()).andReturn(localFileParentPath).times(1);464 EasyMock.expect(localFileParentPath.exists()).andReturn(false).times(1);465 EasyMock.expect(localFile.getParentFile()).andReturn(localFileParentPath).times(1);466 EasyMock.expect(localFileParentPath.mkdirs()).andReturn(true).times(1);467 // mock step 6468 EasyMock.expect(localFile.createNewFile()).andReturn(true).times(1);469 // mock step 7470 FileOutputStream fos = PowerMock.createStrictMock(FileOutputStream.class);471 PowerMock.expectStrictNew(FileOutputStream.class, localFile, true).andReturn(fos).times(1);472 EasyMock.expect(ftp.retrieveFile("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml", fos)).andReturn(false).times(1);473 // mock step 8474 fos.close();475 EasyMock.expectLastCall().times(1);476 PowerMock.replay(ftp, ftpFile, localFile, localFileParentPath, FileOutputStream.class, fos);477 // =================== Input ===================478 // =================== Process ===================479 Method method = ServerToClient.class.getDeclaredMethod("doTransfer");480 boolean result = (Boolean) method.invoke(client);481 // =================== Output ===================482 Assert.assertEquals(false, result);483 // =================== After ===================484 PowerMock.verify(ftp, ftpFile, localFile, localFileParentPath, FileOutputStream.class, fos);485 }486 /**487 * Download process, when remote file exists, and local file not exists, and create local file success,488 * and retrieve remote file success, success is expected.489 *490 * @throws Exception491 */492 @Test493 @PrepareForTest({ ServerToClient.class })494 public void testDoTransferDownload006() throws Exception {495 // =================== Before ===================496 ServerToClient client = new ServerToClient();497 // ftpVO498 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");499 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");500 field1.setAccessible(true);501 field1.set(client, vo);502 // localFile503 File localFile = PowerMock.createStrictMock(File.class);504 Field field2 = ServerToClient.class.getDeclaredField("localFile");505 field2.setAccessible(true);506 field2.set(client, localFile);507 // ftp508 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);509 Field field3 = ServerToClient.class.getDeclaredField("ftp");510 field3.setAccessible(true);511 field3.set(client, ftp);512 // mock step 1513 EasyMock.expect(ftp.setFileType(FTP.BINARY_FILE_TYPE)).andReturn(true).times(1);514 // mock step 2515 ftp.enterLocalPassiveMode();516 EasyMock.expectLastCall().times(1);517 // mock step 3518 FTPFile ftpFile = PowerMock.createStrictMock(FTPFile.class);519 FTPFile[] ftpFiles = {ftpFile};520 EasyMock.expect(ftp.listFiles("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml")).andReturn(ftpFiles).times(1);521 // mock step 4522 EasyMock.expect(localFile.exists()).andReturn(false);523 // mock step 5524 File localFileParentPath = PowerMock.createStrictMock(File.class);525 EasyMock.expect(localFile.getParentFile()).andReturn(localFileParentPath).times(1);526 EasyMock.expect(localFileParentPath.exists()).andReturn(false).times(1);527 EasyMock.expect(localFile.getParentFile()).andReturn(localFileParentPath).times(1);528 EasyMock.expect(localFileParentPath.mkdirs()).andReturn(true).times(1);529 // mock step 6530 EasyMock.expect(localFile.createNewFile()).andReturn(true).times(1);531 // mock step 7532 FileOutputStream fos = PowerMock.createStrictMock(FileOutputStream.class);533 PowerMock.expectStrictNew(FileOutputStream.class, localFile, true).andReturn(fos).times(1);534 EasyMock.expect(ftp.retrieveFile("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml", fos)).andReturn(true).times(1);535 // mock step 8536 fos.close();537 EasyMock.expectLastCall().times(1);538 PowerMock.replay(ftp, ftpFile, localFile, localFileParentPath, FileOutputStream.class, fos);539 // =================== Input ===================540 // =================== Process ===================541 Method method = ServerToClient.class.getDeclaredMethod("doTransfer");542 boolean result = (Boolean) method.invoke(client);543 // =================== Output ===================544 Assert.assertEquals(true, result);545 // =================== After ===================546 PowerMock.verify(ftp, ftpFile, localFile, localFileParentPath, FileOutputStream.class, fos);547 }548 /**549 * Download process, when remote file exists, and local file exists, and is not resume broken transfer mode,550 * but fail to delete local file, failure is expected.551 *552 * @throws Exception553 */554 @Test555 public void testDoTransferDownload007() throws Exception {556 // =================== Before ===================557 ServerToClient client = new ServerToClient();558 client.setResumeBroken(false);559 // ftpVO560 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");561 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");562 field1.setAccessible(true);563 field1.set(client, vo);564 // localFile565 File localFile = PowerMock.createStrictMock(File.class);566 Field field2 = ServerToClient.class.getDeclaredField("localFile");567 field2.setAccessible(true);568 field2.set(client, localFile);569 // ftp570 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);571 Field field3 = ServerToClient.class.getDeclaredField("ftp");572 field3.setAccessible(true);573 field3.set(client, ftp);574 // mock step 1575 EasyMock.expect(ftp.setFileType(FTP.BINARY_FILE_TYPE)).andReturn(true).times(1);576 // mock step 2577 ftp.enterLocalPassiveMode();578 EasyMock.expectLastCall().times(1);579 // mock step 3580 FTPFile ftpFile = PowerMock.createStrictMock(FTPFile.class);581 FTPFile[] ftpFiles = {ftpFile};582 EasyMock.expect(ftp.listFiles("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml")).andReturn(ftpFiles).times(1);583 // mock step 4584 EasyMock.expect(localFile.exists()).andReturn(true).times(1);585 // mock step 5586 EasyMock.expect(localFile.delete()).andReturn(false).times(1);587 EasyMock.replay(ftp, ftpFile, localFile);588 // =================== Input ===================589 // =================== Process ===================590 Method method = ServerToClient.class.getDeclaredMethod("doTransfer");591 boolean result = (Boolean) method.invoke(client);592 // =================== Output ===================593 Assert.assertEquals(false, result);594 // =================== After ===================595 EasyMock.verify(ftp, ftpFile, localFile);596 }597 /**598 * Download process, when remote file exists, and local file exists, and is not resume broken transfer mode,599 * and delete local file success, but fail to recreate local file, failure is expected.600 *601 * @throws Exception602 */603 @Test604 public void testDoTransferDownload008() throws Exception {605 // =================== Before ===================606 ServerToClient client = new ServerToClient();607 client.setResumeBroken(false);608 // ftpVO609 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");610 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");611 field1.setAccessible(true);612 field1.set(client, vo);613 // localFile614 File localFile = PowerMock.createStrictMock(File.class);615 Field field2 = ServerToClient.class.getDeclaredField("localFile");616 field2.setAccessible(true);617 field2.set(client, localFile);618 // ftp619 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);620 Field field3 = ServerToClient.class.getDeclaredField("ftp");621 field3.setAccessible(true);622 field3.set(client, ftp);623 // mock step 1624 EasyMock.expect(ftp.setFileType(FTP.BINARY_FILE_TYPE)).andReturn(true).times(1);625 // mock step 2626 ftp.enterLocalPassiveMode();627 EasyMock.expectLastCall().times(1);628 // mock step 3629 FTPFile ftpFile = PowerMock.createStrictMock(FTPFile.class);630 FTPFile[] ftpFiles = {ftpFile};631 EasyMock.expect(ftp.listFiles("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml")).andReturn(ftpFiles).times(1);632 // mock step 4633 EasyMock.expect(localFile.exists()).andReturn(true).times(1);634 // mock step 5635 EasyMock.expect(localFile.delete()).andReturn(true).times(1);636 EasyMock.expect(localFile.createNewFile()).andReturn(false).times(1);637 EasyMock.replay(ftp, ftpFile, localFile);638 // =================== Input ===================639 // =================== Process ===================640 Method method = ServerToClient.class.getDeclaredMethod("doTransfer");641 boolean result = (Boolean) method.invoke(client);642 // =================== Output ===================643 Assert.assertEquals(false, result);644 // =================== After ===================645 EasyMock.verify(ftp, ftpFile, localFile);646 }647 /**648 * Download process, when remote file exists, and local file exists, and is not resume broken transfer mode,649 * and delete local file success, and recreate local file success, and retrieve remote file success,650 * success is expected.651 *652 * @throws Exception653 */654 @Test655 @PrepareForTest({ ServerToClient.class })656 public void testDoTransferDownload009() throws Exception {657 // =================== Before ===================658 ServerToClient client = new ServerToClient();659 client.setResumeBroken(false);660 // ftpVO661 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");662 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");663 field1.setAccessible(true);664 field1.set(client, vo);665 // localFile666 File localFile = PowerMock.createStrictMock(File.class);667 Field field2 = ServerToClient.class.getDeclaredField("localFile");668 field2.setAccessible(true);669 field2.set(client, localFile);670 // ftp671 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);672 Field field3 = ServerToClient.class.getDeclaredField("ftp");673 field3.setAccessible(true);674 field3.set(client, ftp);675 // mock step 1676 EasyMock.expect(ftp.setFileType(FTP.BINARY_FILE_TYPE)).andReturn(true).times(1);677 // mock step 2678 ftp.enterLocalPassiveMode();679 EasyMock.expectLastCall().times(1);680 // mock step 3681 FTPFile ftpFile = PowerMock.createStrictMock(FTPFile.class);682 FTPFile[] ftpFiles = {ftpFile};683 EasyMock.expect(ftp.listFiles("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml")).andReturn(ftpFiles).times(1);684 // mock step 4685 EasyMock.expect(localFile.exists()).andReturn(true).times(1);686 // mock step 5687 EasyMock.expect(localFile.delete()).andReturn(true).times(1);688 EasyMock.expect(localFile.createNewFile()).andReturn(true).times(1);689 // mock step 6690 FileOutputStream fos = PowerMock.createStrictMock(FileOutputStream.class);691 PowerMock.expectStrictNew(FileOutputStream.class, localFile, true).andReturn(fos).times(1);692 EasyMock.expect(ftp.retrieveFile("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml", fos)).andReturn(true).times(1);693 // mock step 7694 fos.close();695 EasyMock.expectLastCall().times(1);696 PowerMock.replay(ftp, ftpFile, localFile, FileOutputStream.class, fos);697 // =================== Input ===================698 // =================== Process ===================699 Method method = ServerToClient.class.getDeclaredMethod("doTransfer");700 boolean result = (Boolean) method.invoke(client);701 // =================== Output ===================702 Assert.assertEquals(true, result);703 // =================== After ===================704 PowerMock.verify(ftp, ftpFile, localFile, FileOutputStream.class, fos);705 }706 /**707 * Download process, when remote file exists, and local file exists, and is resume broken transfer mode,708 * but local file size is larger than remote file size, failure is expected.709 *710 * @throws Exception711 */712 @Test713 public void testDoTransferDownload010() throws Exception {714 // =================== Before ===================715 ServerToClient client = new ServerToClient();716 // ftpVO717 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");718 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");719 field1.setAccessible(true);720 field1.set(client, vo);721 // localFile722 File localFile = PowerMock.createStrictMock(File.class);723 Field field2 = ServerToClient.class.getDeclaredField("localFile");724 field2.setAccessible(true);725 field2.set(client, localFile);726 // ftp727 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);728 Field field3 = ServerToClient.class.getDeclaredField("ftp");729 field3.setAccessible(true);730 field3.set(client, ftp);731 // mock step 1732 EasyMock.expect(ftp.setFileType(FTP.BINARY_FILE_TYPE)).andReturn(true).times(1);733 // mock step 2734 ftp.enterLocalPassiveMode();735 EasyMock.expectLastCall().times(1);736 // mock step 3737 FTPFile ftpFile = PowerMock.createStrictMock(FTPFile.class);738 FTPFile[] ftpFiles = {ftpFile};739 EasyMock.expect(ftp.listFiles("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml")).andReturn(ftpFiles).times(1);740 // mock step 4741 EasyMock.expect(localFile.exists()).andReturn(true).times(1);742 // mock step 5743 EasyMock.expect(localFile.length()).andReturn(100L).times(1);744 EasyMock.expect(ftpFile.getSize()).andReturn(99L).times(1);745 EasyMock.replay(ftp, ftpFile, localFile);746 // =================== Input ===================747 // =================== Process ===================748 Method method = ServerToClient.class.getDeclaredMethod("doTransfer");749 boolean result = (Boolean) method.invoke(client);750 // =================== Output ===================751 Assert.assertEquals(false, result);752 // =================== After ===================753 EasyMock.verify(ftp, ftpFile, localFile);754 }755 /**756 * Download process, when remote file exists, and local file exists, and is resume broken transfer mode,757 * but local file size is equal to remote file size, failure is expected.758 *759 * @throws Exception760 */761 @Test762 public void testDoTransferDownload011() throws Exception {763 // =================== Before ===================764 ServerToClient client = new ServerToClient();765 // ftpVO766 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");767 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");768 field1.setAccessible(true);769 field1.set(client, vo);770 // localFile771 File localFile = PowerMock.createStrictMock(File.class);772 Field field2 = ServerToClient.class.getDeclaredField("localFile");773 field2.setAccessible(true);774 field2.set(client, localFile);775 // ftp776 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);777 Field field3 = ServerToClient.class.getDeclaredField("ftp");778 field3.setAccessible(true);779 field3.set(client, ftp);780 // mock step 1781 EasyMock.expect(ftp.setFileType(FTP.BINARY_FILE_TYPE)).andReturn(true).times(1);782 // mock step 2783 ftp.enterLocalPassiveMode();784 EasyMock.expectLastCall().times(1);785 // mock step 3786 FTPFile ftpFile = PowerMock.createStrictMock(FTPFile.class);787 FTPFile[] ftpFiles = {ftpFile};788 EasyMock.expect(ftp.listFiles("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml")).andReturn(ftpFiles).times(1);789 // mock step 4790 EasyMock.expect(localFile.exists()).andReturn(true).times(1);791 // mock step 5792 EasyMock.expect(localFile.length()).andReturn(100L).times(1);793 EasyMock.expect(ftpFile.getSize()).andReturn(100L).times(1);794 EasyMock.replay(ftp, ftpFile, localFile);795 // =================== Input ===================796 // =================== Process ===================797 Method method = ServerToClient.class.getDeclaredMethod("doTransfer");798 boolean result = (Boolean) method.invoke(client);799 // =================== Output ===================800 Assert.assertEquals(false, result);801 // =================== After ===================802 EasyMock.verify(ftp, ftpFile, localFile);803 }804 /**805 * Download process, when remote file exists, and local file exists, and is resume broken transfer mode,806 * and local file size is smaller than remote file size, and retrieve remote file success, success is expected.807 *808 * @throws Exception809 */810 @Test811 @PrepareForTest({ ServerToClient.class })812 public void testDoTransferDownload012() throws Exception {813 // =================== Before ===================814 ServerToClient client = new ServerToClient();815 // ftpVO816 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");817 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");818 field1.setAccessible(true);819 field1.set(client, vo);820 // localFile821 File localFile = PowerMock.createStrictMock(File.class);822 Field field2 = ServerToClient.class.getDeclaredField("localFile");823 field2.setAccessible(true);824 field2.set(client, localFile);825 // ftp826 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);827 Field field3 = ServerToClient.class.getDeclaredField("ftp");828 field3.setAccessible(true);829 field3.set(client, ftp);830 // mock step 1831 EasyMock.expect(ftp.setFileType(FTP.BINARY_FILE_TYPE)).andReturn(true).times(1);832 // mock step 2833 ftp.enterLocalPassiveMode();834 EasyMock.expectLastCall().times(1);835 // mock step 3836 FTPFile ftpFile = PowerMock.createStrictMock(FTPFile.class);837 FTPFile[] ftpFiles = {ftpFile};838 EasyMock.expect(ftp.listFiles("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml")).andReturn(ftpFiles).times(1);839 // mock step 4840 EasyMock.expect(localFile.exists()).andReturn(true).times(1);841 // mock step 5842 EasyMock.expect(localFile.length()).andReturn(99L).times(1);843 EasyMock.expect(ftpFile.getSize()).andReturn(100L).times(1);844 // mock step 6845 ftp.setRestartOffset(99L);846 EasyMock.expectLastCall().times(1);847 // mock step 7848 FileOutputStream fos = PowerMock.createStrictMock(FileOutputStream.class);849 PowerMock.expectStrictNew(FileOutputStream.class, localFile, true).andReturn(fos).times(1);850 EasyMock.expect(ftp.retrieveFile("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml", fos)).andReturn(true).times(1);851 // mock step 8852 fos.close();853 EasyMock.expectLastCall().times(1);854 PowerMock.replay(ftp, ftpFile, localFile, FileOutputStream.class, fos);855 // =================== Input ===================856 // =================== Process ===================857 Method method = ServerToClient.class.getDeclaredMethod("doTransfer");858 boolean result = (Boolean) method.invoke(client);859 // =================== Output ===================860 Assert.assertEquals(true, result);861 // =================== After ===================862 PowerMock.verify(ftp, ftpFile, localFile, FileOutputStream.class, fos);863 }864 /**865 * Upload process, when local file not exists, failure is expected.866 *867 * @throws Exception868 */869 @Test870 public void testDoTransferUpload001() throws Exception {871 // =================== Before ===================872 ServerToClient client = new ServerToClient();873 client.setTransferMode(FTPTransferMode.UPLOAD);874 // ftpVO875 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");876 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");877 field1.setAccessible(true);878 field1.set(client, vo);879 // localFile880 File localFile = PowerMock.createStrictMock(File.class);881 Field field2 = ServerToClient.class.getDeclaredField("localFile");882 field2.setAccessible(true);883 field2.set(client, localFile);884 // ftp885 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);886 Field field3 = ServerToClient.class.getDeclaredField("ftp");887 field3.setAccessible(true);888 field3.set(client, ftp);889 // mock step 1890 EasyMock.expect(ftp.setFileType(FTP.BINARY_FILE_TYPE)).andReturn(true).times(1);891 // mock step 2892 ftp.enterLocalPassiveMode();893 EasyMock.expectLastCall().times(1);894 // mock step 3895 EasyMock.expect(localFile.exists()).andReturn(false).times(1);896 EasyMock.replay(ftp, localFile);897 // =================== Input ===================898 // =================== Process ===================899 Method method = ServerToClient.class.getDeclaredMethod("doTransfer");900 boolean result = (Boolean) method.invoke(client);901 // =================== Output ===================902 Assert.assertEquals(false, result);903 // =================== After ===================904 EasyMock.verify(ftp, localFile);905 }906 /**907 * Upload process, when local file exists, and remote file not exists, but fail to create remote file parent directory,908 * failure is expected.909 *910 * @throws Exception911 */912 @Test913 public void testDoTransferUpload002() throws Exception {914 // =================== Before ===================915 ServerToClient client = PowerMock.createPartialMock(ServerToClient.class, "changeDir");916 client.setTransferMode(FTPTransferMode.UPLOAD);917 // ftpVO918 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");919 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");920 field1.setAccessible(true);921 field1.set(client, vo);922 // localFile923 File localFile = PowerMock.createStrictMock(File.class);924 Field field2 = ServerToClient.class.getDeclaredField("localFile");925 field2.setAccessible(true);926 field2.set(client, localFile);927 // ftp928 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);929 Field field3 = ServerToClient.class.getDeclaredField("ftp");930 field3.setAccessible(true);931 field3.set(client, ftp);932 // mock step 1933 EasyMock.expect(ftp.setFileType(FTP.BINARY_FILE_TYPE)).andReturn(true).times(1);934 // mock step 2935 ftp.enterLocalPassiveMode();936 EasyMock.expectLastCall().times(1);937 // mock step 3938 EasyMock.expect(localFile.exists()).andReturn(true).times(1);939 // mock step 4940 FTPFile[] ftpFiles = {};941 EasyMock.expect(ftp.listFiles("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml")).andReturn(ftpFiles).times(1);942 // mock step 5943 PowerMock.expectPrivate(client, "changeDir", ftp, "/ftp/cmdfile").andReturn(false).times(1);944 PowerMock.replay(ServerToClient.class, client, ftp, localFile);945 // =================== Input ===================946 // =================== Process ===================947 Method method = ServerToClient.class.getDeclaredMethod("doTransfer");948 boolean result = (Boolean) method.invoke(client);949 // =================== Output ===================950 Assert.assertEquals(false, result);951 // =================== After ===================952 PowerMock.verify(ServerToClient.class, client, ftp, localFile);953 }954 /**955 * Upload process, when local file exists, and remote file not exists, and create remote file parent directory success,956 * but fail to store remote file, failure is expected.957 *958 * @throws Exception959 */960 @Test961 @PrepareForTest({ ServerToClient.class })962 public void testDoTransferUpload003() throws Exception {963 // =================== Before ===================964 ServerToClient client = PowerMock.createPartialMock(ServerToClient.class, "changeDir");965 client.setTransferMode(FTPTransferMode.UPLOAD);966 // ftpVO967 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");968 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");969 field1.setAccessible(true);970 field1.set(client, vo);971 // localFile972 File localFile = PowerMock.createStrictMock(File.class);973 Field field2 = ServerToClient.class.getDeclaredField("localFile");974 field2.setAccessible(true);975 field2.set(client, localFile);976 // ftp977 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);978 Field field3 = ServerToClient.class.getDeclaredField("ftp");979 field3.setAccessible(true);980 field3.set(client, ftp);981 // mock step 1982 EasyMock.expect(ftp.setFileType(FTP.BINARY_FILE_TYPE)).andReturn(true).times(1);983 // mock step 2984 ftp.enterLocalPassiveMode();985 EasyMock.expectLastCall().times(1);986 // mock step 3987 EasyMock.expect(localFile.exists()).andReturn(true).times(1);988 // mock step 4989 FTPFile[] ftpFiles = {};990 EasyMock.expect(ftp.listFiles("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml")).andReturn(ftpFiles).times(1);991 // mock step 5992 PowerMock.expectPrivate(client, "changeDir", ftp, "/ftp/cmdfile").andReturn(true).times(1);993 // mock step 6994 FileInputStream fis = PowerMock.createStrictMock(FileInputStream.class);995 PowerMock.expectStrictNew(FileInputStream.class, localFile).andReturn(fis).times(1);996 EasyMock.expect(fis.skip(0L)).andReturn(0L).times(1);997 // mock step 7998 EasyMock.expect(ftp.storeFile("04_VOD_20110411093958_001047556.xml", fis)).andReturn(false).times(1);999 // mock step 81000 fis.close();1001 EasyMock.expectLastCall().times(1);1002 PowerMock.replay(ServerToClient.class, client, ftp, localFile, FileInputStream.class, fis);1003 // =================== Input ===================1004 // =================== Process ===================1005 Method method = ServerToClient.class.getDeclaredMethod("doTransfer");1006 boolean result = (Boolean) method.invoke(client);1007 // =================== Output ===================1008 Assert.assertEquals(false, result);1009 // =================== After ===================1010 PowerMock.verify(ServerToClient.class, client, ftp, localFile, FileInputStream.class, fis);1011 }1012 /**1013 * Upload process, when local file exists, and remote file not exists, and create remote file parent directory success,1014 * and store remote file success, success is expected.1015 *1016 * @throws Exception1017 */1018 @Test1019 @PrepareForTest({ ServerToClient.class })1020 public void testDoTransferUpload004() throws Exception {1021 // =================== Before ===================1022 ServerToClient client = PowerMock.createPartialMock(ServerToClient.class, "changeDir");1023 client.setTransferMode(FTPTransferMode.UPLOAD);1024 // ftpVO1025 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");1026 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");1027 field1.setAccessible(true);1028 field1.set(client, vo);1029 // localFile1030 File localFile = PowerMock.createStrictMock(File.class);1031 Field field2 = ServerToClient.class.getDeclaredField("localFile");1032 field2.setAccessible(true);1033 field2.set(client, localFile);1034 // ftp1035 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);1036 Field field3 = ServerToClient.class.getDeclaredField("ftp");1037 field3.setAccessible(true);1038 field3.set(client, ftp);1039 // mock step 11040 EasyMock.expect(ftp.setFileType(FTP.BINARY_FILE_TYPE)).andReturn(true).times(1);1041 // mock step 21042 ftp.enterLocalPassiveMode();1043 EasyMock.expectLastCall().times(1);1044 // mock step 31045 EasyMock.expect(localFile.exists()).andReturn(true).times(1);1046 // mock step 41047 FTPFile[] ftpFiles = {};1048 EasyMock.expect(ftp.listFiles("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml")).andReturn(ftpFiles).times(1);1049 // mock step 51050 PowerMock.expectPrivate(client, "changeDir", ftp, "/ftp/cmdfile").andReturn(true).times(1);1051 // mock step 61052 FileInputStream fis = PowerMock.createStrictMock(FileInputStream.class);1053 PowerMock.expectStrictNew(FileInputStream.class, localFile).andReturn(fis).times(1);1054 EasyMock.expect(fis.skip(0L)).andReturn(0L).times(1);1055 // mock step 71056 EasyMock.expect(ftp.storeFile("04_VOD_20110411093958_001047556.xml", fis)).andReturn(true).times(1);1057 // mock step 81058 fis.close();1059 EasyMock.expectLastCall().times(1);1060 PowerMock.replay(ServerToClient.class, client, ftp, localFile, FileInputStream.class, fis);1061 // =================== Input ===================1062 // =================== Process ===================1063 Method method = ServerToClient.class.getDeclaredMethod("doTransfer");1064 boolean result = (Boolean) method.invoke(client);1065 // =================== Output ===================1066 Assert.assertEquals(true, result);1067 // =================== After ===================1068 PowerMock.verify(ServerToClient.class, client, ftp, localFile, FileInputStream.class, fis);1069 }1070 /**1071 * Upload process, when local file exists, and remote file exists, and is not resume broken transfer mode,1072 * but fail to delete remote file, failure is expected.1073 *1074 * @throws Exception1075 */1076 @Test1077 public void testDoTransferUpload005() throws Exception {1078 // =================== Before ===================1079 ServerToClient client = new ServerToClient();1080 client.setTransferMode(FTPTransferMode.UPLOAD);1081 client.setResumeBroken(false);1082 // ftpVO1083 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");1084 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");1085 field1.setAccessible(true);1086 field1.set(client, vo);1087 // localFile1088 File localFile = PowerMock.createStrictMock(File.class);1089 Field field2 = ServerToClient.class.getDeclaredField("localFile");1090 field2.setAccessible(true);1091 field2.set(client, localFile);1092 // ftp1093 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);1094 Field field3 = ServerToClient.class.getDeclaredField("ftp");1095 field3.setAccessible(true);1096 field3.set(client, ftp);1097 // mock step 11098 EasyMock.expect(ftp.setFileType(FTP.BINARY_FILE_TYPE)).andReturn(true).times(1);1099 // mock step 21100 ftp.enterLocalPassiveMode();1101 EasyMock.expectLastCall().times(1);1102 // mock step 31103 EasyMock.expect(localFile.exists()).andReturn(true).times(1);1104 // mock step 41105 FTPFile ftpFile = PowerMock.createStrictMock(FTPFile.class);1106 FTPFile[] ftpFiles = {ftpFile};1107 EasyMock.expect(ftp.listFiles("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml")).andReturn(ftpFiles).times(1);1108 // mock step 51109 EasyMock.expect(ftp.deleteFile("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml")).andReturn(false).times(1);1110 EasyMock.replay(ftp, localFile);1111 // =================== Input ===================1112 // =================== Process ===================1113 Method method = ServerToClient.class.getDeclaredMethod("doTransfer");1114 boolean result = (Boolean) method.invoke(client);1115 // =================== Output ===================1116 Assert.assertEquals(false, result);1117 // =================== After ===================1118 EasyMock.verify(ftp, localFile);1119 }1120 /**1121 * Upload process, when local file exists, and remote file exists, and is not resume broken transfer mode,1122 * and delete remote file success, and store remote file success, success is expected.1123 *1124 * @throws Exception1125 */1126 @Test1127 @PrepareForTest({ ServerToClient.class })1128 public void testDoTransferUpload006() throws Exception {1129 // =================== Before ===================1130 ServerToClient client = PowerMock.createPartialMock(ServerToClient.class, "changeDir");1131 client.setTransferMode(FTPTransferMode.UPLOAD);1132 client.setResumeBroken(false);1133 // ftpVO1134 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");1135 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");1136 field1.setAccessible(true);1137 field1.set(client, vo);1138 // localFile1139 File localFile = PowerMock.createStrictMock(File.class);1140 Field field2 = ServerToClient.class.getDeclaredField("localFile");1141 field2.setAccessible(true);1142 field2.set(client, localFile);1143 // ftp1144 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);1145 Field field3 = ServerToClient.class.getDeclaredField("ftp");1146 field3.setAccessible(true);1147 field3.set(client, ftp);1148 // mock step 11149 EasyMock.expect(ftp.setFileType(FTP.BINARY_FILE_TYPE)).andReturn(true).times(1);1150 // mock step 21151 ftp.enterLocalPassiveMode();1152 EasyMock.expectLastCall().times(1);1153 // mock step 31154 EasyMock.expect(localFile.exists()).andReturn(true).times(1);1155 // mock step 41156 FTPFile ftpFile = PowerMock.createStrictMock(FTPFile.class);1157 FTPFile[] ftpFiles = {ftpFile};1158 EasyMock.expect(ftp.listFiles("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml")).andReturn(ftpFiles).times(1);1159 // mock step 51160 EasyMock.expect(ftp.deleteFile("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml")).andReturn(true).times(1);1161 // mock step 61162 PowerMock.expectPrivate(client, "changeDir", ftp, "/ftp/cmdfile").andReturn(true).times(1);1163 // mock step 71164 FileInputStream fis = PowerMock.createStrictMock(FileInputStream.class);1165 PowerMock.expectStrictNew(FileInputStream.class, localFile).andReturn(fis).times(1);1166 EasyMock.expect(fis.skip(0L)).andReturn(0L).times(1);1167 // mock step 81168 EasyMock.expect(ftp.storeFile("04_VOD_20110411093958_001047556.xml", fis)).andReturn(true).times(1);1169 // mock step 81170 fis.close();1171 EasyMock.expectLastCall().times(1);1172 PowerMock.replay(ServerToClient.class, client, ftp, localFile, FileInputStream.class, fis);1173 // =================== Input ===================1174 // =================== Process ===================1175 Method method = ServerToClient.class.getDeclaredMethod("doTransfer");1176 boolean result = (Boolean) method.invoke(client);1177 // =================== Output ===================1178 Assert.assertEquals(true, result);1179 // =================== After ===================1180 PowerMock.verify(ServerToClient.class, client, ftp, localFile, FileInputStream.class, fis);1181 }1182 /**1183 * Upload process, when local file exists, and remote file exists, and is resume broken transfer mode,1184 * local file size is smaller than remote file size, failure is expected.1185 *1186 * @throws Exception1187 */1188 @Test1189 public void testDoTransferUpload007() throws Exception {1190 // =================== Before ===================1191 ServerToClient client = new ServerToClient();1192 client.setTransferMode(FTPTransferMode.UPLOAD);1193 // ftpVO1194 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");1195 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");1196 field1.setAccessible(true);1197 field1.set(client, vo);1198 // localFile1199 File localFile = PowerMock.createStrictMock(File.class);1200 Field field2 = ServerToClient.class.getDeclaredField("localFile");1201 field2.setAccessible(true);1202 field2.set(client, localFile);1203 // ftp1204 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);1205 Field field3 = ServerToClient.class.getDeclaredField("ftp");1206 field3.setAccessible(true);1207 field3.set(client, ftp);1208 // mock step 11209 EasyMock.expect(ftp.setFileType(FTP.BINARY_FILE_TYPE)).andReturn(true).times(1);1210 // mock step 21211 ftp.enterLocalPassiveMode();1212 EasyMock.expectLastCall().times(1);1213 // mock step 31214 EasyMock.expect(localFile.exists()).andReturn(true).times(1);1215 // mock step 41216 FTPFile ftpFile = PowerMock.createStrictMock(FTPFile.class);1217 FTPFile[] ftpFiles = {ftpFile};1218 EasyMock.expect(ftp.listFiles("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml")).andReturn(ftpFiles).times(1);1219 // mock step 51220 EasyMock.expect(localFile.length()).andReturn(99L).times(1);1221 EasyMock.expect(ftpFile.getSize()).andReturn(100L).times(1);1222 EasyMock.replay(ftp, localFile, ftpFile);1223 // =================== Input ===================1224 // =================== Process ===================1225 Method method = ServerToClient.class.getDeclaredMethod("doTransfer");1226 boolean result = (Boolean) method.invoke(client);1227 // =================== Output ===================1228 Assert.assertEquals(false, result);1229 // =================== After ===================1230 EasyMock.verify(ftp, localFile, ftpFile);1231 }1232 /**1233 * Upload process, when local file exists, and remote file exists, and is resume broken transfer mode,1234 * local file size is equal to remote file size, failure is expected.1235 *1236 * @throws Exception1237 */1238 @Test1239 public void testDoTransferUpload008() throws Exception {1240 // =================== Before ===================1241 ServerToClient client = new ServerToClient();1242 client.setTransferMode(FTPTransferMode.UPLOAD);1243 // ftpVO1244 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");1245 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");1246 field1.setAccessible(true);1247 field1.set(client, vo);1248 // localFile1249 File localFile = PowerMock.createStrictMock(File.class);1250 Field field2 = ServerToClient.class.getDeclaredField("localFile");1251 field2.setAccessible(true);1252 field2.set(client, localFile);1253 // ftp1254 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);1255 Field field3 = ServerToClient.class.getDeclaredField("ftp");1256 field3.setAccessible(true);1257 field3.set(client, ftp);1258 // mock step 11259 EasyMock.expect(ftp.setFileType(FTP.BINARY_FILE_TYPE)).andReturn(true).times(1);1260 // mock step 21261 ftp.enterLocalPassiveMode();1262 EasyMock.expectLastCall().times(1);1263 // mock step 31264 EasyMock.expect(localFile.exists()).andReturn(true).times(1);1265 // mock step 41266 FTPFile ftpFile = PowerMock.createStrictMock(FTPFile.class);1267 FTPFile[] ftpFiles = {ftpFile};1268 EasyMock.expect(ftp.listFiles("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml")).andReturn(ftpFiles).times(1);1269 // mock step 51270 EasyMock.expect(localFile.length()).andReturn(100L).times(1);1271 EasyMock.expect(ftpFile.getSize()).andReturn(100L).times(1);1272 EasyMock.replay(ftp, localFile, ftpFile);1273 // =================== Input ===================1274 // =================== Process ===================1275 Method method = ServerToClient.class.getDeclaredMethod("doTransfer");1276 boolean result = (Boolean) method.invoke(client);1277 // =================== Output ===================1278 Assert.assertEquals(false, result);1279 // =================== After ===================1280 EasyMock.verify(ftp, localFile, ftpFile);1281 }1282 /**1283 * Upload process, when local file exists, and remote file exists, and is resume broken transfer mode,1284 * local file size is larger than remote file size, but fail to set local file skip size, failure is expected.1285 *1286 * @throws Exception1287 */1288 @Test1289 @PrepareForTest({ ServerToClient.class })1290 public void testDoTransferUpload009() throws Exception {1291 // =================== Before ===================1292 ServerToClient client = PowerMock.createPartialMock(ServerToClient.class, "changeDir");1293 client.setTransferMode(FTPTransferMode.UPLOAD);1294 client.setResumeBroken(true);1295 // ftpVO1296 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");1297 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");1298 field1.setAccessible(true);1299 field1.set(client, vo);1300 // localFile1301 File localFile = PowerMock.createStrictMock(File.class);1302 Field field2 = ServerToClient.class.getDeclaredField("localFile");1303 field2.setAccessible(true);1304 field2.set(client, localFile);1305 // ftp1306 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);1307 Field field3 = ServerToClient.class.getDeclaredField("ftp");1308 field3.setAccessible(true);1309 field3.set(client, ftp);1310 // mock step 11311 EasyMock.expect(ftp.setFileType(FTP.BINARY_FILE_TYPE)).andReturn(true).times(1);1312 // mock step 21313 ftp.enterLocalPassiveMode();1314 EasyMock.expectLastCall().times(1);1315 // mock step 31316 EasyMock.expect(localFile.exists()).andReturn(true).times(1);1317 // mock step 41318 FTPFile ftpFile = PowerMock.createStrictMock(FTPFile.class);1319 FTPFile[] ftpFiles = {ftpFile};1320 EasyMock.expect(ftp.listFiles("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml")).andReturn(ftpFiles).times(1);1321 // mock step 51322 EasyMock.expect(localFile.length()).andReturn(100L).times(1);1323 EasyMock.expect(ftpFile.getSize()).andReturn(99L).times(1);1324 // mock step 61325 ftp.setRestartOffset(99L);1326 EasyMock.expectLastCall().times(1);1327 // mock step 71328 PowerMock.expectPrivate(client, "changeDir", ftp, "/ftp/cmdfile").andReturn(true).times(1);1329 // mock step 81330 FileInputStream fis = PowerMock.createStrictMock(FileInputStream.class);1331 PowerMock.expectStrictNew(FileInputStream.class, localFile).andReturn(fis).times(1);1332 EasyMock.expect(fis.skip(99L)).andReturn(0L).times(1);1333 // mock step 91334 fis.close();1335 EasyMock.expectLastCall();1336 PowerMock.replay(ServerToClient.class, client, ftp, localFile, ftpFile, FileInputStream.class, fis);1337 // =================== Input ===================1338 // =================== Process ===================1339 Method method = ServerToClient.class.getDeclaredMethod("doTransfer");1340 boolean result = (Boolean) method.invoke(client);1341 // =================== Output ===================1342 Assert.assertEquals(false, result);1343 // =================== After ===================1344 PowerMock.verify(ServerToClient.class, client, ftp, localFile, ftpFile, FileInputStream.class, fis);1345 }1346 /**1347 * Upload process, when local file exists, and remote file exists, and is resume broken transfer mode,1348 * local file size is larger than remote file size, and set local file skip size success, and store remote file success,1349 * success is expected.1350 *1351 * @throws Exception1352 */1353 @Test1354 @PrepareForTest({ ServerToClient.class })1355 public void testDoTransferUpload010() throws Exception {1356 // =================== Before ===================1357 ServerToClient client = PowerMock.createPartialMock(ServerToClient.class, "changeDir");1358 client.setTransferMode(FTPTransferMode.UPLOAD);1359 client.setResumeBroken(true);1360 // ftpVO1361 FTPFileInfoVO vo = AbstractFTPClient.convertFTPUrlToVO("ftp://username:password@192.168.19.251/ftp/cmdfile/04_VOD_20110411093958_001047556.xml");1362 Field field1 = ServerToClient.class.getDeclaredField("ftpVO");1363 field1.setAccessible(true);1364 field1.set(client, vo);1365 // localFile1366 File localFile = PowerMock.createStrictMock(File.class);1367 Field field2 = ServerToClient.class.getDeclaredField("localFile");1368 field2.setAccessible(true);1369 field2.set(client, localFile);1370 // ftp1371 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);1372 Field field3 = ServerToClient.class.getDeclaredField("ftp");1373 field3.setAccessible(true);1374 field3.set(client, ftp);1375 // mock step 11376 EasyMock.expect(ftp.setFileType(FTP.BINARY_FILE_TYPE)).andReturn(true).times(1);1377 // mock step 21378 ftp.enterLocalPassiveMode();1379 EasyMock.expectLastCall().times(1);1380 // mock step 31381 EasyMock.expect(localFile.exists()).andReturn(true).times(1);1382 // mock step 41383 FTPFile ftpFile = PowerMock.createStrictMock(FTPFile.class);1384 FTPFile[] ftpFiles = {ftpFile};1385 EasyMock.expect(ftp.listFiles("/ftp/cmdfile/04_VOD_20110411093958_001047556.xml")).andReturn(ftpFiles).times(1);1386 // mock step 51387 EasyMock.expect(localFile.length()).andReturn(100L).times(1);1388 EasyMock.expect(ftpFile.getSize()).andReturn(99L).times(1);1389 // mock step 61390 ftp.setRestartOffset(99L);1391 EasyMock.expectLastCall().times(1);1392 // mock step 71393 PowerMock.expectPrivate(client, "changeDir", ftp, "/ftp/cmdfile").andReturn(true).times(1);1394 // mock step 81395 FileInputStream fis = PowerMock.createStrictMock(FileInputStream.class);1396 PowerMock.expectStrictNew(FileInputStream.class, localFile).andReturn(fis).times(1);1397 EasyMock.expect(fis.skip(99L)).andReturn(99L).times(1);1398 // mock step 91399 EasyMock.expect(ftp.storeFile("04_VOD_20110411093958_001047556.xml", fis)).andReturn(true).times(1);1400 // mock step 101401 fis.close();1402 EasyMock.expectLastCall();1403 PowerMock.replay(ServerToClient.class, client, ftp, localFile, ftpFile, FileInputStream.class, fis);1404 // =================== Input ===================1405 // =================== Process ===================1406 Method method = ServerToClient.class.getDeclaredMethod("doTransfer");1407 boolean result = (Boolean) method.invoke(client);1408 // =================== Output ===================1409 Assert.assertEquals(true, result);1410 // =================== After ===================1411 PowerMock.verify(ServerToClient.class, client, ftp, localFile, ftpFile, FileInputStream.class, fis);1412 }1413 /**1414 * When transfer mode is set to null, failure is expected.1415 *1416 * @throws Exception1417 */1418 @Test1419 public void testDoTransferOther001() throws Exception {1420 // =================== Before ===================1421 ServerToClient client = new ServerToClient();1422 client.setTransferMode(null);1423 // =================== Input ===================1424 // =================== Process ===================1425 Method method = ServerToClient.class.getDeclaredMethod("doTransfer");1426 boolean result = (Boolean) method.invoke(client);1427 // =================== Output ===================1428 Assert.assertEquals(false, result);1429 // =================== After ===================1430 }1431 /**1432 * When FTP Client is not connected to server, logout and disconnect are not to be executed.1433 *1434 * @throws Exception1435 */1436 @Test1437 public void testLogoutAndDisconnect001() throws Exception {1438 // =================== Before ===================1439 ServerToClient client = new ServerToClient();1440 // ftp1441 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);1442 Field field = ServerToClient.class.getDeclaredField("ftp");1443 field.setAccessible(true);1444 field.set(client, ftp);1445 // mock step 11446 EasyMock.expect(ftp.isConnected()).andReturn(false).times(1);1447 // mock step 21448 EasyMock.expect(ftp.isConnected()).andReturn(false).times(1);1449 EasyMock.replay(ftp);1450 // =================== Input ===================1451 // =================== Process ===================1452 Method method = ServerToClient.class.getDeclaredMethod("logoutAndDisconnect");1453 method.invoke(client);1454 // =================== Output ===================1455 // =================== After ===================1456 EasyMock.verify(ftp);1457 }1458 /**1459 * When FTP Client is connected to server, logout and disconnect are to be executed.1460 *1461 * @throws Exception1462 */1463 @Test1464 public void testLogoutAndDisconnect002() throws Exception {1465 // =================== Before ===================1466 ServerToClient client = new ServerToClient();1467 // ftp1468 FTPClient ftp = PowerMock.createStrictMock(FTPClient.class);1469 Field field = ServerToClient.class.getDeclaredField("ftp");1470 field.setAccessible(true);1471 field.set(client, ftp);1472 // mock step 11473 EasyMock.expect(ftp.isConnected()).andReturn(true).times(1);1474 // mock step 21475 EasyMock.expect(ftp.logout()).andReturn(true).times(1);1476 // mock step 31477 EasyMock.expect(ftp.isConnected()).andReturn(true).times(1);1478 // mock step 41479 ftp.disconnect();1480 EasyMock.expectLastCall().times(1);1481 EasyMock.replay(ftp);1482 // =================== Input ===================...

Full Screen

Full Screen

Source:FTPLogCommandListenerTest.java Github

copy

Full Screen

...17 @Test18 public void testProtocolCommandSent001() {19 // =================== Before ===================20 // mock step 121 Logger logger = EasyMock.createStrictMock(Logger.class);22 EasyMock.expect(logger.isDebugEnabled()).andReturn(false).times(1);23 EasyMock.replay(logger);24 // =================== Input ===================25 // =================== Process ===================26 FTPLogCommandListener listener = new FTPLogCommandListener(logger);27 listener.protocolCommandSent(null);28 // =================== Output ===================29 // =================== After ===================30 EasyMock.verify(logger);31 }32 /**33 * When debug log is on, but event message is null, expects nothing in output.34 */35 @Test36 public void testProtocolCommandSent002() {37 // =================== Before ===================38 // mock step 139 Logger logger = EasyMock.createStrictMock(Logger.class);40 EasyMock.expect(logger.isDebugEnabled()).andReturn(true).times(1);41 // mock step 242 ProtocolCommandEvent event = PowerMock.createStrictMock(ProtocolCommandEvent.class);43 EasyMock.expect(event.getMessage()).andReturn(null).times(1);44 EasyMock.replay(logger, event);45 // =================== Input ===================46 // =================== Process ===================47 FTPLogCommandListener listener = new FTPLogCommandListener(logger);48 listener.protocolCommandSent(event);49 // =================== Output ===================50 // =================== After ===================51 EasyMock.verify(logger, event);52 }53 /**54 * When debug log is on, and event message is "aaa", expects "aaa" in output.55 */56 @Test57 public void testProtocolCommandSent003() {58 // =================== Before ===================59 // mock step 160 Logger logger = EasyMock.createStrictMock(Logger.class);61 EasyMock.expect(logger.isDebugEnabled()).andReturn(true).times(1);62 // mock step 263 ProtocolCommandEvent event = PowerMock.createStrictMock(ProtocolCommandEvent.class);64 EasyMock.expect(event.getMessage()).andReturn("aaa").times(1);65 // mock step 366 EasyMock.expect(event.getMessage()).andReturn("aaa").times(1);67 logger.debug("aaa");68 EasyMock.expectLastCall().times(1);69 EasyMock.replay(logger, event);70 // =================== Input ===================71 // =================== Process ===================72 FTPLogCommandListener listener = new FTPLogCommandListener(logger);73 listener.protocolCommandSent(event);74 // =================== Output ===================75 // =================== After ===================76 EasyMock.verify(logger, event);77 }78 /**79 * When debug log is on, and event message is "aaa" + "line.separator", expects "aaa" in output.80 */81 @Test82 public void testProtocolCommandSent004() {83 // =================== Before ===================84 // mock step 185 Logger logger = EasyMock.createStrictMock(Logger.class);86 EasyMock.expect(logger.isDebugEnabled()).andReturn(true).times(1);87 // mock step 288 ProtocolCommandEvent event = PowerMock.createStrictMock(ProtocolCommandEvent.class);89 EasyMock.expect(event.getMessage()).andReturn("aaa" + System.getProperty("line.separator")).times(1);90 // mock step 391 EasyMock.expect(event.getMessage()).andReturn("aaa" + System.getProperty("line.separator")).times(1);92 logger.debug("aaa");93 EasyMock.expectLastCall().times(1);94 EasyMock.replay(logger, event);95 // =================== Input ===================96 // =================== Process ===================97 FTPLogCommandListener listener = new FTPLogCommandListener(logger);98 listener.protocolCommandSent(event);99 // =================== Output ===================100 // =================== After ===================101 EasyMock.verify(logger, event);102 }103 /**104 * When debug log is off, expects nothing in output.105 */106 @Test107 public void testProtocolReplyReceived001() {108 // =================== Before ===================109 // mock step 1110 Logger logger = EasyMock.createStrictMock(Logger.class);111 EasyMock.expect(logger.isDebugEnabled()).andReturn(false).times(1);112 EasyMock.replay(logger);113 // =================== Input ===================114 // =================== Process ===================115 FTPLogCommandListener listener = new FTPLogCommandListener(logger);116 listener.protocolReplyReceived(null);117 // =================== Output ===================118 // =================== After ===================119 EasyMock.verify(logger);120 }121 /**122 * When debug log is on, but event message is null, expects nothing in output.123 */124 @Test125 public void testProtocolReplyReceived002() {126 // =================== Before ===================127 // mock step 1128 Logger logger = EasyMock.createStrictMock(Logger.class);129 EasyMock.expect(logger.isDebugEnabled()).andReturn(true).times(1);130 // mock step 2131 ProtocolCommandEvent event = PowerMock.createStrictMock(ProtocolCommandEvent.class);132 EasyMock.expect(event.getMessage()).andReturn(null).times(1);133 EasyMock.replay(logger, event);134 // =================== Input ===================135 // =================== Process ===================136 FTPLogCommandListener listener = new FTPLogCommandListener(logger);137 listener.protocolReplyReceived(event);138 // =================== Output ===================139 // =================== After ===================140 EasyMock.verify(logger, event);141 }142 /**143 * When debug log is on, and event message is "aaa", expects "aaa" in output.144 */145 @Test146 public void testProtocolReplyReceived003() {147 // =================== Before ===================148 // mock step 1149 Logger logger = EasyMock.createStrictMock(Logger.class);150 EasyMock.expect(logger.isDebugEnabled()).andReturn(true).times(1);151 // mock step 2152 ProtocolCommandEvent event = PowerMock.createStrictMock(ProtocolCommandEvent.class);153 EasyMock.expect(event.getMessage()).andReturn("aaa").times(1);154 // mock step 3155 EasyMock.expect(event.getMessage()).andReturn("aaa").times(1);156 logger.debug("aaa");157 EasyMock.expectLastCall().times(1);158 EasyMock.replay(logger, event);159 // =================== Input ===================160 // =================== Process ===================161 FTPLogCommandListener listener = new FTPLogCommandListener(logger);162 listener.protocolReplyReceived(event);163 // =================== Output ===================164 // =================== After ===================165 EasyMock.verify(logger, event);166 }167 /**168 * When debug log is on, and event message is "aaa" + "line.separator", expects "aaa" in output.169 */170 @Test171 public void testProtocolReplyReceived004() {172 // =================== Before ===================173 // mock step 1174 Logger logger = EasyMock.createStrictMock(Logger.class);175 EasyMock.expect(logger.isDebugEnabled()).andReturn(true).times(1);176 // mock step 2177 ProtocolCommandEvent event = PowerMock.createStrictMock(ProtocolCommandEvent.class);178 EasyMock.expect(event.getMessage()).andReturn("aaa" + System.getProperty("line.separator")).times(1);179 // mock step 3180 EasyMock.expect(event.getMessage()).andReturn("aaa" + System.getProperty("line.separator")).times(1);181 logger.debug("aaa");182 EasyMock.expectLastCall().times(1);183 EasyMock.replay(logger, event);184 // =================== Input ===================185 // =================== Process ===================186 FTPLogCommandListener listener = new FTPLogCommandListener(logger);187 listener.protocolReplyReceived(event);188 // =================== Output ===================189 // =================== After ===================190 EasyMock.verify(logger, event);191 }...

Full Screen

Full Screen

Source:UrlValidatorTest.java Github

copy

Full Screen

1package de.juwimm.cms.client.util.test;23import static junit.framework.Assert.assertEquals;4import static org.powermock.api.easymock.PowerMock.createNiceMock;5import static org.powermock.api.easymock.PowerMock.createStrictMock;6import static org.powermock.api.easymock.PowerMock.expectLastCall;7import static org.powermock.api.easymock.PowerMock.expectStrictNew;8import static org.powermock.api.easymock.PowerMock.expectNew;9import static org.powermock.api.easymock.PowerMock.replayAll;10import static org.powermock.api.easymock.PowerMock.verifyAll;1112import java.net.HttpURLConnection;13import java.net.MalformedURLException;14import java.net.URL;15import java.util.ResourceBundle;1617import org.junit.Before;18import org.junit.Test;19import org.junit.runner.RunWith;20import org.powermock.core.classloader.annotations.PrepareForTest;21import org.powermock.modules.junit4.PowerMockRunner;2223import de.juwimm.cms.common.Constants;24import de.juwimm.cms.util.UrlValidator;2526@RunWith(PowerMockRunner.class)27@PrepareForTest( {URL.class, UrlValidator.class})28public class UrlValidatorTest {2930 @Before31 public void setUp() {32 Constants.rb = ResourceBundle.getBundle("CMS", Constants.CMS_LOCALE);33 }3435 @Test36 public void testValidateHappyFlow() throws Exception {37 String testLink = "igoogle.com";3839 URL url = createStrictMock(URL.class);40 HttpURLConnection conn = createNiceMock(HttpURLConnection.class);4142 expectStrictNew(URL.class, "http://" + testLink).andReturn(url);43 url.openConnection();44 expectLastCall().andReturn(conn);45 conn.setConnectTimeout(2000);46 expectLastCall();47 conn.setReadTimeout(5000);48 expectLastCall();49 conn.setInstanceFollowRedirects(true);50 expectLastCall();51 conn.setRequestProperty("User-agent", "crawler");52 expectLastCall();53 conn.connect();54 expectLastCall();55 conn.getResponseCode();56 expectLastCall().andReturn(200);57 conn.getURL();58 expectLastCall().andReturn(url);59 url.toExternalForm();60 expectLastCall().andReturn(testLink);6162 replayAll();63 String actual = UrlValidator.validate(testLink);64 assertEquals(testLink, actual);65 verifyAll();66 }6768 @Test69 public void testValidateInvalidUrl() throws Exception {70 String testLink = "cd+45";7172 URL url = createStrictMock(URL.class);73 HttpURLConnection conn = createNiceMock(HttpURLConnection.class);7475 expectStrictNew(URL.class, "http://" + testLink).andReturn(url);76 url.openConnection();77 expectLastCall().andThrow(new MalformedURLException());7879 replayAll();80 String actual = UrlValidator.validate(testLink);81 assertEquals("Invalid URL", actual);82 verifyAll();83 }84} ...

Full Screen

Full Screen

createStrictMock

Using AI Code Generation

copy

Full Screen

1import org.powermock.api.easymock.PowerMock;2import org.powermock.api.easymock.annotation.Mock;3import org.powermock.core.classloader.annotations.PrepareForTest;4import org.powermock.modules.junit4.PowerMockRunner;5import org.junit.runner.RunWith;6import org.junit.Test;7import static org.powermock.api.easymock.PowerMock.*;8import static org.easymock.EasyMock.*;9import static org.junit.Assert.assertEquals;10@RunWith(PowerMockRunner.class)11@PrepareForTest({4.class})12public class 4 {13 private 4 mock4;14 public void test4() {15 mock4 = createStrictMock(4.class);16 expect(mock4.add(1, 2)).andReturn(3);17 replay(mock4);18 assertEquals(3, mock4.add(1, 2));19 verify(mock4);20 }21}22import org.powermock.api.easymock.PowerMock;23import org.powermock.api.easymock.annotation.Mock;24import org.powermock.core.classloader.annotations.PrepareForTest;25import org.powermock.modules.junit4.PowerMockRunner;26import org.junit.runner.RunWith;27import org.junit.Test;28import static org.powermock.api.easymock.PowerMock.*;29import static org.easymock.EasyMock.*;30import static org.junit.Assert.assertEquals;31@RunWith(PowerMockRunner.class)32@PrepareForTest({4.class})33public class 4 {34 private 4 mock4;35 public void test4() {36 mock4 = createMock(4.class);37 expect(mock4.add(1, 2)).andReturn(3);38 replay(mock4);39 assertEquals(3, mock4.add(1, 2));40 verify(mock4);41 }42}43import org.powermock.api.easymock.PowerMock;44import org.powermock.api.easymock.annotation.Mock;45import org.powermock.core.classloader.annotations.PrepareForTest;46import org.powermock.modules.junit4.PowerMockRunner;47import org.junit.runner.RunWith;48import org.junit.Test;49import static org.powermock.api.easymock.PowerMock.*;50import static

Full Screen

Full Screen

createStrictMock

Using AI Code Generation

copy

Full Screen

1package org.powermock.examples.tutorial.easymock;2import static org.easymock.EasyMock.expect;3import static org.powermock.api.easymock.PowerMock.createStrictMock;4import static org.powermock.api.easymock.PowerMock.expectNew;5import static org.powermock.api.easymock.PowerMock.replay;6import static org.powermock.api.easymock.PowerMock.verify;7import java.io.IOException;8import java.util.List;9import org.junit.Test;10public class Example4 {11 public void testStrictMock() throws Exception {12 final List<String> mockedList = createStrictMock(List.class);13 expect(mockedList.get(0)).andReturn("first");14 expect(mockedList.get(1)).andReturn("second");15 expect(mockedList.get(2)).andThrow(new IOException());16 expect(mockedList.get(3)).andReturn("fourth");17 expect(mockedList.get(4)).andReturn("fifth");18 expect(mockedList.get(5)).andReturn("sixth");19 replay(mockedList);20 mockedList.get(0);21 mockedList.get(1);22 mockedList.get(2);23 mockedList.get(3);24 mockedList.get(4);25 mockedList.get(5);26 verify(mockedList);27 }28}29package org.powermock.examples.tutorial.easymock;30import static org.easymock.EasyMock.expect;31import static org.powermock.api.easymock.PowerMock.createMockBuilder;32import static org.powermock.api.easymock.PowerMock.expectNew;33import static org.powermock.api.easymock.PowerMock.replay;34import static org.powermock.api.easymock.PowerMock.verify;35import java.io.IOException;36import java.util.List;37import org.junit.Test;38public class Example5 {39 public void testMockBuilder() throws Exception {40 final List<String> mockedList = createMockBuilder(List.class).addMockedMethod("get", int.class).createMock();41 expect(mockedList.get(0)).andReturn("first");42 expect(mockedList.get(1)).andReturn("second");43 expect(mockedList.get(2)).andThrow(new IOException());44 expect(mockedList.get(3)).andReturn("fourth");45 expect(mockedList.get(4)).andReturn("fifth

Full Screen

Full Screen

createStrictMock

Using AI Code Generation

copy

Full Screen

1package com.automation.mock;2import static org.easymock.EasyMock.expect;3import static org.powermock.api.easymock.PowerMock.createStrictMock;4import static org.powermock.api.easymock.PowerMock.replay;5import static org.powermock.api.easymock.PowerMock.verify;6import java.util.ArrayList;7import java.util.List;8import org.junit.Test;9public class TestClass {10 public void test() {11 List mockList = createStrictMock(ArrayList.class);12 expect(mockList.get(0)).andReturn("one");13 expect(mockList.get(1)).andReturn("two");14 replay(mockList);15 System.out.println(mockList.get(0));16 System.out.println(mockList.get(1));17 verify(mockList);18 }19}

Full Screen

Full Screen

createStrictMock

Using AI Code Generation

copy

Full Screen

1import org.powermock.api.easymock.PowerMock;2import java.util.ArrayList;3import java.util.List;4public class 4 {5 public static void main(String[] args) {6 List<String> list = PowerMock.createStrictMock(ArrayList.class);7 }8}9public static <T> T createStrictMock(ClassLoader classLoader, Class<T> clazz)10import org.powermock.api.easymock.PowerMock;11import java.util.ArrayList;12import java.util.List;13public class 5 {14 public static void main(String[] args) {15 List<String> list = PowerMock.createStrictMock(ArrayList.class);16 }17}

Full Screen

Full Screen

createStrictMock

Using AI Code Generation

copy

Full Screen

1import static org.powermock.api.easymock.PowerMock.createStrictMock;2import static org.junit.Assert.*;3import org.junit.Test;4public class Test4 {5 public void test4() {6 Class4 class4 = createStrictMock(Class4.class);7 assertNotNull(class4);8 }9}10import static org.powermock.api.easymock.PowerMock.createStrictMock;11import static org.junit.Assert.*;12import org.junit.Test;13public class Test5 {14 public void test5() {15 Class5 class5 = createStrictMock(Class5.class);16 assertNotNull(class5);17 }18}19import static org.powermock.api.easymock.PowerMock.createStrictMock;20import static org.junit.Assert.*;21import org.junit.Test;22public class Test6 {23 public void test6() {24 Class6 class6 = createStrictMock(Class6.class);25 assertNotNull(class6);26 }27}28import static org.powermock.api.easymock.PowerMock.createStrictMock;29import static org.junit.Assert.*;30import org.junit.Test;31public class Test7 {32 public void test7() {33 Class7 class7 = createStrictMock(Class7.class);34 assertNotNull(class7);35 }36}37import static org.powermock.api.easymock.PowerMock.createStrictMock;38import static org.junit.Assert.*;39import org.junit.Test;40public class Test8 {41 public void test8() {42 Class8 class8 = createStrictMock(Class8.class);43 assertNotNull(class8);44 }45}46import static org.powermock.api.easymock.PowerMock.createStrictMock;47import static org.junit.Assert.*;48import org.junit.Test;49public class Test9 {

Full Screen

Full Screen

createStrictMock

Using AI Code Generation

copy

Full Screen

1package com.example;2import static org.powermock.api.easymock.PowerMock.*;3import org.junit.After;4import org.junit.Before;5import org.junit.Test;6public class Test1 {7 private Test2 test2;8 public void setUp() throws Exception {9 test2 = createStrictMock(Test2.class);10 }11 public void tearDown() throws Exception {12 test2 = null;13 }14 public void test() {15 test2.test1("test");16 test2.test2("test");17 test2.test3("test");18 test2.test4("test");19 test2.test5("test");20 test2.test6("test");21 test2.test7("test");22 test2.test8("test");23 test2.test9("test");24 test2.test10("test");25 test2.test11("test");26 test2.test12("test");27 test2.test13("test");28 test2.test14("test");29 test2.test15("test");30 test2.test16("test");31 test2.test17("test");32 test2.test18("test");33 test2.test19("test");34 test2.test20("test");35 test2.test21("test");36 test2.test22("test");37 test2.test23("test");38 test2.test24("test");39 test2.test25("test");40 test2.test26("test");41 test2.test27("test");42 test2.test28("test");43 test2.test29("test");44 test2.test30("test");45 test2.test31("test");46 test2.test32("test");47 test2.test33("test");48 test2.test34("test");49 test2.test35("test");50 test2.test36("test");51 test2.test37("test");52 test2.test38("test");53 test2.test39("test");54 test2.test40("test");55 test2.test41("test");56 test2.test42("test");57 test2.test43("test");58 test2.test44("test");59 test2.test45("test");60 test2.test46("test");61 test2.test47("test");62 test2.test48("test");63 test2.test49("test");64 test2.test50("test

Full Screen

Full Screen

createStrictMock

Using AI Code Generation

copy

Full Screen

1package com.powermock;2import static org.powermock.api.easymock.PowerMock.*;3import org.junit.Test;4public class Test4 {5 public void testCreateStrictMock() {6 Test1 test1 = createStrictMock(Test1.class);7 replayAll();8 test1.method1();9 verifyAll();10 }11}12 at org.junit.Assert.fail(Assert.java:88)13 at org.junit.Assert.failNotEquals(Assert.java:834)14 at org.junit.Assert.assertEquals(Assert.java:118)15 at org.junit.Assert.assertEquals(Assert.java:144)16 at org.powermock.api.easymock.PowerMock.replay(PowerMock.java:78)17 at org.powermock.api.easymock.PowerMock.replayAll(PowerMock.java:85)18 at com.powermock.Test4.testCreateStrictMock(Test4.java:16)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful