jdalbey

JUnit Testing Exercise - map coverage

Nov 22nd, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. /*  Web-CAT tells us that the removeLocation() and removeStore() methods aren't covered.
  2.     Write a JUnit test to accomplish this.
  3.     The constructor() for Store is:
  4.         public Store(String name, String address, String city)
  5. */
  6. import java.util.*;
  7. import java.io.*;
  8. /**
  9.  * Maps locations to stores
  10.  *
  11.  * @author Joe Student
  12.  * @version 11/10/13
  13.  */
  14. public class LocationMap
  15. {
  16.     /** Internal map */
  17.     private Map<String, Set<Store>> map;
  18.    
  19.     /**
  20.      * Create new location map
  21.      */
  22.     public LocationMap()
  23.     {
  24.         map = new HashMap<String, Set<Store>>();
  25.     }
  26.    
  27.     /**
  28.      * Add a location to the map
  29.      * @param location name of location
  30.      */
  31.     public void addLocation(String location)
  32.     {
  33.         map.put(location, new HashSet<Store>());
  34.     }
  35.    
  36.     /**
  37.      * Remove a location from map
  38.      *
  39.      * @param location Location to remove
  40.      * @return Set Set of stores in location
  41.      */
  42.     public Set<Store> removeLocation(String location)
  43.     {
  44.         return map.remove(location);
  45.     }
  46.    
  47.     /**
  48.      * Gets stores in location
  49.      *
  50.      * @param location Name of location
  51.      * @return Set Set of stores in location
  52.      */
  53.     public Set<Store> getStores(String location)
  54.     {
  55.         return map.get(location);
  56.     }
  57.    
  58.     /**
  59.      * Removes a store from the map
  60.      *
  61.      * @param store Store to remove
  62.      */
  63.     public void removeStore(Store store)
  64.     {
  65.         //remove each instance of the store
  66.         for (Map.Entry<String, Set<Store>> pair : map.entrySet())
  67.         {
  68.             Set<Store> stores = pair.getValue();
  69.             stores.remove(store);
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment