Skip to content

Commit ada6a78

Browse files
authored
#288 - Fix Global Properties loading for OpenMRS 2.7+ (#289)
1 parent 3bfbc1f commit ada6a78

File tree

6 files changed

+136
-6
lines changed

6 files changed

+136
-6
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ See the [documentation on Initializer's logging properties](readme/rtprops.md#lo
206206

207207
## Releases notes
208208

209+
#### Version 2.9.0
210+
* Fix for InitializerSerializer to ensure compatibility with OpenMRS version 2.7.0+
211+
209212
#### Version 2.8.0
210213
* Ampath forms translation files will now generate checksums.
211214
* Enhancement to ensure that when an Ampath forms file is loaded, a new resource with the existing Ampath forms translations is created.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* This Source Code Form is subject to the terms of the Mozilla Public License,
3+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5+
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6+
*
7+
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8+
* graphic logo is a trademark of OpenMRS Inc.
9+
*/
10+
package org.openmrs.module.initializer.api.loaders;
11+
12+
import org.junit.Assert;
13+
import org.junit.Test;
14+
import org.openmrs.GlobalProperty;
15+
import org.openmrs.api.context.Context;
16+
import org.openmrs.module.initializer.DomainBaseModuleContextSensitive_2_7_Test;
17+
import org.openmrs.module.initializer.api.gp.GlobalPropertiesLoader;
18+
import org.springframework.beans.factory.annotation.Autowired;
19+
20+
/**
21+
* This test is intended to be a copy of the GlobalPropertiesLoaderIntegrationTest from the api module,
22+
* but included within the api-2.7 module to test that global properties loading from xml works successfully
23+
* in an OpenMRS 2.7 environment
24+
*/
25+
public class GlobalPropertiesLoaderIntegration_2_7_Test extends DomainBaseModuleContextSensitive_2_7_Test {
26+
27+
@Autowired
28+
private GlobalPropertiesLoader loader;
29+
30+
@Test
31+
public void loadGlobalProperties_shouldLoadGlobalProperties() {
32+
33+
// Replay
34+
loader.load();
35+
36+
// Verif
37+
Assert.assertEquals("GP one one", Context.getAdministrationService().getGlobalProperty("gp.gp11"));
38+
Assert.assertEquals("GP one two", Context.getAdministrationService().getGlobalProperty("gp.gp12"));
39+
Assert.assertEquals("GP two one", Context.getAdministrationService().getGlobalProperty("gp.gp21"));
40+
Assert.assertEquals("GP three one", Context.getAdministrationService().getGlobalProperty("gp.gp31"));
41+
Assert.assertEquals("GP three two", Context.getAdministrationService().getGlobalProperty("gp.gp32"));
42+
Assert.assertEquals("GP three three", Context.getAdministrationService().getGlobalProperty("gp.gp33"));
43+
}
44+
45+
@Test
46+
public void load_shouldOverrideGlobalProperties() {
47+
48+
// Setup
49+
Context.getAdministrationService().saveGlobalProperty(new GlobalProperty("gp.gp11", "foobar"));
50+
Assert.assertEquals("foobar", Context.getAdministrationService().getGlobalProperty("gp.gp11"));
51+
52+
// Replay
53+
loader.load();
54+
55+
// Verif
56+
Assert.assertEquals("GP one one", Context.getAdministrationService().getGlobalProperty("gp.gp11"));
57+
}
58+
59+
@Test
60+
public void load_shouldNotAffectOtherGlobalProperties() {
61+
62+
// Setup
63+
Context.getAdministrationService().saveGlobalProperty(new GlobalProperty("gp.foo", "Foo"));
64+
Context.getAdministrationService().saveGlobalProperty(new GlobalProperty("gp.bar", "Bar"));
65+
Context.getAdministrationService().saveGlobalProperty(new GlobalProperty("gp.baz", "Baz"));
66+
67+
// Replay
68+
loader.load();
69+
70+
// Verif
71+
Assert.assertEquals("Foo", Context.getAdministrationService().getGlobalProperty("gp.foo"));
72+
Assert.assertEquals("Bar", Context.getAdministrationService().getGlobalProperty("gp.bar"));
73+
Assert.assertEquals("Baz", Context.getAdministrationService().getGlobalProperty("gp.baz"));
74+
}
75+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<config>
2+
<globalProperties>
3+
<globalProperty>
4+
<property>gp.gp11</property>
5+
<value>GP one one</value>
6+
</globalProperty>
7+
<globalProperty>
8+
<property>gp.gp12</property>
9+
<value>GP one two</value>
10+
</globalProperty>
11+
</globalProperties>
12+
</config>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<config>
2+
<globalProperties>
3+
<globalProperty>
4+
<property>gp.gp21</property>
5+
<value>GP two one</value>
6+
</globalProperty>
7+
</globalProperties>
8+
</config>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<config>
2+
<comment>
3+
It should be ok to just add some comments like this.
4+
</comment>
5+
<globalProperties>
6+
<globalProperty>
7+
<property>gp.gp31</property>
8+
<value>GP three one</value>
9+
</globalProperty>
10+
<globalProperty>
11+
<property>gp.gp32</property>
12+
<value>GP three two</value>
13+
</globalProperty>
14+
<globalProperty>
15+
<property>gp.gp33</property>
16+
<value>GP three three</value>
17+
</globalProperty>
18+
</globalProperties>
19+
</config>

api/src/main/java/org/openmrs/module/initializer/api/InitializerSerializer.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
package org.openmrs.module.initializer.api;
22

3-
import java.io.InputStream;
4-
5-
import org.openmrs.GlobalProperty;
6-
import org.openmrs.module.idgen.IdentifierSource;
7-
import org.openmrs.module.initializer.api.gp.GlobalPropertiesConfig;
8-
93
import com.thoughtworks.xstream.XStream;
104
import com.thoughtworks.xstream.converters.UnmarshallingContext;
115
import com.thoughtworks.xstream.converters.reflection.ReflectionConverter;
@@ -14,12 +8,22 @@
148
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
159
import com.thoughtworks.xstream.mapper.Mapper;
1610
import com.thoughtworks.xstream.mapper.MapperWrapper;
11+
import org.openmrs.GlobalProperty;
12+
import org.openmrs.module.idgen.IdentifierSource;
13+
import org.openmrs.module.initializer.api.gp.GlobalPropertiesConfig;
14+
import org.slf4j.Logger;
15+
import org.slf4j.LoggerFactory;
16+
17+
import java.io.InputStream;
18+
import java.lang.reflect.Method;
1719

1820
/**
1921
* Use this serializer instead of a bare {@link XStream} if you want to ignore unmapped fields.
2022
*/
2123
public class InitializerSerializer extends XStream {
2224

25+
private static final Logger log = LoggerFactory.getLogger(InitializerSerializer.class);
26+
2327
public InitializerSerializer() {
2428
super();
2529
}
@@ -53,6 +57,15 @@ public static XStream getGlobalPropertiesConfigSerializer() {
5357
xs.alias("config", GlobalPropertiesConfig.class);
5458
xs.alias("globalProperty", GlobalProperty.class);
5559
xs.aliasField("value", GlobalProperty.class, "propertyValue");
60+
try {
61+
Method allowTypeHierarchy = XStream.class.getMethod("allowTypeHierarchy", Class.class);
62+
allowTypeHierarchy.invoke(xs, GlobalPropertiesConfig.class);
63+
allowTypeHierarchy.invoke(xs, GlobalProperty.class);
64+
log.debug("Successfully configured global properties config serializer with allowed types");
65+
}
66+
catch (Exception e) {
67+
log.debug("Error configuring global properties config serializer with allowed types", e);
68+
}
5669
return xs;
5770
}
5871

0 commit comments

Comments
 (0)