Skip to content

Commit

Permalink
utility method to avoid repetitive temp dir declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
robfletcher committed Sep 28, 2012
1 parent b1f6238 commit 76e2e14
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 67 deletions.
3 changes: 2 additions & 1 deletion src/test/groovy/co/freeside/betamax/AnnotationSpec.groovy
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import groovyx.net.http.RESTClient
import org.junit.Rule import org.junit.Rule
import spock.lang.* import spock.lang.*
import static co.freeside.betamax.proxy.jetty.BetamaxProxy.X_BETAMAX import static co.freeside.betamax.proxy.jetty.BetamaxProxy.X_BETAMAX
import static co.freeside.betamax.util.FileUtils.newTempDir
import static java.net.HttpURLConnection.HTTP_OK import static java.net.HttpURLConnection.HTTP_OK
import static org.apache.http.HttpHeaders.VIA import static org.apache.http.HttpHeaders.VIA


@Stepwise @Stepwise
class AnnotationSpec extends Specification { class AnnotationSpec extends Specification {


@Shared @AutoCleanup('deleteDir') File tapeRoot = new File(System.properties.'java.io.tmpdir', 'tapes') @Shared @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
@Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot) @Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot)
@AutoCleanup('stop') SimpleServer endpoint = new SimpleServer() @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
RESTClient http RESTClient http
Expand Down
6 changes: 3 additions & 3 deletions src/test/groovy/co/freeside/betamax/LocalhostSpec.groovy
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package co.freeside.betamax
import co.freeside.betamax.httpclient.BetamaxRoutePlanner import co.freeside.betamax.httpclient.BetamaxRoutePlanner
import co.freeside.betamax.proxy.jetty.SimpleServer import co.freeside.betamax.proxy.jetty.SimpleServer
import co.freeside.betamax.util.server.EchoHandler import co.freeside.betamax.util.server.EchoHandler
import groovyx.net.http.HttpResponseDecorator import groovyx.net.http.*
import groovyx.net.http.RESTClient
import org.junit.Rule import org.junit.Rule
import spock.lang.* import spock.lang.*
import static co.freeside.betamax.TapeMode.WRITE_ONLY import static co.freeside.betamax.TapeMode.WRITE_ONLY
import static co.freeside.betamax.util.FileUtils.newTempDir
import static java.net.HttpURLConnection.HTTP_OK import static java.net.HttpURLConnection.HTTP_OK
import static org.apache.http.HttpHeaders.VIA import static org.apache.http.HttpHeaders.VIA


Expand All @@ -16,7 +16,7 @@ import static org.apache.http.HttpHeaders.VIA
@Unroll @Unroll
class LocalhostSpec extends Specification { class LocalhostSpec extends Specification {


@Shared @AutoCleanup('deleteDir') File tapeRoot = new File(System.properties.'java.io.tmpdir', 'tapes') @Shared @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
@Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot) @Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot)


@Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer() @Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
Expand Down
3 changes: 2 additions & 1 deletion src/test/groovy/co/freeside/betamax/proxy/HttpsSpec.groovy
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.DefaultHttpClient import org.apache.http.impl.client.DefaultHttpClient
import org.junit.Rule import org.junit.Rule
import spock.lang.* import spock.lang.*
import static co.freeside.betamax.util.FileUtils.newTempDir
import static org.apache.http.HttpHeaders.VIA import static org.apache.http.HttpHeaders.VIA
import static org.apache.http.HttpStatus.SC_OK import static org.apache.http.HttpStatus.SC_OK


