Skip to content

Commit 59b0d31

Browse files
mmoquiSilverYoCha
authored andcommitted
Feature #10762
Define a new object in SilverpeasConfigurationProperties: Context. This object defines a configuration context that can be used by any components implied in a task of SilverSetup as well by any scripts executed by those tasks. The context is publicly available in the settings global variable and it is represented by a Map. Once the build is finished, the properties set in the context are then persisted into the file SILVERPEAS_HOME/configuration/.config. The goal of the Context object is to allow any scripts and components used in tasks to keep track of any configuration, migration or installation context so that they can adapt their behaviour at the next execution.
1 parent 3a3c501 commit 59b0d31

15 files changed

+214
-76
lines changed

build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ buildscript {
3737
}
3838

3939
plugins {
40+
id 'java-gradle-plugin'
4041
id 'org.ajoberstar.grgit' version '3.0.0' apply true
4142
id 'idea'
4243
id 'maven'
@@ -112,13 +113,17 @@ dependencies {
112113
compile 'org.apache.commons:commons-dbcp2:2.2.0'
113114
compile 'commons-codec:commons-codec:1.10'
114115
compile 'javax.jcr:jcr:2.0'
115-
compile 'org.apache.jackrabbit:jackrabbit-core:2.8.0'
116+
compile 'org.apache.jackrabbit:jackrabbit-core:2.18.2'
116117
runtime 'org.eclipse.jetty:jetty-jndi:9.4.8.v20171121'
117118
runtime 'org.eclipse.jetty:jetty-util:9.4.8.v20171121'
118119
runtime 'com.h2database:h2:1.4.196'
119120
runtime 'org.postgresql:postgresql:42.1.4'
120121
runtime 'net.sourceforge.jtds:jtds:1.3.1'
122+
testCompile gradleTestKit()
121123
testCompile 'junit:junit:4.12'
124+
testImplementation('org.spockframework:spock-core:1.1-groovy-2.4') {
125+
exclude module: 'groovy-all'
126+
}
122127
}
123128

124129
ext {

src/main/groovy/org/silverpeas/setup/SilverpeasConfigurationProperties.groovy

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ package org.silverpeas.setup
2626
import org.gradle.api.Project
2727

2828
import javax.inject.Inject
29+
import java.nio.file.Files
2930

3031
/**
3132
* Properties for the configuration of Silverpeas. Such properties include the location of the
@@ -73,15 +74,62 @@ class SilverpeasConfigurationProperties {
7374
*/
7475
final Map settings = [:]
7576

77+
/**
78+
* Context of a configuration process of Silverpeas: it is a set of context properties left to the
79+
* discretion of the different steps executed in the configuration. The context is serialized so
80+
* that it can be retrieved in the next configuration process by the different steps so that they
81+
* can adapt their behaviour according to the properties they have set.
82+
*/
83+
final Context context
84+
7685
@Inject
7786
SilverpeasConfigurationProperties(Project project, File silverpeasHome) {
7887
configurationHome = project.file("${silverpeasHome.path}/configuration")
7988
jbossConfigurationDir = project.file("${silverpeasHome.path}/configuration/jboss")
8089
silverpeasConfigurationDir = project.file("${silverpeasHome.path}/configuration/silverpeas")
8190
jbossModulesDir = project.file("${jbossConfigurationDir.path}/modules")
91+
context = new Context(configurationHome, settings)
8292
}
8393

8494
void setSettings(final Map configProperties) {
8595
this.settings.putAll(configProperties)
8696
}
97+
98+
/**
99+
* Context of a configuration process. It sets in the settings variable (that is shared by all
100+
* the steps implied within a configuration of Silverpeas) the peculiar <code>context</code>
101+
* attribute that is a dictionary of all the context properties the steps can set for their usage.
102+
*/
103+
static class Context {
104+
final private File file
105+
final private Map props = [:]
106+
107+
/**
108+
* Constructs a new configuration context.
109+
* @param storageDir the directory into which the context will be saved.
110+
* @param settings the dictionary to use to store the context properties. A peculiar
111+
* <code>context</code> key will be put with as values a Map object.
112+
*/
113+
private Context(File storageDir, final Map settings) {
114+
file = new File(storageDir, '.context')
115+
settings.context = props
116+
if (Files.exists(file.toPath())) {
117+
file.text.eachLine { line ->
118+
String[] keyValue = line.trim().split(':')
119+
props[keyValue[0].trim()] = keyValue[1].trim()
120+
}
121+
}
122+
}
123+
124+
void save() {
125+
if (!Files.exists(file.toPath())) {
126+
Files.createFile(file.toPath())
127+
}
128+
file.withWriter('UTF-8') { w ->
129+
props.each { k, v ->
130+
w.println("${k}: ${v}")
131+
}
132+
}
133+
}
134+
}
87135
}

src/main/groovy/org/silverpeas/setup/SilverpeasSetupPlugin.groovy

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ class SilverpeasSetupPlugin implements Plugin<Project> {
6565
def extension = project.extensions.create(EXTENSION, SilverpeasSetupExtension, project)
6666
initSilverpeasSetupExtention(extension)
6767

68+
project.gradle.buildFinished {
69+
extension.config.context.save()
70+
}
71+
6872
SilverpeasSetupService setupService = new SilverpeasSetupService(extension.config.settings)
6973
ManagedBeanContainer.registry()
7074
.register(new DataSourceProvider(extension.config.settings))
@@ -118,7 +122,7 @@ class SilverpeasSetupPlugin implements Plugin<Project> {
118122
Task migration = project.tasks.create(MIGRATE.name, SilverpeasMigrationTask) {
119123
it.migrationHome = extension.migrationHome
120124
it.config = extension.config
121-
}.dependsOn(configuration)
125+
}.dependsOn(construction)
122126

123127
project.tasks.create(INSTALL.name, SilverpeasInstallationTask) {
124128
it.deploymentDir = extension.deploymentDir

src/main/groovy/org/silverpeas/setup/api/ManagedBeanContainer.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ManagedBeanContainer {
3232
static class Registry {
3333

3434
Registry register(def bean, String name) {
35-
beans.put(name, bean)
35+
beans.putIfAbsent(name, bean)
3636
return this
3737
}
3838

src/test/groovy/org/silverpeas/setup/configuration/SilverpeasConfigurationTaskTest.groovy

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,51 @@
11
package org.silverpeas.setup.configuration
22

3-
import org.gradle.api.Project
4-
import org.gradle.testfixtures.ProjectBuilder
5-
import org.silverpeas.setup.test.TestSetUp
63

7-
import static org.silverpeas.setup.api.SilverpeasSetupTaskNames.CONFIGURE_SILVERPEAS
4+
import org.silverpeas.setup.test.TestContext
5+
6+
import java.time.LocalDate
7+
8+
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS
89

910
/**
1011
* Test the case of the configuration of Silverpeas performed by a dedicated Gradle task.
1112
* @author mmoquillon
1213
*/
1314
class SilverpeasConfigurationTaskTest extends GroovyTestCase {
1415

15-
private TestSetUp testSetUp
16-
private Project project
16+
private TestContext context
1717

1818
@Override
1919
void setUp() {
2020
super.setUp()
21-
testSetUp = TestSetUp.setUp()
22-
23-
System.setProperty('SILVERPEAS_HOME', testSetUp.resourcesDir)
24-
System.setProperty('JBOSS_HOME', testSetUp.resourcesDir)
25-
26-
project = ProjectBuilder.builder().build()
27-
project.apply plugin: 'silversetup'
21+
context = TestContext.create().setUpSystemEnv().initGradleProject()
22+
}
2823

29-
project.silversetup.logging.logDir = new File(project.buildDir, 'log')
30-
project.silversetup.logging.useLogger = false
24+
@Override
25+
void tearDown() throws Exception {
26+
super.tearDown()
27+
context.cleanUp()
3128
}
3229

3330
void testSilverpeasConfiguration() {
34-
TestContext context = new TestContext().before()
31+
TestProperties testProperties = new TestProperties().before()
3532

36-
project.tasks.findByPath(CONFIGURE_SILVERPEAS.name).configureSilverpeas()
33+
def result = context.getGradleRunner(true)
34+
.withArguments('configure_silverpeas')
35+
.build()
36+
assert result.task(':configure_silverpeas').outcome == SUCCESS
3737

38-
context.after()
38+
testProperties.after()
3939

40-
assertThePropertiesFileAreCorrectlyConfigured(context)
41-
assertTheCustomerWorkflowIsCorrectlyConfigured(context)
42-
assertTheWorkflowEngineIsCorrectlyConfiguredByGroovyScript(context)
40+
assertThePropertiesFileAreCorrectlyConfigured(testProperties)
41+
assertTheCustomerWorkflowIsCorrectlyConfigured(testProperties)
42+
assertTheWorkflowEngineIsCorrectlyConfiguredByGroovyScript(testProperties)
43+
assertTheConfigContextIsCorrectlySaved(testProperties)
4344
}
4445

45-
void assertTheCustomerWorkflowIsCorrectlyConfigured(TestContext context) {
46-
def before = context.xmlconf.before
47-
def after = context.xmlconf.after
46+
void assertTheCustomerWorkflowIsCorrectlyConfigured(TestProperties props) {
47+
def before = props.xmlconf.before
48+
def after = props.xmlconf.after
4849
assert after.adefCreateSupplier.actions.action.find { it.@name == 'Archiver'}
4950
.consequences.consequence.find { it.@value == 'achats'}
5051
.triggers.trigger.param.find { it.@name == 'targetComponentId'}.@value !=
@@ -106,14 +107,14 @@ class SilverpeasConfigurationTaskTest extends GroovyTestCase {
106107
.consequences.consequence.triggers.trigger.param.find { it.@name == 'targetTopicId'}.@value == '100'
107108
}
108109

109-
void assertThePropertiesFileAreCorrectlyConfigured(TestContext context) {
110-
def before = context.settings.before
111-
def after = context.settings.after
110+
void assertThePropertiesFileAreCorrectlyConfigured(TestProperties props) {
111+
def before = props.settings.before
112+
def after = props.settings.after
112113

113114
assert after.autDomainSQL['fallbackType'] == 'always' &&
114115
after.autDomainSQL['fallbackType'] == before.autDomainSQL['fallbackType']
115116
assert after.autDomainSQL['autServer0.SQLJDBCUrl'] ==
116-
"jdbc:h2:file:${testSetUp.resourcesDir}/h2/test;MV_STORE=FALSE;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE" &&
117+
"jdbc:h2:file:${context.resourcesDir}/h2/test;MV_STORE=FALSE;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE" &&
117118
after.autDomainSQL['autServer0.SQLJDBCUrl'] != before.autDomainSQL['autServer0.SQLJDBCUrl']
118119
assert after.autDomainSQL['autServer0.SQLAccessLogin'] == 'sa' &&
119120
after.autDomainSQL['autServer0.SQLAccessLogin'] != before.autDomainSQL['autServer0.SQLAccessLogin']
@@ -129,10 +130,10 @@ class SilverpeasConfigurationTaskTest extends GroovyTestCase {
129130
!after.castorSettings['CastorJDODatabaseFileURL'].empty
130131
}
131132

132-
void assertTheWorkflowEngineIsCorrectlyConfiguredByGroovyScript(TestContext context) {
133+
void assertTheWorkflowEngineIsCorrectlyConfiguredByGroovyScript(TestProperties props) {
133134
// the JDO Castor doesn't support H2, so the engine is set up by default to postgresql
134-
def before = context.xmlconf.before
135-
def after = context.xmlconf.after
135+
def before = props.xmlconf.before
136+
def after = props.xmlconf.after
136137

137138
assert after.workflowDatabaseConf.@engine == 'postgresql' &&
138139
after.workflowDatabaseConf.@engine.text() != before.workflowDatabaseConf.@engine.text()
@@ -147,4 +148,11 @@ class SilverpeasConfigurationTaskTest extends GroovyTestCase {
147148
"file:///${System.getProperty('SILVERPEAS_HOME')}/resources/instanceManager/fast_mapping.xml"
148149
}
149150

151+
void assertTheConfigContextIsCorrectlySaved(TestProperties props) {
152+
assert props.configContext.before['status is'] == props.configContext.after['status is']
153+
assert props.configContext.before['installed at'] == props.configContext.after['installed at']
154+
assert props.configContext.before['updated at'] == props.configContext.after['updated at']
155+
assert props.configContext.after['context handled at'] == LocalDate.now().toString()
156+
}
157+
150158
}

src/test/groovy/org/silverpeas/setup/configuration/TestContext.groovy renamed to src/test/groovy/org/silverpeas/setup/configuration/TestProperties.groovy

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package org.silverpeas.setup.configuration
33
import groovy.util.slurpersupport.GPathResult
44

55
/**
6-
* A context of a unit test on the Silverpeas configuration.
6+
* A set of properties to use in the tests on the Silverpeas configuration.
77
* @author mmoquillon
88
*/
9-
class TestContext {
9+
class TestProperties {
1010

1111
def settings = [
1212
before: [
@@ -38,10 +38,15 @@ class TestContext {
3838
]
3939
]
4040

41-
TestContext() {
41+
def configContext = [
42+
before: [:],
43+
after: [:]
44+
]
45+
46+
TestProperties() {
4247
}
4348

44-
TestContext before() {
49+
TestProperties before() {
4550
settings.before.autDomainSQL = loadPropertiesFrom('authentication/autDomainSQL.properties')
4651
settings.before.system = loadPropertiesFrom('systemSettings.properties')
4752
settings.before.scheduler = loadPropertiesFrom('workflow/engine/schedulerSettings.properties')
@@ -51,10 +56,12 @@ class TestContext {
5156
xmlconf.before.adefCreateProduct = loadXmlFileFrom('/data/workflowRepository/ADEFCreateProduct.xml')
5257
xmlconf.before.workflowDatabaseConf = loadXmlFileFrom('/resources/instanceManager/database.xml')
5358
xmlconf.before.workflowFastDatabaseConf = loadXmlFileFrom('/resources/instanceManager/fast_database.xml')
59+
60+
configContext.before = loadKeyValuesFileFrom('/configuration/.context')
5461
return this
5562
}
5663

57-
TestContext after() {
64+
TestProperties after() {
5865
settings.after.autDomainSQL = loadPropertiesFrom('authentication/autDomainSQL.properties')
5966
settings.after.system = loadPropertiesFrom('systemSettings.properties')
6067
settings.after.scheduler = loadPropertiesFrom('workflow/engine/schedulerSettings.properties')
@@ -64,6 +71,8 @@ class TestContext {
6471
xmlconf.after.adefCreateProduct = loadXmlFileFrom('/data/workflowRepository/ADEFCreateProduct.xml')
6572
xmlconf.after.workflowDatabaseConf = loadXmlFileFrom('/resources/instanceManager/database.xml')
6673
xmlconf.after.workflowFastDatabaseConf = loadXmlFileFrom('/resources/instanceManager/fast_database.xml')
74+
75+
configContext.after = loadKeyValuesFileFrom('/configuration/.context')
6776
return this
6877
}
6978

@@ -81,4 +90,13 @@ class TestContext {
8190
is.close()
8291
return result
8392
}
93+
94+
private Map loadKeyValuesFileFrom(String path) {
95+
final Map keyValuePairs = [:]
96+
getClass().getResourceAsStream(path).text.eachLine { line ->
97+
String[] keyValue = line.trim().split(':')
98+
keyValuePairs[keyValue[0].trim()] = keyValue[1].trim()
99+
}
100+
return keyValuePairs
101+
}
84102
}

src/test/groovy/org/silverpeas/setup/installation/SilverpeasInstallationTaskTest.groovy

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import groovy.mock.interceptor.MockFor
44
import org.gradle.api.Project
55
import org.gradle.testfixtures.ProjectBuilder
66
import org.silverpeas.setup.api.JBossServer
7-
import org.silverpeas.setup.test.TestSetUp
7+
import org.silverpeas.setup.test.TestContext
88

99
import static org.silverpeas.setup.api.SilverpeasSetupTaskNames.INSTALL
1010
/**
@@ -14,16 +14,13 @@ import static org.silverpeas.setup.api.SilverpeasSetupTaskNames.INSTALL
1414
class SilverpeasInstallationTaskTest extends GroovyTestCase {
1515

1616
private Project project
17-
protected TestSetUp testSetUp
17+
protected TestContext context
1818

1919
@Override
2020
void setUp() {
2121
super.setUp()
2222

23-
testSetUp = TestSetUp.setUp()
24-
25-
System.setProperty('SILVERPEAS_HOME', testSetUp.resourcesDir)
26-
System.setProperty('JBOSS_HOME', testSetUp.resourcesDir)
23+
context = TestContext.create().setUpSystemEnv()
2724

2825
project = ProjectBuilder.builder().build()
2926
project.apply plugin: 'silversetup'

src/test/groovy/org/silverpeas/setup/migration/AbstractDatabaseTest.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ import groovy.sql.Sql
2727
import org.silverpeas.setup.api.ManagedBeanContainer
2828
import org.silverpeas.setup.api.SilverpeasSetupService
2929
import org.silverpeas.setup.test.DatabaseSetUp
30-
import org.silverpeas.setup.test.TestSetUp
30+
import org.silverpeas.setup.test.TestContext
3131
/**
3232
* The common class for all test cases about a database migration.
3333
* @author mmoquillon
3434
*/
3535
abstract class AbstractDatabaseTest extends GroovyTestCase {
3636

3737
protected DatabaseSetUp databaseSetUp
38-
protected TestSetUp testSetUp
38+
protected TestContext context
3939

4040
DatabaseSetUp initDatabaseSetUp() {
4141
return DatabaseSetUp.setUp(withDatasource: true).createSrPackagesTable()
@@ -44,7 +44,7 @@ abstract class AbstractDatabaseTest extends GroovyTestCase {
4444
@Override
4545
void setUp() {
4646
super.setUp()
47-
testSetUp = TestSetUp.setUp()
47+
context = TestContext.create()
4848
databaseSetUp = initDatabaseSetUp()
4949
ManagedBeanContainer.registry().register(new SilverpeasSetupService([:]))
5050
}
@@ -57,7 +57,7 @@ abstract class AbstractDatabaseTest extends GroovyTestCase {
5757

5858
def prepareInitialData(String module, String version) {
5959
databaseSetUp.prepare { Sql sql ->
60-
sql.executeScript("${testSetUp.migrationHome}/db/h2/${module}/${version}/create_table.sql")
60+
sql.executeScript("${context.migrationHome}/db/h2/${module}/${version}/create_table.sql")
6161
sql.executeUpdate("INSERT INTO sr_packages (sr_package, sr_version) VALUES (:module, :version)",
6262
[module: module, version: version])
6363
}

0 commit comments

Comments
 (0)