Navigation Menu

Skip to content

Commit

Permalink
Removed some unused classes and fixed more codenarc violations
Browse files Browse the repository at this point in the history
  • Loading branch information
robfletcher committed Sep 22, 2012
1 parent 8bf10e1 commit eb3c51a
Show file tree
Hide file tree
Showing 22 changed files with 145 additions and 358 deletions.
5 changes: 4 additions & 1 deletion config/codenarc/codenarc.xml
Expand Up @@ -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>
12 changes: 9 additions & 3 deletions src/main/groovy/co/freeside/betamax/MatchRule.groovy
Expand Up @@ -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
}
Expand Down
Expand Up @@ -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)
Expand Down
Expand Up @@ -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)
}

Expand Down
Expand Up @@ -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)
}

Expand Down
Expand Up @@ -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)

}
}

Expand Down
11 changes: 8 additions & 3 deletions src/main/groovy/co/freeside/betamax/tape/MemoryTape.groovy
Expand Up @@ -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) {
Expand All @@ -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),
Expand Down Expand Up @@ -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
}
Expand Down
Expand Up @@ -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) {
Expand All @@ -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..."
Expand All @@ -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)
}
Expand Down
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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())

Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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)
Expand Down
Expand Up @@ -7,6 +7,7 @@ import spock.lang.Specification
class ChainedHttpHandlerSpec extends Specification {

ChainedHttpHandler handler = new ChainedHttpHandler() {
@Override
Response handle(Request request) {
throw new UnsupportedOperationException()
}
Expand Down
Expand Up @@ -28,7 +28,7 @@ class TapeReaderSpec extends Specification {
tape.seek(request) >> false

and:
recorder.getTape() >> tape
recorder.tape >> tape

when:
handler.handle(request)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Expand Up @@ -25,7 +25,7 @@ class TapeWriterSpec extends Specification {

and:
def tape = Mock(Tape)
recorder.getTape() >> tape
recorder.tape >> tape
tape.isWritable() >> true

when:
Expand All @@ -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)
Expand All @@ -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:
Expand Down
Expand Up @@ -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
Expand All @@ -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
Expand Down
Expand Up @@ -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
)

Expand Down Expand Up @@ -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
)

Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

}

0 comments on commit eb3c51a

Please sign in to comment.