-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#107 start separating JUnit specific code from general configuration
- Loading branch information
1 parent
a028550
commit 888a89b
Showing
27 changed files
with
250 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
betamax-core/src/main/java/co/freeside/betamax/junit/RecorderRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2013 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package co.freeside.betamax.junit; | ||
|
||
import java.util.*; | ||
import java.util.logging.*; | ||
import co.freeside.betamax.*; | ||
import org.junit.rules.*; | ||
import org.junit.runner.*; | ||
import org.junit.runners.model.*; | ||
import static java.util.logging.Level.*; | ||
|
||
/** | ||
* This is a wrapper for the Betamax {@link co.freeside.betamax.Recorder} that can be used as a | ||
* _JUnit @Rule_ allowing tests annotated with `@Betamax` to run with the | ||
* Betamax HTTP proxy in the background. | ||
*/ | ||
public class RecorderRule implements TestRule { | ||
|
||
private final Recorder recorder; | ||
private final Logger log = Logger.getLogger(RecorderRule.class.getName()); | ||
|
||
public RecorderRule(Recorder recorder) { | ||
this.recorder = recorder; | ||
} | ||
|
||
@Override | ||
public Statement apply(final Statement statement, Description description) { | ||
final Betamax annotation = description.getAnnotation(Betamax.class); | ||
if (annotation != null) { | ||
log.fine("found @Betamax annotation on '" + description.getDisplayName() + "'"); | ||
return new Statement() { | ||
@Override | ||
public void evaluate() throws Throwable { | ||
Map<String, Object> map = new LinkedHashMap<String, Object>(2); | ||
map.put("mode", annotation.mode()); | ||
map.put("match", Arrays.asList(annotation.match())); | ||
try { | ||
recorder.start(annotation.tape(), map); | ||
statement.evaluate(); | ||
} catch (Exception e) { | ||
log.log(SEVERE, "Caught exception starting Betamax", e); | ||
} finally { | ||
recorder.stop(); | ||
} | ||
} | ||
}; | ||
} else { | ||
log.fine(String.format("no @Betamax annotation on '%s'", description.getDisplayName())); | ||
return statement; | ||
} | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
betamax-core/src/main/java/co/freeside/betamax/util/TypedProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2013 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package co.freeside.betamax.util; | ||
|
||
import java.util.*; | ||
|
||
public class TypedProperties extends Properties { | ||
|
||
public static boolean getBoolean(Properties properties, String key, boolean defaultValue) { | ||
String value = properties.getProperty(key); | ||
return value != null ? Boolean.valueOf(value) : defaultValue; | ||
} | ||
|
||
public static boolean getBoolean(Properties properties, String key) { | ||
return getBoolean(properties, key, false); | ||
} | ||
|
||
public static int getInteger(Properties properties, String key, int defaultValue) { | ||
String value = properties.getProperty(key); | ||
return value != null ? Integer.parseInt(value) : defaultValue; | ||
} | ||
|
||
public static int getInteger(Properties properties, String key) { | ||
return getInteger(properties, key, 0); | ||
} | ||
|
||
public static <T extends Enum<T>> T getEnum(Properties properties, String key, T defaultValue) { | ||
String value = properties.getProperty(key); | ||
T anEnum = Enum.valueOf((Class<T>) defaultValue.getClass(), value); | ||
return value != null ? anEnum : defaultValue; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.