forked from hub4j/github-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPayloadRule.java
More file actions
189 lines (171 loc) · 4.95 KB
/
Copy pathPayloadRule.java
File metadata and controls
189 lines (171 loc) · 4.95 KB
1
2
3
4
5
6
7
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package org.kohsuke.github;
import org.apache.commons.io.IOUtils;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.util.function.Function;
import javax.annotation.Nonnull;
// TODO: Auto-generated Javadoc
/**
* The Class PayloadRule.
*
* @author Stephen Connolly
*/
public class PayloadRule implements TestRule {
private final String type;
private Class<?> testClass;
private String resourceName;
/**
* Instantiates a new payload rule.
*
* @param type
* the type
*/
public PayloadRule(String type) {
this.type = type;
}
/**
* Apply.
*
* @param base
* the base
* @param description
* the description
* @return the statement
*/
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Payload payload = description.getAnnotation(Payload.class);
resourceName = payload == null ? description.getMethodName() : payload.value();
testClass = description.getTestClass();
try {
base.evaluate();
} finally {
resourceName = null;
}
}
};
}
/**
* As input stream.
*
* @return the input stream
* @throws FileNotFoundException
* the file not found exception
*/
public InputStream asInputStream() throws FileNotFoundException {
String name = resourceName.startsWith("/")
? resourceName + type
: testClass.getSimpleName() + "/" + resourceName + type;
InputStream stream = testClass.getResourceAsStream(name);
if (stream == null) {
throw new FileNotFoundException(String.format("Resource %s from class %s", name, testClass));
}
return stream;
}
/**
* As bytes.
*
* @return the byte[]
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public byte[] asBytes() throws IOException {
InputStream input = asInputStream();
try {
return IOUtils.toByteArray(input);
} finally {
IOUtils.closeQuietly(input);
}
}
/**
* As string.
*
* @param encoding
* the encoding
* @return the string
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public String asString(Charset encoding) throws IOException {
return new String(asBytes(), encoding.name());
}
/**
* As string.
*
* @param encoding
* the encoding
* @return the string
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public String asString(String encoding) throws IOException {
return new String(asBytes(), encoding);
}
/**
* As string.
*
* @return the string
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public String asString() throws IOException {
return new String(asBytes(), Charset.defaultCharset().name());
}
/**
* As reader.
*
* @return the reader
* @throws FileNotFoundException
* the file not found exception
*/
public Reader asReader() throws FileNotFoundException {
return new InputStreamReader(asInputStream(), Charset.defaultCharset());
}
/**
* As reader.
*
* @param transformer
* the transformer
* @return the reader
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public Reader asReader(@Nonnull Function<String, String> transformer) throws IOException {
String payloadString = asString();
return new StringReader(transformer.apply(payloadString));
}
/**
* As reader.
*
* @param encoding
* the encoding
* @return the reader
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public Reader asReader(String encoding) throws IOException {
return new InputStreamReader(asInputStream(), encoding);
}
/**
* As reader.
*
* @param encoding
* the encoding
* @return the reader
* @throws FileNotFoundException
* the file not found exception
*/
public Reader asReader(Charset encoding) throws FileNotFoundException {
return new InputStreamReader(asInputStream(), encoding);
}
}