All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mockit.internal.startup.PathToAgentJar Maven / Gradle / Ivy

Go to download

JMockit is a Java toolkit for automated developer testing. It contains APIs for the creation of the objects to be tested, for mocking dependencies, and for faking external APIs; JUnit (4 & 5) and TestNG test runners are supported. It also contains an advanced code coverage tool.

There is a newer version: 1.49
Show newest version
/*
 * Copyright (c) 2006 Rogério Liesenfeld
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.internal.startup;

import java.io.*;
import java.util.jar.*;
import java.util.regex.*;
import javax.annotation.*;

import mockit.internal.*;

final class PathToAgentJar
{
   private static final Pattern JAR_REGEX = Pattern.compile(".*jmockit[-.\\d]*.jar");

   @Nonnull
   static String getPathToJarFile()
   {
      String jarFilePath = findPathToJarFileFromClasspath();

      if (jarFilePath == null) {
         jarFilePath = findOrCreateBootstrappingJarFileInTempDir();
      }

      return jarFilePath;
   }

   @Nullable
   private static String findPathToJarFileFromClasspath()
   {
      String[] classPath = System.getProperty("java.class.path").split(File.pathSeparator);

      for (String cpEntry : classPath) {
         if (JAR_REGEX.matcher(cpEntry).matches()) {
            return cpEntry;
         }
      }

      return null;
   }

   @Nonnull
   private static String findOrCreateBootstrappingJarFileInTempDir()
   {
      String tempDir = System.getProperty("java.io.tmpdir");
      String currentVersion = currentVersion();
      File bootstrapJar = new File(tempDir, "jmockitAgent-" + currentVersion + ".jar");

      if (bootstrapJar.exists()) {
         return bootstrapJar.getPath();
      }

      createBootstrapJarInTempDir(bootstrapJar);

      return bootstrapJar.getPath();
   }

   @Nonnull
   private static String currentVersion()
   {
      Class thisClass = PathToAgentJar.class;
      String currentVersion = thisClass.getPackage().getImplementationVersion();

      if (currentVersion == null) { // only happens when the class is loaded from the main/target/classes dir
         currentVersion = "current";
      }

      return currentVersion;
   }

   private static void createBootstrapJarInTempDir(@Nonnull File bootstrapJar)
   {
      Manifest manifest = new Manifest();
      Attributes attrs = manifest.getMainAttributes();
      attrs.putValue("Manifest-Version", "1.0");
      attrs.putValue("Agent-Class", InstrumentationHolder.class.getName());
      attrs.putValue("Can-Redefine-Classes", "true");
      attrs.putValue("Can-Retransform-Classes", "true");

      byte[] classFile = ClassFile.readFromFile(InstrumentationHolder.class).b;

      try {
         JarOutputStream output = new JarOutputStream(new FileOutputStream(bootstrapJar), manifest);
         JarEntry classEntry = new JarEntry(InstrumentationHolder.class.getName().replace('.', '/') + ".class");
         output.putNextEntry(classEntry);
         output.write(classFile);
         output.close();
      }
      catch (IOException e) {
         throw new RuntimeException(e);
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy