Esempio n. 1
0
  public static void testEnum() throws Exception {
    Builder b = new Builder();
    b.addClasspath(new File("bin"));
    b.setProperty("Export-Package", "test.metatype");
    b.setProperty("-metatype", "*");
    b.build();
    assertEquals(0, b.getErrors().size());
    assertEquals(0, b.getWarnings().size());

    Resource r = b.getJar().getResource("OSGI-INF/metatype/test.metatype.MetatypeTest$Enums.xml");
    IO.copy(r.openInputStream(), System.err);

    Document d = db.parse(r.openInputStream());
    assertEquals(
        "http://www.osgi.org/xmlns/metatype/v1.1.0", d.getDocumentElement().getNamespaceURI());

    Properties p = new Properties();
    p.setProperty("r", "requireConfiguration");
    p.setProperty("i", "ignoreConfiguration");
    p.setProperty("o", "optionalConfiguration");
    Enums enums = Configurable.createConfigurable(Enums.class, (Map<Object, Object>) p);
    assertEquals(Enums.X.requireConfiguration, enums.r());
    assertEquals(Enums.X.ignoreConfiguration, enums.i());
    assertEquals(Enums.X.optionalConfiguration, enums.o());
  }
Esempio n. 2
0
  /**
   * Test if we can select
   *
   * @throws Exception
   */
  public void testSelect() throws Exception {
    Jar bjara = getContractExporter("atest", "2.5", "${exports}");
    Jar bjarb = getContractExporter("btest", "2.5", "${exports}");

    Builder a = newBuilder();
    a.setTrace(true);

    a.addClasspath(bjara); // 1x
    a.addClasspath(bjarb); // 2x
    a.setProperty(Constants.CONTRACT, "atest;alpha=1");
    a.setImportPackage("org.osgi.service.cm,*");
    a.setProperty("Export-Package", "test.refer");
    Jar ajar = a.build();
    assertTrue(a.check());
    ajar.getManifest().write(System.out);

    Domain domain = Domain.domain(ajar.getManifest());
    Parameters p = domain.getRequireCapability();
    p.remove("osgi.ee");
    assertNotNull(p);
    assertEquals(1, p.size());
    Attrs attrs = p.get("osgi.contract");
    String alpha = attrs.get("alpha");
    assertEquals("1", alpha);
    assertEquals("(&(osgi.contract=atest)(version=2.5.0))", attrs.get("filter:"));
  }
Esempio n. 3
0
 public static void testBundleActivationPolicyMultiple() throws Exception {
   Builder v = new Builder();
   v.setProperty("Private-Package", "test.activator");
   v.addClasspath(new File("bin"));
   v.setProperty(Constants.BUNDLE_ACTIVATIONPOLICY, "lazy;hello:=1,2");
   v.build();
   assertTrue(v.check("Bundle-ActivationPolicy has too many arguments lazy;hello:=1,2"));
 }
Esempio n. 4
0
 public static void testBundleActivationPolicyBad() throws Exception {
   Builder v = new Builder();
   v.setProperty("Private-Package", "test.activator");
   v.addClasspath(new File("bin"));
   v.setProperty(Constants.BUNDLE_ACTIVATIONPOLICY, "eager");
   v.build();
   assertTrue(v.check("Bundle-ActivationPolicy set but is not set to lazy: eager"));
 }
Esempio n. 5
0
 /** Test if we can get the version from the source and apply the default policy. */
 public static void testVersionPolicyImportedExportsDefaultPolicy() throws Exception {
   Builder b = new Builder();
   b.addClasspath(IO.getFile("jar/osgi.jar"));
   b.addClasspath(new File("bin"));
   b.setProperty("Export-Package", "org.osgi.service.event");
   b.setProperty("Private-Package", "test.refer");
   b.build();
   String s = b.getImports().getByFQN("org.osgi.service.event").get("version");
   assertEquals("[1.0,2)", s);
 }
Esempio n. 6
0
 /** Test export annotation. */
 public static void testExportAnnotation() throws Exception {
   Builder a = new Builder();
   a.addClasspath(new File("bin"));
   a.setProperty("build", "xyz");
   a.setProperty("Export-Package", "test.versionpolicy.api");
   a.build();
   Map<String, String> attrs = a.getExports().getByFQN("test.versionpolicy.api");
   assertEquals("1.2.0.xyz", attrs.get("version"));
   assertEquals("PrivateImpl", attrs.get("exclude:"));
   assertEquals("a", attrs.get("mandatory:"));
 }
