##### Coverage Report - org.jbehave.core.model.Meta

Classes in this File | Line Coverage | Branch Coverage | Complexity
Meta |
100% | 42/42
100% | 16/16
| 1.5625;1.562
Meta$Property |
100% | 9/9
N/A | N/A
| 1.5625;1.562

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