Permalink
Browse files

utility method to avoid repetitive temp dir declarations

  • Loading branch information...
1 parent b1f6238 commit 76e2e14a19616dedae72908c8abe3dcfd277185e Rob Fletcher committed Sep 28, 2012
@@ -7,13 +7,14 @@ import groovyx.net.http.RESTClient
import org.junit.Rule
import spock.lang.*
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 org.apache.http.HttpHeaders.VIA
@Stepwise
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)
@AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
RESTClient http
@@ -3,11 +3,11 @@ package co.freeside.betamax
import co.freeside.betamax.httpclient.BetamaxRoutePlanner
import co.freeside.betamax.proxy.jetty.SimpleServer
import co.freeside.betamax.util.server.EchoHandler
-import groovyx.net.http.HttpResponseDecorator
-import groovyx.net.http.RESTClient
+import groovyx.net.http.*
import org.junit.Rule
import spock.lang.*
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 org.apache.http.HttpHeaders.VIA
@@ -16,7 +16,7 @@ import static org.apache.http.HttpHeaders.VIA
@Unroll
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)
@Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
@@ -9,14 +9,15 @@ import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.DefaultHttpClient
import org.junit.Rule
import spock.lang.*
+import static co.freeside.betamax.util.FileUtils.newTempDir
import static org.apache.http.HttpHeaders.VIA
import static org.apache.http.HttpStatus.SC_OK
@Issue('https://github.com/robfletcher/betamax/issues/34')
@Unroll
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)
@Shared @AutoCleanup('stop') SimpleServer httpsEndpoint = new SimpleSecureServer(5001)
@Shared @AutoCleanup('stop') SimpleServer httpEndpoint = new SimpleServer()
@@ -2,19 +2,18 @@ package co.freeside.betamax.proxy
import co.freeside.betamax.Recorder
import co.freeside.betamax.httpclient.BetamaxRoutePlanner
-import co.freeside.betamax.proxy.jetty.ProxyServer
-import co.freeside.betamax.proxy.jetty.SimpleServer
+import co.freeside.betamax.proxy.jetty.*
import co.freeside.betamax.util.server.EchoHandler
import groovyx.net.http.RESTClient
import spock.lang.*
-
+import static co.freeside.betamax.util.FileUtils.newTempDir
import static org.apache.http.HttpHeaders.VIA
@Issue('https://github.com/robfletcher/betamax/issues/16')
@Unroll
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()
@AutoCleanup('ejectTape') Recorder recorder = new Recorder(tapeRoot: tapeRoot)
@AutoCleanup('stop') ProxyServer proxy = new ProxyServer(recorder)
@@ -1,20 +1,19 @@
package co.freeside.betamax.proxy
-import co.freeside.betamax.Betamax
-import co.freeside.betamax.Recorder
+import co.freeside.betamax.*
import co.freeside.betamax.proxy.jetty.SimpleServer
import co.freeside.betamax.util.server.HelloHandler
import org.junit.Rule
import spock.lang.*
-
+import static co.freeside.betamax.util.FileUtils.newTempDir
import static co.freeside.betamax.util.server.HelloHandler.HELLO_WORLD
import static java.net.HttpURLConnection.HTTP_OK
import static org.apache.http.HttpHeaders.VIA
@Issue('https://github.com/robfletcher/betamax/issues/54')
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)
@Shared @AutoCleanup('stop') SimpleServer proxyServer = new SimpleServer()
@@ -1,27 +1,24 @@
package co.freeside.betamax.proxy
-import co.freeside.betamax.Betamax
-import co.freeside.betamax.Recorder
+import co.freeside.betamax.*
import co.freeside.betamax.httpclient.BetamaxRoutePlanner
import co.freeside.betamax.proxy.jetty.SimpleServer
import co.freeside.betamax.util.server.EchoHandler
-import groovyx.net.http.HttpURLClient
-import groovyx.net.http.RESTClient
-import org.apache.commons.httpclient.HttpClient
-import org.apache.commons.httpclient.ProxyHost
+import groovyx.net.http.*
+import org.apache.commons.httpclient.*
import org.apache.commons.httpclient.methods.GetMethod
import org.apache.http.HttpHost
import org.junit.Rule
import spock.lang.*
-
+import static co.freeside.betamax.util.FileUtils.newTempDir
import static java.net.HttpURLConnection.HTTP_OK
import static org.apache.http.HttpHeaders.VIA
import static org.apache.http.conn.params.ConnRoutePNames.DEFAULT_PROXY
@Unroll
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)
@Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
@@ -2,19 +2,18 @@ package co.freeside.betamax.proxy
import co.freeside.betamax.Recorder
import co.freeside.betamax.httpclient.BetamaxRoutePlanner
-import co.freeside.betamax.proxy.jetty.ProxyServer
-import co.freeside.betamax.proxy.jetty.SimpleServer
+import co.freeside.betamax.proxy.jetty.*
import co.freeside.betamax.util.server.EchoHandler
import groovyx.net.http.RESTClient
import org.yaml.snakeyaml.Yaml
import spock.lang.*
-
+import static co.freeside.betamax.util.FileUtils.newTempDir
import static java.net.HttpURLConnection.HTTP_OK
@Stepwise
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('stop') ProxyServer proxy = new ProxyServer(recorder)
@AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
@@ -1,27 +1,22 @@
package co.freeside.betamax.proxy
-import co.freeside.betamax.Betamax
-import co.freeside.betamax.Recorder
+import co.freeside.betamax.*
import co.freeside.betamax.httpclient.BetamaxRoutePlanner
import co.freeside.betamax.proxy.jetty.SimpleServer
import co.freeside.betamax.util.server.SlowHandler
-import groovyx.net.http.HttpResponseException
-import groovyx.net.http.RESTClient
+import groovyx.net.http.*
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler
import org.junit.Rule
-import spock.lang.AutoCleanup
-import spock.lang.Issue
-import spock.lang.Shared
-import spock.lang.Specification
-
+import spock.lang.*
+import static co.freeside.betamax.util.FileUtils.newTempDir
import static java.net.HttpURLConnection.HTTP_GATEWAY_TIMEOUT
@Issue('https://github.com/robfletcher/betamax/issues/20')
class ProxyTimeoutSpec extends Specification {
@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()
RESTClient http
@@ -4,14 +4,14 @@ import co.freeside.betamax.Recorder
import co.freeside.betamax.httpclient.BetamaxRoutePlanner
import groovyx.net.http.RESTClient
import spock.lang.*
-
import static co.freeside.betamax.MatchRule.host
+import static co.freeside.betamax.util.FileUtils.newTempDir
import static org.apache.http.HttpHeaders.VIA
@Issue('https://github.com/robfletcher/betamax/issues/9')
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 RESTClient http = new RESTClient()
@@ -2,23 +2,17 @@ package co.freeside.betamax.recorder
import co.freeside.betamax.Recorder
import co.freeside.betamax.httpclient.BetamaxRoutePlanner
-import co.freeside.betamax.proxy.jetty.ProxyServer
-import co.freeside.betamax.proxy.jetty.SimpleServer
+import co.freeside.betamax.proxy.jetty.*
import co.freeside.betamax.util.server.EchoHandler
-import groovyx.net.http.HttpResponseException
-import groovyx.net.http.RESTClient
-import spock.lang.AutoCleanup
-import spock.lang.Shared
-import spock.lang.Specification
-
-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
+import groovyx.net.http.*
+import spock.lang.*
+import static co.freeside.betamax.TapeMode.*
+import static co.freeside.betamax.util.FileUtils.newTempDir
+import static java.net.HttpURLConnection.*
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 @AutoCleanup('stop') ProxyServer proxy = new ProxyServer(recorder)
@Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
@@ -1,15 +1,13 @@
package co.freeside.betamax.tape
import co.freeside.betamax.tape.yaml.YamlTapeLoader
-import spock.lang.AutoCleanup
-import spock.lang.Shared
-import spock.lang.Specification
-import spock.lang.Unroll
+import spock.lang.*
+import static co.freeside.betamax.util.FileUtils.newTempDir
@Unroll
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'"() {
given:
@@ -6,12 +6,13 @@ import co.freeside.betamax.proxy.jetty.SimpleServer
import co.freeside.betamax.util.server.HelloHandler
import org.junit.Rule
import spock.lang.*
+import static co.freeside.betamax.util.FileUtils.newTempDir
import static co.freeside.betamax.util.server.HelloHandler.HELLO_WORLD
import static java.util.concurrent.TimeUnit.SECONDS
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)
@Shared @AutoCleanup('stop') SimpleServer endpoint = new SimpleServer()
@@ -7,17 +7,14 @@ import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.DefaultHttpClient
import org.junit.Rule
import org.yaml.snakeyaml.Yaml
-import spock.lang.AutoCleanup
-import spock.lang.Issue
-import spock.lang.Shared
-import spock.lang.Specification
-
+import spock.lang.*
import static co.freeside.betamax.TapeMode.WRITE_ONLY
+import static co.freeside.betamax.util.FileUtils.newTempDir
@Issue('https://github.com/robfletcher/betamax/issues/50')
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)
private DefaultHttpClient httpClient = new DefaultHttpClient()
@@ -1,14 +1,12 @@
package co.freeside.betamax.tape
import co.freeside.betamax.tape.yaml.YamlTapeLoader
-import spock.lang.AutoCleanup
-import spock.lang.Issue
-import spock.lang.Shared
-import spock.lang.Specification
+import spock.lang.*
+import static co.freeside.betamax.util.FileUtils.newTempDir
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() {
tapeRoot.mkdirs()
@@ -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.