Skip to content

Commit 4486cea

Browse files
committed
Fixing an NPE when the component which is being inherited from doesn't exist. (#69)
1 parent 4fc0f7c commit 4486cea

File tree

9 files changed

+96
-12
lines changed

9 files changed

+96
-12
lines changed

olcut-config-edn/src/test/java/com/oracle/labs/mlrg/olcut/config/edn/OverrideTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828

2929
package com.oracle.labs.mlrg.olcut.config.edn;
3030

31+
import com.oracle.labs.mlrg.olcut.config.io.ConfigLoaderException;
3132
import com.oracle.labs.mlrg.olcut.config.ConfigurationManager;
3233
import com.oracle.labs.mlrg.olcut.config.StringConfigurable;
3334
import com.oracle.labs.mlrg.olcut.config.StringleConfigurable;
3435

3536
import java.io.IOException;
37+
import org.junit.jupiter.api.Assertions;
3638
import org.junit.jupiter.api.BeforeAll;
3739
import org.junit.jupiter.api.Test;
3840

@@ -96,4 +98,12 @@ public void overrideWithSubType() throws IOException {
9698
assertEquals("d", sc2.three);
9799
assertEquals("e", sc2.four);
98100
}
101+
102+
@Test
103+
public void overrideIncorrectName() {
104+
Assertions.assertThrows(ConfigLoaderException.class,
105+
() -> {
106+
ConfigurationManager cm = new ConfigurationManager("overrideIncorrect.xml");
107+
});
108+
}
99109
}

