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

Classes in this File | Line Coverage | Branch Coverage | Complexity
StoryLocation |
100% | 18/18
100% | 4/4
| 1.8571428571428572;1.857

1 |  |
     package org.jbehave.core.io;
2 |  |
3 |  |
     import static org.apache.commons.lang.StringUtils.removeStart;
4 |  |
5 |  |
     import java.net.MalformedURLException;
6 |  |
     import java.net.URL;
7 |  |
8 |  |
     import org.apache.commons.lang.builder.ToStringBuilder;
9 |  |
     import org.apache.commons.lang.builder.ToStringStyle;
10 |  |
11 |  |
     /**
12 |  |
      * <p>
13 |  |
      * Abstraction of a story location, handling cases in which story path is defined
14 |  |
      * as a resource in classpath or as a URL.
15 |  |
      * </p>
16 |  |
      * <p>Given a code location URL and a story path, it provides the methods:
17 |  |
      * <ul>
18 |  |
      * <li>{@link #getURL()}: the story location URL, prefixing the code location external form if story path is not a URL</li>
19 |  |
      * <li>{@link #getPath()}: the story location path, removing the code location external form if story path is a URL</li>
20 |  |
      * </ul>
21 |  |
      * </p>
22 |  |
      */
23 |  |
     public class StoryLocation {
24 |  |
25 |  |
             private final URL codeLocation;
26 |  |
             private final String storyPath;
27 |  |
             private final boolean storyPathIsURL;
28 |  |
29 | 80 |
             public StoryLocation(URL codeLocation, String storyPath) {
30 | 80 |
                     this.codeLocation = codeLocation;
31 | 80 |
                     this.storyPath = storyPath;
32 | 80 |
                     this.storyPathIsURL = isURL(storyPath);
33 | 80 |
             }
34 |  |
35 |  |
         public URL getCodeLocation() {
36 | 89 |
                     return codeLocation;
37 |  |
             }
38 |  |
39 |  |
             public String getStoryPath() {
40 | 3 |
                     return storyPath;
41 |  |
             }
42 |  |
43 |  |
             public String getURL() {
44 | 3 |
                     if (storyPathIsURL) {
45 | 2 |
                             return storyPath;
46 |  |
                     } else {
47 | 1 |
                             return codeLocation.toExternalForm() + storyPath;
48 |  |
                     }
49 |  |
             }
50 |  |
51 |  |
             public String getPath() {
52 | 85 |
                     if (storyPathIsURL) {
53 | 4 |
                             return removeStart(storyPath, codeLocation.toExternalForm());
54 |  |
                     } else {
55 | 81 |
                             return storyPath;
56 |  |
                     }
57 |  |
             }
58 |  |
59 |  |
             private boolean isURL(String path) {
60 |  |
                     try {
61 | 80 |
                             new URL(path);
62 | 4 |
                             return true;
63 | 76 |
                     } catch (MalformedURLException e) {
64 | 76 |
                             return false;
65 |  |
                     }
66 |  |
             }
67 |  |
68 |  |
             @Override
69 |  |
             public String toString() {
70 | 3 |
                     return ToStringBuilder.reflectionToString(this,
71 |  |
                                     ToStringStyle.SHORT_PREFIX_STYLE);
72 |  |
             }
73 |  |
     }