Permalink
Browse files

Removed some unused classes and fixed more codenarc violations

  • Loading branch information...
1 parent 8bf10e1 commit eb3c51ae205258fbd74907208d6ba624362796a8 Rob Fletcher committed Sep 22, 2012
Showing with 145 additions and 358 deletions.
  1. +4 −1 config/codenarc/codenarc.xml
  2. +9 −3 src/main/groovy/co/freeside/betamax/MatchRule.groovy
  3. +0 −2 src/main/groovy/co/freeside/betamax/encoding/AbstractEncoder.groovy
  4. +3 −1 src/main/groovy/co/freeside/betamax/message/http/HttpResponseAdapter.groovy
  5. +3 −1 src/main/groovy/co/freeside/betamax/proxy/handler/ChainedHttpHandler.groovy
  6. +0 −7 src/main/groovy/co/freeside/betamax/proxy/jetty/BetamaxProxy.groovy
  7. +8 −3 src/main/groovy/co/freeside/betamax/tape/MemoryTape.groovy
  8. +6 −3 src/main/groovy/co/freeside/betamax/tape/yaml/YamlTapeLoader.groovy
  9. +15 −15 src/test/groovy/co/freeside/betamax/message/servlet/ServletRequestAdapterSpec.groovy
  10. +1 −0 src/test/groovy/co/freeside/betamax/proxy/handler/ChainedHttpHandlerSpec.groovy
  11. +4 −4 src/test/groovy/co/freeside/betamax/proxy/handler/TapeReaderSpec.groovy
  12. +3 −3 src/test/groovy/co/freeside/betamax/proxy/handler/TapeWriterSpec.groovy
  13. +3 −2 src/test/groovy/co/freeside/betamax/proxy/jetty/BetamaxProxySpec.groovy
  14. +9 −9 src/test/groovy/co/freeside/betamax/recorder/RecorderConfigurationSpec.groovy
  15. +0 −9 src/test/groovy/co/freeside/betamax/tape/WriteTapeToYamlSpec.groovy
  16. +0 −63 src/test/groovy/co/freeside/betamax/util/RequestCapturingMockHttpClient.groovy
  17. +1 −0 src/test/groovy/co/freeside/betamax/util/server/EchoHandler.groovy
  18. +1 −0 src/test/groovy/co/freeside/betamax/util/server/SlowHandler.groovy
  19. +0 −67 src/test/groovy/co/freeside/betamax/util/servlet/AbstractMockServletMessage.groovy
  20. +0 −162 src/test/groovy/co/freeside/betamax/util/servlet/MockHttpServletRequest.groovy
  21. +73 −1 src/test/groovy/co/freeside/betamax/util/servlet/MockHttpServletResponse.groovy
  22. +2 −2 src/test/groovy/co/freeside/betamax/util/servlet/MockServletOutputStream.groovy
@@ -46,13 +46,16 @@
<rule-config name="MethodName">
<property name="doNotApplyToClassNames" value="*Spec"/>
</rule-config>
+ <exclude name="FactoryMethodName"/>
</ruleset-ref>
<ruleset-ref path="rulesets/security.xml">
<exclude name="JavaIoPackageAccess"/>
</ruleset-ref>
<ruleset-ref path="rulesets/serialization.xml"/>
<ruleset-ref path="rulesets/size.xml"/>
- <ruleset-ref path="rulesets/unnecessary.xml"/>
+ <ruleset-ref path="rulesets/unnecessary.xml">
+ <exclude name="UnnecessaryPackageReference"/>
+ </ruleset-ref>
<ruleset-ref path="rulesets/unused.xml"/>
</ruleset>
@@ -68,11 +68,17 @@ enum MatchRule implements Comparator<Request> {
@Override
int compare(Request a, Request b) {
def result = a.headers.size() <=> b.headers.size()
- if (result != 0) return result
- if (a.headers.keySet() != b.headers.keySet()) return -1 // wouldn't work if we cared about ordering...
+ if (result != 0) {
+ return result
+ }
+ if (a.headers.keySet() != b.headers.keySet()) {
+ return -1 // wouldn't work if we cared about ordering...
+ }
for (header in a.headers) {
result = header.value <=> b.headers[header.key]
- if (result != 0) return result
+ if (result != 0) {
+ return result
+ }
}
0
}
@@ -21,12 +21,10 @@ import java.nio.charset.Charset
abstract class AbstractEncoder {
final String decode(InputStream input, String charset = Charset.defaultCharset().toString()) {
- if (!charset) charset = Charset.defaultCharset().toString()
new InputStreamReader(getDecodingInputStream(input), charset).text
}
final byte[] encode(String input, String charset = Charset.defaultCharset().toString()) {
- if (!charset) charset = Charset.defaultCharset().toString()
def out = new ByteArrayOutputStream()
getEncodingOutputStream(out).withStream { OutputStream stream ->
stream << input.getBytes(charset)
@@ -41,7 +41,9 @@ class HttpResponseAdapter extends AbstractMessage implements Response {
}
InputStream getBodyAsBinary() {
- if (body == null) throw new IllegalStateException('cannot read the body of a response that does not have one')
+ if (body == null) {
+ throw new IllegalStateException('cannot read the body of a response that does not have one')
+ }
new ByteArrayInputStream(body)
}
@@ -8,7 +8,9 @@ abstract class ChainedHttpHandler implements HttpHandler {
private HttpHandler next
protected final Response chain(Request request) {
- if (!next) throw new IllegalStateException('attempted to chain from the last handler in the chain')
+ if (!next) {
+ throw new IllegalStateException('attempted to chain from the last handler in the chain')
+ }
next.handle(request)
}
@@ -26,22 +26,15 @@ class BetamaxProxy extends AbstractHandler {
@Override
void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) {
def betamaxRequest = new ServletRequestAdapter(request)
-
try {
-
def betamaxResponse = handlerChain.handle(betamaxRequest)
sendResponse(betamaxResponse, response)
-
} catch (ProxyException e) {
-
log.log SEVERE, 'exception in proxy processing', e
response.sendError(e.httpStatus, e.message)
-
} catch (Exception e) {
-
log.log SEVERE, 'error recording HTTP exchange', e
response.sendError(HTTP_INTERNAL_ERROR, e.message)
-
}
}
@@ -67,7 +67,9 @@ class MemoryTape implements Tape {
}
Response play(Request request) {
- if (!mode.readable) throw new IllegalStateException('the tape is not readable')
+ if (!mode.readable) {
+ throw new IllegalStateException('the tape is not readable')
+ }
int position = findMatch(request)
if (position < 0) {
@@ -78,7 +80,9 @@ class MemoryTape implements Tape {
}
void record(Request request, Response response) {
- if (!mode.writable) throw new IllegalStateException('the tape is not writable')
+ if (!mode.writable) {
+ throw new IllegalStateException('the tape is not writable')
+ }
def interaction = new RecordedInteraction(
request: recordRequest(request),
@@ -128,7 +132,8 @@ class MemoryTape implements Tape {
}
}
if (response.hasBody()) {
- clone.body = isTextContentType(response.contentType) && isPrintable(response.bodyAsText.text) ? response.bodyAsText.text : response.bodyAsBinary.bytes
+ boolean representAsText = isTextContentType(response.contentType) && isPrintable(response.bodyAsText.text)
+ clone.body = representAsText ? response.bodyAsText.text : response.bodyAsBinary.bytes
}
clone
}
@@ -24,7 +24,10 @@ import java.util.logging.Logger
class YamlTapeLoader implements TapeLoader<YamlTape> {
- final File tapeRoot
+ public static final String FILE_CHARSET = 'UTF-8'
+
+ final File tapeRoot
+
private static final log = Logger.getLogger(YamlTapeLoader.name)
YamlTapeLoader(File tapeRoot) {
@@ -34,7 +37,7 @@ class YamlTapeLoader implements TapeLoader<YamlTape> {
YamlTape loadTape(String name) {
def file = fileFor(name)
if (file.isFile()) {
- def tape = file.withReader('UTF-8') { reader ->
+ def tape = file.withReader(FILE_CHARSET) { reader ->
YamlTape.readFrom(reader)
}
log.info "loaded tape with ${tape.size()} recorded interactions from file $file.name..."
@@ -48,7 +51,7 @@ class YamlTapeLoader implements TapeLoader<YamlTape> {
def file = fileFor(tape.name)
file.parentFile.mkdirs()
if (tape.isDirty()) {
- file.withWriter('UTF-8') { writer ->
+ file.withWriter(FILE_CHARSET) { writer ->
log.info "writing tape $tape to file $file.name..."
tape.writeTo(writer)
}
@@ -18,8 +18,8 @@ class ServletRequestAdapterSpec extends Specification {
void 'request can read basic fields'() {
given:
- servletRequest.getMethod() >> 'GET'
- servletRequest.getRequestURL() >> new StringBuffer('http://freeside.co/betamax')
+ servletRequest.method >> 'GET'
+ servletRequest.requestURL >> new StringBuffer('http://freeside.co/betamax')
and:
def request = new ServletRequestAdapter(servletRequest)
@@ -31,8 +31,8 @@ class ServletRequestAdapterSpec extends Specification {
void 'request target includes query string'() {
given:
- servletRequest.getRequestURL() >> new StringBuffer('http://freeside.co/betamax')
- servletRequest.getQueryString() >> 'q=1'
+ servletRequest.requestURL >> new StringBuffer('http://freeside.co/betamax')
+ servletRequest.queryString >> 'q=1'
and:
def request = new ServletRequestAdapter(servletRequest)
@@ -43,7 +43,7 @@ class ServletRequestAdapterSpec extends Specification {
void 'request can read headers'() {
given:
- servletRequest.getHeaderNames() >> new IteratorEnumeration([IF_NONE_MATCH, ACCEPT_ENCODING].iterator())
+ servletRequest.headerNames >> new IteratorEnumeration([IF_NONE_MATCH, ACCEPT_ENCODING].iterator())
servletRequest.getHeaders(IF_NONE_MATCH) >> new IteratorEnumeration(['abc123'].iterator())
servletRequest.getHeaders(ACCEPT_ENCODING) >> new IteratorEnumeration(['gzip', 'deflate'].iterator())
@@ -57,7 +57,7 @@ class ServletRequestAdapterSpec extends Specification {
void 'request headers are immutable'() {
given:
- servletRequest.getHeaderNames() >> EMPTY_ENUMERATION
+ servletRequest.headerNames >> EMPTY_ENUMERATION
servletRequest.getHeaders(_) >> EMPTY_ENUMERATION
and:
@@ -74,11 +74,11 @@ class ServletRequestAdapterSpec extends Specification {
given:
def bodyText = 'value=\u00a31'
def bodyBytes = bodyText.getBytes('ISO-8859-1')
- servletRequest.getInputStream() >> new MockServletInputStream(new ByteArrayInputStream(bodyBytes))
- servletRequest.getContentType() >> 'application/x-www-form-urlencoded; charset=ISO-8859-1'
- servletRequest.getContentLength() >> 8
- servletRequest.getCharacterEncoding() >> 'ISO-8859-1'
- servletRequest.getHeaderNames() >> EMPTY_ENUMERATION
+ servletRequest.inputStream >> new MockServletInputStream(new ByteArrayInputStream(bodyBytes))
+ servletRequest.contentType >> 'application/x-www-form-urlencoded; charset=ISO-8859-1'
+ servletRequest.contentLength >> 8
+ servletRequest.characterEncoding >> 'ISO-8859-1'
+ servletRequest.headerNames >> EMPTY_ENUMERATION
servletRequest.getHeaders(_) >> EMPTY_ENUMERATION
and:
@@ -92,10 +92,10 @@ class ServletRequestAdapterSpec extends Specification {
void 'request body is readable as binary'() {
given:
def body = 'value=\u00a31'.getBytes('ISO-8859-1')
- servletRequest.getInputStream() >> new MockServletInputStream(new ByteArrayInputStream(body))
- servletRequest.getContentType() >> 'application/x-www-form-urlencoded; charset=ISO-8859-1'
- servletRequest.getContentLength() >> 8
- servletRequest.getCharacterEncoding() >> 'ISO-8859-1'
+ servletRequest.inputStream >> new MockServletInputStream(new ByteArrayInputStream(body))
+ servletRequest.contentType >> 'application/x-www-form-urlencoded; charset=ISO-8859-1'
+ servletRequest.contentLength >> 8
+ servletRequest.characterEncoding >> 'ISO-8859-1'
and:
def request = new ServletRequestAdapter(servletRequest)
@@ -7,6 +7,7 @@ import spock.lang.Specification
class ChainedHttpHandlerSpec extends Specification {
ChainedHttpHandler handler = new ChainedHttpHandler() {
+ @Override
Response handle(Request request) {
throw new UnsupportedOperationException()
}
@@ -28,7 +28,7 @@ class TapeReaderSpec extends Specification {
tape.seek(request) >> false
and:
- recorder.getTape() >> tape
+ recorder.tape >> tape
when:
handler.handle(request)
@@ -41,7 +41,7 @@ class TapeReaderSpec extends Specification {
given:
def tape = Mock(Tape)
tape.isReadable() >> false
- recorder.getTape() >> tape
+ recorder.tape >> tape
when:
handler.handle(request)
@@ -56,7 +56,7 @@ class TapeReaderSpec extends Specification {
def tape = Mock(Tape)
tape.isReadable() >> true
tape.seek(request) >> true
- recorder.getTape() >> tape
+ recorder.tape >> tape
when:
def result = handler.handle(request)
@@ -73,7 +73,7 @@ class TapeReaderSpec extends Specification {
void 'throws an exception if there is no tape'() {
given:
- recorder.getTape() >> null
+ recorder.tape >> null
when:
handler.handle(request)
@@ -25,7 +25,7 @@ class TapeWriterSpec extends Specification {
and:
def tape = Mock(Tape)
- recorder.getTape() >> tape
+ recorder.tape >> tape
tape.isWritable() >> true
when:
@@ -40,7 +40,7 @@ class TapeWriterSpec extends Specification {
void 'throws an exception if there is no tape inserted'() {
given:
- recorder.getTape() >> null
+ recorder.tape >> null
when:
handler.handle(request)
@@ -53,7 +53,7 @@ class TapeWriterSpec extends Specification {
void 'throws an exception if the tape is not writable'() {
given:
def tape = Mock(Tape)
- recorder.getTape() >> tape
+ recorder.tape >> tape
tape.isWritable() >> false
when:
@@ -6,10 +6,11 @@ import co.freeside.betamax.message.servlet.ServletRequestAdapter
import co.freeside.betamax.proxy.handler.HttpHandler
import co.freeside.betamax.proxy.handler.ProxyException
import co.freeside.betamax.util.message.BasicResponse
-import co.freeside.betamax.util.servlet.MockHttpServletRequest
import co.freeside.betamax.util.servlet.MockHttpServletResponse
import spock.lang.Specification
+import javax.servlet.http.HttpServletRequest
+
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR
import static org.apache.http.HttpHeaders.ETAG
import static org.apache.http.HttpHeaders.VIA
@@ -18,7 +19,7 @@ class BetamaxProxySpec extends Specification {
BetamaxProxy proxy = new BetamaxProxy()
- MockHttpServletRequest request = new MockHttpServletRequest()
+ HttpServletRequest request = [:] as HttpServletRequest
MockHttpServletResponse response = new MockHttpServletResponse()
Response betamaxResponse
@@ -29,11 +29,11 @@ class RecorderConfigurationSpec extends Specification {
void 'recorder configuration is overridden by map arguments'() {
given:
def recorder = new Recorder(
- tapeRoot: new File(tmpdir, 'tapes'),
- proxyPort: 1337,
- defaultMode: READ_ONLY,
- proxyTimeout: 30000,
- ignoreHosts: ['localhost'],
+ tapeRoot: new File(tmpdir, 'tapes'),
+ proxyPort: 1337,
+ defaultMode: READ_ONLY,
+ proxyTimeout: 30000,
+ ignoreHosts: ['localhost'],
ignoreLocalhost: true
)
@@ -153,11 +153,11 @@ class RecorderConfigurationSpec extends Specification {
and:
def recorder = new Recorder(
- tapeRoot: new File('test/fixtures/tapes'),
+ tapeRoot: new File('test/fixtures/tapes'),
proxyPort: 1234,
- defaultMode: WRITE_ONLY,
- proxyTimeout: 10000,
- ignoreHosts: ['github.com'],
+ defaultMode: WRITE_ONLY,
+ proxyTimeout: 10000,
+ ignoreHosts: ['github.com'],
ignoreLocalhost: false
)
@@ -5,8 +5,6 @@ import co.freeside.betamax.message.Response
import co.freeside.betamax.tape.yaml.YamlTape
import co.freeside.betamax.util.message.BasicRequest
import co.freeside.betamax.util.message.BasicResponse
-import org.apache.http.HttpEntity
-import org.apache.http.entity.ByteArrayEntity
import org.yaml.snakeyaml.Yaml
import spock.lang.Shared
import spock.lang.Specification
@@ -183,11 +181,4 @@ class WriteTapeToYamlSpec extends Specification {
writer.toString().contains('body: !!binary |-')
}
- private HttpEntity createEntity(String text, String contentType, String encoding, String charset) {
- def entity = new ByteArrayEntity(text.getBytes(charset))
- entity.setContentEncoding(encoding)
- entity.setContentType(contentType)
- return entity
- }
-
}
Oops, something went wrong.

0 comments on commit eb3c51a

Please sign in to comment.