Skip to content

Commit

Permalink
Merge pull request #1265 from SweeXordious/Fixes_path_containing_spaces
Browse files Browse the repository at this point in the history
Fixes folder path containing space
  • Loading branch information
rach-id authored Sep 22, 2020
2 parents 64c38fa + aae8585 commit 94c13a7
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
Expand Down Expand Up @@ -59,7 +60,12 @@ private Optional<File> compileClass(final String name) {

File sourceFile = null;
for (final URL url : urls) {
final File file = new File(url.getFile(), path + ".java");
File file;
try {
file = new File(URLDecoder.decode(url.getPath(), "UTF-8"), path + ".java");
} catch (Exception e) {
file = new File(url.getFile(), path + ".java");
}

if (file.exists()) {
sourceFile = file;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2020 Web3 Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.web3j.codegen.unit.gen;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Objects;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import org.web3j.codegen.unit.gen.java.Setup;

public class CompilerClassLoaderTest {

String javaSourcePath = "org/com/test/contract/";
String greeterSourceName = "Greeter";
URL javaSourcesUrl = Objects.requireNonNull(Setup.class.getClassLoader().getResource("java"));
@TempDir static File temp;

@Test
public void compileClassTest() throws ClassNotFoundException {
CompilerClassLoader compilerClassLoader = new CompilerClassLoader(temp, javaSourcesUrl);
Assertions.assertNotEquals(
null,
compilerClassLoader.loadClass(
(javaSourcePath + greeterSourceName).replace(File.separator, ".")));
}

@Test
public void compileClassWithPathContainingSpacesTest()
throws ClassNotFoundException, IOException {
File tempWithSpaces = new File(temp, "With Spaces");
File greeterSource =
new File(
Paths.get(
javaSourcesUrl.getPath(),
javaSourcePath,
greeterSourceName + ".java")
.toString());
Assertions.assertTrue(greeterSource.exists());

String javaPathWithSpaces = tempWithSpaces.toString() + File.separator + "java";
File sourcePathWithSpaces = new File(javaPathWithSpaces + File.separator + javaSourcePath);
Assertions.assertTrue(sourcePathWithSpaces.mkdirs());

Files.copy(
greeterSource.toPath(),
Paths.get(
sourcePathWithSpaces.toPath()
+ File.separator
+ greeterSourceName
+ ".java"));
File greeterSourceWithSpaces = new File(sourcePathWithSpaces, greeterSourceName + ".java");
Assertions.assertTrue(greeterSourceWithSpaces.exists());

CompilerClassLoader compilerClassLoader =
new CompilerClassLoader(
tempWithSpaces, new File(javaPathWithSpaces).toURI().toURL());
Assertions.assertNotEquals(
null,
compilerClassLoader.loadClass(
(javaSourcePath + greeterSourceName).replace(File.separator, ".")));
}
}

0 comments on commit 94c13a7

Please sign in to comment.