1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
package test.beast.integration;
import java.io.*;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import beast.base.util.FileUtils;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
*
* Test for java package dependencies
*
*/
public class DependencyTest {
private static class StreamGobbler implements Runnable {
private InputStream inputStream;
private Consumer<String> consumer;
public StreamGobbler(InputStream inputStream, Consumer<String> consumer) {
this.inputStream = inputStream;
this.consumer = consumer;
}
@Override
public void run() {
new BufferedReader(new InputStreamReader(inputStream)).lines()
.forEach(consumer);
}
}
@Disabled("Needs the user dir, which is intentionally void on Debian buildds")
@Test
public void testMutualDependencyOfPackageDependencies() throws Exception {
// obtain package dependencies in dot file by executing
// jdeps -dotoutput /tmp/jdeps/ beast
String jdepsDir = "/tmp/jdeps";
System.err.println("Running jdeps...");
if (!new File(jdepsDir).exists()) {
new File(jdepsDir).mkdirs();
}
ProcessBuilder builder = new ProcessBuilder();
builder.command("jdeps", "-dotoutput", jdepsDir+"/", "beast");
String dir = System.getProperty("user.dir");
if (dir.indexOf("/src/") > 0) {
dir = dir.substring(0, dir.indexOf("/src/"));
}
builder.directory(new File(dir + "/build/"));
Process process = builder.start();
StreamGobbler streamGobbler =
new StreamGobbler(process.getInputStream(), System.out::println);
Executors.newSingleThreadExecutor().submit(streamGobbler);
int exitCode = process.waitFor();
assert exitCode == 0;
// parse /tmp/jdeps/beast.dot for dependencies
System.err.println("Processing dependencies");
String dotFile = FileUtils.load(new File(jdepsDir + "/beast.dot"));
Set<String> edges = new HashSet<>();
for (String str : dotFile.split("\n")) {
if (str.contains("->")) {
String [] strs = str.split("->");
String from = strs[0].trim().replaceAll("\"","");
String to = strs[1].replaceAll("\"","");
if (to.indexOf("(") > 0) {
to = to.substring(0, to.indexOf("(")).trim();
}
edges.add(from + "->" + to);
}
}
Set<String> cycles = new HashSet<>();
for (String edge : edges) {
String [] str = edge.split("->");
String from = str[0];
String to = str[1];
String d = to + "->" + from;
if (edges.contains(d)) {
if (from.compareTo(to)> 0) {
cycles.add(from + " <-> " + to);
} else {
cycles.add(to + " <-> " + from);
}
}
}
if (cycles.size() > 0) {
System.err.println("Cycles found:");
for (String cycle : cycles) {
System.err.println(cycle);
}
} else {
System.err.println("Bravo! No cycles of length 2 found");
}
assertEquals(cycles.size(), 0);
// test for cycles longer than 2
Map<String, Set<String>> neighbours = new HashMap<>();
for (String edge : edges) {
String [] str = edge.split("->");
String from = str[0];
String to = str[1];
if (from.startsWith("beast")) {
if (!neighbours.containsKey(from)) {
neighbours.put(from, new HashSet<>());
}
if (to.startsWith("beast")) {
neighbours.get(from).add(to);
}
}
}
// create partial order
List<String> order = new ArrayList<>();
Set<String> done = new HashSet<>();
boolean progress = true;
while (progress && neighbours.size() > 0) {
progress = false;
for (String n : neighbours.keySet()) {
Set<String> neighbors = neighbours.get(n);
boolean canOrder = true;
for (String n2 : neighbors) {
if (!done.contains(n2)) {
canOrder = false;
break;
}
}
if (canOrder) {
order.add(n);
done.add(n);
neighbours.remove(n);
progress = true;
break;
}
}
}
// report if the ordering cannot be completed
if (!progress) {
System.err.println("Cyclic dependency larger than 2 detected");
for (String str : order) {
System.err.println(str);
}
System.err.println("Fine so far, but cannot add the following:");
for (String n : neighbours.keySet()) {
System.err.print(n);
System.err.print(" depends on ");
for (String n2 : neighbours.get(n)) {
if (!done.contains(n2)) {
System.err.print(n2 + ", ");
}
}
System.err.print(" Done: ");
for (String n2 : neighbours.get(n)) {
if (done.contains(n2)) {
System.err.print(n2 + ", ");
}
}
System.err.println();
}
}
assertEquals(true, progress);
System.err.println("Bravo! No cycles of length >2 found");
System.err.print("Done");
}
}
|