Skip to content

Commit

Permalink
[ds] Place properties warning on class; extra warning for =
Browse files Browse the repository at this point in the history
Changed so that warnings about missing properties files are shown on the class itself
rather than on the bnd.bnd file. Extra hint if filename contains an = sign.

Fixes #6265

Signed-off-by: Fr Jeremy Krieg <fr.jkrieg@greekwelfaresa.org.au>
  • Loading branch information
kriegfrj committed Sep 18, 2024
1 parent e5ecfe7 commit 4c63652
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 4 deletions.
108 changes: 108 additions & 0 deletions biz.aQute.bndlib.tests/test/test/component/DSAnnotationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,114 @@ public void testProperties() throws Exception {
xt.assertAttribute("Integer", "scr:component/property[@name='\u0343\u0344\u0345\u0346']/@type");
}

/**
* Property test (missing properties files)
*/

@Component(properties = {
"some.missing.file.prop"
})
public static class PropertiesTestMissingFile {

}

@Test
public void testProperties_missingFile_warnings() throws Exception {
Builder b = new Builder();
b.setProperty(Constants.DSANNOTATIONS, "test.component.*$PropertiesTestMissingFile");
b.setProperty("Private-Package", "test.component");
b.addClasspath(new File("bin_test"));

Jar jar = b.build();
assertThat(b.getWarnings()).as("size")
.hasSize(1);
assertThat(b.getWarnings()
.get(0)).as("warning")
.contains("properties")
.contains(PropertiesTestMissingFile.class.getName())
.contains("not found")
.contains("some.missing.file.prop")
.doesNotMatch(".*mean.*property.*[?]");
}

@Component(factoryProperties = {
"some.missing.file.prop"
})
public static class FactoryPropertiesTestMissingFile {

}

@Test
public void testFactoryProperties_missingFile_warnings() throws Exception {
Builder b = new Builder();
b.setProperty(Constants.DSANNOTATIONS, "test.component.*$FactoryPropertiesTestMissingFile");
b.setProperty("Private-Package", "test.component");
b.addClasspath(new File("bin_test"));

Jar jar = b.build();
assertThat(b.getWarnings()).as("size")
.hasSize(1);
assertThat(b.getWarnings()
.get(0)).as("warning")
.contains("factoryProperties")
.contains(FactoryPropertiesTestMissingFile.class.getName())
.contains("not found")
.contains("some.missing.file.prop")
.doesNotMatch(".*mean.*factoryProperty.*[?]");
}

@Component(properties = {
"some=value"
})
public static class PropertiesTestMisconfiguredProperty {

}

@Test
public void testProperties_misconfiguredProperty_warnings() throws Exception {
Builder b = new Builder();
b.setProperty(Constants.DSANNOTATIONS, "test.component.*$PropertiesTestMisconfiguredProperty");
b.setProperty("Private-Package", "test.component");
b.addClasspath(new File("bin_test"));

Jar jar = b.build();
assertThat(b.getWarnings()).as("size")
.hasSize(1);
assertThat(b.getWarnings()
.get(0)).as("warning")
.contains("properties")
.contains(PropertiesTestMisconfiguredProperty.class.getName())
.contains("not found")
.contains("some=value")
.matches(".*mean.*property.*[?]");
}

@Component(factoryProperties = {
"some=value"
})
public static class FactoryPropertiesTestMisconfiguredProperty {

}

@Test
public void testFactoryProperties_misconfiguredProperty_warnings() throws Exception {
Builder b = new Builder();
b.setProperty(Constants.DSANNOTATIONS, "test.component.*$FactoryPropertiesTestMisconfiguredProperty");
b.setProperty("Private-Package", "test.component");
b.addClasspath(new File("bin_test"));

Jar jar = b.build();
assertThat(b.getWarnings()).as("size")
.hasSize(1);
assertThat(b.getWarnings()
.get(0)).as("warning")
.contains("factoryProperties")
.contains(FactoryPropertiesTestMisconfiguredProperty.class.getName())
.contains("not found")
.contains("some=value")
.matches(".*mean.*factoryProperty.*[?]");
}

/**
* Check that a DS 1.0 compotible class with annotations ends up with the DS
* 1.0 (no) namespace
Expand Down
27 changes: 25 additions & 2 deletions biz.aQute.bndlib/src/aQute/bnd/component/ComponentDef.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import aQute.bnd.component.annotations.ConfigurationPolicy;
import aQute.bnd.component.annotations.ServiceScope;
import aQute.bnd.component.error.DeclarativeServicesAnnotationError;
import aQute.bnd.component.error.DeclarativeServicesAnnotationError.ErrorType;
import aQute.bnd.osgi.Analyzer;
import aQute.bnd.osgi.Descriptors.TypeRef;
import aQute.bnd.version.Version;
Expand Down Expand Up @@ -130,11 +132,32 @@ void prepare(Analyzer analyzer) throws Exception {
properties.stream()
.filter(p -> !analyzer.getJar()
.exists(p))
.forEach(p -> analyzer.warning("The properties entry \"%s\" is not found in the jar", p));
.forEach(p -> {
String msg = "[%s] The properties entry \"%s\" is not found in the jar";
if (p.indexOf('=') >= 0) {
msg += " - did you mean to use the \"property\" attribute?";
}
final DeclarativeServicesAnnotationError details = new DeclarativeServicesAnnotationError(name, null,
null, ErrorType.MISSING_PROPERTIES_FILE);

analyzer.warning(msg, details.location(), p)
.details(details);
});

factoryProperties.stream()
.filter(p -> !analyzer.getJar()
.exists(p))
.forEach(p -> analyzer.warning("The factoryProperties entry \"%s\" is not found in the jar", p));
.forEach(p -> {
String msg = "[%s] The factoryProperties entry \"%s\" is not found in the jar";
if (p.indexOf('=') >= 0) {
msg += " - did you mean to use the \"factoryProperty\" attribute?";
}
final DeclarativeServicesAnnotationError factoryDetails = new DeclarativeServicesAnnotationError(name,
null, null, ErrorType.MISSING_FACTORYPROPERTIES_FILE);

analyzer.warning(msg, factoryDetails.location(), p)
.details(factoryDetails);
});
}

private void prepareVersion(Analyzer analyzer) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public enum ErrorType {
CONSTRUCTOR_SIGNATURE_ERROR,
VERSION_MISMATCH,
ANYSERVICE_NO_TARGET,
OPTIONAL_FIELD_WITH_MULTIPLE;
OPTIONAL_FIELD_WITH_MULTIPLE,
MISSING_PROPERTIES_FILE,
MISSING_FACTORYPROPERTIES_FILE;
}

public final String className;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* This package provides Declarative Service Annotation Error information.
*/
@Version("1.8.0")
@Version("1.9.0")
package aQute.bnd.component.error;

import org.osgi.annotation.versioning.Version;

0 comments on commit 4c63652

Please sign in to comment.