Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

can set, get and log sham random seed

 * sham object now properly sets and gets random seed from underlying RNG
 * grails plugin can set, get and log sham seed. Logging occurs to hardcoded sham.log file for now
 * bump version to 0.2-SNAPSHOT
  • Loading branch information...
commit cbb1ade02ef6f19670cffd14535e58f3c48e5dd0 1 parent 252be3d
@tomdcc authored
View
14 backlog.md
@@ -8,8 +8,8 @@ library
! set seed from system property
* person
-! * address
-! * phone
+ * address
+ * phone
* name
* rewrite using spew gen
* simpler, allows weights etc
@@ -25,22 +25,18 @@ library
* products
! * spew gen name
+! limit sentence length
+
* locale stuff
* javadoc
plugin
------
- * config
- * image base dir
-
plugin driver
-------------
- * domain model in functional tests for e.g. news site
- * web page to demo layout on
- * create fixtures using sham
-
+ * flesh out domain model to show off properly
build
View
2  build.gradle
@@ -2,5 +2,5 @@ subprojects {
repositories {
mavenCentral()
}
- version = '0.1'
+ version = '0.2-SNAPSHOT'
}
View
2  sham-grails-plugin-driver/application.properties
@@ -3,5 +3,5 @@
app.grails.version=1.3.7
app.name=sham-grails-plugin-driver
app.servlet.version=2.5
-app.version=0.1
+app.version=0.2-SNAPSHOT
View
2  sham-grails-plugin-driver/grails-app/conf/BuildConfig.groovy
@@ -32,7 +32,7 @@ grails.project.dependency.resolution = {
compile ':fixtures:1.1'
compile ':build-test-data:1.1.2'
- compile ':sham:0.1'
+ compile ':sham:0.2-SNAPSHOT'
compile ':cache-headers:1.1.5'
View
15 sham-grails-plugin/ShamGrailsPlugin.groovy
@@ -1,8 +1,12 @@
import org.shamdata.Sham
+import org.apache.log4j.Logger
+import org.apache.log4j.FileAppender
+import org.apache.log4j.SimpleLayout
+import org.apache.log4j.Level
class ShamGrailsPlugin {
// the plugin version
- def version = "0.1"
+ def version = "0.2-SNAPSHOT"
// the version or versions of Grails the plugin is designed for
def grailsVersion = "1.3.7 > *"
// the other plugins this plugin depends on
@@ -32,6 +36,10 @@ Brief description of the plugin.
bean.factoryMethod = "getInstance"
imageBaseDir = '/images'
}
+ shamLog(Logger) { bean ->
+ bean.factoryMethod = "getInstance"
+ bean.constructorArgs = ["sham"]
+ }
}
def doWithDynamicMethods = { ctx ->
@@ -41,7 +49,10 @@ Brief description of the plugin.
def doWithApplicationContext = { applicationContext ->
applicationContext.sham.servletContext = applicationContext.servletContext
- // TODO Implement post initialization spring config (optional)
+ def logger = applicationContext.shamLog
+ logger.additivity = false
+ logger.addAppender(new FileAppender(new SimpleLayout(), "sham.log"))
+ logger.level = Level.DEBUG
}
def onChange = { event ->
View
3  sham-grails-plugin/application.properties
@@ -1,2 +1,5 @@
+#Grails Metadata file
+#Tue Feb 07 18:51:53 GMT 2012
app.grails.version=1.3.7
app.name=sham
+plugins.svn=1.0.2
View
2  sham-grails-plugin/grails-app/conf/BuildConfig.groovy
@@ -27,7 +27,7 @@ grails.project.dependency.resolution = {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.
// runtime 'mysql:mysql-connector-java:5.1.13'
- compile 'org.shamdata:sham:0.1'
+ compile 'org.shamdata:sham:0.2-SNAPSHOT'
test "org.objenesis:objenesis:1.2"
}
View
5 sham-grails-plugin/grails-app/conf/ShamUrlMappings.groovy
@@ -2,7 +2,8 @@ class ShamUrlMappings {
static mappings = {
"/sham/run-fixtures"(controller: 'sham', action: 'run')
-// "/sham/log-seed"(controller: 'sham', action: 'logSeed')
-
+ "/sham/log-seed"(controller: 'sham', action: 'logSeed')
+ "/sham/seed"(controller: 'sham', action: 'getSeed')
+ "/sham/set-seed"(controller: 'sham', action: 'setSeed')
}
}
View
24 sham-grails-plugin/grails-app/controllers/grails/plugin/sham/ShamController.groovy
@@ -1,8 +1,12 @@
package grails.plugin.sham
+import grails.converters.JSON
+
class ShamController {
def fixtureLoader
+ def sham
+ def shamLog
def run = {
if(!params.fixtures) {
@@ -24,4 +28,24 @@ class ShamController {
private String joiner(String uri) {
uri.contains('?') ? '&' : '?'
}
+
+ def logSeed = {
+ shamLog.info("${params.prefix ? params.prefix + ', ' : ''}sham seed: $sham.seed")
+ getSeed()
+ }
+
+ def setSeed = {
+ sham.seed = params.seed as long
+ shamLog.info("${params.prefix ? params.prefix + ', ' : ''}set sham seed to: $sham.seed")
+ getSeed()
+ }
+
+ def getSeed = {
+ def seed = sham.seed
+ if (request.xhr) {
+ render seed as JSON
+ } else {
+ render text: seed
+ }
+ }
}
View
10 sham-grails-plugin/test/integration/grails/plugin/sham/PluginSpec.groovy
@@ -2,6 +2,7 @@ package grails.plugin.sham
import grails.plugin.spock.IntegrationSpec
import org.shamdata.Sham
+import org.apache.log4j.Logger
class PluginSpec extends IntegrationSpec {
@@ -19,5 +20,14 @@ class PluginSpec extends IntegrationSpec {
and: "it's the same instance as Sham.instance returns"
sham.is(Sham.instance)
+
+ when: 'app asks for shamLog'
+ def shamLog = grailsApplication.mainContext.shamLog
+
+ then: "it's there"
+ shamLog
+
+ and: "it's a Logger instance"
+ shamLog instanceof Logger
}
}
View
59 sham-grails-plugin/test/unit/grails/plugin/sham/ShamControllerSpec.groovy
@@ -3,6 +3,8 @@ package grails.plugin.sham
import grails.plugin.spock.ControllerSpec
import grails.plugin.fixtures.FixtureLoader
import spock.lang.Unroll
+import org.shamdata.Sham
+import org.apache.log4j.Logger
class ShamControllerSpec extends ControllerSpec {
@@ -64,4 +66,61 @@ class ShamControllerSpec extends ControllerSpec {
'http:/www.foo.com/someTarget?foo' | '&'
}
+ @Unroll
+ def "controller logs seed to log"() {
+ given: 'sham instance with known seed'
+ def sham = new Sham()
+ sham.seed = seed
+ controller.sham = sham
+ def shamLog = Mock(Logger)
+ controller.shamLog = shamLog
+
+ when: 'call logSeed'
+ controller.params.prefix = prefix
+ controller.logSeed()
+
+ then: 'logger called with expected message'
+ 1 * shamLog.info(expectedValue)
+
+ and: 'returns seed'
+ controller.renderArgs.text == seed
+
+ where:
+ seed | prefix | expectedValue
+ 1001 | null | "sham seed: 1001"
+ 1234 | 'o hai' | "o hai, sham seed: 1234"
+ }
+
+ @Unroll
+ def "controller can set sham seed"() {
+ given: 'sham instance'
+ def sham = new Sham()
+ controller.sham = sham
+
+ def shamLog = Mock(Logger)
+ controller.shamLog = shamLog
+
+ when:
+ controller.params.seed = seed as String
+ controller.params.prefix = prefix
+ controller.setSeed()
+
+ then: 'seed set'
+ sham.seed == seed
+
+ and: 'expected log message logged'
+ 1 * shamLog.info(expectedLog)
+
+ and: 'returns seed'
+ controller.renderArgs.text == seed
+
+ where:
+ seed | prefix | expectedLog
+ 1001 | null | "set sham seed to: 1001"
+ 1234 | 'o hai' | "o hai, set sham seed to: 1234"
+
+ }
+
+
+
}
View
29 sham/src/main/java/org/shamdata/Sham.java
@@ -9,11 +9,13 @@
import org.shamdata.image.ServletContextImagePicker;
import javax.servlet.ServletContext;
+import java.lang.reflect.Field;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
+import java.util.concurrent.atomic.AtomicLong;
/**
*
@@ -139,4 +141,31 @@ public URL nextImage(String relativeDir) {
public void setImageBaseDir(String imageBaseDir) {
this.imageBaseDir = imageBaseDir;
}
+
+ public void setSeed(long seedVal) {
+ try {
+ Field f = Random.class.getDeclaredField("seed"); //NoSuchFieldException
+ f.setAccessible(true);
+ AtomicLong seed = (AtomicLong) f.get(random);
+ seed.set(seedVal);
+ } catch(NoSuchFieldException e) {
+ throw new RuntimeException("Couldn't access seed field - perhaps JDK Random object not laid out as expected?", e);
+ } catch(IllegalAccessException e) {
+ throw new RuntimeException("Couldn't access seed field - are you running under a SecurityManager?", e);
+ } catch(SecurityException e) {
+ throw new RuntimeException("Couldn't access seed field - are you running under a SecurityManager?", e);
+ }
+ }
+
+ public Long getSeed() {
+ try {
+ Field f = Random.class.getDeclaredField("seed"); //NoSuchFieldException
+ f.setAccessible(true);
+ AtomicLong seed = (AtomicLong) f.get(random);
+ return seed == null ? null : seed.get();
+ } catch(Exception e) {
+ // not allowed to access
+ return null;
+ }
+ }
}
View
20 sham/src/test/groovy/org/shamdata/ShamSpec.groovy
@@ -140,4 +140,24 @@ class ShamSpec extends Specification {
where:
num << [1, 3, 5]
}
+
+ def "set seed sets seed to given state"() {
+ given: 'sham instance'
+ def sham = new Sham()
+ long newSeed = 1234L
+
+ when: 'set seed'
+ sham.seed = newSeed
+
+ then: 'set on underlying random instance'
+ sham.random.seed.get() == newSeed
+ }
+
+ def "get seed returns seed to given state"() {
+ given: 'sham instance'
+ def sham = new Sham()
+
+ expect: 'get seed return underlying random seed'
+ sham.seed == sham.random.seed.get()
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.