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