Search in sources :

Example 66 with Resource

use of aQute.bnd.osgi.Resource in project bnd by bndtools.

the class BNDMetatypeTest method testADWithInheritance.

public void testADWithInheritance() throws Exception {
    Builder b = new Builder();
    b.addClasspath(new File("bin"));
    b.setProperty("Export-Package", "test.metatype");
    b.setProperty("-metatype", "*");
    b.setProperty("-metatypeannotations", "!*");
    b.setProperty("-metatype-inherit", "true");
    b.setProperty("-fixupmessages.bndannodeprecated", "Bnd metatype annotations are deprecated");
    b.build();
    Resource r = b.getJar().getResource("OSGI-INF/metatype/test.metatype.BNDMetatypeTest$TestADWithInheritanceChild.xml");
    assertOk(b);
    System.err.println(b.getJar().getResources().keySet());
    assertNotNull(r);
    IO.copy(r.openInputStream(), System.err);
    Document d = db.parse(r.openInputStream());
    assertAD(d, "fromChild", "From child", "fromChild", null, null, null, 0, "String", null, null, null);
    assertAD(d, "fromSuperOne", "From super one", "fromSuperOne", null, null, null, 0, "String", null, null, null);
    assertAD(d, "fromSuperTwo", "From super two", "fromSuperTwo", null, null, null, 0, "String", null, null, null);
}
Also used : Builder(aQute.bnd.osgi.Builder) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Resource(aQute.bnd.osgi.Resource) Document(org.w3c.dom.Document) File(java.io.File)

Example 67 with Resource

use of aQute.bnd.osgi.Resource in project bnd by bndtools.

the class BNDMetatypeTest method testNaming.

public void testNaming() throws Exception {
    Map<String, Object> map = Create.map();
    map.put("_secret", "_secret");
    map.put("_secret", "_secret");
    map.put(".secret", ".secret");
    map.put("$new", "$new");
    map.put("new", "new");
    map.put("secret", "secret");
    map.put("a_b_c", "a_b_c");
    map.put("a.b.c", "a.b.c");
    map.put(".a_b", ".a_b");
    map.put("$$$$a_b", "$$$$a_b");
    map.put("$$$$a.b", "$$$$a.b");
    map.put("a", "a");
    map.put("a$", "a$");
    map.put("a$$", "a$$");
    map.put("a$.$", "a$.$");
    map.put("a$_$", "a$_$");
    map.put("a..", "a..");
    map.put("noid", "noid");
    map.put("nullid", "nullid");
    Naming trt = Configurable.createConfigurable(Naming.class, map);
    // By name
    assertEquals("secret", trt.secret());
    assertEquals("_secret", trt.__secret());
    assertEquals(".secret", trt._secret());
    assertEquals("new", trt.$new());
    assertEquals("$new", trt.$$new());
    assertEquals("a.b.c", trt.a_b_c());
    assertEquals("a_b_c", trt.a__b__c());
    assertEquals(".a_b", trt._a__b());
    assertEquals("$$$$a.b", trt.$$$$$$$$a_b());
    assertEquals("$$$$a_b", trt.$$$$$$$$a__b());
    assertEquals("a", trt.a$());
    assertEquals("a$", trt.a$$());
    assertEquals("a$", trt.a$$$());
    assertEquals("a$.$", trt.a$$_$$());
    assertEquals("a$_$", trt.a$$__$$());
    assertEquals("a..", trt.a_$_());
    assertEquals("noid", trt.noid());
    assertEquals("nullid", trt.nullid());
    // By AD
    assertEquals("secret", trt.xsecret());
    assertEquals("_secret", trt.x__secret());
    assertEquals(".secret", trt.x_secret());
    assertEquals("new", trt.x$new());
    assertEquals("$new", trt.x$$new());
    assertEquals("a.b.c", trt.xa_b_c());
    assertEquals("a_b_c", trt.xa__b__c());
    assertEquals(".a_b", trt.x_a__b());
    assertEquals("$$$$a.b", trt.x$$$$$$$$a_b());
    assertEquals("$$$$a_b", trt.x$$$$$$$$a__b());
    assertEquals("a", trt.xa$());
    assertEquals("a$", trt.xa$$());
    assertEquals("a$", trt.xa$$$());
    assertEquals("a$.$", trt.xa$$_$$());
    Builder b = new Builder();
    b.addClasspath(new File("bin"));
    b.setProperty("Export-Package", "test.metatype");
    b.setProperty("-metatype", "*");
    b.setProperty("-metatypeannotations", "!*");
    b.setProperty("-fixupmessages.bndannodeprecated", "Bnd metatype annotations are deprecated");
    b.build();
    assertOk(b);
    Resource r = b.getJar().getResource("OSGI-INF/metatype/test.metatype.BNDMetatypeTest$Naming.xml");
    IO.copy(r.openInputStream(), System.err);
    Document d = db.parse(r.openInputStream(), "UTF-8");
    assertEquals("http://www.osgi.org/xmlns/metatype/v1.1.0", d.getDocumentElement().getNamespaceURI());
}
Also used : Builder(aQute.bnd.osgi.Builder) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Resource(aQute.bnd.osgi.Resource) Document(org.w3c.dom.Document) File(java.io.File)