Esempio n. 7
0
 /**
  * See if we a can override the version from the export statement and the version from the source.
  */
 public static void testImportOverridesDiscoveredVersion() throws Exception {
   Builder b = new Builder();
   b.addClasspath(IO.getFile("jar/osgi.jar"));
   b.addClasspath(new File("bin"));
   b.setProperty("Export-Package", "org.osgi.service.event");
   b.setProperty("Private-Package", "test.refer");
   b.setProperty("Import-Package", "org.osgi.service.event;version=2.1.3.q");
   b.build();
   String s = b.getImports().getByFQN("org.osgi.service.event").get("version");
   assertEquals("2.1.3.q", s);
 }
Esempio n. 8
0
  private void testFilePath(String path, String pattern, boolean good) throws Exception {
    Builder b = new Builder();
    try {
      b.setProperty("-includeresource", path + ";literal='x'");
      if (pattern != null) b.setProperty(Constants.INVALIDFILENAMES, pattern);

      b.build();
      if (good) assertTrue(b.check());
      else assertTrue(b.check("Invalid file/directory"));
    } finally {
      b.close();
    }
  }
Esempio n. 9
0
  /** Test default package versions. */
  public static void testDefaultPackageVersion() throws Exception {
    Builder a = new Builder();
    a.addClasspath(new File("bin"));
    a.setProperty("Bundle-Version", "1.2.3");
    a.setProperty("Export-Package", "test.refer");
    Jar jar = a.build();

    Manifest m = jar.getManifest();
    Parameters exports =
        Processor.parseHeader(m.getMainAttributes().getValue(Constants.EXPORT_PACKAGE), null);
    Map<String, String> attrs = exports.get("test.refer");
    assertNotNull(attrs);
    assertEquals("1.2.3", attrs.get("version"));
  }
Esempio n. 10
0
  public static void testADWithInheritance() throws Exception {
    Builder b = new Builder();
    b.addClasspath(new File("bin"));
    b.setProperty("Export-Package", "test.metatype");
    b.setProperty("-metatype", "*");
    b.setProperty("-metatype-inherit", "true");
    b.build();
    Resource r =
        b.getJar()
            .getResource(
                "OSGI-INF/metatype/test.metatype.MetatypeTest$TestADWithInheritanceChild.xml");
    assertEquals(0, b.getErrors().size());
    assertEquals(0, b.getWarnings().size());
    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);
  }
Esempio n. 11
0
  /**
   * Test if we can detect an overlap, and then if we can control the overlap
   *
   * @throws Exception
   */
  public void testOverlap() throws Exception {
    Jar bjar = getContractExporter("test", "2.5", "${exports}");

    Builder a = newBuilder();
    a.setTrace(true);

    a.addClasspath(bjar); // 1x
    a.addClasspath(bjar); // 2x
    a.setProperty(Constants.CONTRACT, "*");
    a.setImportPackage("org.osgi.service.cm,*");
    a.setProperty("Export-Package", "test.refer");
    Jar ajar = a.build();
    assertTrue(
        a.check(
            "Contracts \\[Contract \\[name=test;version=2.5.0;from=biz.aQute.bndlib.tests\\], Contract \\[name=test;version=2.5.0"));
  }
Esempio n. 12
0
 public static void testBundleActivationPolicyNone() throws Exception {
   Builder v = new Builder();
   v.setProperty("Private-Package", "test.activator");
   v.addClasspath(new File("bin"));
   v.build();
   assertTrue(v.check());
 }
Esempio n. 13
0
  /** Test import provide:. */
  public static void testExportProvided() throws Exception {
    Builder a = new Builder();
    a.addClasspath(IO.getFile("jar/osgi.jar"));
    a.addClasspath(new File("bin"));
    a.setProperty("Private-Package", "test.refer");
    a.setProperty("Export-Package", "org.osgi.service.http;provide:=true");
    Jar jar = a.build();
    Map<String, String> event = a.getImports().getByFQN("org.osgi.service.event");
    assertEquals("[1.0,2)", event.get("version"));
    Map<String, String> http = a.getImports().getByFQN("org.osgi.service.http");
    assertEquals("[1.2,1.3)", http.get("version"));

    Manifest m = jar.getManifest();
    String imports = m.getMainAttributes().getValue(Constants.IMPORT_PACKAGE);
    assertFalse(imports.contains(Constants.PROVIDE_DIRECTIVE));
  }