@Issue('https://github.com/robfletcher/betamax/issues/34') @Issue('https://github.com/robfletcher/betamax/issues/34')
@Unroll @Unroll
class HttpsSpec extends Specification { class HttpsSpec extends Specification {


@Shared @AutoCleanup('deleteDir') File tapeRoot = new File(System.properties.'java.io.tmpdir', 'tapes') @Shared @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
@Rule @AutoCleanup('ejectTape') Recorder recorder = new Recorder(tapeRoot: tapeRoot, sslSupport: true) @Rule @AutoCleanup('ejectTape') Recorder recorder = new Recorder(tapeRoot: tapeRoot, sslSupport: true)
@Shared @AutoCleanup('stop') SimpleServer httpsEndpoint = new SimpleSecureServer(5001) @Shared @AutoCleanup('stop') SimpleServer httpsEndpoint = new SimpleSecureServer(5001)
@Shared @AutoCleanup('stop') SimpleServer httpEndpoint = new SimpleServer() @Shared @AutoCleanup('stop') SimpleServer httpEndpoint = new SimpleServer()
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ package co.freeside.betamax.proxy


import co.freeside.betamax.Recorder import co.freeside.betamax.Recorder
import co.freeside.betamax.httpclient.BetamaxRoutePlanner import co.freeside.betamax.httpclient.BetamaxRoutePlanner
import co.freeside.betamax.proxy.jetty.ProxyServer import co.freeside.betamax.proxy.jetty.*
import co.freeside.betamax.proxy.jetty.SimpleServer
import co.freeside.betamax.util.server.EchoHandler import co.freeside.betamax.util.server.EchoHandler
import groovyx.net.http.RESTClient import groovyx.net.http.RESTClient
import spock.lang.* import spock.lang.*

import static co.freeside.betamax.util.FileUtils.newTempDir
import static org.apache.http.HttpHeaders.VIA import static org.apache.http.HttpHeaders.VIA


@Issue('https://github.com/robfletcher/betamax/issues/16') @Issue('https://github.com/robfletcher/betamax/issues/16')
@Unroll @Unroll
class IgnoreHostsSpec extends Specification { class IgnoreHostsSpec extends Specification {


@Shared @AutoCleanup('deleteDir') File tapeRoot = new File(System.properties.'java.io.tmpdir', 'tapes') @Shared @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
@Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer() @Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
@AutoCleanup('ejectTape') Recorder recorder = new Recorder(tapeRoot: tapeRoot) @AutoCleanup('ejectTape') Recorder recorder = new Recorder(tapeRoot: tapeRoot)
@AutoCleanup('stop') ProxyServer proxy = new ProxyServer(recorder) @AutoCleanup('stop') ProxyServer proxy = new ProxyServer(recorder)
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,20 +1,19 @@
package co.freeside.betamax.proxy package co.freeside.betamax.proxy


import co.freeside.betamax.Betamax import co.freeside.betamax.*
import co.freeside.betamax.Recorder
import co.freeside.betamax.proxy.jetty.SimpleServer import co.freeside.betamax.proxy.jetty.SimpleServer
import co.freeside.betamax.util.server.HelloHandler import co.freeside.betamax.util.server.HelloHandler
import org.junit.Rule import org.junit.Rule
import spock.lang.* import spock.lang.*

import static co.freeside.betamax.util.FileUtils.newTempDir
import static co.freeside.betamax.util.server.HelloHandler.HELLO_WORLD import static co.freeside.betamax.util.server.HelloHandler.HELLO_WORLD
import static java.net.HttpURLConnection.HTTP_OK import static java.net.HttpURLConnection.HTTP_OK
import static org.apache.http.HttpHeaders.VIA import static org.apache.http.HttpHeaders.VIA


@Issue('https://github.com/robfletcher/betamax/issues/54') @Issue('https://github.com/robfletcher/betamax/issues/54')
class PreExistingProxySpec extends Specification { class PreExistingProxySpec extends Specification {


@AutoCleanup('deleteDir') File tapeRoot = new File(System.properties.'java.io.tmpdir', 'tapes') @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
@Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot) @Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot)
@Shared @AutoCleanup('stop') SimpleServer proxyServer = new SimpleServer() @Shared @AutoCleanup('stop') SimpleServer proxyServer = new SimpleServer()


Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,27 +1,24 @@
package co.freeside.betamax.proxy package co.freeside.betamax.proxy


import co.freeside.betamax.Betamax import co.freeside.betamax.*
import co.freeside.betamax.Recorder
import co.freeside.betamax.httpclient.BetamaxRoutePlanner import co.freeside.betamax.httpclient.BetamaxRoutePlanner
import co.freeside.betamax.proxy.jetty.SimpleServer import co.freeside.betamax.proxy.jetty.SimpleServer
import co.freeside.betamax.util.server.EchoHandler import co.freeside.betamax.util.server.EchoHandler
import groovyx.net.http.HttpURLClient import groovyx.net.http.*
import groovyx.net.http.RESTClient import org.apache.commons.httpclient.*
import org.apache.commons.httpclient.HttpClient
import org.apache.commons.httpclient.ProxyHost
import org.apache.commons.httpclient.methods.GetMethod import org.apache.commons.httpclient.methods.GetMethod
import org.apache.http.HttpHost import org.apache.http.HttpHost
import org.junit.Rule import org.junit.Rule
import spock.lang.* import spock.lang.*

import static co.freeside.betamax.util.FileUtils.newTempDir
import static java.net.HttpURLConnection.HTTP_OK import static java.net.HttpURLConnection.HTTP_OK
import static org.apache.http.HttpHeaders.VIA import static org.apache.http.HttpHeaders.VIA
import static org.apache.http.conn.params.ConnRoutePNames.DEFAULT_PROXY import static org.apache.http.conn.params.ConnRoutePNames.DEFAULT_PROXY


@Unroll @Unroll
class ProxyNetworkCommsSpec extends Specification { class ProxyNetworkCommsSpec extends Specification {


@AutoCleanup('deleteDir') File tapeRoot = new File(System.properties.'java.io.tmpdir', 'tapes') @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
@Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot) @Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot)
@Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer() @Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ package co.freeside.betamax.proxy


import co.freeside.betamax.Recorder import co.freeside.betamax.Recorder
import co.freeside.betamax.httpclient.BetamaxRoutePlanner import co.freeside.betamax.httpclient.BetamaxRoutePlanner
import co.freeside.betamax.proxy.jetty.ProxyServer import co.freeside.betamax.proxy.jetty.*
import co.freeside.betamax.proxy.jetty.SimpleServer
import co.freeside.betamax.util.server.EchoHandler import co.freeside.betamax.util.server.EchoHandler
import groovyx.net.http.RESTClient import groovyx.net.http.RESTClient
import org.yaml.snakeyaml.Yaml import org.yaml.snakeyaml.Yaml
import spock.lang.* import spock.lang.*

import static co.freeside.betamax.util.FileUtils.newTempDir
import static java.net.HttpURLConnection.HTTP_OK import static java.net.HttpURLConnection.HTTP_OK


@Stepwise @Stepwise
class ProxyRecordAndPlaybackSpec extends Specification { class ProxyRecordAndPlaybackSpec extends Specification {


@Shared @AutoCleanup('deleteDir') File tapeRoot = new File(System.properties.'java.io.tmpdir', 'tapes') @Shared @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
@Shared @AutoCleanup('ejectTape') Recorder recorder = new Recorder(tapeRoot: tapeRoot) @Shared @AutoCleanup('ejectTape') Recorder recorder = new Recorder(tapeRoot: tapeRoot)
@Shared @AutoCleanup('stop') ProxyServer proxy = new ProxyServer(recorder) @Shared @AutoCleanup('stop') ProxyServer proxy = new ProxyServer(recorder)
@AutoCleanup('stop') SimpleServer endpoint = new SimpleServer() @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
Expand Down
15 changes: 5 additions & 10 deletions src/test/groovy/co/freeside/betamax/proxy/ProxyTimeoutSpec.groovy
Original file line number Original file line Diff line number Diff line change
@@ -1,27 +1,22 @@
package co.freeside.betamax.proxy package co.freeside.betamax.proxy


import co.freeside.betamax.Betamax import co.freeside.betamax.*
import co.freeside.betamax.Recorder
import co.freeside.betamax.httpclient.BetamaxRoutePlanner import co.freeside.betamax.httpclient.BetamaxRoutePlanner
import co.freeside.betamax.proxy.jetty.SimpleServer import co.freeside.betamax.proxy.jetty.SimpleServer
import co.freeside.betamax.util.server.SlowHandler import co.freeside.betamax.util.server.SlowHandler
import groovyx.net.http.HttpResponseException import groovyx.net.http.*
import groovyx.net.http.RESTClient
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler import org.apache.http.impl.client.DefaultHttpRequestRetryHandler
import org.junit.Rule import org.junit.Rule
import spock.lang.AutoCleanup import spock.lang.*
import spock.lang.Issue import static co.freeside.betamax.util.FileUtils.newTempDir
import spock.lang.Shared
import spock.lang.Specification

import static java.net.HttpURLConnection.HTTP_GATEWAY_TIMEOUT import static java.net.HttpURLConnection.HTTP_GATEWAY_TIMEOUT


@Issue('https://github.com/robfletcher/betamax/issues/20') @Issue('https://github.com/robfletcher/betamax/issues/20')
class ProxyTimeoutSpec extends Specification { class ProxyTimeoutSpec extends Specification {


@Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot, proxyTimeout: 100) @Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot, proxyTimeout: 100)


@Shared @AutoCleanup('deleteDir') File tapeRoot = new File(System.properties.'java.io.tmpdir', 'tapes') @Shared @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
@AutoCleanup('stop') SimpleServer endpoint = new SimpleServer() @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
RESTClient http RESTClient http


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import co.freeside.betamax.Recorder
import co.freeside.betamax.httpclient.BetamaxRoutePlanner import co.freeside.betamax.httpclient.BetamaxRoutePlanner
import groovyx.net.http.RESTClient import groovyx.net.http.RESTClient
import spock.lang.* import spock.lang.*

import static co.freeside.betamax.MatchRule.host import static co.freeside.betamax.MatchRule.host
import static co.freeside.betamax.util.FileUtils.newTempDir
import static org.apache.http.HttpHeaders.VIA import static org.apache.http.HttpHeaders.VIA


@Issue('https://github.com/robfletcher/betamax/issues/9') @Issue('https://github.com/robfletcher/betamax/issues/9')
class RequestMatchingSpec extends Specification { class RequestMatchingSpec extends Specification {


@Shared @AutoCleanup('deleteDir') File tapeRoot = new File(System.properties.'java.io.tmpdir', 'tapes') @Shared @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
@Shared Recorder recorder = new Recorder(tapeRoot: tapeRoot) @Shared Recorder recorder = new Recorder(tapeRoot: tapeRoot)
@Shared RESTClient http = new RESTClient() @Shared RESTClient http = new RESTClient()


Expand Down
20 changes: 7 additions & 13 deletions src/test/groovy/co/freeside/betamax/recorder/TapeModeSpec.groovy
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@ package co.freeside.betamax.recorder


import co.freeside.betamax.Recorder import co.freeside.betamax.Recorder
import co.freeside.betamax.httpclient.BetamaxRoutePlanner import co.freeside.betamax.httpclient.BetamaxRoutePlanner
import co.freeside.betamax.proxy.jetty.ProxyServer import co.freeside.betamax.proxy.jetty.*
import co.freeside.betamax.proxy.jetty.SimpleServer
import co.freeside.betamax.util.server.EchoHandler import co.freeside.betamax.util.server.EchoHandler
import groovyx.net.http.HttpResponseException import groovyx.net.http.*
import groovyx.net.http.RESTClient import spock.lang.*
import spock.lang.AutoCleanup import static co.freeside.betamax.TapeMode.*
import spock.lang.Shared import static co.freeside.betamax.util.FileUtils.newTempDir
import spock.lang.Specification import static java.net.HttpURLConnection.*

import static co.freeside.betamax.TapeMode.READ_ONLY
import static co.freeside.betamax.TapeMode.WRITE_ONLY
import static java.net.HttpURLConnection.HTTP_FORBIDDEN
import static java.net.HttpURLConnection.HTTP_OK


class TapeModeSpec extends Specification { class TapeModeSpec extends Specification {


@Shared @AutoCleanup('deleteDir') File tapeRoot = new File(System.properties.'java.io.tmpdir', 'tapes') @Shared @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
@Shared Recorder recorder = new Recorder(tapeRoot: tapeRoot) @Shared Recorder recorder = new Recorder(tapeRoot: tapeRoot)
@Shared @AutoCleanup('stop') ProxyServer proxy = new ProxyServer(recorder) @Shared @AutoCleanup('stop') ProxyServer proxy = new ProxyServer(recorder)
@Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer() @Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,15 +1,13 @@
package co.freeside.betamax.tape package co.freeside.betamax.tape


import co.freeside.betamax.tape.yaml.YamlTapeLoader import co.freeside.betamax.tape.yaml.YamlTapeLoader
import spock.lang.AutoCleanup import spock.lang.*
import spock.lang.Shared import static co.freeside.betamax.util.FileUtils.newTempDir
import spock.lang.Specification
import spock.lang.Unroll


@Unroll @Unroll
class FilenameNormalizationSpec extends Specification { class FilenameNormalizationSpec extends Specification {


@Shared @AutoCleanup('deleteDir') File tapeRoot = new File(System.properties.'java.io.tmpdir', 'tapes') @Shared @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')


void "a tape named '#tapeName' is written to a file called '#filename'"() { void "a tape named '#tapeName' is written to a file called '#filename'"() {
given: given:
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import co.freeside.betamax.proxy.jetty.SimpleServer
import co.freeside.betamax.util.server.HelloHandler import co.freeside.betamax.util.server.HelloHandler
import org.junit.Rule import org.junit.Rule
import spock.lang.* import spock.lang.*
import static co.freeside.betamax.util.FileUtils.newTempDir
import static co.freeside.betamax.util.server.HelloHandler.HELLO_WORLD import static co.freeside.betamax.util.server.HelloHandler.HELLO_WORLD
import static java.util.concurrent.TimeUnit.SECONDS import static java.util.concurrent.TimeUnit.SECONDS


class MultiThreadedTapeWritingSpec extends Specification { class MultiThreadedTapeWritingSpec extends Specification {


@Shared @AutoCleanup('deleteDir') File tapeRoot = new File(System.properties.'java.io.tmpdir', 'tapes') @Shared @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
@Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot) @Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot)
@Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer() @Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()


Expand Down
9 changes: 3 additions & 6 deletions src/test/groovy/co/freeside/betamax/tape/PostBodySpec.groovy
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@ import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.DefaultHttpClient import org.apache.http.impl.client.DefaultHttpClient
import org.junit.Rule import org.junit.Rule
import org.yaml.snakeyaml.Yaml import org.yaml.snakeyaml.Yaml
import spock.lang.AutoCleanup import spock.lang.*
import spock.lang.Issue
import spock.lang.Shared
import spock.lang.Specification

import static co.freeside.betamax.TapeMode.WRITE_ONLY import static co.freeside.betamax.TapeMode.WRITE_ONLY
import static co.freeside.betamax.util.FileUtils.newTempDir


@Issue('https://github.com/robfletcher/betamax/issues/50') @Issue('https://github.com/robfletcher/betamax/issues/50')
class PostBodySpec extends Specification { class PostBodySpec extends Specification {


@Shared @AutoCleanup('deleteDir') File tapeRoot = new File(System.properties.'java.io.tmpdir', 'tapes') @Shared @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')
@Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot) @Rule Recorder recorder = new Recorder(tapeRoot: tapeRoot)


private DefaultHttpClient httpClient = new DefaultHttpClient() private DefaultHttpClient httpClient = new DefaultHttpClient()
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,14 +1,12 @@
package co.freeside.betamax.tape package co.freeside.betamax.tape


import co.freeside.betamax.tape.yaml.YamlTapeLoader import co.freeside.betamax.tape.yaml.YamlTapeLoader
import spock.lang.AutoCleanup import spock.lang.*
import spock.lang.Issue import static co.freeside.betamax.util.FileUtils.newTempDir
import spock.lang.Shared
import spock.lang.Specification


class YamlTapeLoaderSpec extends Specification { class YamlTapeLoaderSpec extends Specification {


@Shared @AutoCleanup('deleteDir') File tapeRoot = new File(System.properties.'java.io.tmpdir', 'tapes') @Shared @AutoCleanup('deleteDir') File tapeRoot = newTempDir('tapes')


void setupSpec() { void setupSpec() {
tapeRoot.mkdirs() tapeRoot.mkdirs()
Expand Down
15 changes: 15 additions & 0 deletions src/test/groovy/co/freeside/betamax/util/FileUtils.groovy
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,15 @@
package co.freeside.betamax.util

class FileUtils {

private static final File TMP = new File(System.properties.'java.io.tmpdir')

static File newTempDir(String name) {
def dir = new File(TMP, name)
dir.mkdirs()
dir
}

private FileUtils() {}

}

0 comments on commit 76e2e14

Please sign in to comment.