/** * Loads new jars in the system class loader using the given paths. * * @param jarFilePaths the paths of a jar file. */ void loadJarFiles(String[] jarFilePaths) { for (String jarFilePath : jarFilePaths) { this.loadJarFile(jarFilePath); } } }
/** * Main method for testing. * * @param provider a provider for the transform runner. */ static void main(TransformRunnerProvider provider) { try { provider.provide().runTransform(); } catch (Exception e) { throw new RuntimeException(e); } }
private void runTransform(Transform transform, TransformOutputProvider outputProvider) throws Exception { // Cleaning output directory. FileUtil.deleteDirectory(outputJarsDir); outputJarsDir.mkdirs(); // Preparing Transform invocation. TransformInput input = new TransformInputBuilder().addJarInputFolder(inputJarsDir).build(); TransformInput referencedInput = new TransformInputBuilder().addJarInput(androidClassPath).build(); TransformInvocation invocation = new TransformInvocationBuilder() .addInput(input) .addReferencedInput(referencedInput) .setOutputProvider(outputProvider) .build(); // Running the transform invocation. transform.transform(invocation); } }
@Test public void addJarInput_whenAddPaths_builtShouldReturnCorrectPaths() throws Exception { File file1 = getTmpFile("tmp1"); File file2 = getTmpFile("tmp2"); TransformInput input = builder.addJarInput(file1.getAbsolutePath(), file2.getAbsolutePath()).build(); assertThat(input.getJarInputs()).hasSize(2); JarInput[] jarInputs = input.getJarInputs().toArray(new JarInput[2]); assertThat(jarInputs[0].getFile().getAbsolutePath()).isEqualTo(file1.getAbsolutePath()); assertThat(jarInputs[1].getFile().getAbsolutePath()).isEqualTo(file2.getAbsolutePath()); }
/** * Starts the transform. Here the transform is instantiated through reflection. If the config file * is specified, a constructor with a single parameter accepting a {@link File} is used, otherwise * an empty constructor. * * @throws Exception for any exception happened during the transform process. */ void runTransform() throws Exception { Transform transform = configFile != null ? transformClass.getConstructor(File.class).newInstance(configFile) : transformClass.newInstance(); TransformOutputProvider transformOutputProvider = new JarsTransformOutputProvider(outputJarsDir, inputJarsDir); runTransform(transform, transformOutputProvider); }
@Test public void whenStarting_shouldRunTransform() throws Exception { final TransformRunner runner = mock(TransformRunner.class); CliTransform.main( new CliTransform.TransformRunnerProvider() { @Override public TransformRunner provide() { return runner; } }); verify(runner).runTransform(); } }
@Test public void addDirectoryInput_builtShouldReturnCorrectFile() throws Exception { File file = getTmpFile("tmp"); TransformInput input = builder.addDirectoryInput(file).build(); assertThat(input.getDirectoryInputs()).hasSize(1); assertThat(input.getDirectoryInputs().toArray(new DirectoryInput[1])[0].getFile()) .isEqualTo(file); }
/** * Adds multiple jar inputs for this transform input. * * @param filePaths the paths of the jars. * @return this instance of the builder. */ TransformInputBuilder addJarInput(String... filePaths) { return addJarInput(Arrays.asList(filePaths)); }
@Test(expected = IllegalArgumentException.class) public void whenOutputProviderNotSet_shouldThrowException() throws Exception { builder.build(); } }
@Before public void setup() throws Exception { builder = new TransformInputBuilder(); }
@Test(expected = IllegalArgumentException.class) public void whenStarting_withOneArgument_shouldThrowException() throws Exception { CliTransform.main(new String[] {"any"}); }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); builder = new TransformInvocationBuilder(); }
@Before public void setUp() throws Exception { File baseFolder = Files.newTemporaryFolder(); this.inputJarFile = new File(baseFolder, NAME); this.outputJarFile = new File(baseFolder, EXPECTED_OUTPUT_JAR); this.inputFolder = new File(baseFolder, INPUT); this.outputFolder = new File(baseFolder, OUTPUT); this.provider = new JarsTransformOutputProvider(outputFolder, inputFolder); }
/** * Builds the {@link TransformInvocation}. * * @return a new {@link TransformInvocation} with the specified inputs. */ TransformInvocation build() { if (outputProvider == null) { throw new IllegalArgumentException("Output provider needs to be specified."); } return new CustomTransformInvocation(inputs, referencedInputs, outputProvider); }
@Override public void deleteAll() throws IOException { FileUtil.deleteDirectory(outputFolder); outputFolder.mkdirs(); }
/** * Adds a directory input for this transform input. * * @param file the file of the directory input. * @return this instance of the builder. */ TransformInputBuilder addDirectoryInput(File file) { if (!file.exists()) { throw new IllegalArgumentException( "Specified directory input doesn't exist: " + file.getAbsolutePath()); } this.directoryInputs.add(new FileDirectoryInput(file)); return this; }
/** * Adds a jar input for this transform input. * * @param file the file of the jar input. * @return this instance of the builder. */ TransformInputBuilder addJarInput(File file) { if (file.exists()) { this.jarInputs.add(new FileJarInput(file)); System.out.println("Adding dependency jar: " + file.getAbsolutePath()); } else { System.out.println("Specified jar input doesn't exist: " + file.getAbsolutePath()); } return this; }
@Test public void getContentLocation() throws Exception { File output = provider.getContentLocation(inputJarFile.getAbsolutePath(), null, null, Format.JAR); assertThat(output.getAbsolutePath()).isEqualTo(outputJarFile.getAbsolutePath()); } }
@Test public void addJarInput_whenAddFile_builtShouldReturnFile() throws Exception { File file = getTmpFile("tmp"); TransformInput input = builder.addJarInput(file).build(); assertThat(input.getJarInputs()).hasSize(1); assertThat(input.getJarInputs().toArray(new JarInput[1])[0].getFile()).isEqualTo(file); }
/** * Adds multiple jar inputs for this transform input. * * @param filePaths the paths of the jars. * @return this instance of the builder. */ TransformInputBuilder addJarInput(List<String> filePaths) { for (String filePath : filePaths) { addJarInput(new File(filePath)); } return this; }