Esempio n. 14
0
  public static void testnativeCode() throws Exception {
    Builder b = new Builder();
    b.addClasspath(new File("bin"));
    b.setProperty("-resourceonly", "true");
    b.setProperty("Include-Resource", "native/win32/NTEventLogAppender-1.2.dll;literal='abc'");
    b.setProperty(
        "Bundle-NativeCode",
        "native/win32/NTEventLogAppender-1.2.dll; osname=Win32; processor=x86");
    b.build();
    Verifier v = new Verifier(b);

    v.verifyNative();
    System.err.println(v.getErrors());
    assertEquals(0, v.getErrors().size());
    v.close();
    b.close();
  }
Esempio n. 15
0
 /** The default policy is truncate micro. Check if this is applied to the import. */
 public static void testImportMicroTruncated() throws Exception {
   Builder b = new Builder();
   b.addClasspath(IO.getFile("jar/osgi.jar"));
   b.setProperty("Import-Package", "org.osgi.service.event");
   b.build();
   String s = b.getImports().getByFQN("org.osgi.service.event").get("version");
   assertEquals("[1.0,2)", s);
 }
Esempio n. 16
0
  /** hardcoded imports */
  public static void testHardcodedImports() throws Exception {
    Builder b = new Builder();
    b.addClasspath(IO.getFile("jar/osgi.jar"));
    b.setProperty("-versionpolicy", "${range;[==,+)}");
    b.setProperty("Private-Package", "org.objectweb.asm");
    b.setProperty("Import-Package", "org.osgi.framework,org.objectweb.asm,abc;version=2.0.0,*");
    b.build();
    Manifest m = b.getJar().getManifest();
    m.write(System.err);
    String s = b.getImports().getByFQN("org.objectweb.asm").get("version");
    assertNull(s);
    s = b.getImports().getByFQN("abc").get("version");
    assertEquals("2.0.0", s);

    s = b.getImports().getByFQN("org.osgi.framework").get("version");
    assertEquals("[1.3,2)", s);
  }
Esempio n. 17
0
  private File create(String bsn, Version v) throws Exception {
    String name = bsn + "-" + v;
    Builder b = new Builder();
    b.setBundleSymbolicName(bsn);
    b.setBundleVersion(v);
    b.setProperty("Random", random++ + "");
    b.setProperty("-resourceonly", true + "");
    b.setIncludeResource("foo;literal='foo'");
    Jar jar = b.build();
    assertTrue(b.check());

    File file = IO.getFile(tmp, name + ".jar");
    file.getParentFile().mkdirs();
    jar.updateModified(System.currentTimeMillis(), "Force it to now");
    jar.write(file);
    b.close();
    return file;
  }
Esempio n. 18
0
  /**
   * Make sure we do not add a contract if not used
   *
   * @throws Exception
   */
  public void testUnused() throws Exception {
    Jar bjara = getContractExporter("atest", "2.5", "${exports}");

    Builder a = newBuilder();
    a.setTrace(true);
    a.addClasspath(bjara);

    a.setProperty(Constants.CONTRACT, "*");
    a.setImportPackage("test.packageinfo,*");
    a.setProperty("Export-Package", "test.refer");
    Jar ajar = a.build();
    assertTrue(a.check());

    Domain domain = Domain.domain(ajar.getManifest());
    Parameters p = domain.getRequireCapability();
    p.remove("osgi.ee");
    assertEquals(0, p.size());
  }
Esempio n. 19
0
  /**
   * Test the warnings that we have no no version
   *
   * @throws Exception
   */
  public void testWarningVersion() throws Exception {
    Jar bjara = getContractExporter("abc", null, "${exports}");

    Builder a = newBuilder();
    a.setTrace(true);
    a.addClasspath(bjara);

    a.setProperty(Constants.CONTRACT, "*");
    a.setImportPackage("test.packageinfo,*");
    a.setProperty("Export-Package", "test.refer");
    Jar ajar = a.build();
    assertTrue(
        a.check(
            "Contract \\[name=abc;version=0.0.0;from=biz.aQute.bndlib.tests] does not declare a version"));

    Domain domain = Domain.domain(ajar.getManifest());
    Parameters p = domain.getRequireCapability();
    p.remove("osgi.ee");
    assertEquals(0, p.size());
  }
Esempio n. 20
0
 /** Verify that an invalid namespace error is actually an error */
 public void verifyNamespace() throws Exception {
   Builder b = new Builder();
   b.setProperty("Require-Capability", "+++,bla.bla");
   b.setProperty("Provide-Capability", "===,bla.bla");
   b.setIncludeResource("foo;literal='foo'");
   Jar inner = b.build();
   assertTrue(
       b.check(
           "The Require-Capability with namespace \\+\\+\\+ is not a symbolic name",
           "The Provide-Capability with namespace === is not a symbolic name"));
 }
