001    package org.hackystat.dailyprojectdata.resource.unittest;
002    
003    import java.math.BigInteger;
004    import java.util.HashMap;
005    import java.util.HashSet;
006    import java.util.Map;
007    import java.util.Set;
008    
009    import org.hackystat.sensorbase.resource.sensordata.jaxb.Properties;
010    import org.hackystat.sensorbase.resource.sensordata.jaxb.Property;
011    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;
012    
013    /**
014     * A data structure that collects the Unit Test pass and fail counts for each member.
015     *
016     * @author Pavel Senin, Philip Johnson
017     *
018     */
019    public class UnitTestCounter {
020    
021      /** Maps members to the number of successful unit test invocations. */
022      private Map<String, Integer> passCount = new HashMap<String, Integer>();
023      
024      /** Maps members to the number of unsuccessful unit test invocations. */
025      private Map<String, Integer> failCount = new HashMap<String, Integer>();
026    
027      /**
028       * Standard constructor does pretty much nothing.
029       */
030      public UnitTestCounter() {
031        // No need to do anything.
032      }
033    
034      /**
035       * Does accounting using sensor data provided.
036       *
037       * @param data unit test sensor data.
038       */
039      public void add(SensorData data) {
040    
041        // Initialize the maps for this user if necessary.
042        String owner = data.getOwner();
043        if (!passCount.containsKey(owner)) {
044          passCount.put(owner, 0);
045          failCount.put(owner, 0);
046        }
047        
048        // Now update the pass or fail count. 
049        // Result property must exist and must be pass or fail.
050        String result = getValue("Result", data);
051        if ((result != null) && (result.equalsIgnoreCase("pass"))) {
052          passCount.put(owner, (passCount.get(owner) + 1));
053        }
054        if ((result != null) && (result.equalsIgnoreCase("fail"))) {
055          failCount.put(owner, (failCount.get(owner) + 1));
056        }
057      }
058      
059      /**
060       * Returns the (first) value associated with key in Properties, or null if not found.
061       * Assumes that keys are unique.
062       * @param key The key
063       * @param data The Sensor Data instance.
064       * @return The value associated with key, or null if not found. 
065       */
066      private String getValue(String key, SensorData data) {
067        Properties properties = data.getProperties();
068        for (Property property : properties.getProperty()) {
069          if (property.getKey().equals(key)) {
070            return property.getValue();
071          }
072        }
073        return null;
074      }
075    
076      /**
077       * Returns the UnitTest failure count associated with Member, or zero if member does not exist.
078       *
079       * @param member The member.
080       * @return The member's failure count.
081       */
082      public BigInteger getFailCount(String member) {
083        int numFails =  ((failCount.containsKey(member)) ? failCount.get(member) : 0);
084        return BigInteger.valueOf(numFails);
085      }
086      
087      /**
088       * Returns the UnitTest pass count associated with Member, or zero if member does not exist.
089       *
090       * @param member The member.
091       * @return The member's pass count.
092       */
093      public BigInteger getPassCount(String member) {
094        int numSuccess =  ((passCount.containsKey(member)) ? passCount.get(member) : 0);
095        return BigInteger.valueOf(numSuccess);
096      }
097    
098      /**
099       * Returns a newly created Set containing all of the members in this Counter.
100       *
101       * @return The set of all members in this UnitTestDPDCounter.
102       */
103      public Set<String> getMembers() {
104        Set<String> members = new HashSet<String>();
105        members.addAll(failCount.keySet());
106        return members;
107      }
108    
109    }