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