Esempio n. 21
0
  private Jar getContractExporter(String name, String version, String uses)
      throws IOException, Exception {
    Builder b = newBuilder();
    Formatter sb = new Formatter();
    try {
      sb.format("osgi.contract");
      if (name != null) sb.format(";osgi.contract=%s", name);
      if (version != null) sb.format(";version:Version=%s", version);
      if (uses != null) sb.format(";uses:='%s'", uses);

      b.setProperty("Provide-Capability", sb.toString());
      b.setProperty("Export-Package", "org.osgi.service.eventadmin,org.osgi.service.cm");
      Jar bjar = b.build();
      assertTrue(b.check());

      return bjar;
    } finally {
      sb.close();
    }
  }
Esempio n. 22
0
  public static void testSimple() throws Exception {
    Builder b = new Builder();
    b.addClasspath(new File("bin"));
    b.setProperty("Export-Package", "test.metatype");
    b.setProperty("-metatype", "*");
    b.build();
    Resource r =
        b.getJar().getResource("OSGI-INF/metatype/test.metatype.MetatypeTest$TestSimple.xml");
    assertEquals(0, b.getErrors().size());
    assertEquals(0, b.getWarnings().size());
    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.MetatypeTest$TestSimple", xpath.evaluate("//OCD/@id", d));
    assertEquals("test.metatype.MetatypeTest$TestSimple", xpath.evaluate("//Designate/@pid", d));
    assertEquals("test.metatype.MetatypeTest$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
     *
     * <p>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));
  }
Esempio n. 23
0
 /** Check if we can set a specific version on the import that does not use a version policy. */
 public static void testImportMicroNotTruncated() throws Exception {
   Builder b = new Builder();
   b.addClasspath(IO.getFile("jar/osgi.jar"));
   b.setProperty(
       "Import-Package",
       "org.osgi.service.event;version=${@}, org.osgi.service.log;version=\"${range;[==,=+)}\"");
   b.build();
   String s = b.getImports().getByFQN("org.osgi.service.event").get("version");
   String l = b.getImports().getByFQN("org.osgi.service.log").get("version");
   assertEquals("1.0.1", s);
   assertEquals("[1.3,1.4)", l);
 }
Esempio n. 24
0
  public void testSimple() throws Exception {
    Jar bjar = getContractExporter("test", "2.5", "${exports}");

    Builder a = newBuilder();
    a.setTrace(true);
    a.addClasspath(bjar);
    a.setProperty(Constants.CONTRACT, "*");
    a.setImportPackage("org.osgi.service.cm,*");
    a.setProperty("Export-Package", "test.refer");
    Jar ajar = a.build();
    assertTrue(a.check());
    Domain domain = Domain.domain(ajar.getManifest());
    Parameters rc = domain.getRequireCapability();
    rc.remove("osgi.ee");
    System.out.println(rc);
    assertEquals(1, rc.size());

    Packages ps = a.getImports();
    assertTrue(ps.containsFQN("org.osgi.service.cm"));
    Attrs attrs = ps.getByFQN("org.osgi.service.cm");
    assertNotNull(attrs);
    assertNull(attrs.getVersion());
  }
Esempio n. 25
0
  /** Check implementation version policy. Uses the package test.versionpolicy.(uses|implemented) */
  static void assertPolicy(String pack, String type) throws Exception {
    Builder a = new Builder();
    a.addClasspath(new File("bin"));
    a.setProperty("Export-Package", "test.versionpolicy.api");
    Jar jar = a.build();

    Builder b = new Builder();
    b.addClasspath(jar);
    b.addClasspath(new File("bin"));

    b.setProperty("-versionpolicy-impl", "IMPL");
    b.setProperty("-versionpolicy-uses", "USES");
    b.setProperty("Private-Package", pack);
    b.build();
    Manifest m = b.getJar().getManifest();
    m.write(System.err);
    Map<String, String> map = b.getImports().getByFQN("test.versionpolicy.api");
    assertNotNull(map);
    // String s = map.get(Constants.IMPLEMENTED_DIRECTIVE);
    // assertEquals("true", s);
    Parameters mp = Processor.parseHeader(m.getMainAttributes().getValue("Import-Package"), null);
    assertEquals(type, mp.get("test.versionpolicy.api").get("version"));
  }