olcut-config-json/src/main/java/com/oracle/labs/mlrg/olcut/config/json/JsonLoader.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,10 @@ protected void parseComponent(ObjectNode node) {
248248
// just in case.
249249
ConfigurationData spd = rpdMap.get(override);
250250
if (spd == null) {
251-
spd = existingRPD.get(override);
252-
if (spd == null) {
253-
throw new ConfigLoaderException("Override for undefined component: "
254-
+ override + ", with name " + curComponent);
251+
if (existingRPD == null || !existingRPD.containsKey(override)) {
252+
throw new ConfigLoaderException("Failed to find base component '"+override+"' inherited from '"+curComponent+"'.");
253+
} else {
254+
spd = existingRPD.get(override);
255255
}
256256
}
257257
if (curType != null && !curType.equals(spd.getClassName())) {

olcut-config-json/src/test/java/com/oracle/labs/mlrg/olcut/config/json/OverrideTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@
3131
import com.oracle.labs.mlrg.olcut.config.ConfigurationManager;
3232
import com.oracle.labs.mlrg.olcut.config.StringConfigurable;
3333
import com.oracle.labs.mlrg.olcut.config.StringleConfigurable;
34+
import com.oracle.labs.mlrg.olcut.config.io.ConfigLoaderException;
3435

3536
import java.io.IOException;
37+
38+
import org.junit.jupiter.api.Assertions;
3639
import org.junit.jupiter.api.BeforeAll;
3740
import org.junit.jupiter.api.Test;
3841

@@ -97,4 +100,12 @@ public void overrideWithSubType() throws IOException {
97100
assertEquals("d", sc2.three);
98101
assertEquals("e", sc2.four);
99102
}
103+
104+
@Test
105+
public void overrideIncorrectName() {
106+
Assertions.assertThrows(ConfigLoaderException.class,
107+
() -> {
108+
ConfigurationManager cm = new ConfigurationManager("overrideIncorrect.json");
109+
});
110+
}
100111
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"config": {
3+
"global-properties": {},
4+
"components": [
5+
{
6+
"name": "a",
7+
"type": "com.oracle.labs.mlrg.olcut.config.StringConfigurable",
8+
"properties": {
9+
"one": "a",
10+
"two": "b",
11+
"three": "c"
12+
}
13+
},
14+
{
15+
"name": "bErr",
16+
"inherit": "aErr",
17+
"properties": {}
18+
}
19+
]
20+
}
21+
}

olcut-config-protobuf/src/main/java/com/oracle/labs/mlrg/olcut/config/protobuf/ProtoLoader.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,10 @@ private void parseComponentProto(ComponentProto component) {
248248
// just in case.
249249
ConfigurationData spd = rpdMap.get(override);
250250
if (spd == null) {
251-
spd = existingRPD.get(override);
252-
if (spd == null) {
253-
throw new ConfigLoaderException("Override for undefined component: "
254-
+ override + ", with name " + name);
251+
if (existingRPD == null || !existingRPD.containsKey(override)) {
252+
throw new ConfigLoaderException("Failed to find base component '"+override+"' inherited from '"+name+"'.");
253+
} else {
254+
spd = existingRPD.get(override);
255255
}
256256
}
257257
if (!type.isEmpty() && !type.equals(spd.getClassName())) {

olcut-config-protobuf/src/test/java/com/oracle/labs/mlrg/olcut/config/protobuf/OverrideTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import com.oracle.labs.mlrg.olcut.config.ConfigurationManager;
3232
import com.oracle.labs.mlrg.olcut.config.StringConfigurable;
3333
import com.oracle.labs.mlrg.olcut.config.StringleConfigurable;
34+
import com.oracle.labs.mlrg.olcut.config.io.ConfigLoaderException;
35+
import org.junit.jupiter.api.Assertions;
3436
import org.junit.jupiter.api.BeforeAll;
3537
import org.junit.jupiter.api.Test;
3638

@@ -47,6 +49,7 @@ public class OverrideTest {
4749
@BeforeAll
4850
public static void setUpClass() throws Exception {
4951
ConfigurationManager.addFileFormatFactory(new ProtoConfigFactory());
52+
ConfigurationManager.addFileFormatFactory(new ProtoTxtConfigFactory());
5053
}
5154

5255
@Test
@@ -97,4 +100,12 @@ public void overrideWithSubType() throws IOException {
97100
assertEquals("d", sc2.three);
98101
assertEquals("e", sc2.four);
99102
}
103+
104+
@Test
105+
public void overrideIncorrectName() {
106+
Assertions.assertThrows(ConfigLoaderException.class,
107+
() -> {
108+
ConfigurationManager cm = new ConfigurationManager("overrideIncorrect.pbtxt");
109+
});
110+
}
100111
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
components {
2+
name: "a"
3+
type: "com.oracle.labs.mlrg.olcut.config.StringConfigurable"
4+
properties {
5+
key: "one"
6+
value: "a"
7+
}
8+
properties {
9+
key: "second"
10+
value: "b"
11+
}
12+
properties {
13+
key: "three"
14+
value: "c"
15+
}
16+
}
17+
components {
18+
name: "bErr",
19+
override: "aErr"
20+
}

olcut-core/src/main/java/com/oracle/labs/mlrg/olcut/config/xml/SAXLoader.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,10 @@ public void startElement(String uri, String localName, String qName,
264264
// just in case.
265265
ConfigurationData spd = rpdMap.get(override);
266266
if (spd == null) {
267-
spd = existingRPD.get(override);
268-
if (spd == null) {
269-
throw new SAXParseException("Override for undefined component: "
270-
+ override, locator);
267+
if (existingRPD == null || !existingRPD.containsKey(override)) {
268+
throw new SAXParseException("Failed to find base component '"+override+"' inherited from '"+curComponent+"'.", locator);
269+
} else {
270+
spd = existingRPD.get(override);
271271
}
272272
}
273273
if (curType != null && !curType.equals(spd.getClassName())) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<config>
3+
4+
<component name="a" type="com.oracle.labs.mlrg.olcut.config.StringConfigurable">
5+
<property name="one" value="a"/>
6+
<property name="two" value="b"/>
7+
<property name="three" value="c"/>
8+
</component>
9+
10+
<component name="bErr" inherit="aErr"/>
11+
</config>

0 commit comments

Comments
 (0)