##### Coverage Report - org.jbehave.core.io.RelativePathCalculator

Classes in this File | Line Coverage | Branch Coverage | Complexity
RelativePathCalculator |
100% | 16/16
87% | 7/8
| 2.5;2.5

1 |  |
     package org.jbehave.core.io;
2 |  |
3 |  |
     import java.util.ArrayList;
4 |  |
     import java.util.LinkedList;
5 |  |
     import java.util.List;
6 |  |
7 |  |
     import static java.util.Arrays.asList;
8 |  |
9 |  |
     /**
10 |  |
      * {@link PathCalculator} that finds given stories relative to the current story.
11 |  |
      */
12 | 2 |
     public class RelativePathCalculator implements PathCalculator {
13 |  |
14 |  |
         public String calculate(String root, String path) {
15 | 6 |
             return join(calculatePath(split(root), split(path)));
16 |  |
         }
17 |  |
18 |  |
         private List<String> split(String path) {
19 | 12 |
             if (path.trim().length() == 0) {
20 | 1 |
                 return new LinkedList<String>();
21 |  |
             }
22 |  |
23 | 11 |
             return new LinkedList<String>(asList(path.replace('\\', '/').split("/")));
24 |  |
         }
25 |  |
26 |  |
         private Iterable<String> calculatePath(List<String> root, List<String> path) {
27 | 6 |
             if (path.get(0).length() == 0) {
28 | 4 |
                 return path.subList(1, path.size());
29 |  |
             }
30 |  |
31 | 2 |
             List<String> list = new ArrayList<String>();
32 | 2 |
             if (root.size() > 0) {
33 | 2 |
                 list.addAll(root.subList(0, root.size() - 1));
34 |  |
             }
35 | 2 |
             list.addAll(path);
36 |  |
37 | 2 |
             return list;
38 |  |
         }
39 |  |
40 |  |
         private String join(Iterable<String> list) {
41 | 6 |
             StringBuilder sb = new StringBuilder();
42 |  |
43 | 6 |
             for (String each : list) {
44 | 8 |
                 sb.append("/").append(each);
45 |  |
             }
46 |  |
47 | 6 |
             return sb.substring(1);
48 |  |
         }
49 |  |
     }