Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.devonfw.tools.ide.common;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
Expand Down Expand Up @@ -78,7 +77,7 @@ public SystemPath(IdeContext context, String envPath) {
*/
public SystemPath(IdeContext context, String envPath, Path ideRoot, Path softwarePath) {

this(context, envPath, ideRoot, softwarePath, File.pathSeparatorChar, Collections.emptyList());
this(context, envPath, ideRoot, softwarePath, System.getProperty("path.separator").charAt(0), Collections.emptyList());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package com.devonfw.tools.ide.locking;

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.channels.FileLock;
import java.nio.file.Files;
import java.nio.file.Path;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Program to check if eclipse workspace is locked. A successful exit (code {@code 0}) indicates that the workspace is free. Otherwise the exit code {@code 1}
* is used to indicate that the workspace is locked. In case of an error (e.g. invalid CLI arguments) the exit code {@code -1} is used.
*/
public class EclipseWorkspaceLockChecker {

private static final Logger LOG = LoggerFactory.getLogger(EclipseWorkspaceLockChecker.class);

/**
* @param args the command-line arguments. There is currently only one argument defined which is the path to the lock file.
*/
Expand All @@ -23,10 +29,10 @@ public static void main(String[] args) {
private int run(String[] args) {

if (args.length != 1) {
System.out.println("Usage: " + EclipseWorkspaceLockChecker.class.getSimpleName() + " <path2workspace>");
LOG.error("Usage: {} <path2workspace>", EclipseWorkspaceLockChecker.class.getSimpleName());
return -1;
}
File lockfile = new File(args[0]);
Path lockfile = Path.of(args[0]);
boolean locked = isLocked(lockfile);
if (locked) {
return 1;
Expand All @@ -35,13 +41,13 @@ private int run(String[] args) {
}

/**
* @param lockfile the {@link File} pointing to the lockfile to check.
* @return {@code true} if the given {@link File} is locked, {@code false} otherwise.
* @param lockfile the {@link Path} pointing to the lockfile to check.
* @return {@code true} if the given {@link Path} is locked, {@code false} otherwise.
*/
public static boolean isLocked(File lockfile) {
public static boolean isLocked(Path lockfile) {

if (lockfile.isFile()) {
try (RandomAccessFile raFile = new RandomAccessFile(lockfile, "rw")) {
if (Files.isRegularFile(lockfile)) {
try (RandomAccessFile raFile = new RandomAccessFile(lockfile.toFile(), "rw")) {
FileLock fileLock = raFile.getChannel().tryLock(0, 1, false);
// success, file was not locked so we immediately unlock again...
fileLock.release();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.devonfw.tools.ide.tool.eclipse;

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.channels.FileLock;
import java.nio.file.Files;
Expand Down Expand Up @@ -120,8 +119,8 @@ protected void configureWorkspace() {
}

/**
* @param lockfile the {@link File} pointing to the lockfile to check.
* @return {@code true} if the given {@link File} is locked, {@code false} otherwise.
* @param lockfile the {@link Path} pointing to the lockfile to check.
* @return {@code true} if the given {@link Path} is locked, {@code false} otherwise.
*/
private static boolean isLocked(Path lockfile) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
Expand Down Expand Up @@ -140,7 +140,7 @@ private void mockMvnMetadataResponses(WireMockRuntimeInfo wireMockRuntimeInfo) {
// <root>/org/springframework/boot/maven-metadata.xml -> org/springframework/boot
Path rel = mvnRoot.relativize(xmlFile);
String packagePath = "/" + rel.toString()
.replace(File.separatorChar, '/');
.replace(FileSystems.getDefault().getSeparator(), "/");

String body = IdeTestContext.readAndResolveBaseUrl(xmlFile, wireMockRuntimeInfo);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;
Expand Down Expand Up @@ -61,7 +61,7 @@ private void mockNpmPackageResponses(WireMockRuntimeInfo wireMockRuntimeInfo) {
// <root>/yarn.json -> "/yarn"
Path rel = npmRoot.relativize(jsonFile);
String packagePath = "/" + rel.toString()
.replace(File.separatorChar, '/')
.replace(FileSystems.getDefault().getSeparator(), "/")
.replaceAll("\\.json$", "");

String body = IdeTestContext.readAndResolveBaseUrl(jsonFile, wireMockRuntimeInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;
Expand Down Expand Up @@ -63,7 +63,7 @@ private void mockPypiPackageResponses(WireMockRuntimeInfo wireMockRuntimeInfo) {
// <root>/pip.json -> "/pip/json"
Path rel = pypiRoot.relativize(jsonFile);
String packageName = rel.toString()
.replace(File.separatorChar, '/')
.replace(FileSystems.getDefault().getSeparator(), "/")
.replaceAll("\\.json$", "");
String packagePath = "/" + packageName + "/json";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.devonfw.tools.ide.locking;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileLock;
import java.nio.file.Files;
import java.nio.file.Path;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -14,39 +15,38 @@
class EclipseWorkspaceLockCheckerTest extends Assertions {

/**
* Test of {@link EclipseWorkspaceLockChecker#isLocked(File)} for an unlocked file.
* Test of {@link EclipseWorkspaceLockChecker#isLocked(Path)} for an unlocked file.
*/
@Test
void testIsLockedUnlockedFile() throws IOException {
File tempFile = File.createTempFile("eclipse-lock-test", ".lock");
Path tempFile = Files.createTempFile("eclipse-lock-test", ".lock");
try {
assertThat(EclipseWorkspaceLockChecker.isLocked(tempFile)).isFalse();
} finally {
tempFile.delete();
Files.deleteIfExists(tempFile);
}
}

/**
* Test of {@link EclipseWorkspaceLockChecker#isLocked(File)} for a locked file.
* Test of {@link EclipseWorkspaceLockChecker#isLocked(Path)} for a locked file.
*/
@Test
void testIsLockedLockedFile() throws IOException {
File tempFile = File.createTempFile("eclipse-lock-test", ".lock");
try (RandomAccessFile raFile = new RandomAccessFile(tempFile, "rw");
Path tempFile = Files.createTempFile("eclipse-lock-test", ".lock");
try (RandomAccessFile raFile = new RandomAccessFile(tempFile.toFile(), "rw");
FileLock lock = raFile.getChannel().lock()) {
assertThat(EclipseWorkspaceLockChecker.isLocked(tempFile)).isTrue();
} finally {
tempFile.delete();
Files.deleteIfExists(tempFile);
}
}

/**
* Test of {@link EclipseWorkspaceLockChecker#isLocked(File)} for a non-existent file.
* Test of {@link EclipseWorkspaceLockChecker#isLocked(Path)} for a non-existent file.
*/
@Test
void testIsLockedNonExistentFile() {
File file = new File("non-existent-file.lock");
Path file = Path.of("non-existent-file.lock");
assertThat(EclipseWorkspaceLockChecker.isLocked(file)).isFalse();
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.devonfw.ide.gui.context;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NotDirectoryException;
Expand Down Expand Up @@ -73,13 +72,10 @@ void testConstructorWithNonExistentDirectory() {
void testConstructorWithFile() throws IOException {

try {
File testFile = ideRoot.resolve("testFile").toFile();
boolean success = testFile.createNewFile();
if (!success) {
throw new RuntimeException("Unable to create test file");
}
Path testFile = ideRoot.resolve("testFile");
Files.createFile(testFile);

projectManager = new ProjectManager(ideRoot.resolve("testFile"));
projectManager = new ProjectManager(testFile);
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage()).contains("Root directory is not a directory");
Expand All @@ -97,10 +93,13 @@ void testRefreshProjects() throws IOException {
Path project0 = ideRoot.resolve("project-0");
Path project6 = ideRoot.resolve("project-6");
FileUtils.copyDirectory(project0.toFile(), project6.toFile());
File adjustedWorkspaceName = project6.resolve("workspaces").resolve("foo-test-0").toFile();
Path oldWorkspace = project6.resolve("workspaces").resolve("foo-test-0");
Path newWorkspace = project6.resolve("workspaces").resolve("foo-test-6");

//as we copied from project-0, the test workspace folder name is still old
assertThat(adjustedWorkspaceName.renameTo(project6.resolve("workspaces").resolve("foo-test-6").toFile())).isTrue();
Files.move(oldWorkspace, newWorkspace);
assertThat(newWorkspace).exists();
assertThat(oldWorkspace).doesNotExist();

// Verify that project-6 is now recognized
List<String> extendedList = new ArrayList<>(VALID_PROJECT_LIST);
Expand Down
Loading