Exemplo n.º 1
0
 /** @tests java.util.jar.Attributes#clear() */
 public void test_clear() {
   a.clear();
   assertNull("a) All entries should be null after clear", a.get("1"));
   assertNull("b) All entries should be null after clear", a.get("2"));
   assertNull("c) All entries should be null after clear", a.get("3"));
   assertNull("d) All entries should be null after clear", a.get("4"));
   assertTrue("Should not contain any keys", !a.containsKey("1"));
 }
 public ManifestAppletDefImpl(String descriptorName, ManifestModel manifest, String appletName) {
   super(descriptorName, manifest);
   this.appletName = appletName;
   Attributes mainAttributes = manifest.getMainAttributes();
   String extensionListValue =
       (mainAttributes.containsKey(Name.EXTENSION_LIST)
           ? String.valueOf(mainAttributes.get(Name.EXTENSION_LIST)) + " " + appletName
           : appletName);
   manifest.getMainAttributes().put(Name.EXTENSION_LIST, extensionListValue);
 }
Exemplo n.º 3
0
  /** @tests java.util.jar.Attributes.put(java.lang.Object, java.lang.Object) */
  public void test_putLjava_lang_ObjectLjava_lang_Object_Null() {

    Attributes attribute = new Attributes();

    assertFalse(attribute.containsKey(null));
    assertFalse(attribute.containsValue(null));
    attribute.put(null, null);
    attribute.put(null, null);
    assertEquals(1, attribute.size());
    assertTrue(attribute.containsKey(null));
    assertTrue(attribute.containsValue(null));
    assertNull(attribute.get(null));

    String value = "It's null";
    attribute.put(null, value);
    assertEquals(1, attribute.size());
    assertEquals(value, attribute.get(null));

    Attributes.Name name = new Attributes.Name("null");
    attribute.put(name, null);
    assertEquals(2, attribute.size());
    assertNull(attribute.get(name));
  }
 public void addExportedPackage(
     final IJavaProject pluginProject, final String... exportedPackages) {
   try {
     IProject _project = pluginProject.getProject();
     final IFile manifestFile = _project.getFile("META-INF/MANIFEST.MF");
     final InputStream manifestContent = manifestFile.getContents();
     Manifest _xtrycatchfinallyexpression = null;
     try {
       _xtrycatchfinallyexpression = new Manifest(manifestContent);
     } finally {
       manifestContent.close();
     }
     final Manifest manifest = _xtrycatchfinallyexpression;
     final Attributes attrs = manifest.getMainAttributes();
     boolean _containsKey = attrs.containsKey("Export-Package");
     if (_containsKey) {
       Object _get = attrs.get("Export-Package");
       String _plus = (_get + ",");
       String _join =
           IterableExtensions.join(((Iterable<?>) Conversions.doWrapArray(exportedPackages)), ",");
       String _plus_1 = (_plus + _join);
       attrs.putValue("Export-Package", _plus_1);
     } else {
       String _join_1 =
           IterableExtensions.join(((Iterable<?>) Conversions.doWrapArray(exportedPackages)), ",");
       attrs.putValue("Export-Package", _join_1);
     }
     final ByteArrayOutputStream out = new ByteArrayOutputStream();
     manifest.write(out);
     byte[] _byteArray = out.toByteArray();
     final ByteArrayInputStream in = new ByteArrayInputStream(_byteArray);
     BufferedInputStream _bufferedInputStream = new BufferedInputStream(in);
     manifestFile.setContents(_bufferedInputStream, true, true, null);
   } catch (Throwable _e) {
     throw Exceptions.sneakyThrow(_e);
   }
 }
Exemplo n.º 5
0
 /** @tests java.util.jar.Attributes#containsKey(java.lang.Object) */
 public void test_containsKeyLjava_lang_Object() {
   assertTrue("a) Should have returned false", !a.containsKey(new Integer(1)));
   assertTrue("b) Should have returned false", !a.containsKey("0"));
   assertTrue("Should have returned true", a.containsKey(new Attributes.Name("1")));
 }
Exemplo n.º 6
0
  @Test(timeout = 30000)
  public void testCreateJarWithClassPath() throws Exception {
    // setup test directory for files
    Assert.assertFalse(tmp.exists());
    Assert.assertTrue(tmp.mkdirs());

    // create files expected to match a wildcard
    List<File> wildcardMatches =
        Arrays.asList(
            new File(tmp, "wildcard1.jar"),
            new File(tmp, "wildcard2.jar"),
            new File(tmp, "wildcard3.JAR"),
            new File(tmp, "wildcard4.JAR"));
    for (File wildcardMatch : wildcardMatches) {
      Assert.assertTrue("failure creating file: " + wildcardMatch, wildcardMatch.createNewFile());
    }

    // create non-jar files, which we expect to not be included in the classpath
    Assert.assertTrue(new File(tmp, "text.txt").createNewFile());
    Assert.assertTrue(new File(tmp, "executable.exe").createNewFile());
    Assert.assertTrue(new File(tmp, "README").createNewFile());

    // create classpath jar
    String wildcardPath = tmp.getCanonicalPath() + File.separator + "*";
    String nonExistentSubdir = tmp.getCanonicalPath() + Path.SEPARATOR + "subdir" + Path.SEPARATOR;
    List<String> classPaths =
        Arrays.asList("", "cp1.jar", "cp2.jar", wildcardPath, "cp3.jar", nonExistentSubdir);
    String inputClassPath = StringUtils.join(File.pathSeparator, classPaths);
    String[] jarCp =
        FileUtil.createJarWithClassPath(
            inputClassPath + File.pathSeparator + "unexpandedwildcard/*",
            new Path(tmp.getCanonicalPath()),
            System.getenv());
    String classPathJar = jarCp[0];
    assertNotEquals(
        "Unexpanded wildcard was not placed in extra classpath",
        jarCp[1].indexOf("unexpanded"),
        -1);

    // verify classpath by reading manifest from jar file
    JarFile jarFile = null;
    try {
      jarFile = new JarFile(classPathJar);
      Manifest jarManifest = jarFile.getManifest();
      Assert.assertNotNull(jarManifest);
      Attributes mainAttributes = jarManifest.getMainAttributes();
      Assert.assertNotNull(mainAttributes);
      Assert.assertTrue(mainAttributes.containsKey(Attributes.Name.CLASS_PATH));
      String classPathAttr = mainAttributes.getValue(Attributes.Name.CLASS_PATH);
      Assert.assertNotNull(classPathAttr);
      List<String> expectedClassPaths = new ArrayList<String>();
      for (String classPath : classPaths) {
        if (classPath.length() == 0) {
          continue;
        }
        if (wildcardPath.equals(classPath)) {
          // add wildcard matches
          for (File wildcardMatch : wildcardMatches) {
            expectedClassPaths.add(wildcardMatch.toURI().toURL().toExternalForm());
          }
        } else {
          File fileCp = null;
          if (!new Path(classPath).isAbsolute()) {
            fileCp = new File(tmp, classPath);
          } else {
            fileCp = new File(classPath);
          }
          if (nonExistentSubdir.equals(classPath)) {
            // expect to maintain trailing path separator if present in input, even
            // if directory doesn't exist yet
            expectedClassPaths.add(fileCp.toURI().toURL().toExternalForm() + Path.SEPARATOR);
          } else {
            expectedClassPaths.add(fileCp.toURI().toURL().toExternalForm());
          }
        }
      }
      List<String> actualClassPaths = Arrays.asList(classPathAttr.split(" "));
      Collections.sort(expectedClassPaths);
      Collections.sort(actualClassPaths);
      Assert.assertEquals(expectedClassPaths, actualClassPaths);
    } finally {
      if (jarFile != null) {
        try {
          jarFile.close();
        } catch (IOException e) {
          LOG.warn("exception closing jarFile: " + classPathJar, e);
        }
      }
    }
  }
Exemplo n.º 7
0
  // initialize build time properties from data in the jar's META-INF/MANIFEST.MF
  private static synchronized void getBuildTimeProperties() {
    if (isInitialized) {
      return;
    }

    /*
    our classloader's classpath may contain more than one .jar, each with a manifest.
    we need to ensure we get our own .jar's manifest, even if the jar is not first on the path.
    we also need to deal with vfs, which does weird things to pathToThisClass e.g.
      normal: jar:file:/foo/bar.jar!/com/arjuna/common/util/ConfigurationInfo.class
      vfszip:/foo/bar.jar/com/arjuna/common/util/ConfigurationInfo.class
    In short, this path finding code is magic and should be approached very cautiously.
     */
    String classFileName = ConfigurationInfo.class.getSimpleName() + ".class";
    String pathToThisClass = ConfigurationInfo.class.getResource(classFileName).toString();

    // we need to strip off the class name bit so we can replace it with the manifest name:
    int suffixLength = (ConfigurationInfo.class.getCanonicalName() + ".class").length();
    // now derive the path to the .jar which contains the class and thus hopefully the right
    // manifest:
    String basePath = pathToThisClass.substring(0, pathToThisClass.length() - suffixLength);
    if (basePath.endsWith("/")) basePath = basePath.substring(0, basePath.length() - 1);

    String pathToManifest = basePath + "/META-INF/MANIFEST.MF";

    InputStream is = null;
    try {
      is = new URL(pathToManifest).openStream();
      Manifest manifest = new Manifest(is);
      Attributes attributes = manifest.getMainAttributes();

      Attributes.Name name = new Attributes.Name("arjuna-properties-file");
      if (attributes.containsKey(name)) {
        propertiesFile = attributes.getValue(name);
      }

      name = new Attributes.Name("arjuna-scm-revision");
      if (attributes.containsKey(name)) {
        sourceId = attributes.getValue(name);
      }

      name = new Attributes.Name("arjuna-builder");
      if (attributes.containsKey(name)) {
        buildId = attributes.getValue(name);
      }

    } catch (FileNotFoundException exception) {
      commonLogger.i18NLogger.warn_could_not_find_manifest(pathToManifest, exception);
    } catch (Exception exception) {
      commonLogger.i18NLogger.warn_could_not_find_manifest(pathToManifest, exception);
    } finally {
      if (is != null) {
        try {
          is.close();
        } catch (IOException e) {
        }
      }
    }

    isInitialized = true;
  }