Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* File: StockCacheTest.java
- * Created: 8/17/2018
- * Author: Sabbir Manandhar
- *
- * Copyright (c) 2018 Hogwart Inc.
- */
- //=======================================================================================
- import junit.framework.Assert;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.powermock.modules.junit4.PowerMockRunner;
- import org.powermock.reflect.Whitebox;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * @author Sabbir Manandhar manandharsabbir@gmail.com
- * @version 1.0
- */
- @RunWith(PowerMockRunner.class)
- public class StockCacheTest {
- @Test
- public void testGetStockDetail() {
- // START Mocked cachedStock
- Map<String, String[]> cachedStock = new HashMap<String, String[]>();
- String stockNumber = "LAS3453SFDSFD";
- String[] stockDetail = new String[]{stockNumber, "This is the most active stock last week", "2500.00"};
- cachedStock.put(stockNumber, stockDetail);
- stockNumber = "UTR3453SFDSFD";
- stockDetail = new String[]{stockNumber, "The most successful", "644500.00"};
- cachedStock.put(stockNumber, stockDetail);
- stockNumber = "UIYR343SFDSFD";
- stockDetail = new String[]{stockNumber, "Heavily beaten.", "-45400.00"};
- cachedStock.put(stockNumber, stockDetail);
- stockNumber = "BJKB923FDSFD";
- stockDetail = new String[]{stockNumber, "Useless", "00.00"};
- cachedStock.put(stockNumber, stockDetail);
- // END Mocked cachedStock
- StockCache stockCache = new StockCache();
- Whitebox.setInternalState(stockCache, "cachedStocks", cachedStock); // SETS THE INTERNAL PRIVATE FIELD
- stockDetail = stockCache.getStockDetail("LAS3453SFDSFD");
- Assert.assertNotNull(stockDetail);
- Assert.assertEquals("This is the most active stock last week", stockDetail[1]);
- Assert.assertEquals("2500.00", stockDetail[2]);
- stockDetail = stockCache.getStockDetail("UIYR343SFDSFD");
- Assert.assertNotNull(stockDetail);
- Assert.assertEquals("Heavily beaten.", stockDetail[1]);
- Assert.assertEquals("-45400.00", stockDetail[2]);
- } // testGetStockDetail
- } // StockCacheTest
- //=======================================================================================
- class StockCache {
- private Map<String, String[]> cachedStocks;
- //-------------------------------------------------------------------------------------
- /**
- * Populates the cached stocks list
- */
- public void initializeCachedStocks() {
- // implementation to populate the cache list
- // could be from disk or network or any other
- } // initializeCachedStocks
- //-------------------------------------------------------------------------------------
- /**
- * Returns the stock details of a stock corresponding to the stock number
- *
- * @param stockNumber stock number of the stock of which we need the detail
- * @return Stock Detail if it exists in the cache, otherwise NULL
- */
- public String[] getStockDetail(String stockNumber) {
- return this.cachedStocks.get(stockNumber);
- } // getStockDetail
- } // StockCache
Add Comment
Please, Sign In to add comment