<commit_before>package uk.ac.ox.ndm.grails.utils.test

import grails.util.Holders
import org.grails.config.PropertySourcesConfig

/**
 * @since 15/09/2015
 */
abstract class CoreUnitSpec extends CoreSpec {

    static doWithConfig(PropertySourcesConfig config) {

        File appFile = new File(config."user.dir" as String, (config.'grails.project.base.dir' as String) +
                                                             '/grails-app/conf/application.groovy')
        if (!appFile.exists()) return

        URL applicationGroovyUrl = appFile.toURI().toURL()
        if (!applicationGroovyUrl) throw new IllegalStateException('We need the application groovy config to be able to test')
        config.merge(new ConfigSlurper().parse(applicationGroovyUrl))
    }

    static doWithSpring = {
        def config = Holders.findApplication().config

        File resourcesFile = new File(config."user.dir" as String, (config.'grails.project.base.dir' as String) +
                                                                   '/grails-app/conf/spring/resources.groovy')
        if (!resourcesFile.exists()) return

        URL resourcesGroovyUrl = resourcesFile.toURI().toURL()
        if (!resourcesGroovyUrl) throw new IllegalStateException('We need the spring resources groovy config to be able to test')
        def spring = new ConfigSlurper().parse(resourcesGroovyUrl)
        spring.beans.dehydrate().rehydrate(delegate, owner, thisObject).run()
    }

    def setup() {
        logger.info "Setting up core unit spec"

        registerConstraints()

        mockDomains(getKnownDataTypes().core)

        addMessageCode('validation.choice.onlyone', 'Property {0} with value {2} is invalid, only one {3} may be set')
        addMessageCode('validation.choice.atleastone', 'Property {0} with value {2} is invalid, at least one {3} must be set')
        addMessageCode('validation.nhsnumber.wronglength',
                       'Property {0} with value {2} is invalid, it must be at least 10 digits for a valid NHS ' +
                       'number')
        addMessageCode('validation.empty', 'Property {0} is invalid, it cannot be empty')
        addMessageCode('validation.hasmany.size.atleast', 'Set must have at least {3} element(s)')
        addMessageCode('validation.hasmany.size.atmost', 'Set cannot have more than {3} element(s)')
        addMessageCode('validation.schema.version', "Schema {3} with version value {2} must be one of {4}")
        addMessageCode('default.not.unique', 'Property [{0}] of class [{1}] with value [{2}] must be unique')
    }
}<commit_msg>Bring back cascade validation & improve unit test setup<commit_after>package uk.ac.ox.ndm.grails.utils.test

import grails.util.Holders
import org.grails.config.PropertySourcesConfig

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths

/**
 * @since 15/09/2015
 */
abstract class CoreUnitSpec extends CoreSpec {

    static doWithConfig(PropertySourcesConfig config) {

        Path appFile = getGrailsDirectory(config).resolve('grails-app/conf/application.groovy')
        if (!Files.exists(appFile)) return

        URL applicationGroovyUrl = appFile.toUri().toURL()
        if (!applicationGroovyUrl) throw new IllegalStateException('We need the application groovy config to be able to test')
        config.merge(new ConfigSlurper().parse(applicationGroovyUrl))
    }

    static doWithSpring = {
        def config = Holders.findApplication().config

        Path resourcesFile = getGrailsDirectory(config).resolve('grails-app/conf/spring/resources.groovy')
        if (!Files.exists(resourcesFile)) return

        URL resourcesGroovyUrl = resourcesFile.toUri().toURL()
        if (!resourcesGroovyUrl) throw new IllegalStateException('We need the spring resources groovy config to be able to test')
        def spring = new ConfigSlurper().parse(resourcesGroovyUrl)
        spring.beans.dehydrate().rehydrate(delegate, owner, thisObject).run()
    }

    static Path getGrailsDirectory(def config){
        Path userDir = Paths.get(config.'user.dir' as String)
        if(config.'grails.project.base.dir' as String) {
            Path projectDir = Paths.get(config.'grails.project.base.dir' as String)
            if (projectDir) {
                userDir = (projectDir.fileName == userDir.fileName) ? userDir : userDir.resolve(projectDir)
            }
        }
        userDir
    }

    def setup() {
        logger.info "Setting up core unit spec"

        registerConstraints()

        mockDomains(getKnownDataTypes().core)

        loadI18nMessages()
    }

    def loadI18nMessages() {

        def config = Holders.findApplication().config

        Path messagesFile = getGrailsDirectory(config).resolve('grails-app/i18n/messages.properties')
        if (!Files.exists(messagesFile)) return

        Properties messages = new Properties()
        messages.load(new FileReader(messagesFile.toFile()))
        messages.stringPropertyNames().each {k ->
            addMessageCode(k, messages.getProperty(k))
        }
    }
}
{'fraction_non_alphanumeric': 0.10742496050552923, 'fraction_numerical': 0.008096366508688783, 'mean_word_length': 2.905165767154973, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6746545', 'n_tokens_mistral': 1449, 'n_tokens_neox': 1356, 'n_words': 352}