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

Classes in this File | Line Coverage | Branch Coverage | Complexity
UnderscoredCamelCaseResolver |
100% | 23/23
100% | 2/2
| 1.1666666666666667;1.167

1 |  |
     package org.jbehave.core.io;
2 |  |
3 |  |
     import java.util.Locale;
4 |  |
     import java.util.regex.Matcher;
5 |  |
6 |  |
     import org.jbehave.core.Embeddable;
7 |  |
8 |  |
     import static java.util.regex.Pattern.compile;
9 |  |
10 |  |
     /**
11 |  |
      * <p>
12 |  |
      * Resolves story paths converting the camel-cased Java core class to
13 |  |
      * lower-case underscore-separated paths e.g.:
14 |  |
      * "org.jbehave.core.ICanLogin.java" -> "org/jbehave/core/i_can_login.story".
15 |  |
      * </p>
16 |  |
      * <p>
17 |  |
      * By default, the {@link AbstractStoryPathResolver#DEFAULT_EXTENSION} is used
18 |  |
      * but this can be configured via the constructor so that we can resolve class
19 |  |
      * to use another or no extension at all, e.g.:
20 |  |
      * "org.jbehave.core.ICanLogin.java" -> "org/jbehave/core/i_can_login".
21 |  |
      * </p>
22 |  |
      * <p>
23 |  |
      * The default resolution pattern {@link #NUMBERS_AS_LOWER_CASE_LETTERS_PATTERN}
24 |  |
      * treats numbers as lower case letters, e.g.:
25 |  |
      * "org.jbehave.core.ICanLoginTo1Site.java" ->
26 |  |
      * "org/jbehave/core/i_can_login_to1_site"
27 |  |
      * </p>
28 |  |
      * <p>
29 |  |
      * Choose {@link #NUMBERS_AS_UPPER_CASE_LETTERS_PATTERN} to treat numbers as
30 |  |
      * upper case letters, e.g.: "org.jbehave.core.ICanLoginTo1Site.java" ->
31 |  |
      * "org/jbehave/core/i_can_login_to_1_site"
32 |  |
      * </p>
33 |  |
      */
34 |  |
     public class UnderscoredCamelCaseResolver extends AbstractStoryPathResolver {
35 |  |
36 |  |
             public static final String NUMBERS_AS_LOWER_CASE_LETTERS_PATTERN = "([A-Z].*?)([A-Z]|\\z)";
37 |  |
             public static final String NUMBERS_AS_UPPER_CASE_LETTERS_PATTERN = "([A-Z0-9].*?)([A-Z0-9]|\\z)";
38 |  |
             private static final String UNDERSCORE = "_";
39 |  |
             private final String resolutionPattern;
40 |  |
         private final Locale locale;
41 | 354 |
         private String wordToRemove = "";
42 |  |
43 |  |
         public UnderscoredCamelCaseResolver() {
44 | 335 |
                     this(DEFAULT_EXTENSION);
45 | 335 |
             }
46 |  |
47 |  |
             public UnderscoredCamelCaseResolver(String extension) {
48 | 353 |
                     this(extension, NUMBERS_AS_LOWER_CASE_LETTERS_PATTERN);
49 | 353 |
             }
50 |  |
51 |  |
         public UnderscoredCamelCaseResolver(String extension,
52 |  |
                 String resolutionPattern) {
53 | 354 |
             this(extension, resolutionPattern, Locale.getDefault());
54 | 354 |
         }
55 |  |
56 |  |
         public UnderscoredCamelCaseResolver(String extension,
57 |  |
                 String resolutionPattern, Locale locale) {
58 | 354 |
             super(extension);
59 | 354 |
             this.resolutionPattern = resolutionPattern;
60 | 354 |
             this.locale = locale;
61 | 354 |
         }
62 |  |
63 |  |
             @Override
64 |  |
             protected String resolveName(
65 |  |
                             Class<? extends Embeddable> embeddableClass) {
66 | 44 |
             String simpleName = embeddableClass.getSimpleName();
67 | 44 |
             simpleName = simpleName.replace(wordToRemove, "");
68 | 44 |
             Matcher matcher = compile(resolutionPattern).matcher(
69 |  |
                     simpleName);
70 | 44 |
                     int startAt = 0;
71 | 44 |
                     StringBuilder builder = new StringBuilder();
72 | 157 |
                     while (matcher.find(startAt)) {
73 | 113 |
                             builder.append(matcher.group(1).toLowerCase(locale));
74 | 113 |
                             builder.append(UNDERSCORE);
75 | 113 |
                             startAt = matcher.start(2);
76 |  |
                     }
77 | 44 |
                     return builder.substring(0, builder.length() - 1);
78 |  |
             }
79 |  |
80 |  |
         public StoryPathResolver removeFromClassName(String wordToRemove) {
81 | 1 |
             this.wordToRemove = wordToRemove;
82 | 1 |
             return this;
83 |  |
         }
84 |  |
     }