|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Andrey Loskutov <loskutov@gmx.de>. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Andrey Loskutov <loskutov@gmx.de> - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.ui.tests; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | + |
| 22 | +import java.io.File; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.RandomAccessFile; |
| 25 | +import java.net.URL; |
| 26 | +import java.nio.channels.FileLock; |
| 27 | +import java.nio.channels.OverlappingFileLockException; |
| 28 | +import java.nio.file.Files; |
| 29 | +import java.nio.file.Path; |
| 30 | +import java.util.Properties; |
| 31 | + |
| 32 | +import org.eclipse.core.tests.harness.FileSystemHelper; |
| 33 | +import org.eclipse.ui.internal.WorkspaceLock; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | + |
| 36 | +/** |
| 37 | + * Tests for {@link org.eclipse.ui.internal.WorkspaceLock} |
| 38 | + */ |
| 39 | +public class WorkspaceLockTest { |
| 40 | + |
| 41 | + Path tempDir = FileSystemHelper.getRandomLocation().toPath().resolve("WorkspaceLockTest"); |
| 42 | + |
| 43 | + /** |
| 44 | + * Test method for {@link org.eclipse.ui.internal.WorkspaceLock#getWorkspaceLockDetails(java.net.URL)}. |
| 45 | + */ |
| 46 | + @Test |
| 47 | + public void testGetWorkspaceLockDetails() throws Exception { |
| 48 | + URL workspaceUrl = tempDir.toUri().toURL(); |
| 49 | + |
| 50 | + // Test when no lock info file exists |
| 51 | + String details = WorkspaceLock.getWorkspaceLockDetails(workspaceUrl); |
| 52 | + assertNull(details, "Should return null when no lock info file exists"); |
| 53 | + |
| 54 | + // Create .metadata/.lock_info with properties |
| 55 | + Path metadataDir = tempDir.resolve(".metadata"); |
| 56 | + Files.createDirectories(metadataDir); |
| 57 | + Path lockInfoFile = metadataDir.resolve(".lock_info"); |
| 58 | + |
| 59 | + Properties props = new Properties(); |
| 60 | + props.setProperty(WorkspaceLock.USER, "testuser"); |
| 61 | + props.setProperty(WorkspaceLock.HOST, "testhost"); |
| 62 | + props.setProperty(WorkspaceLock.DISPLAY, ":0"); |
| 63 | + props.setProperty(WorkspaceLock.PROCESS_ID, "1234"); |
| 64 | + try (var os = Files.newOutputStream(lockInfoFile)) { |
| 65 | + props.store(os, null); |
| 66 | + } |
| 67 | + |
| 68 | + details = WorkspaceLock.getWorkspaceLockDetails(workspaceUrl); |
| 69 | + assertNotNull(details, "Should return details when lock info file exists"); |
| 70 | + assertTrue(details.contains("testuser"), "Should contain user info"); |
| 71 | + assertTrue(details.contains("testhost"), "Should contain host info"); |
| 72 | + assertTrue(details.contains(":0"), "Should contain display info"); |
| 73 | + assertTrue(details.contains("1234"), "Should contain PID info"); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Test method for {@link org.eclipse.ui.internal.WorkspaceLock#getLockInfoFile(java.net.URL)}. |
| 78 | + */ |
| 79 | + @Test |
| 80 | + public void testGetLockInfoFile() throws Exception { |
| 81 | + URL workspaceUrl = tempDir.toUri().toURL(); |
| 82 | + Path lockInfoFile = WorkspaceLock.getLockInfoFile(workspaceUrl); |
| 83 | + assertNotNull(lockInfoFile, "Should return a path"); |
| 84 | + assertEquals(tempDir.resolve(".metadata").resolve(".lock_info"), lockInfoFile, |
| 85 | + "Should point to .metadata/.lock_info"); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Test method for {@link org.eclipse.ui.internal.WorkspaceLock#getLockFile(java.net.URL)}. |
| 90 | + */ |
| 91 | + @Test |
| 92 | + public void testGetLockFile() throws Exception { |
| 93 | + URL workspaceUrl = tempDir.toUri().toURL(); |
| 94 | + Path lockFile = WorkspaceLock.getLockFile(workspaceUrl); |
| 95 | + assertNotNull(lockFile, "Should return a path"); |
| 96 | + assertEquals(tempDir.resolve(".metadata").resolve(".lock"), lockFile, "Should point to .metadata/.lock"); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Test method for {@link org.eclipse.ui.internal.WorkspaceLock#isWorkspaceLocked(java.net.URL)}. |
| 101 | + */ |
| 102 | + @Test |
| 103 | + public void testIsWorkspaceLocked() throws Exception { |
| 104 | + URL workspaceUrl = tempDir.toUri().toURL(); |
| 105 | + |
| 106 | + // Test when no lock file exists |
| 107 | + assertFalse(WorkspaceLock.isWorkspaceLocked(workspaceUrl), "Should not be locked when no lock file exists"); |
| 108 | + |
| 109 | + // Create .metadata/.lock file & lock it |
| 110 | + Path metadataDir = tempDir.resolve(".metadata"); |
| 111 | + Files.createDirectories(metadataDir); |
| 112 | + Path lockFile = metadataDir.resolve(".lock"); |
| 113 | + try (RandomAccessFile lock = lock(lockFile.toFile())) { |
| 114 | + assertTrue(WorkspaceLock.isWorkspaceLocked(workspaceUrl), "Should be locked"); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Mimics |
| 120 | + * {@linkplain org.eclipse.osgi.internal.location.Locker_JavaNio#lock(File)} |
| 121 | + * locking a file using Java NIO and returning the RandomAccessFile if |
| 122 | + * successful, null if the file is already locked by another process. |
| 123 | + */ |
| 124 | + static RandomAccessFile lock(File lockFile) { |
| 125 | + try { |
| 126 | + RandomAccessFile raFile = new RandomAccessFile(lockFile, "rw"); |
| 127 | + try { |
| 128 | + FileLock lock = raFile.getChannel().tryLock(0, 1, false); |
| 129 | + if (lock == null) { |
| 130 | + raFile.close(); |
| 131 | + return null; |
| 132 | + } |
| 133 | + return raFile; |
| 134 | + } catch (OverlappingFileLockException e) { |
| 135 | + raFile.close(); |
| 136 | + return null; |
| 137 | + } |
| 138 | + } catch (IOException e) { |
| 139 | + // already locked by some process, should not happen in test |
| 140 | + return null; |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments