Skip to content

Commit 3c02d91

Browse files
committed
[Tech] clean code.
1 parent 78a94b5 commit 3c02d91

2 files changed

Lines changed: 16 additions & 15 deletions

File tree

src/test/java/com/github/greengerong/PreRenderSEOFilterTest.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@
2323
import java.util.HashMap;
2424

2525
import static com.github.greengerong.PrerenderSeoService.ESCAPED_FRAGMENT_KEY;
26+
import static org.apache.http.HttpStatus.SC_NOT_FOUND;
2627
import static org.apache.http.HttpStatus.SC_OK;
28+
import static org.apache.http.client.methods.HttpGet.METHOD_NAME;
2729
import static org.mockito.Mockito.*;
2830

2931
@RunWith(MockitoJUnitRunner.class)
3032
public class PreRenderSEOFilterTest {
3133

32-
public static final int NOT_FOUND = 404;
3334
private PreRenderSEOFilter preRenderSEOFilter;
3435

3536
@Mock
@@ -95,7 +96,7 @@ public void should_handle_when_url_with_escaped_fragment_() throws Exception {
9596
final StatusLine statusLine = mock(StatusLine.class);
9697

9798
when(servletRequest.getRequestURL()).thenReturn(new StringBuffer("http://localhost/test"));
98-
when(servletRequest.getMethod()).thenReturn(HttpGet.METHOD_NAME);
99+
when(servletRequest.getMethod()).thenReturn(METHOD_NAME);
99100
when(servletRequest.getHeaderNames()).thenReturn(mock(Enumeration.class));
100101
when(httpClient.execute(httpGet)).thenReturn(httpResponse);
101102
when(httpResponse.getStatusLine()).thenReturn(statusLine);
@@ -120,7 +121,7 @@ public void should_not_handle_when_user_agent_is_not_crawler() throws Exception
120121
preRenderSEOFilter.init(filterConfig);
121122

122123
when(servletRequest.getRequestURL()).thenReturn(new StringBuffer("http://localhost/test"));
123-
when(servletRequest.getMethod()).thenReturn(HttpGet.METHOD_NAME);
124+
when(servletRequest.getMethod()).thenReturn(METHOD_NAME);
124125
when(servletRequest.getParameterMap()).thenReturn(Maps.<String, String>newHashMap());
125126
when(servletRequest.getHeader("User-Agent")).thenReturn("no");
126127
//when
@@ -138,7 +139,7 @@ public void should_not_handle_when_url_is_a_resource() throws Exception {
138139
preRenderSEOFilter.init(filterConfig);
139140

140141
when(servletRequest.getRequestURL()).thenReturn(new StringBuffer("http://localhost/test.js"));
141-
when(servletRequest.getMethod()).thenReturn(HttpGet.METHOD_NAME);
142+
when(servletRequest.getMethod()).thenReturn(METHOD_NAME);
142143
when(servletRequest.getParameterMap()).thenReturn(Maps.<String, String>newHashMap());
143144
when(servletRequest.getHeader("User-Agent")).thenReturn("crawler1");
144145
//when
@@ -157,7 +158,7 @@ public void should_not_handle_when_white_list_is_not_empty_and_url_is_not_in_whi
157158
preRenderSEOFilter.init(filterConfig);
158159

159160
when(servletRequest.getRequestURL()).thenReturn(new StringBuffer("http://localhost/test"));
160-
when(servletRequest.getMethod()).thenReturn(HttpGet.METHOD_NAME);
161+
when(servletRequest.getMethod()).thenReturn(METHOD_NAME);
161162
when(servletRequest.getParameterMap()).thenReturn(Maps.<String, String>newHashMap());
162163
when(servletRequest.getHeader("User-Agent")).thenReturn("crawler1");
163164
//when
@@ -176,7 +177,7 @@ public void should_not_handle_when_black_list_is_not_empty_and_url_is_in_black_l
176177
preRenderSEOFilter.init(filterConfig);
177178

178179
when(servletRequest.getRequestURL()).thenReturn(new StringBuffer("http://localhost/test"));
179-
when(servletRequest.getMethod()).thenReturn(HttpGet.METHOD_NAME);
180+
when(servletRequest.getMethod()).thenReturn(METHOD_NAME);
180181
when(servletRequest.getParameterMap()).thenReturn(Maps.<String, String>newHashMap());
181182
when(servletRequest.getHeader("User-Agent")).thenReturn("crawler1");
182183
//when
@@ -197,7 +198,7 @@ public void should_handle_when_user_agent_is_crawler_and_url_is_not_resource_and
197198
final StatusLine statusLine = mock(StatusLine.class);
198199

199200
when(servletRequest.getRequestURL()).thenReturn(new StringBuffer("http://localhost/test"));
200-
when(servletRequest.getMethod()).thenReturn(HttpGet.METHOD_NAME);
201+
when(servletRequest.getMethod()).thenReturn(METHOD_NAME);
201202
when(servletRequest.getHeader("User-Agent")).thenReturn("crawler1");
202203

203204
when(servletRequest.getHeaderNames()).thenReturn(mock(Enumeration.class));
@@ -227,7 +228,7 @@ public void should_handle_when_every_thing_is_ok_but_prerender_server_response_i
227228
final StatusLine statusLine = mock(StatusLine.class);
228229

229230
when(servletRequest.getRequestURL()).thenReturn(new StringBuffer("http://localhost/test"));
230-
when(servletRequest.getMethod()).thenReturn(HttpGet.METHOD_NAME);
231+
when(servletRequest.getMethod()).thenReturn(METHOD_NAME);
231232
when(servletRequest.getHeader("User-Agent")).thenReturn("crawler1");
232233

233234
when(servletRequest.getHeaderNames()).thenReturn(mock(Enumeration.class));
@@ -236,7 +237,7 @@ public void should_handle_when_every_thing_is_ok_but_prerender_server_response_i
236237
final HashMap<String, String> map = Maps.newHashMap();
237238
map.put(ESCAPED_FRAGMENT_KEY, "");
238239
when(servletRequest.getParameterMap()).thenReturn(map);
239-
when(statusLine.getStatusCode()).thenReturn(NOT_FOUND);
240+
when(statusLine.getStatusCode()).thenReturn(SC_NOT_FOUND);
240241
when(httpResponse.getAllHeaders()).thenReturn(new Header[0]);
241242
when(servletResponse.getWriter()).thenReturn(printWriter);
242243

@@ -247,7 +248,7 @@ public void should_handle_when_every_thing_is_ok_but_prerender_server_response_i
247248
//then
248249
verify(httpClient).execute(httpGet);
249250
verify(filterChain, never()).doFilter(servletRequest, servletResponse);
250-
verify(servletResponse).setStatus(NOT_FOUND);
251+
verify(servletResponse).setStatus(SC_NOT_FOUND);
251252
}
252253

253254

@@ -264,7 +265,7 @@ public void should_handle_when_user_agent_is_crawler_and_url_is_not_resource_and
264265
final StatusLine statusLine = mock(StatusLine.class);
265266

266267
when(servletRequest.getRequestURL()).thenReturn(new StringBuffer("http://localhost/test"));
267-
when(servletRequest.getMethod()).thenReturn(HttpGet.METHOD_NAME);
268+
when(servletRequest.getMethod()).thenReturn(METHOD_NAME);
268269
when(servletRequest.getHeader("User-Agent")).thenReturn("crawler1");
269270

270271
when(servletRequest.getHeaderNames()).thenReturn(mock(Enumeration.class));
@@ -298,7 +299,7 @@ public void should_use_request_url_from_custom_header_if_available() throws Exce
298299
final StatusLine statusLine = mock(StatusLine.class);
299300

300301
when(servletRequest.getRequestURL()).thenReturn(new StringBuffer("http://localhost/test"));
301-
when(servletRequest.getMethod()).thenReturn(HttpGet.METHOD_NAME);
302+
when(servletRequest.getMethod()).thenReturn(METHOD_NAME);
302303
when(servletRequest.getHeader("X-Forwarded-URL")).thenReturn("http://my.public.domain.com/");
303304

304305
when(servletRequest.getHeaderNames()).thenReturn(mock(Enumeration.class));

src/test/java/com/github/greengerong/PrerenderConfigTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.github.greengerong;
22

3+
import com.google.common.collect.Maps;
34
import org.apache.http.impl.client.CloseableHttpClient;
45
import org.junit.Test;
56

6-
import java.util.HashMap;
77
import java.util.Map;
88

99
import static org.hamcrest.core.Is.is;
@@ -14,7 +14,7 @@ public class PrerenderConfigTest {
1414
@Test(expected = Exception.class)
1515
public void should_throw_exception_if_invalid_timeout_value_specified() throws Exception {
1616
//given
17-
Map<String, String> configuration = new HashMap<String, String>();
17+
Map<String, String> configuration = Maps.newHashMap();
1818
configuration.put("socketTimeout", "not_an_int");
1919
PrerenderConfig config = new PrerenderConfig(configuration);
2020
//when
@@ -24,7 +24,7 @@ public void should_throw_exception_if_invalid_timeout_value_specified() throws E
2424
@Test
2525
public void should_pass_if_correct_timeout_value_specified() throws Exception {
2626
//given
27-
Map<String, String> configuration = new HashMap<String, String>();
27+
Map<String, String> configuration = Maps.newHashMap();
2828
configuration.put("socketTimeout", "1000");
2929
PrerenderConfig config = new PrerenderConfig(configuration);
3030
//when

0 commit comments

Comments
 (0)