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 Nov 1, 2024
1 parent 4fc0f7c commit 4486cea
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@

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

import com.oracle.labs.mlrg.olcut.config.io.ConfigLoaderException;
import com.oracle.labs.mlrg.olcut.config.ConfigurationManager;
import com.oracle.labs.mlrg.olcut.config.StringConfigurable;
import com.oracle.labs.mlrg.olcut.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 @@ -96,4 +98,12 @@ 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("overrideIncorrect.xml");
});
}
}
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 @@ -31,8 +31,11 @@
import com.oracle.labs.mlrg.olcut.config.ConfigurationManager;
import com.oracle.labs.mlrg.olcut.config.StringConfigurable;
import com.oracle.labs.mlrg.olcut.config.StringleConfigurable;
import com.oracle.labs.mlrg.olcut.config.io.ConfigLoaderException;

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 @@ -97,4 +100,12 @@ 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("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.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 @@ -31,6 +31,8 @@
import com.oracle.labs.mlrg.olcut.config.ConfigurationManager;
import com.oracle.labs.mlrg.olcut.config.StringConfigurable;
import com.oracle.labs.mlrg.olcut.config.StringleConfigurable;
import com.oracle.labs.mlrg.olcut.config.io.ConfigLoaderException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

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

@Test
Expand Down Expand Up @@ -97,4 +100,12 @@ 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("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.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 @@ -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
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.config.StringConfigurable">
<property name="one" value="a"/>
<property name="two" value="b"/>
<property name="three" value="c"/>
</component>

<component name="bErr" inherit="aErr"/>
</config>

0 comments on commit 4486cea

Please sign in to comment.