Esempio n. 26
0
 /** Test the strict flag */
 public void testStrict() throws Exception {
   Builder bmaker = new Builder();
   bmaker.addClasspath(IO.getFile("jar/osgi.jar"));
   bmaker.addClasspath(new File("bin"));
   bmaker.setProperty(
       "Export-Package",
       "org.osgi.service.eventadmin;version='[1,2)',org.osgi.framework;version=x13,test;-remove-attribute:=version,test.lib;specification-version=12,test.split");
   bmaker.setProperty(
       "Import-Package",
       "foo;version=1,bar;version='[1,x2)',baz;version='[2,1)',baz2;version='(1,1)',*");
   bmaker.setProperty("-strict", "true");
   Jar jar = bmaker.build();
   assertTrue(
       bmaker.check(
           "\\QExport-Package or -exportcontents refers to missing package 'org.osgi.service.eventadmin'\\E",
           "Import Package clauses without version range \\(excluding javax\\.\\*\\):",
           "Import Package bar has an invalid version range syntax \\[1,x2\\)",
           "Import Package baz2 has an empty version range syntax \\(1,1\\), likely want to use \\[1.0.0,1.0.0\\]",
           "Import Package baz has an invalid version range syntax \\[2,1\\):Low Range is higher than High Range: 2.0.0-1.0.0",
           "Import Package clauses which use a version instead of a version range. This imports EVERY later package and not as many expect until the next major number: \\[foo\\]",
           "Export Package org.osgi.framework version has invalid syntax: x13",
           "Export Package test.lib uses deprecated specification-version instead of version",
           "Export Package org.osgi.service.eventadmin version is a range: \\[1,2\\); Exports do not allow for ranges."));
 }
Esempio n. 27
0
  /**
   * Create a require capality directive test
   *
   * @throws Exception
   */
  public void testValidDirectivesOnRequirement() throws Exception {
    Builder b = new Builder();
    b.addClasspath(IO.getFile("jar/osgi.jar"));
    b.setExportPackage("org.osgi.framework");
    b.setProperty(
        "Require-Capability",
        "test; resolution:=mandatory, "
            + " test; resolution:=optional, "
            + " test; cardinality:=single, "
            + " test; cardinality:=multiple, "
            + " test; effective:=foo, "
            + " test; filter:=\"(&(version>=1.1)(long.list=1)(long.list=2))\", "
            + " test; x-custom:=bar, ");

    b.build();
    assertTrue(b.check());
  }
Esempio n. 28
0
  /**
   * Test if the implementation of "AnnotatedProviderInterface", which is annotated with OSGi
   * R6 @ProviderType, causes import of the api package to use the provider version policy
   */
  public static void testProviderTypeR6() throws Exception {
    Builder b = new Builder();
    b.addClasspath(new File("bin"));
    b.setPrivatePackage("test.versionpolicy.implemented.osgi");
    b.setProperty("build", "123");

    Jar jar = b.build();
    assertTrue(b.check());
    Manifest m = jar.getManifest();
    m.write(System.err);

    Domain d = Domain.domain(m);
    Parameters params = d.getImportPackage();
    Attrs attrs = params.get("test.version.annotations.osgi");
    assertNotNull(attrs);
    assertEquals("[1.2,1.3)", attrs.get("version"));
  }
Esempio n. 29
0
  /**
   * Tests if the implementation of the EventHandler (which is marked as a ConsumerType) causes the
   * import of the api package to use the consumer version policy.
   */
  public static void testConsumerType() throws Exception {
    Builder a = new Builder();
    a.addClasspath(new File("bin"));
    a.setPrivatePackage("test.versionpolicy.uses");
    a.setExportPackage("test.versionpolicy.api");
    a.setProperty("build", "123");
    Jar jar = a.build();
    assertTrue(a.check());
    Manifest m = jar.getManifest();
    m.write(System.err);
    Domain d = Domain.domain(m);

    Parameters parameters = d.getImportPackage();
    Attrs attrs = parameters.get("test.versionpolicy.api");
    assertNotNull(attrs);
    assertEquals("[1.2,2)", attrs.get("version"));
  }
Esempio n. 30
0
  /**
   * Tests if the implementation of the EventAdmin (which is marked as a ProviderType) causes the
   * import of the api package to use the provider version policy.
   */
  public static void testProviderType() throws Exception {
    Builder a = new Builder();
    a.addClasspath(new File("bin"));
    a.setPrivatePackage("test.versionpolicy.implemented");
    a.setExportPackage("test.versionpolicy.api");
    a.setImportPackage("test.versionpolicy.api"); // what changed so this is
    // not automatically
    // added?
    a.setProperty("build", "123");
    Jar jar = a.build();
    assertTrue(a.check());
    Manifest m = jar.getManifest();
    m.write(System.err);
    Domain d = Domain.domain(m);

    Parameters parameters = d.getImportPackage();
    Attrs attrs = parameters.get("test.versionpolicy.api");
    assertNotNull(attrs);
    assertEquals("[1.2,1.3)", attrs.get("version"));
  }