1 | |
package org.jbehave.core.io; |
2 | |
|
3 | |
import java.io.File; |
4 | |
import java.io.FileInputStream; |
5 | |
import java.net.URL; |
6 | |
import java.util.ArrayList; |
7 | |
import java.util.List; |
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | 20 | public class LoadFromRelativeFile implements ResourceLoader, StoryLoader { |
35 | |
|
36 | |
private final StoryFilePath[] traversals; |
37 | |
private final URL location; |
38 | |
|
39 | |
public LoadFromRelativeFile(URL location) { |
40 | 6 | this(location, mavenModuleStoryFilePath("src/test/java")); |
41 | 6 | } |
42 | |
|
43 | 8 | public LoadFromRelativeFile(URL location, StoryFilePath... traversals) { |
44 | 8 | this.traversals = traversals; |
45 | 8 | this.location = location; |
46 | 8 | } |
47 | |
|
48 | |
public String loadResourceAsText(String resourcePath) { |
49 | 0 | List<String> traversalPaths = new ArrayList<String>(); |
50 | 0 | String locationPath = normalise(new File(CodeLocations.getPathFromURL(location)).getAbsolutePath()); |
51 | 0 | for (StoryFilePath traversal : traversals) { |
52 | 0 | String filePath = locationPath.replace(traversal.toRemove, traversal.relativePath) + "/" + resourcePath; |
53 | 0 | File file = new File(filePath); |
54 | 0 | if (file.exists()) { |
55 | 0 | return loadContent(filePath); |
56 | |
} else { |
57 | 0 | traversalPaths.add(filePath); |
58 | |
} |
59 | |
} |
60 | 0 | throw new StoryResourceNotFound(resourcePath, traversalPaths); |
61 | |
} |
62 | |
|
63 | |
public String loadStoryAsText(String storyPath) { |
64 | 7 | List<String> traversalPaths = new ArrayList<String>(); |
65 | 7 | String locationPath = new File(CodeLocations.getPathFromURL(location)).getAbsolutePath(); |
66 | 8 | for (StoryFilePath traversal : traversals) { |
67 | 6 | String filePath = locationPath.replace(traversal.toRemove, traversal.relativePath) + "/" + storyPath; |
68 | 6 | File file = new File(filePath); |
69 | 6 | if (file.exists()) { |
70 | 5 | return loadContent(filePath); |
71 | |
} else { |
72 | 1 | traversalPaths.add(filePath); |
73 | |
} |
74 | |
} |
75 | 2 | throw new StoryResourceNotFound(storyPath, traversalPaths); |
76 | |
} |
77 | |
|
78 | |
protected String loadContent(String path) { |
79 | |
try { |
80 | 6 | return IOUtils.toString(new FileInputStream(new File(path)), true); |
81 | 1 | } catch (Exception e) { |
82 | 1 | throw new InvalidStoryResource(path, e); |
83 | |
} |
84 | |
} |
85 | |
|
86 | |
private static String normalise(String path) { |
87 | 20 | return path.replace('\\', '/'); |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | 12 | public static class StoryFilePath { |
95 | |
private final String toRemove; |
96 | |
private final String relativePath; |
97 | |
|
98 | 10 | public StoryFilePath(String toRemove, String relativePath) { |
99 | 10 | this.toRemove = normalise(toRemove); |
100 | 10 | this.relativePath = normalise(relativePath); |
101 | 10 | } |
102 | |
|
103 | |
} |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
public static StoryFilePath mavenModuleStoryFilePath(String relativePath) { |
114 | 7 | return new StoryFilePath("target/classes", relativePath); |
115 | |
} |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public static StoryFilePath mavenModuleTestStoryFilePath(String relativePath) { |
126 | 1 | return new StoryFilePath("target/test-classes", relativePath); |
127 | |
} |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public static StoryFilePath intellijProjectStoryFilePath(String relativePath) { |
138 | 1 | return new StoryFilePath("classes/production", relativePath); |
139 | |
} |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
public static StoryFilePath intellijProjectTestStoryFilePath(String relativePath) { |
150 | 1 | return new StoryFilePath("classes/test", relativePath); |
151 | |
} |
152 | |
|
153 | |
} |