Skip to content
Snippets Groups Projects
Commit ae6eb333 authored by jez04's avatar jez04
Browse files

feat: add copy test script

parent 3c8764fe
No related merge requests found
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class CopyTests {
private static Logger log = java.util.logging.Logger.getLogger(CopyTests.class.getName());
public static final List<String> skipNames = List.of("target", ".settings", ".project", ".classpath");
public static final String TAR_GZ_PATTERN = ".*\\.tar\\.gz";
public static void main(String[] args) {
try {
if (args.length == 0) {
printUsage();
}
String destProjectDir = null;
String sourceDir = null;
for (String arg : args) {
if (destProjectDir == null) {
destProjectDir = arg;
} else if (sourceDir == null) {
sourceDir = arg;
} else {
log.log(Level.WARNING, "Uknown argument '{}'.", arg);
}
}
if (destProjectDir == null) {
destProjectDir = ".";
}
if (sourceDir == null) {
sourceDir = "template/java-tests/";
}
copyTests(Paths.get(sourceDir), Paths.get(destProjectDir));
} catch (Exception e) {
e.printStackTrace();
}
}
private static void printUsage() {
log.info("Usage:\n java [--source 21 --enable-preview] CopyTest.java [<destProjectDir> [<sourceDir>]]");
}
private static void copyTests(Path sourceDir, Path descProjectDir) {
Path javaTestDir = descProjectDir.resolve("src").resolve("test").resolve("java");
List<File> javaTestFiles = Arrays.asList(sourceDir.toFile().listFiles()).stream()
.filter(f -> f.getName().matches(".*\\.java")).toList();
for (File file : javaTestFiles) {
try (Stream<String> lines = Files.lines(file.toPath())) {
String packagePath = lines.filter(line -> !line.isBlank())
.filter(line -> line.matches("\\s*package .*")).findFirst()
.map(line -> line.replaceAll("\\s*package ", "").replace(";", ""))
.map(packageStr -> packageStr.replace('.', '/')).orElse("");
Path target = javaTestDir.resolve(packagePath);
target.toFile().mkdirs();
Files.copy(file.toPath(), target.resolve(file.getName()), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
log.log(Level.SEVERE, "Cannot copy test file.", e);
}
}
}
private static void download(String urlString, Path destPath, boolean unzip, int cutoffParrent) {
try (InputStream inputStream = new URL(urlString).openStream()) {
if (unzip) {
destPath.toFile().mkdirs();
extractZipStream(inputStream, destPath, cutoffParrent);
} else {
Path parent = destPath.getParent();
if (parent != null) {
parent.toFile().mkdirs();
}
Files.copy(inputStream, destPath, StandardCopyOption.REPLACE_EXISTING);
}
} catch (MalformedURLException ex) {
log.log(Level.SEVERE, "Cannot connect to git", ex);
} catch (IOException ex) {
log.log(Level.SEVERE, "Cannot douwnload and extract multi project evaluator", ex);
}
}
private static final int BUFFER_SIZE = 4096;
/**
* Extracts a zip file specified by the zipFilePath to a directory specified by
* destDirectory (will be created if does not exists)
*
* @param zipFilePath
* @throws IOException
*/
public static void unzip(File zipFilePath, Path destDir) throws IOException {
if (!destDir.toFile().exists()) {
destDir.toFile().mkdir();
}
try (InputStream in = new FileInputStream(zipFilePath)) {
extractZipStream(in, destDir);
}
}
private static void extractZipStream(InputStream zipInputStream, Path destDir) throws IOException {
extractZipStream(zipInputStream, destDir, 0);
}
private static void extractZipStream(InputStream zipInputStream, Path destDir, int cutoffParent)
throws FileNotFoundException, IOException {
ZipInputStream zipIn = new ZipInputStream(zipInputStream);
ZipEntry entry = zipIn.getNextEntry();
// iterates over entries in the zip file
while (entry != null) {
Path entryPath = Paths.get(entry.getName());
if (entry.isDirectory() && entryPath.getNameCount() <= cutoffParent) {
entryPath = Paths.get("");
} else if (!entry.isDirectory() && entryPath.getNameCount() <= cutoffParent) {
entryPath = entryPath.getFileName();
} else {
entryPath = entryPath.subpath(cutoffParent, entryPath.getNameCount());
}
Path filePath = destDir.resolve(entryPath);
if (!entry.isDirectory()) {
// if the entry is a file, extracts it
extractFile(zipIn, filePath);
} else {
// if the entry is a directory, make the directory
File dir = filePath.toFile();
dir.mkdirs();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
}
/**
* Extracts a zip entry (file entry)
*
* @param zipIn
* @param filePath
* @throws IOException
*/
private static void extractFile(ZipInputStream zipIn, Path filePath) throws IOException {
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath.toFile()))) {
byte[] bytesIn = new byte[BUFFER_SIZE];
int read = 0;
while ((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
}
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment