Guest User

Untitled

a guest
Aug 17th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. /* File: StockCacheTest.java
  2. * Created: 8/17/2018
  3. * Author: Sabbir Manandhar
  4. *
  5. * Copyright (c) 2018 Hogwart Inc.
  6. */
  7.  
  8. //=======================================================================================
  9.  
  10. import junit.framework.Assert;
  11. import org.junit.Test;
  12. import org.junit.runner.RunWith;
  13. import org.powermock.modules.junit4.PowerMockRunner;
  14. import org.powermock.reflect.Whitebox;
  15.  
  16. import java.util.HashMap;
  17. import java.util.Map;
  18.  
  19. /**
  20. * @author Sabbir Manandhar manandharsabbir@gmail.com
  21. * @version 1.0
  22. */
  23. @RunWith(PowerMockRunner.class)
  24. public class StockCacheTest {
  25.  
  26.  
  27. @Test
  28. public void testGetStockDetail() {
  29. // START Mocked cachedStock
  30. Map<String, String[]> cachedStock = new HashMap<String, String[]>();
  31. String stockNumber = "LAS3453SFDSFD";
  32. String[] stockDetail = new String[]{stockNumber, "This is the most active stock last week", "2500.00"};
  33. cachedStock.put(stockNumber, stockDetail);
  34.  
  35. stockNumber = "UTR3453SFDSFD";
  36. stockDetail = new String[]{stockNumber, "The most successful", "644500.00"};
  37. cachedStock.put(stockNumber, stockDetail);
  38.  
  39. stockNumber = "UIYR343SFDSFD";
  40. stockDetail = new String[]{stockNumber, "Heavily beaten.", "-45400.00"};
  41. cachedStock.put(stockNumber, stockDetail);
  42.  
  43. stockNumber = "BJKB923FDSFD";
  44. stockDetail = new String[]{stockNumber, "Useless", "00.00"};
  45. cachedStock.put(stockNumber, stockDetail);
  46. // END Mocked cachedStock
  47.  
  48. StockCache stockCache = new StockCache();
  49. Whitebox.setInternalState(stockCache, "cachedStocks", cachedStock); // SETS THE INTERNAL PRIVATE FIELD
  50.  
  51. stockDetail = stockCache.getStockDetail("LAS3453SFDSFD");
  52. Assert.assertNotNull(stockDetail);
  53. Assert.assertEquals("This is the most active stock last week", stockDetail[1]);
  54. Assert.assertEquals("2500.00", stockDetail[2]);
  55.  
  56. stockDetail = stockCache.getStockDetail("UIYR343SFDSFD");
  57. Assert.assertNotNull(stockDetail);
  58. Assert.assertEquals("Heavily beaten.", stockDetail[1]);
  59. Assert.assertEquals("-45400.00", stockDetail[2]);
  60. } // testGetStockDetail
  61.  
  62. } // StockCacheTest
  63.  
  64. //=======================================================================================
  65.  
  66. class StockCache {
  67.  
  68. private Map<String, String[]> cachedStocks;
  69.  
  70. //-------------------------------------------------------------------------------------
  71.  
  72. /**
  73. * Populates the cached stocks list
  74. */
  75. public void initializeCachedStocks() {
  76. // implementation to populate the cache list
  77. // could be from disk or network or any other
  78. } // initializeCachedStocks
  79.  
  80. //-------------------------------------------------------------------------------------
  81.  
  82. /**
  83. * Returns the stock details of a stock corresponding to the stock number
  84. *
  85. * @param stockNumber stock number of the stock of which we need the detail
  86. * @return Stock Detail if it exists in the cache, otherwise NULL
  87. */
  88. public String[] getStockDetail(String stockNumber) {
  89. return this.cachedStocks.get(stockNumber);
  90. } // getStockDetail
  91.  
  92. } // StockCache
Add Comment
Please, Sign In to add comment