Skip to content

Commit

Permalink
Merge pull request #1639 from CDCgov/application_context_fix_unreacha…
Browse files Browse the repository at this point in the history
…ble_code

Application context fix unreachable code
  • Loading branch information
luis-pabon-tf authored Dec 5, 2024
2 parents c46e4e1 + 18c2f48 commit a533d0d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ private static void injectIntoField(Field field) {

Object declaringClassImplementation =
getDeclaringClassImplementation(declaringClassesToTry);
if (declaringClassImplementation == null) {
return;
}

field.trySetAccessible();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gov.hhs.cdc.trustedintermediary.context

import gov.hhs.cdc.trustedintermediary.wrappers.Logger
import spock.lang.Specification

import javax.inject.Inject
Expand All @@ -12,6 +13,12 @@ class ApplicationContextTest extends Specification {
void test()
}

class NonSingletonClazz {
@Inject
Logger logger
void test() {}
}

static class DogCow implements TestingInterface {

@Override
Expand Down Expand Up @@ -55,6 +62,36 @@ class ApplicationContextTest extends Specification {
implementors == ApplicationContext.getImplementors(TestingInterface)
}

def "injectIntoNonSingleton unhappy path"() {
given:
def nonSingletonClass = new NonSingletonClazz()
def object = new Object()
ApplicationContext.register(Logger, object)
when:
ApplicationContext.injectIntoNonSingleton(nonSingletonClass)
then:
thrown(IllegalArgumentException)
}

def "injectIntoNonSingleton unhappy path when fieldImplementation runs into an error"() {
given:
def nonSingletonClass = new NonSingletonClazz()
when:
ApplicationContext.injectIntoNonSingleton(nonSingletonClass)
then:
thrown(IllegalArgumentException)
}

def "injectIntoNonSingleton unhappy path when fieldImplementation is null"() {
given:
def nonSingletonClass = new NonSingletonClazz()
when:
ApplicationContext.skipMissingImplementations = true
ApplicationContext.injectIntoNonSingleton(nonSingletonClass)
then:
noExceptionThrown()
}

def "implementation injection test"() {
given:
def injectedValue = "DogCow"
Expand Down Expand Up @@ -167,6 +204,25 @@ class ApplicationContextTest extends Specification {
Files.deleteIfExists(directoryPath)
}

def "registering an unsupported injection class"() {
given:
def injectedValue = "DogCow"
def injectionInstantiation = new InjectionDeclaringClass()

TestApplicationContext.register(List.class, injectionInstantiation)
// notice above that I'm registering the injectionInstantiation object as a List class.
// injectionInstantiation is of class InjectionDeclaringClass,
// and InjectionDeclaringClass doesn't implement List (it only implements AFieldInterface).
TestApplicationContext.register(String.class, injectedValue)

when:
TestApplicationContext.injectRegisteredImplementations()
injectionInstantiation.getAField()

then:
thrown(NullPointerException)
}

class InjectionDeclaringClass {
@Inject
private String aField
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class TestApplicationContext extends ApplicationContext {
TEST_ENV_VARS.clear()
}

def static injectRegisteredImplementations(def skip = true) {
skipMissingImplementations = skip
doInjectRegisteredImplementations()
def static injectRegisteredImplementations() {
skipMissingImplementations = true
ApplicationContext.injectRegisteredImplementations()
}

def static addEnvironmentVariable(String key, String value) {
Expand Down

0 comments on commit a533d0d

Please sign in to comment.