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

[junit5.cm] handle just configurations from annotations #777

Merged
merged 3 commits into from
Jan 22, 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 @@ -31,15 +31,18 @@

public class BlockingConfigurationHandlerImpl implements ConfigurationListener, BlockingConfigurationHandler {


private Map<String, CountDownLatch> updateMap = new HashMap<String, CountDownLatch>();
private Map<String, CountDownLatch> deleteMap = new HashMap<String, CountDownLatch>();

@Override
public boolean update(Configuration configuration, Dictionary<String, Object> dictionary, long timeout)
throws InterruptedException, IOException {

CountDownLatch latch = createCountdownLatchUpdate(configuration.getPid());
configuration.update(dictionary);
boolean updatedBecauseDifferent = configuration.updateIfDifferent(dictionary);
if (!updatedBecauseDifferent) {
return true;
}
boolean isOk = latch.await(timeout, TimeUnit.MILLISECONDS);
return isOk;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.osgi.test.junit5.cm;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Store.CloseableResource;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;

public class ConfigCloseableResource implements CloseableResource {
private ExtensionContext extensionContext;
private BlockingConfigurationHandler timeoutListener;
private List<ConfigurationHolder> holders = new ArrayList<ConfigurationHolder>();

public ConfigCloseableResource(ExtensionContext extensionContext, BlockingConfigurationHandler timeoutListener) {
this.extensionContext = extensionContext;
this.timeoutListener = timeoutListener;
}

@Override
public void close() throws Throwable {
ConfigurationAdmin ca = ConfigurationExtension.configurationAdmin(extensionContext);
holders.stream()
.forEach(holder -> {

if (holder == null) {
return;
}

if (holder.getConfiguration() == null) {
return;
}

ConfigurationCopy configuration = holder.getConfiguration();
Optional<ConfigurationCopy> configurationCopyBefore = holder.getBeforeConfiguration();

try {
if (configurationCopyBefore.isPresent()) {

ConfigurationCopy copy = configurationCopyBefore.get();
Configuration conf = null;
if (copy.getFactoryPid() != null) {
String name = copy.getPid()
.substring(copy.getFactoryPid()
.length() + 1);
conf = ca.getFactoryConfiguration(copy.getFactoryPid(), name, copy.getBundleLocation());
} else {
conf = ca.getConfiguration(copy.getPid(), copy.getBundleLocation());
}
timeoutListener.update(conf, copy.getProperties(), 3000);
} else {

String pid = configuration.getPid();

Configuration configurationToDelete = ConfigUtil.getConfigsByServicePid(ca, pid, 1000);
if (configurationToDelete != null) {
timeoutListener.delete(configurationToDelete, 3000);
}
}
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}

public void addAll(List<ConfigurationHolder> holders) {
holders.forEach(this::add);
}

public void add(ConfigurationHolder holder) {

// We may know the state of the Configuration.
ConfigurationCopy compateConfig = holder.getConfiguration();
Optional<ConfigurationHolder> preStoresConfig = holders.stream()
.filter(c -> {
return c.getConfiguration()
.getPid()
.endsWith(compateConfig.getPid());
})
.findAny();

// if we habe a state, we can ignore later ons
if (!preStoresConfig.isPresent()) {
holders.add(holder);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@
package org.osgi.test.junit5.cm;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -97,71 +94,6 @@ static List<ConfigurationCopy> cloneConfigurations(List<Configuration> configura

}

static void resetConfig(BlockingConfigurationHandler timeoutListener, ConfigurationAdmin ca,
List<ConfigurationCopy> copys) throws Exception {

List<ConfigurationCopy> leftOvers = new ArrayList<ConfigurationCopy>(copys);
List<Configuration> configurations = ConfigUtil.getAllConfigurations(ca);

configurations.stream()
.forEach((conf) -> {
boolean match = copys.stream()
.anyMatch((copy) -> {
if (Objects.equals(conf.getPid(), copy.getPid())) {
try {
timeoutListener.update(conf, copy.getProperties(), 3000);
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
leftOvers.remove(copy);
return true;
} else {
return false;
}
});
try {
if (!match) {
String pid = conf.getPid();
timeoutListener.delete(conf, 3000);

}
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
leftOvers.stream()
.forEach((copy) -> {
try {
Configuration conf = null;
if (copy.getFactoryPid() != null) {
String name = copy.getPid()
.substring(copy.getFactoryPid()
.length() + 1);
conf = ca.getFactoryConfiguration(copy.getFactoryPid(), name, copy.getBundleLocation());
} else {
conf = ca.getConfiguration(copy.getPid(), copy.getBundleLocation());
}

try {
timeoutListener.update(conf, copy.getProperties(), 3000);

} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

} catch (IOException e) {
throw new UncheckedIOException(e);
}
});

}

public static <K, V> Dictionary<K, V> copy(Dictionary<K, V> dictionary) {
Dictionary<K, V> copy = new Hashtable<>();
Enumeration<K> keys = dictionary.keys();
Expand All @@ -172,5 +104,4 @@ public static <K, V> Dictionary<K, V> copy(Dictionary<K, V> dictionary) {
return copy;
}


}
Loading