Coverage Report - org.jbehave.core.model.Meta
 
Classes in this File Line Coverage Branch Coverage Complexity
Meta
86%
31/36
57%
8/14
1.467
Meta$Property
100%
9/9
N/A
1.467
 
 1  
 package org.jbehave.core.model;
 2  
 
 3  
 import java.util.HashSet;
 4  
 import java.util.List;
 5  
 import java.util.Properties;
 6  
 import java.util.Set;
 7  
 import java.util.TreeSet;
 8  
 
 9  
 import org.apache.commons.lang.StringUtils;
 10  
 import org.apache.commons.lang.builder.ToStringBuilder;
 11  
 import org.apache.commons.lang.builder.ToStringStyle;
 12  
 
 13  
 public class Meta {
 14  
 
 15  1
     public static final Meta EMPTY = new Meta();
 16  
 
 17  
     private static final String BLANK = "";
 18  
 
 19  
     private final Properties properties;
 20  
 
 21  
     public Meta() {
 22  1
         this(new Properties());
 23  1
     }
 24  
 
 25  36
     public Meta(Properties properties) {
 26  36
         this.properties = properties;
 27  36
     }
 28  
 
 29  19
     public Meta(List<String> properties) {
 30  19
         this.properties = new Properties();
 31  19
         parse(properties);
 32  19
     }
 33  
 
 34  
     private void parse(List<String> propertiesAsString) {
 35  19
         for (String propertyAsString : new HashSet<String>(propertiesAsString)) {
 36  21
             Property property = new Property(propertyAsString);
 37  21
             this.properties.setProperty(property.getName(), property.getValue());
 38  21
         }
 39  19
     }
 40  
 
 41  
     public Set<String> getPropertyNames() {
 42  67
         Set<String> names = new TreeSet<String>();
 43  67
         for (Object key : properties.keySet()) {
 44  55
             names.add((String) key);
 45  
         }
 46  67
         return names;
 47  
     }
 48  
 
 49  
     public boolean hasProperty(String name) {
 50  0
         return properties.containsKey(name);
 51  
     }
 52  
 
 53  
     public String getProperty(String name) {
 54  60
         String value = properties.getProperty(name);
 55  60
         if (value == null) {
 56  1
             return BLANK;
 57  
         }
 58  59
         return value;
 59  
     }
 60  
 
 61  
     public Meta inheritFrom(Meta meta) {       
 62  15
         return inherit(this, meta);
 63  
     }
 64  
 
 65  
     private Meta inherit(Meta child, Meta parent) {
 66  15
         Set<String> names = new HashSet<String>(child.getPropertyNames());
 67  
         // only names that are not already present in the child are added
 68  15
         names.addAll(parent.getPropertyNames());
 69  15
         Properties inherited = new Properties();
 70  15
         for (String name : names) {
 71  0
             if (child.hasProperty(name)) {
 72  0
                 inherited.put(name, child.getProperty(name));
 73  0
             } else if (parent.hasProperty(name)) {
 74  0
                 inherited.put(name, parent.getProperty(name));
 75  
             }
 76  
         }
 77  15
         return new Meta(inherited);
 78  
     }
 79  
 
 80  
     public boolean isEmpty() {
 81  18
         return EMPTY == this;
 82  
     }
 83  
 
 84  
     @Override
 85  
     public String toString() {
 86  27
         return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
 87  
     }
 88  
 
 89  
     public static class Property {
 90  
 
 91  
         private static final String SPACE = " ";
 92  
 
 93  
         private String propertyAsString;
 94  
         private String name;
 95  
         private String value;
 96  
 
 97  45
         public Property(String propertyAsString) {
 98  45
             this.propertyAsString = propertyAsString.trim();
 99  45
             parse();
 100  45
         }
 101  
 
 102  
         private void parse() {
 103  45
             name = StringUtils.substringBefore(propertyAsString, SPACE).trim();
 104  45
             value = StringUtils.substringAfter(propertyAsString, SPACE).trim();
 105  45
         }
 106  
 
 107  
         public String getName() {
 108  45
             return name;
 109  
         }
 110  
 
 111  
         public String getValue() {
 112  45
             return value;
 113  
         }
 114  
 
 115  
     }
 116  
 
 117  
 }