This repository was archived by the owner on Mar 3, 2026. It is now read-only.
forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathURLAssetTest.java
More file actions
195 lines (169 loc) · 6.31 KB
/
Copy pathURLAssetTest.java
File metadata and controls
195 lines (169 loc) · 6.31 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
190
191
192
193
194
195
package org.jooby.internal;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.jooby.MediaType;
import org.jooby.test.MockUnit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.google.common.io.ByteStreams;
@RunWith(PowerMockRunner.class)
@PrepareForTest({URLAsset.class, URL.class })
public class URLAssetTest {
@Test
public void name() throws Exception {
assertEquals("pom.xml",
new URLAsset(file("pom.xml").toURI().toURL(), "pom.xml", MediaType.js)
.name());
}
@Test
public void toStr() throws Exception {
assertEquals("URLAssetTest.js(application/javascript)",
new URLAsset(file("src/test/resources/org/jooby/internal/URLAssetTest.js").toURI().toURL(),
"URLAssetTest.js", MediaType.js)
.toString());
}
@Test
public void path() throws Exception {
assertEquals("/path/URLAssetTest.js", new URLAsset(getClass().getResource("URLAssetTest.js"),
"/path/URLAssetTest.js", MediaType.js).path());
}
@Test
public void lastModified() throws Exception {
assertTrue(new URLAsset(file("src/test/resources/org/jooby/internal/URLAssetTest.js").toURI()
.toURL(), "URLAssetTest.js", MediaType.js)
.lastModified() > 0);
}
@Test
public void lastModifiedFileNotFound() throws Exception {
assertTrue(new URLAsset(file("src/test/resources/org/jooby/internal/URLAssetTest.missing")
.toURI().toURL(), "URLAssetTest.missing", MediaType.js)
.lastModified() == -1);
}
@Test(expected = Exception.class)
public void headerFailNoConnection() throws Exception {
new MockUnit(URL.class)
.expect(unit -> {
URL url = unit.get(URL.class);
expect(url.openConnection()).andThrow(new Exception("intentional err"));
})
.run(unit -> {
new URLAsset(unit.get(URL.class), "path.js", MediaType.js);
});
}
@Test(expected = IllegalStateException.class)
public void headerFailWithConnection() throws Exception {
new MockUnit(URL.class)
.expect(unit -> {
InputStream stream = unit.mock(InputStream.class);
stream.close();
URLConnection conn = unit.mock(URLConnection.class);
conn.setUseCaches(false);
expect(conn.getContentLengthLong()).andThrow(
new IllegalStateException("intentional err"));
expect(conn.getInputStream()).andReturn(stream);
URL url = unit.get(URL.class);
expect(url.getProtocol()).andReturn("http");
expect(url.openConnection()).andReturn(conn);
})
.run(unit -> {
new URLAsset(unit.get(URL.class), "pa.ks", MediaType.js);
});
}
@Test
public void noLastModifiednoLen() throws Exception {
new MockUnit(URL.class)
.expect(unit -> {
InputStream stream = unit.mock(InputStream.class);
stream.close();
URLConnection conn = unit.mock(URLConnection.class);
conn.setUseCaches(false);
expect(conn.getContentLengthLong()).andReturn(0L);
expect(conn.getLastModified()).andReturn(0L);
expect(conn.getInputStream()).andReturn(stream);
URL url = unit.get(URL.class);
expect(url.getProtocol()).andReturn("http");
expect(url.openConnection()).andReturn(conn);
})
.run(unit -> {
URLAsset asset = new URLAsset(unit.get(URL.class), "pa.ks", MediaType.js);
assertEquals(0, asset.length());
assertEquals(-1, asset.lastModified());
});
}
@Test(expected = IllegalStateException.class)
public void headersStreamCloseFails() throws Exception {
new MockUnit(URL.class)
.expect(unit -> {
InputStream stream = unit.mock(InputStream.class);
stream.close();
expectLastCall().andThrow(new IOException("ignored"));
URLConnection conn = unit.mock(URLConnection.class);
conn.setUseCaches(false);
expect(conn.getContentLengthLong()).andThrow(
new IllegalStateException("intentional err"));
expect(conn.getInputStream()).andReturn(stream);
URL url = unit.get(URL.class);
expect(url.getProtocol()).andReturn("http");
expect(url.openConnection()).andReturn(conn);
})
.run(unit -> {
new URLAsset(unit.get(URL.class), "ala.la", MediaType.js);
});
}
@Test
public void length() throws Exception {
assertEquals(15, new URLAsset(file("src/test/resources/org/jooby/internal/URLAssetTest.js")
.toURI().toURL(), "URLAssetTest.js",
MediaType.js).length());
}
@Test
public void type() throws Exception {
assertEquals(MediaType.js,
new URLAsset(file("src/test/resources/org/jooby/internal/URLAssetTest.js").toURI().toURL(),
"URLAssetTest.js", MediaType.js)
.type());
}
@Test
public void stream() throws Exception {
InputStream stream = new URLAsset(
file("src/test/resources/org/jooby/internal/URLAssetTest.js").toURI().toURL(),
"URLAssetTest.js", MediaType.js)
.stream();
assertEquals("function () {}\n", new String(ByteStreams.toByteArray(stream)));
stream.close();
}
@Test(expected = NullPointerException.class)
public void nullFile() throws Exception {
new URLAsset((URL) null, "", MediaType.js);
}
@Test(expected = NullPointerException.class)
public void nullType() throws Exception {
new URLAsset(file("src/test/resources/org/jooby/internal/URLAssetTest.js").toURI().toURL(),
"", null);
}
/**
* Attempt to load a file from multiple location. required by unit and integration tests.
*
* @param location
* @return
*/
private File file(final String location) {
for (String candidate : new String[]{location, "jooby/" + location,
"../../jooby/" + location }) {
File file = new File(candidate);
if (file.exists()) {
return file;
}
}
return new File(location);
}
}