Skip to content

Commit

Permalink
configure with properties file
Browse files Browse the repository at this point in the history
  • Loading branch information
robfletcher committed Aug 23, 2011
1 parent 40148e6 commit 0a0dd02
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 2 deletions.
8 changes: 8 additions & 0 deletions readme.md
Expand Up @@ -85,6 +85,14 @@ The `Recorder` class has some configuration properties that you can override:
* *tapeRoot*: the base directory where tape files are stored. Defaults to `src/test/resources/betamax/tapes`.
* *proxyPort*: the port the Betamax proxy listens on. Defaults to `5555`.

### Configuring Betamax with a properties files

If you have a file called `betamax.properties` somewhere in your classpath it will be picked up by the `Recorder`
constructor. Each property for the `Recorder` is simply prefixed with `betamax.`. For example:

betamax.tapeRoot=test/fixtures/tapes
betamax.proxyPort=1337

## Caveats

By default [Apache _HTTPClient_][3] takes no notice of Java's HTTP proxy settings. The Betamax proxy can only intercept
Expand Down
27 changes: 25 additions & 2 deletions src/main/groovy/betamax/Recorder.groovy
Expand Up @@ -32,15 +32,38 @@ import org.junit.runners.model.*
@Log4j
class Recorder implements MethodRule {

static final String DEFAULT_TAPE_ROOT = "src/test/resources/betamax/tapes"
static final int DEFAULT_PROXY_PORT = 5555

Recorder() {
def props = getClass().classLoader.getResource("betamax.properties")
if (props) {
def properties = new Properties()
props.withReader { reader ->
properties.load(reader)
}
configureFromProperties(properties)
}
}

Recorder(Properties properties) {
configureFromProperties(properties)
}

private void configureFromProperties(Properties properties) {
tapeRoot = new File(properties.getProperty("betamax.tapeRoot", DEFAULT_TAPE_ROOT))
proxyPort = properties.getProperty("betamax.proxyPort")?.toInteger() ?: DEFAULT_PROXY_PORT
}

/**
* The port the Betamax proxy will listen on.
*/
int proxyPort = 5555
int proxyPort = DEFAULT_PROXY_PORT

/**
* The base directory where tape files are stored.
*/
File tapeRoot = new File("src/test/resources/betamax/tapes")
File tapeRoot = new File(DEFAULT_TAPE_ROOT)

/**
* The strategy for reading and writing tape files.
Expand Down
89 changes: 89 additions & 0 deletions src/test/groovy/betamax/RecorderConfigurationSpec.groovy
@@ -0,0 +1,89 @@
package betamax

import spock.lang.*

class RecorderConfigurationSpec extends Specification {

@Shared String tmpdir = System.properties."java.io.tmpdir"

def "recorder gets default configuration if not overridden and no properties file exists"() {
given:
def recorder = new Recorder()

expect:
recorder.tapeRoot == new File("src/test/resources/betamax/tapes")
recorder.proxyPort == 5555
}

def "recorder configuration is overridden by map arguments"() {
given:
def recorder = new Recorder(tapeRoot: new File(tmpdir, "tapes"), proxyPort: 1337)

expect:
recorder.tapeRoot == new File(tmpdir, "tapes")
recorder.proxyPort == 1337
}

def "recorder picks up configuration from properties"() {
given:
def properties = new Properties()
properties.setProperty("betamax.tapeRoot", "$tmpdir/tapes")
properties.setProperty("betamax.proxyPort", "1337")

and:
def recorder = new Recorder(properties)

expect:
recorder.tapeRoot == new File(tmpdir, "tapes")
recorder.proxyPort == 1337
}

def "recorder picks up configuration from properties file"() {
given:
def propertiesFile = new File(tmpdir, "betamax.properties")
def properties = new Properties()
properties.setProperty("betamax.tapeRoot", "$tmpdir/tapes")
properties.setProperty("betamax.proxyPort", "1337")
propertiesFile.withWriter { writer ->
properties.store(writer, null)
}

and:
Recorder.classLoader.addURL(new File(tmpdir).toURL())

and:
def recorder = new Recorder()

expect:
recorder.tapeRoot == new File(tmpdir, "tapes")
recorder.proxyPort == 1337

cleanup:
propertiesFile.delete()
}

def "constructor arguments take precendence over a properties file"() {
given:
def propertiesFile = new File(tmpdir, "betamax.properties")
def properties = new Properties()
properties.setProperty("betamax.tapeRoot", "$tmpdir/tapes")
properties.setProperty("betamax.proxyPort", "1337")
propertiesFile.withWriter { writer ->
properties.store(writer, null)
}

and:
Recorder.classLoader.addURL(new File(tmpdir).toURL())

and:
def recorder = new Recorder(tapeRoot: new File("test/fixtures/tapes"), proxyPort: 1234)

expect:
recorder.tapeRoot == new File("test/fixtures/tapes")
recorder.proxyPort == 1234

cleanup:
propertiesFile.delete()
}

}

0 comments on commit 0a0dd02

Please sign in to comment.