Skip to content

Commit

Permalink
feat(autodoc): suppoort context property on autodoc (#192)
Browse files Browse the repository at this point in the history
* feat(autodoc): suppoort context property on autodoc

* pr remarks

* chore: dependencies file
  • Loading branch information
wolf4ood committed Nov 16, 2023
1 parent 3cc2fc5 commit 382094b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DEPENDENCIES
Expand Up @@ -85,7 +85,7 @@ maven/mavencentral/org.apache.httpcomponents/httpclient/4.5.2, Apache-2.0, appro
maven/mavencentral/org.apache.httpcomponents/httpcore/4.4.4, Apache-2.0, approved, CQ11716
maven/mavencentral/org.apiguardian/apiguardian-api/1.1.2, Apache-2.0, approved, clearlydefined
maven/mavencentral/org.assertj/assertj-core/3.23.1, Apache-2.0, approved, clearlydefined
maven/mavencentral/org.checkerframework/checker-compat-qual/2.5.2, MIT, approved, clearlydefined
maven/mavencentral/org.checkerframework/checker-compat-qual/2.5.2, GPL-2.0-only with Classpath-Exception-2.0, approved, #11598
maven/mavencentral/org.checkerframework/checker-qual/3.12.0, MIT, approved, clearlydefined
maven/mavencentral/org.codehaus.mojo/animal-sniffer-annotations/1.17, MIT, approved, clearlydefined
maven/mavencentral/org.eclipse.edc/autodoc-processor/0.3.2-SNAPSHOT, Apache-2.0, approved, technology.edc
Expand Down
Expand Up @@ -49,6 +49,7 @@
* Contains methods for introspecting any given extension (represented by an {@link Element}) using the Java Compiler API.
*/
public class ExtensionIntrospector {
public static final String CONTEXT_ATTRIBUTE = "context";
private final Elements elementUtils;

public ExtensionIntrospector(Elements elementUtils) {
Expand Down Expand Up @@ -132,9 +133,13 @@ private Stream<? extends Element> getEnclosedElementsAnnotatedWith(Element exten
* Maps a {@link ConfigurationSetting} from an {@link Setting} annotation.
*/
private ConfigurationSetting createConfigurationSetting(VariableElement settingElement) {
var prefix = resolveConfigurationPrefix(settingElement);
var keyValue = prefix + settingElement.getConstantValue().toString();
var settingMirror = mirrorFor(Setting.class, settingElement);
var prefix = attributeValue(String.class, CONTEXT_ATTRIBUTE, settingMirror, elementUtils);
if (prefix.isEmpty()) {
prefix = resolveConfigurationPrefix(settingElement);
}

var keyValue = prefix + settingElement.getConstantValue().toString();

return ConfigurationSetting.Builder.newInstance().key(keyValue)
.description(attributeValue(String.class, "value", settingMirror, elementUtils))
Expand Down
Expand Up @@ -19,4 +19,9 @@ public interface Constants {
String TEST_SETTING_KEY = "edc.test.setting";
String TEST_SETTING_DEFAULT_VALUE = "default value";
String TEST_SPI_MODULE = "Test SPI Module";

String TEST_FIELD_PREFIX_SETTING_KEY = "edc.prefix.field.";
String TEST_CLASS_PREFIX_SETTING_KEY = "edc.prefix.class.";
String TEST_SETTING_ID_KEY = "id";

}
Expand Up @@ -19,8 +19,10 @@
import org.eclipse.edc.plugins.autodoc.core.processor.testextensions.RequiredService;
import org.eclipse.edc.plugins.autodoc.core.processor.testextensions.SampleExtensionWithoutAnnotation;
import org.eclipse.edc.plugins.autodoc.core.processor.testextensions.SecondExtension;
import org.eclipse.edc.plugins.autodoc.core.processor.testextensions.SettingContextExtension;
import org.eclipse.edc.plugins.autodoc.core.processor.testextensions.SomeOtherService;
import org.eclipse.edc.plugins.autodoc.core.processor.testextensions.SomeService;
import org.eclipse.edc.runtime.metamodel.domain.ConfigurationSetting;
import org.eclipse.edc.runtime.metamodel.domain.EdcServiceExtension;
import org.eclipse.edc.runtime.metamodel.domain.Service;
import org.eclipse.edc.runtime.metamodel.domain.ServiceReference;
Expand All @@ -34,7 +36,10 @@
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.edc.plugins.autodoc.core.processor.Constants.TEST_CLASS_PREFIX_SETTING_KEY;
import static org.eclipse.edc.plugins.autodoc.core.processor.Constants.TEST_FIELD_PREFIX_SETTING_KEY;
import static org.eclipse.edc.plugins.autodoc.core.processor.Constants.TEST_SETTING_DEFAULT_VALUE;
import static org.eclipse.edc.plugins.autodoc.core.processor.Constants.TEST_SETTING_ID_KEY;
import static org.eclipse.edc.plugins.autodoc.core.processor.TestFunctions.filterManifest;
import static org.eclipse.edc.plugins.autodoc.core.processor.TestFunctions.readManifest;

Expand All @@ -46,10 +51,10 @@ void verifyManifestContainsExtension() {
var modules = readManifest(filterManifest(tempDir));
assertThat(modules).hasSize(1);
assertThat(modules.get(0).getExtensions())
.hasSize(2)
.hasSize(3)
.extracting(EdcServiceExtension::getName)
.containsOnly(SampleExtensionWithoutAnnotation.class.getSimpleName(),
SecondExtension.class.getSimpleName())
SecondExtension.class.getSimpleName(), SettingContextExtension.class.getSimpleName())
.doesNotContain(NotAnExtension.class.getName()); //explicitly exclude this
}

Expand All @@ -65,7 +70,7 @@ void verifyManifestContainsCorrectElements() {
.allSatisfy(e -> assertThat(e.getCategories()).isNotNull().isEmpty())
.allSatisfy(e -> assertThat(e.getOverview()).isNull())
.extracting(EdcServiceExtension::getName)
.containsOnly(SampleExtensionWithoutAnnotation.class.getSimpleName(), SecondExtension.class.getSimpleName());
.containsOnly(SampleExtensionWithoutAnnotation.class.getSimpleName(), SecondExtension.class.getSimpleName(), SettingContextExtension.class.getSimpleName());

var ext1 = extensions.stream().filter(e -> e.getName().equals(SampleExtensionWithoutAnnotation.class.getSimpleName()))
.findFirst()
Expand Down Expand Up @@ -102,6 +107,25 @@ void verifyManifestContainsCorrectElements() {
assertThat(ext2.getConfiguration()).isEmpty();
}

@Test
void verifyManifestContainsCorrectSettingsWithContext() {
task.call();

var modules = readManifest(filterManifest(tempDir));
assertThat(modules).hasSize(1);
var extensions = modules.get(0).getExtensions();

var ext1 = extensions.stream().filter(e -> e.getName().equals(SettingContextExtension.class.getSimpleName()))
.findFirst()
.orElseThrow();


assertThat(ext1.getConfiguration())
.extracting(ConfigurationSetting::getKey)
.contains(TEST_CLASS_PREFIX_SETTING_KEY + TEST_SETTING_ID_KEY, TEST_FIELD_PREFIX_SETTING_KEY + TEST_SETTING_ID_KEY);

}

@Override
protected List<File> getCompilationUnits() {
var f = new File("src/test/java/org/eclipse/edc/plugins/autodoc/core/processor/testextensions/");
Expand Down
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/

package org.eclipse.edc.plugins.autodoc.core.processor.testextensions;

import org.eclipse.edc.plugins.autodoc.core.processor.Constants;
import org.eclipse.edc.runtime.metamodel.annotation.Setting;
import org.eclipse.edc.runtime.metamodel.annotation.SettingContext;
import org.eclipse.edc.spi.system.ServiceExtension;

import static org.eclipse.edc.plugins.autodoc.core.processor.Constants.TEST_CLASS_PREFIX_SETTING_KEY;
import static org.eclipse.edc.plugins.autodoc.core.processor.Constants.TEST_FIELD_PREFIX_SETTING_KEY;


@SettingContext(TEST_CLASS_PREFIX_SETTING_KEY)
public class SettingContextExtension implements ServiceExtension {

@Setting(context = TEST_FIELD_PREFIX_SETTING_KEY)
private static final String TEST_FIELD_PREFIX_SETTING = Constants.TEST_SETTING_ID_KEY;

@Setting
private static final String TEST_CLASS_PREFIX_SETTING = Constants.TEST_SETTING_ID_KEY;

}

0 comments on commit 382094b

Please sign in to comment.