Skip to content

Commit

Permalink
Fixing an NPE when the component which is being inherited from doesn'…
Browse files Browse the repository at this point in the history
…t exist. (#69)
  • Loading branch information
Craigacp committed Oct 5, 2023
1 parent ad4e8a0 commit dcde0af
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,10 @@ protected void parseComponent(ObjectNode node) {
// just in case.
ConfigurationData spd = rpdMap.get(override);
if (spd == null) {
spd = existingRPD.get(override);
if (spd == null) {
throw new ConfigLoaderException("Override for undefined component: "
+ override + ", with name " + curComponent);
if (existingRPD == null || !existingRPD.containsKey(override)) {
throw new ConfigLoaderException("Failed to find base component '"+override+"' inherited from '"+curComponent+"'.");
} else {
spd = existingRPD.get(override);
}
}
if (curType != null && !curType.equals(spd.getClassName())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@
package com.oracle.labs.mlrg.olcut.config.json.test;

import com.oracle.labs.mlrg.olcut.config.ConfigurationManager;
import com.oracle.labs.mlrg.olcut.config.io.ConfigLoaderException;
import com.oracle.labs.mlrg.olcut.config.json.JsonConfigFactory;
import com.oracle.labs.mlrg.olcut.test.config.StringConfigurable;
import com.oracle.labs.mlrg.olcut.test.config.StringleConfigurable;

import java.io.IOException;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -99,4 +102,13 @@ public void overrideWithSubType() throws IOException {
assertEquals("d", sc2.three);
assertEquals("e", sc2.four);
}

@Test
public void overrideIncorrectName() {
Assertions.assertThrows(ConfigLoaderException.class,
() -> {
ConfigurationManager cm = new ConfigurationManager(createModuleResourceString(this.getClass(),
"overrideIncorrect.json"));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"config": {
"global-properties": {},
"components": [
{
"name": "a",
"type": "com.oracle.labs.mlrg.olcut.test.config.StringConfigurable",
"properties": {
"one": "a",
"two": "b",
"three": "c"
}
},
{
"name": "bErr",
"inherit": "aErr",
"properties": {}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,10 @@ private void parseComponentProto(ComponentProto component) {
// just in case.
ConfigurationData spd = rpdMap.get(override);
if (spd == null) {
spd = existingRPD.get(override);
if (spd == null) {
throw new ConfigLoaderException("Override for undefined component: "
+ override + ", with name " + name);
if (existingRPD == null || !existingRPD.containsKey(override)) {
throw new ConfigLoaderException("Failed to find base component '"+override+"' inherited from '"+name+"'.");
} else {
spd = existingRPD.get(override);
}
}
if (!type.isEmpty() && !type.equals(spd.getClassName())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
package com.oracle.labs.mlrg.olcut.config.protobuf.test;

import com.oracle.labs.mlrg.olcut.config.ConfigurationManager;
import com.oracle.labs.mlrg.olcut.config.io.ConfigLoaderException;
import com.oracle.labs.mlrg.olcut.config.protobuf.ProtoConfigFactory;
import com.oracle.labs.mlrg.olcut.config.protobuf.ProtoTxtConfigFactory;
import com.oracle.labs.mlrg.olcut.test.config.StringConfigurable;
import com.oracle.labs.mlrg.olcut.test.config.StringleConfigurable;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand All @@ -49,6 +52,7 @@ public class OverrideTest {
@BeforeAll
public static void setUpClass() throws Exception {
ConfigurationManager.addFileFormatFactory(new ProtoConfigFactory());
ConfigurationManager.addFileFormatFactory(new ProtoTxtConfigFactory());
}

@Test
Expand Down Expand Up @@ -99,4 +103,13 @@ public void overrideWithSubType() throws IOException {
assertEquals("d", sc2.three);
assertEquals("e", sc2.four);
}

@Test
public void overrideIncorrectName() {
Assertions.assertThrows(ConfigLoaderException.class,
() -> {
ConfigurationManager cm = new ConfigurationManager(createModuleResourceString(this.getClass(),
"overrideIncorrect.pbtxt"));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
components {
name: "a"
type: "com.oracle.labs.mlrg.olcut.test.config.StringConfigurable"
properties {
key: "one"
value: "a"
}
properties {
key: "second"
value: "b"
}
properties {
key: "three"
value: "c"
}
}
components {
name: "bErr",
override: "aErr"
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.io.IOException;

import com.oracle.labs.mlrg.olcut.config.ConfigurationManager;
import com.oracle.labs.mlrg.olcut.config.io.ConfigLoaderException;
import com.oracle.labs.mlrg.olcut.test.config.StringConfigurable;
import com.oracle.labs.mlrg.olcut.test.config.StringleConfigurable;
import org.junit.jupiter.api.Assertions;
Expand All @@ -44,10 +45,6 @@
*/
public class OverrideTest {

public OverrideTest() {
}


@Test
public void overrideWithSameType() throws IOException {
ConfigurationManager cm = new ConfigurationManager(createModuleResourceString(this.getClass(), "overrideConfig.xml"));
Expand Down Expand Up @@ -96,4 +93,13 @@ public void overrideWithSubType() throws IOException {
Assertions.assertEquals("d", sc2.three);
Assertions.assertEquals("e", sc2.four);
}

@Test
public void overrideIncorrectName() {
Assertions.assertThrows(ConfigLoaderException.class,
() -> {
ConfigurationManager cm = new ConfigurationManager(createModuleResourceString(this.getClass(),
"overrideIncorrect.xml"));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<config>

<component name="a" type="com.oracle.labs.mlrg.olcut.test.config.StringConfigurable">
<property name="one" value="a"/>
<property name="two" value="b"/>
<property name="three" value="c"/>
</component>

<component name="bErr" inherit="aErr"/>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,10 @@ public void startElement(String uri, String localName, String qName,
// just in case.
ConfigurationData spd = rpdMap.get(override);
if (spd == null) {
spd = existingRPD.get(override);
if (spd == null) {
throw new SAXParseException("Override for undefined component: "
+ override, locator);
if (existingRPD == null || !existingRPD.containsKey(override)) {
throw new SAXParseException("Failed to find base component '"+override+"' inherited from '"+curComponent+"'.", locator);
} else {
spd = existingRPD.get(override);
}
}
if (curType != null && !curType.equals(spd.getClassName())) {
Expand Down

0 comments on commit dcde0af

Please sign in to comment.