Skip to content

Commit

Permalink
Merge pull request #6082 from pkriens/featue/propertykey2
Browse files Browse the repository at this point in the history
Update of BndEditModel
  • Loading branch information
pkriens authored Apr 19, 2024
2 parents abb7714 + f37f857 commit d2fc304
Show file tree
Hide file tree
Showing 16 changed files with 439 additions and 260 deletions.
24 changes: 8 additions & 16 deletions biz.aQute.bndlib.tests/test/test/BndEditModelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,6 @@ public void testVariableInSystemPackages() throws Exception {
.mergeProperties(Constants.RUNSYSTEMPACKAGES));
}

@Test
public void testRunReposShared() throws Exception {
Workspace ws = new Workspace(new File("testresources/ws"));
BndEditModel model = new BndEditModel(ws);
File f = new File("testresources/ws/p7/syspkg.bndrun");
model.setBndResource(f);
model.setBndResourceName("syspkg.bndrun");
model.loadFrom(f);

List<String> runrepos = model.getRunRepos();
assertEquals(1, runrepos.size());
assertEquals("testing", runrepos.get(0));
}

@Test
public void testRemovePropertyFromStandalone() throws Exception {
File runFile = IO.getFile("testresources/standalone.bndrun");
Expand Down Expand Up @@ -200,7 +186,7 @@ public void testGetProperties() throws Exception {
BndEditModel model = new BndEditModel(run);

assertThat(model.getWorkspace()).isNotNull();
assertThat(model.getProject()).isNotNull();
assertThat(model.getOwner()).isNotNull();

model.setGenericString("a", "AA");

Expand Down Expand Up @@ -248,8 +234,14 @@ public void testGetPropertiesWithoutParent() throws Exception {
BndEditModel model = new BndEditModel();
model.setGenericString("foo", "FOO");
Processor p = model.getProperties();

assertThat(p.getProperty("foo")).isEqualTo("FOO");
assertThat(p.getPropertyKeys(false)).contains("foo");

model.loadFrom("");
p = model.getProperties();
assertThat(p.getProperty("foo")).isNull();
assertThat(p.getPropertyKeys(false)).doesNotContain("foo");

}

@Test
Expand Down
13 changes: 12 additions & 1 deletion biz.aQute.bndlib.tests/test/test/PropertiesTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand Down Expand Up @@ -49,14 +50,24 @@ public void testFilter() {
p1.setProperty("dan", "bandera");
p1.setProperty("susan", "sarandon");
p1.setProperty("jon", "bostrom");
p1.setProperty("bj", "hargrave");

Processor p2 = new Processor(p1);
p2.setForceLocal(Arrays.asList("dan"));
p2.setForceLocal(Arrays.asList("dan", "bj"));
p2.setProperty("susan", "schwarze");
p2.setProperty("bj", "ibm");

assertNull(p2.getProperty("dan"));
assertEquals("sarandon", p1.getProperty("susan"));
assertEquals("schwarze", p2.getProperty("susan"));
assertEquals("bostrom", p1.getProperty("jon"));
assertEquals("bostrom", p2.getProperty("jon"));
assertEquals(null, p2.getProperty("dan"));
assertEquals("bandera", p1.getProperty("dan"));
assertEquals("ibm", p2.getProperty("bj"));
assertEquals("hargrave", p1.getProperty("bj"));

assertThat(p2.getPropertyKeys(true)).containsExactlyInAnyOrder("susan", "jon", "bj");
}

@Test
Expand Down
28 changes: 25 additions & 3 deletions biz.aQute.bndlib.tests/test/test/bndmodel/BndModelTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package test.bndmodel;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -14,6 +15,7 @@
import org.junit.jupiter.api.Test;

import aQute.bnd.build.model.BndEditModel;
import aQute.bnd.build.model.clauses.ExportedPackage;
import aQute.bnd.build.model.clauses.VersionedClause;
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Processor;
Expand Down Expand Up @@ -109,15 +111,35 @@ public void testSetBuildPath(@InjectTemporaryDirectory
@Test
public void testParent() throws Exception {

BndEditModel model = new BndEditModel();
model.setRunFw("${fw}"); // set changes
File f = IO.getFile("testresources/bndmodel/test-01.bndrun");
BndEditModel model = new BndEditModel();
model.setBndResource(f);

model.load();
model.setRunFw("${fw}"); // set changes
Processor p = model.getProperties();

assertEquals("a, b, c", p.getProperty(Constants.RUNBUNDLES), "Set in file, refers to macro");
assertEquals("framework", p.getProperty(Constants.RUNFW), "Changes, refers to macro");

}

/**
* Check if we can get a processor of the model and verify that we get the
* proper properties.
*/
@Test
public void testTypes() throws Exception {

File f = IO.getFile("testresources/bndmodel/test-01.bndrun");
BndEditModel model = new BndEditModel();
model.setBndResource(f);
model.load();
model.add("Export-Package", "foo;version=1,bar;version=2;xyz=1");

List<ExportedPackage> exportedPackages = model.getExportedPackages();
assertThat(exportedPackages).hasSize(2);
model.add("Export-Package", "foo2;version=2,bar2;version=2;xyz=2");
exportedPackages = model.getExportedPackages();
assertThat(exportedPackages).hasSize(4);
}
}
58 changes: 51 additions & 7 deletions biz.aQute.bndlib/src/aQute/bnd/build/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -3678,18 +3678,62 @@ public Optional<Processor> findProcessor(File file) {
.endsWith(".bndrun"))
return Optional.of(Run.createRun(getWorkspace(), file));

try (ProjectBuilder builder = getBuilder(null)) {
for (Builder b : builder.getSubBuilders()) {
if (file.equals(b.getPropertiesFile())) {
Processor sub = new Processor(this);
sub.setProperties(file);
return Optional.of(sub);
}
for (SubProject sub : getSubProjects()) {
if (file.equals(sub.getPropertiesFile())) {
return Optional.of(sub);
}
}
return Optional.empty();
} catch (Exception e) {
throw Exceptions.duck(e);
}
}

/**
* Return a list of sub projects.
*/

@SuppressWarnings("resource")
public List<SubProject> getSubProjects() {

String sub = getProperty(SUB);
if (sub == null || sub.trim()
.length() == 0 || EMPTY_HEADER.equals(sub)) {
return Collections.emptyList();
}

if (isTrue(getProperty(NOBUNDLES)))
return Collections.emptyList();

Set<File> parentFiles = new HashSet<>();
Processor rover = this;
while (rover != null) {
parentFiles.add(rover.getPropertiesFile());
rover = rover.getParent();
}

List<SubProject> subProjects = new ArrayList<>();
Instructions instructions = new Instructions(sub);
List<File> members = IO.listFiles(getBase());
nextFile: while (!members.isEmpty()) {

File file = members.remove(0);
if (!file.isFile() || file.getName()
.startsWith(".") || parentFiles.contains(file))
continue nextFile;

for (Instruction instruction : instructions.keySet()) {
if (instruction.matches(file.getName())) {

if (!instruction.isNegated()) {
subProjects.add(new SubProject(this, file));
}
continue nextFile;
}
}
}
return subProjects;

}

}
51 changes: 51 additions & 0 deletions biz.aQute.bndlib/src/aQute/bnd/build/SubProject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package aQute.bnd.build;

import java.io.File;

import aQute.bnd.osgi.Processor;
import aQute.lib.strings.Strings;

/**
* Models a sub project when the `-sub` instruction is used.
*/
public class SubProject extends Processor {

final Project project;
final String name;

SubProject(Project project, File properties) {
super(project);
assert properties != null && properties.isFile();
this.project = project;
String[] parts = Strings.extension(properties.getName());
if (parts == null)
name = properties.getName();
else
name = parts[0];

setBase(project.getBase());
this.setProperties(properties);
use(project);
}

@Override
public String toString() {
return project + "." + getName();
}

public Project getProject() {
return project;
}

public String getName() {
return name;
}

public ProjectBuilder getProjectBuilder() throws Exception {
ProjectBuilder builder = new ProjectBuilder(project);
builder.setProperties(getPropertiesFile());
builder.use(this);
addClose(this);
return builder;
}
}
Loading

0 comments on commit d2fc304

Please sign in to comment.