garydgregory commented on code in PR #432:
URL: https://github.com/apache/commons-cli/pull/432#discussion_r3863884793
##########
src/test/java/org/apache/commons/cli/ConverterTests.java:
##########
@@ -72,6 +111,45 @@ void testClass() throws Exception {
assertNotNull(Converter.CLASS.apply(AClassWithoutADefaultConstructor.class.getName()));
}
+ @Test
+ void testClassDoesNotInitialize() throws Exception {
+ final Class<?> cls =
Converter.CLASS.apply(AClassWithAStaticInitializer.class.getName());
+ assertFalse(classInitializerRan);
+ assertEquals(AClassWithAStaticInitializer.class, cls);
+ cls.getConstructor().newInstance();
+ assertTrue(classInitializerRan);
+ }
+
+ @Test
+ void testClassValidatedBeforeInitialization() throws Exception {
+ // The caller pattern this enables: resolve the option, gate it with
isAssignableFrom, then instantiate.
+ // The assignability check must not run the class's static
initializer; only newInstance() should.
+ final Class<?> cls = Converter.CLASS.apply(PluginImpl.class.getName());
+ assertTrue(Plugin.class.isAssignableFrom(cls));
+ assertFalse(pluginInitializerRan);
+ final Object instance = cls.getConstructor().newInstance();
+ assertTrue(instance instanceof Plugin);
+ assertTrue(pluginInitializerRan);
+ }
+
+ @Test
+ void testClassNotInitializedThroughPublicApi() throws Exception {
Review Comment:
Now we're talking! 😄 Running the CI builds...
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]