Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PropertiesBasedConfigurationBuilder needs better error information fo… #587

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -164,25 +164,38 @@ private void addPropertiesFromSystem(String key, Set<String> values) {
}

/**
* Adds matches from detected resource bundles
* Adds matches from detected resource bundles.
*
* @param key The key to match
* @param values The currently found values
*/
private void addPropertiesFromResourceBundle(String key, Set<String> values) {
try {
addPropertiesFromResourceBundle(key, values, new StringBuilder());
}

/**
* Adds matches from detected resource bundles
*
* @param key The key to match
* @param values The currently found values
* @param info a StringBuilder to append information about the found property, useful for debugging duplicates
*/
private void addPropertiesFromResourceBundle(String key, Set<String> values, StringBuilder info) {
try {
int count = 0;
for (Enumeration<URL> e = getResources(RESOURCE_BUNDLE); e.hasMoreElements();) {

URL url = e.nextElement();
Properties properties = new Properties();
InputStream propertyStream = url.openStream();

try {

properties.load(propertyStream);
addProperty(key, properties.getProperty(key), values);

String value = properties.getProperty(key);
if (value != null) {
values.add(value);
info.append(String.format("\t%d: %s=%s\n", count++, url.toExternalForm(), value));
}
} finally {
if (propertyStream != null) {
propertyStream.close();
Expand Down Expand Up @@ -296,15 +309,20 @@ protected <T> Class<T> getClassValue(String propertyName, Class<T> expectedType,

private String getValue(String propertyName, boolean required) {
Set<String> values = getPropertyValues(propertyName);
if (values.size() == 0) {
if (values.isEmpty()) {
if (required) {
throw new IllegalArgumentException("Cannot find required property " + propertyName
+ ", check that it is specified. See cdiLiteMode flag if testing CDI Lite.");
}
return null;
} else if (values.size() > 1) {
throw new IllegalArgumentException(
"More than one value given for " + propertyName + ", not sure which one to use!");
// Gather where the duplicate values come from
String systemValue = System.getProperty(propertyName);
StringBuilder info = new StringBuilder(
"More than one value given for " + propertyName + ", not sure which one to use!\n");
info.append("Sources for the property\n\tSystemProperty=").append(systemValue).append('\n');
addPropertiesFromResourceBundle(propertyName, values, info);
throw new IllegalArgumentException(info.toString());
} else {
return values.iterator().next();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2024, Red Hat, Inc., and individual contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.cdi.tck.test.porting;

import org.jboss.cdi.tck.api.Configuration;
import org.jboss.cdi.tck.impl.ConfigurationFactory;
import org.testng.Assert;
import org.testng.annotations.Test;

/**
* Validate that the CDI {@link Configuration} can be loaded and has the expected values
* as defined in the pom.xml surefire plugin configuration and META-INF/cdi-tck.properties.
*/
public class ConfigurationTest {
@Test
public void testCDIConfiguration() {
Configuration config = ConfigurationFactory.get();
Assert.assertEquals(config.getCoreMode(), Boolean.FALSE);
Assert.assertEquals(config.getLibraryDirectory(), "target/dependency/lib");
Assert.assertEquals(config.getTestDataSource(), "java:jboss/datasources/ExampleDS");
Assert.assertEquals(config.getTestJmsConnectionFactory(), "java:/ConnectionFactory");
Assert.assertEquals(config.getTestJmsQueue(), "java:/queue/test");
Assert.assertEquals(config.getTestJmsTopic(), "java:/topic/test");

Assert.assertTrue(config.getBeans() instanceof DummyBeans);
Assert.assertTrue(config.getContexts() instanceof DummyContexts);
Assert.assertTrue(config.getContextuals() instanceof DummyContextuals);
Assert.assertTrue(config.getCreationalContexts() instanceof DummyCreationalContexts);
Assert.assertTrue(config.getEl() instanceof DummyEL);
}
}
2 changes: 1 addition & 1 deletion impl/src/test/resources/META-INF/cdi-tck.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# CDI TCK configuration to run test from IDE
# Alter the path accordingly (absolute or relative from the tck/impl dir)
org.jboss.cdi.tck.libraryDirectory=../../../weld/core/jboss-tck-runner/1.1/target/dependency/lib
org.jboss.cdi.tck.libraryDirectory=target/dependency/lib
# JNDI DS
org.jboss.cdi.tck.testDataSource=java:jboss/datasources/ExampleDS
# JNDI JMS
Expand Down