Example 68 with Resource

use of aQute.bnd.osgi.Resource in project bnd by bndtools.

the class BNDMetatypeTest method testAD.

public void testAD() throws Exception {
    Builder b = new Builder();
    b.addClasspath(new File("bin"));
    b.setProperty("Export-Package", "test.metatype");
    b.setProperty("-metatype", "*");
    b.setProperty("-metatypeannotations", "!*");
    b.setProperty("-fixupmessages.bndannodeprecated", "Bnd metatype annotations are deprecated");
    b.build();
    Resource r = b.getJar().getResource("OSGI-INF/metatype/test.metatype.BNDMetatypeTest$TestAD.xml");
    assertOk(b);
    System.err.println(b.getJar().getResources().keySet());
    assertNotNull(r);
    IO.copy(r.openInputStream(), System.err);
    Document d = db.parse(r.openInputStream());
    assertAD(d, "noSettings", "No settings", "noSettings", null, null, null, 0, "String", null, null, null);
    assertAD(d, "withId", "With id", "id", null, null, null, 0, "String", null, null, null);
    assertAD(d, "name", "name", "withName", null, null, null, 0, "String", null, null, null);
    assertAD(d, "withMax", "With max", "withMax", null, "1", null, 0, "String", null, null, null);
    assertAD(d, "withMin", "With min", "withMin", "-1", null, null, 0, "String", null, null, null);
    assertAD(d, "withC1", "With c1", "withC1", null, null, null, 1, "String", null, null, null);
    assertAD(d, "withC0", "With c0", "withC0", null, null, null, 2147483647, "String", null, null, null);
    assertAD(d, "withC_1", "With c 1", "withC.1", null, null, null, -1, "String", null, null, null);
    assertAD(d, "withC_1ButArray", "With c 1 but array", "withC.1ButArray", null, null, null, -1, "String", null, null, null);
    assertAD(d, "withC1ButCollection", "With c1 but collection", "withC1ButCollection", null, null, null, 1, "String", null, null, null);
    assertAD(d, "withInt", "With int", "withInt", null, null, null, 0, "String", null, null, null);
    assertAD(d, "withString", "With string", "withString", null, null, null, 0, "Integer", null, null, null);
    assertAD(d, "a", "A", "a", null, null, null, 0, "String", "description_xxx\"xxx'xxx", null, null);
    assertAD(d, "valuesOnly", "Values only", "valuesOnly", null, null, null, 0, "String", null, new String[] { "a", "b" }, new String[] { "a", "b" });
    assertAD(d, "labelsAndValues", "Labels and values", "labelsAndValues", null, null, null, 0, "String", null, new String[] { "a", "b" }, new String[] { "A", "A" });
}
Also used : Builder(aQute.bnd.osgi.Builder) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Resource(aQute.bnd.osgi.Resource) Document(org.w3c.dom.Document) File(java.io.File)

Example 69 with Resource

use of aQute.bnd.osgi.Resource in project bnd by bndtools.

the class BNDMetatypeTest method testSimple.

public void testSimple() throws Exception {
    Builder b = new Builder();
    b.addClasspath(new File("bin"));
    b.setProperty("Export-Package", "test.metatype");
    b.setProperty("-metatype", "*");
    b.setProperty("-metatypeannotations", "!*");
    b.setProperty("-fixupmessages.bndannodeprecated", "Bnd metatype annotations are deprecated");
    b.build();
    Resource r = b.getJar().getResource("OSGI-INF/metatype/test.metatype.BNDMetatypeTest$TestSimple.xml");
    assertOk(b);
    System.err.println(b.getJar().getResources().keySet());
    assertNotNull(r);
    IO.copy(r.openInputStream(), System.err);
    Document d = db.parse(r.openInputStream());
    assertEquals("TestSimple", xpath.evaluate("//OCD/@name", d));
    assertEquals("simple", xpath.evaluate("//OCD/@description", d));
    assertEquals("test.metatype.BNDMetatypeTest$TestSimple", xpath.evaluate("//OCD/@id", d));
    assertEquals("test.metatype.BNDMetatypeTest$TestSimple", xpath.evaluate("//Designate/@pid", d));
    assertEquals("test.metatype.BNDMetatypeTest$TestSimple", xpath.evaluate("//Object/@ocdref", d));
    assertEquals("simple", xpath.evaluate("//OCD/AD[@id='simple']/@id", d));
    assertEquals("Simple", xpath.evaluate("//OCD/AD[@id='simple']/@name", d));
    assertEquals("String", xpath.evaluate("//OCD/AD[@id='simple']/@type", d));
    assertEquals("true", xpath.evaluate("//OCD/AD[@id='notSoSimple']/@required", d));
    /**
		 * https://github.com/bndtools/bnd/issues/281 Using the Bnd annotations
		 * library (1.52.3), the generated metatype file will have
		 * required='false' for all fields annotated with @Meta.AD(). When this
		 * annotation is omitted, or when the required property is explicitly
		 * set, the field is correctly marked as required. Taking a glance at
		 * the code, the bug appears to be due to aQute.bnd.osgi.Annotation
		 * using aQute.bnd.annotation.metatype.Configurable internally for
		 * bridging Bnd-annotations to Java-annotations. This configurable only
		 * obtains the values from the Bnd-annotation, omitting the defaults
		 * defined in the Java annotation. The workaround is to explicitly
		 * mention the required property on each field annotated with @Meta.AD.
		 */
    assertEquals("true", xpath.evaluate("//OCD/AD[@id='simple']/@required", d));
    assertEquals(Integer.MAX_VALUE + "", xpath.evaluate("//OCD/AD[@id='notSoSimple']/@cardinality", d));
}
Also used : Builder(aQute.bnd.osgi.Builder) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Resource(aQute.bnd.osgi.Resource) Document(org.w3c.dom.Document) File(java.io.File)

Example 70 with Resource

use of aQute.bnd.osgi.Resource in project bnd by bndtools.

the class BNDMetatypeTest method assertOCD.

private void assertOCD(Builder b, String cname, String id, String name, String description, String designate, boolean factory, String localization) throws Exception {
    Resource r = b.getJar().getResource("OSGI-INF/metatype/" + cname + ".xml");
    assertNotNull(r);
    IO.copy(r.openInputStream(), System.err);
    Document d = db.parse(r.openInputStream());
    assertEquals(id, xpath.evaluate("//OCD/@id", d, XPathConstants.STRING));
    assertEquals(name, xpath.evaluate("//OCD/@name", d, XPathConstants.STRING));
    assertEquals(localization == null ? cname : localization, xpath.evaluate("//@localization", d, XPathConstants.STRING));
    assertEquals(description == null ? "" : description, xpath.evaluate("//OCD/@description", d, XPathConstants.STRING));
    if (designate == null) {
        assertEquals(id, xpath.evaluate("//Designate/@pid", d, XPathConstants.STRING));
        if (factory)
            assertEquals(id, xpath.evaluate("//Designate/@factoryPid", d, XPathConstants.STRING));
    } else {
        assertEquals(designate, xpath.evaluate("//Designate/@pid", d, XPathConstants.STRING));
        if (factory)
            assertEquals(designate, xpath.evaluate("//Designate/@factoryPid", d, XPathConstants.STRING));
    }
    assertEquals(id, xpath.evaluate("//Object/@ocdref", d, XPathConstants.STRING));
}
Also used : Resource(aQute.bnd.osgi.Resource) Document(org.w3c.dom.Document)

Aggregations

Resource (aQute.bnd.osgi.Resource)147 Jar (aQute.bnd.osgi.Jar)87 Builder (aQute.bnd.osgi.Builder)83 File (java.io.File)76 XmlTester (aQute.bnd.test.XmlTester)48 JarResource (aQute.bnd.osgi.JarResource)20 Attributes (java.util.jar.Attributes)20 Map (java.util.Map)19 Manifest (java.util.jar.Manifest)19 FileResource (aQute.bnd.osgi.FileResource)17 LogService (org.osgi.service.log.LogService)15 HashMap (java.util.HashMap)14 Document (org.w3c.dom.Document)14 Properties (java.util.Properties)12 EmbeddedResource (aQute.bnd.osgi.EmbeddedResource)11 DocumentBuilder (javax.xml.parsers.DocumentBuilder)11 InputStream (java.io.InputStream)9 Attrs (aQute.bnd.header.Attrs)8 ArrayList (java.util.ArrayList)8 TreeMap (java.util.TreeMap)8