From 4d3017d0a535024f56dafcce1f3b52edaeee5d3d Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Tue, 11 Jun 2024 14:39:54 +0200 Subject: [PATCH 01/35] Proposal: Opening/Closing Mechanism for Zip Files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Eclipse IDE has no built in functionality to open Zip Files and read or manipulate their content. Because of this, other operations like searching inside of Zip Files or comparing two Zip Files were also not possible. This pull request introduces a mechanism for handling Zip Files within the Eclipse workspace, enhancing the functionality to read and write Zip files. The primary goal is to provide a seamless experience for developers working with zip archives directly within Eclipse. Zip files must be opened manually within the workspace by using the new command "Open Zip File" in the menu when right clicking the zip file. It is also possible to open nested zip files. Zip Files are opened by replacing the file in the workspace with a linked folder that reads and writes the Zip File in the file system. By closing the Zip FIle, the linked folder will be deleted and the file can be seen in the workspace again. Please note that only ZIP Archives are supported in this current implementation. Other archive types can be added in future improvements. Also linked Zip Files can not be opened with this implementation because the Zip File must be local. An additional PR for the repository **eclipse.platform.ui** that grants access to the open/close mechanism for zip files over UI can be found in the following: https://github.com/eclipse-platform/eclipse.platform.ui/pull/1947 Co-Authored-By: David Erdös fix rebase --- .../org.eclipse.core.filesystem/plugin.xml | 7 + .../eclipse/core/filesystem/ZipFileUtil.java | 108 ++++ .../internal/filesystem/zip/ZipFileStore.java | 557 ++++++++++++++++++ .../filesystem/zip/ZipFileSystem.java | 21 + .../org.eclipse.core.resources/plugin.xml | 6 + .../propertytester/ZipFilePropertyTester.java | 73 +++ .../core/internal/resources/ResourceTree.java | 83 ++- .../org/eclipse/core/resources/IResource.java | 9 + .../core/resources/ZipFileTransformer.java | 113 ++++ .../plugin.xml | 77 +-- .../internal/filesystem/zip/ZipFileStore.java | 245 -------- .../zip/ZipFileSystemContributor.java | 82 --- .../filesystem/CollapseZipHandler.java | 79 --- .../examples/filesystem/ExpandZipHandler.java | 66 --- .../resources/ZipFileSystem/BasicText.zip | Bin 0 -> 126 bytes .../resources/ZipFileSystem/BasicTextNew.zip | Bin 0 -> 126 bytes .../resources/ZipFileSystem/DeepNested.zip | Bin 0 -> 1268 bytes .../resources/ZipFileSystem/Empty.zip | Bin 0 -> 22 bytes .../resources/ZipFileSystem/Fake.zip | 1 + .../ZipFileSystem/NestedZipFileParent.zip | Bin 0 -> 1478 bytes .../ZipFileSystem/PasswordProtected.zip | Bin 0 -> 173 bytes .../filesystem/zip/AllZipFileSystemTests.java | 26 + .../core/tests/filesystem/zip/CloseTest.java | 52 ++ .../core/tests/filesystem/zip/CopyTest.java | 211 +++++++ .../core/tests/filesystem/zip/CreateTest.java | 64 ++ .../core/tests/filesystem/zip/DeleteTest.java | 92 +++ .../core/tests/filesystem/zip/MoveTest.java | 298 ++++++++++ .../core/tests/filesystem/zip/OpenTest.java | 162 +++++ .../core/tests/filesystem/zip/RenameTest.java | 73 +++ .../core/tests/filesystem/zip/SetupTest.java | 55 ++ .../zip/ZipFileSystemTestSetup.java | 159 +++++ .../filesystem/zip/ZipFileSystemTestUtil.java | 142 +++++ .../resources/AutomatedResourceTests.java | 1 + 33 files changed, 2293 insertions(+), 569 deletions(-) create mode 100644 resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/filesystem/ZipFileUtil.java create mode 100644 resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java rename resources/{examples/org.eclipse.ui.examples.filesystem => bundles/org.eclipse.core.filesystem}/src/org/eclipse/core/internal/filesystem/zip/ZipFileSystem.java (79%) create mode 100644 resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/propertytester/ZipFilePropertyTester.java create mode 100644 resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java delete mode 100644 resources/examples/org.eclipse.ui.examples.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java delete mode 100644 resources/examples/org.eclipse.ui.examples.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileSystemContributor.java delete mode 100644 resources/examples/org.eclipse.ui.examples.filesystem/src/org/eclipse/ui/examples/filesystem/CollapseZipHandler.java delete mode 100644 resources/examples/org.eclipse.ui.examples.filesystem/src/org/eclipse/ui/examples/filesystem/ExpandZipHandler.java create mode 100644 resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicText.zip create mode 100644 resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicTextNew.zip create mode 100644 resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/DeepNested.zip create mode 100644 resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/Empty.zip create mode 100644 resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/Fake.zip create mode 100644 resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/NestedZipFileParent.zip create mode 100644 resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/PasswordProtected.zip create mode 100644 resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/AllZipFileSystemTests.java create mode 100644 resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CloseTest.java create mode 100644 resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CopyTest.java create mode 100644 resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CreateTest.java create mode 100644 resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/DeleteTest.java create mode 100644 resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/MoveTest.java create mode 100644 resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/OpenTest.java create mode 100644 resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/RenameTest.java create mode 100644 resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/SetupTest.java create mode 100644 resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestSetup.java create mode 100644 resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestUtil.java diff --git a/resources/bundles/org.eclipse.core.filesystem/plugin.xml b/resources/bundles/org.eclipse.core.filesystem/plugin.xml index a91ee65ed6d..de665c388f7 100644 --- a/resources/bundles/org.eclipse.core.filesystem/plugin.xml +++ b/resources/bundles/org.eclipse.core.filesystem/plugin.xml @@ -16,4 +16,11 @@ + + + + + diff --git a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/filesystem/ZipFileUtil.java b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/filesystem/ZipFileUtil.java new file mode 100644 index 00000000000..4baad731c4c --- /dev/null +++ b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/filesystem/ZipFileUtil.java @@ -0,0 +1,108 @@ +/******************************************************************************* + * Copyright (c) 2024 Vector Informatik GmbH and others. + * + * This program and the accompanying materials are made available under the terms of the Eclipse + * Public License 2.0 which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: Vector Informatik GmbH - initial API and implementation + *******************************************************************************/ + +package org.eclipse.core.filesystem; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.Set; +import org.eclipse.core.internal.filesystem.zip.ZipFileStore; +import org.eclipse.core.runtime.CoreException; + +/** + * Utility class to determine if a file is an archive based on file header information. + * This class checks for known file signatures to identify if a given file is a ZIP archive + * or a format based on ZIP, such as EPUB, JAR, ODF, and OOXML. + * + * @since 1.11 + */ +public class ZipFileUtil { + + // Initializes known archive file signatures from Wikipedia's list of file signatures in the following order: + // 1. Standard ZIP file, 2. Empty archive, 3. Spanned archive + // (https://en.wikipedia.org/wiki/List_of_file_signatures) + private static final Set ARCHIVE_FILE_SIGNATURES = Set.of(0x504B0304, 0x504B0506, 0x504B0708); + + /** + * Determines if the given {@link IFileStore} represents an open ZIP file. + * This can be used to check if operations on a ZIP file should be allowed or handled differently. + * + * @param store The file store to check. + * @return true if the store is an instance of {@link ZipFileStore}, false otherwise. + */ + public static boolean isInsideOpenZipFile(IFileStore store) { + return store instanceof ZipFileStore; + } + + public static boolean isInsideOpenZipFile(URI locationURI) { + IFileStore store; + try { + store = EFS.getStore(locationURI); + } catch (CoreException e) { + return false; + } + return isInsideOpenZipFile(store); + } + + //TODO Implement this method + public static boolean isOpenZipFile(IFileStore store) { + if (isInsideOpenZipFile(store)) { + ZipFileStore zipStore = (ZipFileStore) store; + return zipStore.getPath().isEmpty(); //if path is empty its the root + } + return false; + } + + public static boolean isOpenZipFile(URI locationURI) { + IFileStore store; + try { + store = EFS.getStore(locationURI); + } catch (CoreException e) { + return false; + } + return isOpenZipFile(store); + } + + public static boolean isNested(URI fileURI) { + if (fileURI.getScheme().contains("zip")) { //$NON-NLS-1$ + return true; + } + return false; + } + + /** + * Checks if the provided {@link InputStream} represents a ZIP archive + * by reading its first four bytes and comparing them against known ZIP file signatures. + * This method throws {@link IOException} if the file signature does not match any known ZIP archive signatures. + * + * @param fis The {@link InputStream} of the file to check. + * @throws IOException If the file signature does not match known ZIP archive signatures + * or an I/O error occurs during reading from the stream. + */ + public static void checkFileForZipHeader(InputStream fis) throws IOException { + byte[] bytes = new byte[4]; + if (fis.read(bytes) == bytes.length) { + ByteBuffer buffer = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN); + int header = buffer.getInt(); + + if (!ARCHIVE_FILE_SIGNATURES.contains(header)) { + throw new IOException("Invalid archive file signature."); // Throws IOException if header is not recognized //$NON-NLS-1$ + } + } else { + // Handle the case where not enough bytes are read + throw new IOException("Could not read enough data to check ZIP file header."); //$NON-NLS-1$ + } + } +} diff --git a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java new file mode 100644 index 00000000000..9fe5829f781 --- /dev/null +++ b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java @@ -0,0 +1,557 @@ +/******************************************************************************* + * Copyright (c) 2022 IBM Corporation and others. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which accompanies this distribution, + * and is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.core.internal.filesystem.zip; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.file.FileSystem; +import java.nio.file.FileSystemAlreadyExistsException; +import java.nio.file.FileSystems; +import java.nio.file.FileVisitOption; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.StandardCopyOption; +import java.nio.file.StandardOpenOption; +import java.nio.file.attribute.BasicFileAttributes; +import java.nio.file.attribute.FileTime; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.EnumSet; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.filesystem.IFileInfo; +import org.eclipse.core.filesystem.IFileStore; +import org.eclipse.core.filesystem.provider.FileInfo; +import org.eclipse.core.filesystem.provider.FileStore; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.osgi.framework.FrameworkUtil; + +/** + * File store implementation representing a file or directory inside a zip file. + * @since 1.11 + */ +public class ZipFileStore extends FileStore { + /** + * The path of this store within the zip file. + */ + private final IPath path; + + /** + * The file store that represents the actual zip file. + */ + private final IFileStore rootStore; + + /** + * Creates a new zip file store. + */ + public ZipFileStore(IFileStore rootStore, IPath path) { + this.rootStore = rootStore; + this.path = path.makeRelative(); + } + + private ZipEntry[] childEntries(IProgressMonitor monitor) throws CoreException { + List entryList = new ArrayList<>(); + String myName = path.toString(); + + try (FileSystem zipFs = openZipFileSystem()) { + Path zipRoot = zipFs.getPath(myName); + Files.walkFileTree(zipRoot, EnumSet.noneOf(FileVisitOption.class), 1, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { + String entryName = zipRoot.relativize(file).toString(); + if (!Files.isDirectory(file)) { + // For files, read attributes and create ZipEntry + ZipEntry zipEntry = new ZipEntry(entryName); + zipEntry.setSize(attrs.size()); + zipEntry.setTime(attrs.lastModifiedTime().toMillis()); + // Compressed size is not directly available; method is set based on ZIP standard + zipEntry.setMethod(ZipEntry.DEFLATED); + entryList.add(zipEntry); + } else { + // For directories, simply add them with a trailing slash + entryList.add(new ZipEntry(entryName + "/")); //$NON-NLS-1$ + } + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { + // Include directories only if they are not the root directory + if (!dir.equals(zipRoot)) { + String dirName = zipRoot.relativize(dir).toString() + "/"; //$NON-NLS-1$ + entryList.add(new ZipEntry(dirName)); + } + return FileVisitResult.CONTINUE; + } + }); + } catch (IOException | URISyntaxException e) { + throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Error reading ZIP file", e)); //$NON-NLS-1$ + } + + return entryList.toArray(new ZipEntry[0]); + } + + @Override + public IFileInfo[] childInfos(int options, IProgressMonitor monitor) throws CoreException { + ZipEntry[] entries = childEntries(monitor); + int entryCount = entries.length; + IFileInfo[] infos = new IFileInfo[entryCount]; + for (int i = 0; i < entryCount; i++) { + infos[i] = convertZipEntryToFileInfo(entries[i]); + } + return infos; + } + + @Override + public String[] childNames(int options, IProgressMonitor monitor) throws CoreException { + ZipEntry[] entries = childEntries(monitor); + int entryCount = entries.length; + String[] names = new String[entryCount]; + for (int i = 0; i < entryCount; i++) { + names[i] = computeName(entries[i]); + } + return names; + } + + /** + * Computes the simple file name for a given zip entry. + */ + private String computeName(ZipEntry entry) { + String name = entry.getName(); + // removes "/" at the end + if (name.endsWith("/")) { //$NON-NLS-1$ + name = name.substring(0, name.length() - 1); + } + // creates last segment after last / + int lastIndex = name.lastIndexOf('/'); + + if (lastIndex != -1) { + return name.substring(lastIndex + 1); + } + //No '/' found + return name; + } + + private IFileInfo convertToIFileInfo(Path zipEntryPath, BasicFileAttributes attrs) { + Path namePath = zipEntryPath.getFileName(); + String name = namePath != null ? namePath.toString() : ""; //$NON-NLS-1$ + FileInfo info = new FileInfo(name); + info.setExists(true); + info.setDirectory(attrs.isDirectory()); + info.setLastModified(attrs.lastModifiedTime().toMillis()); + info.setLength(attrs.size()); + return info; + } + + /** + * Creates a file info object corresponding to a given zip entry + * + * @param entry the zip entry + * @return The file info for a zip entry + */ + private IFileInfo convertZipEntryToFileInfo(ZipEntry entry) { + FileInfo info = new FileInfo(computeName(entry)); + if (entry.isDirectory()) { + info.setLastModified(EFS.NONE); + } else { + info.setLastModified(entry.getTime()); + } + + info.setExists(true); + info.setDirectory(entry.isDirectory()); + info.setLength(entry.getSize()); + return info; + } + + @Override + protected void copyDirectory(IFileInfo sourceInfo, IFileStore destination, int options, IProgressMonitor monitor) throws CoreException { + if (!(destination instanceof ZipFileStore)) { + super.copyDirectory(sourceInfo, destination, options, monitor); + return; + } + + if (!sourceInfo.isDirectory()) { + throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Source is not a directory")); //$NON-NLS-1$ + } + + try (FileSystem zipFs = openZipFileSystem()) { + Path sourceDir = zipFs.getPath(this.path.toString()); + FileSystem destFs = ((ZipFileStore) destination).openZipFileSystem(); + Path destDir = destFs.getPath(((ZipFileStore) destination).path.toString()); + + // Use Files.walk to iterate over each entry in the directory + Files.walk(sourceDir).forEach(sourcePath -> { + try { + Path destPath = destDir.resolve(sourceDir.relativize(sourcePath)); + if (Files.isDirectory(sourcePath)) { + Files.createDirectories(destPath); + } else { + Files.copy(sourcePath, destPath, StandardCopyOption.REPLACE_EXISTING); + } + } catch (IOException e) { + throw new RuntimeException("Error copying directory contents", e); //$NON-NLS-1$ + } + }); + } catch (IOException | URISyntaxException e) { + throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Error copying directory within ZIP", e)); //$NON-NLS-1$ + } + } + + @Override + protected void copyFile(IFileInfo sourceInfo, IFileStore destination, int options, IProgressMonitor monitor) throws CoreException { + if (!(destination instanceof ZipFileStore)) { + super.copyFile(sourceInfo, destination, options, monitor); + return; + } + + if (sourceInfo.isDirectory()) { + throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Source is a directory, not a file")); //$NON-NLS-1$ + } + + try (FileSystem zipFs = openZipFileSystem()) { + Path sourcePath = zipFs.getPath(this.path.toString()); + FileSystem destFs = ((ZipFileStore) destination).openZipFileSystem(); + Path destPath = destFs.getPath(((ZipFileStore) destination).path.toString()); + + // Copy the file with REPLACE_EXISTING option to overwrite if it already exists + Files.copy(sourcePath, destPath, StandardCopyOption.REPLACE_EXISTING); + } catch (IOException | URISyntaxException e) { + throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Error copying file within ZIP", e)); //$NON-NLS-1$ + } + } + + @Override + public void delete(int options, IProgressMonitor monitor) throws CoreException { + Path toDelete = null; + try (FileSystem zipFs = openZipFileSystem()) { + toDelete = zipFs.getPath(path.toString()); + if (Files.exists(toDelete)) { + deleteRecursive(toDelete); + } + } catch (IOException | URISyntaxException e) { + throw new CoreException(new Status(IStatus.ERROR, "org.eclipse.core.filesystem.zip", "Error deleting file from zip: " + toDelete, e)); //$NON-NLS-1$ //$NON-NLS-2$ + } + } + + private void deleteRecursive(Path pathToDelete) throws IOException { + if (Files.isDirectory(pathToDelete)) { + // Use try-with-resources to close the directory stream automatically + try (Stream entries = Files.walk(pathToDelete)) { + // We need to sort it in reverse order so directories come after their contents + List sortedPaths = entries.sorted(Comparator.reverseOrder()).collect(Collectors.toList()); + for (Path entry : sortedPaths) { + Files.delete(entry); + } + } + } else { + Files.delete(pathToDelete); + } + } + + @Override + public IFileInfo fetchInfo(int options, IProgressMonitor monitor) throws CoreException { + try (FileSystem zipFs = openZipFileSystem()) { + Path zipEntryPath = zipFs.getPath(path.toString()); + if (Files.exists(zipEntryPath)) { + BasicFileAttributes attrs = Files.readAttributes(zipEntryPath, BasicFileAttributes.class); + return convertToIFileInfo(zipEntryPath, attrs); + } + } catch (IOException | URISyntaxException e) { + throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Error accessing ZIP file", e)); //$NON-NLS-1$ + } + + // Correctly set up FileInfo before returning + FileInfo notFoundInfo = new FileInfo(path.lastSegment()); + notFoundInfo.setExists(false); + return notFoundInfo; + } + + /** + * Finds the zip entry with the given name in this zip file. Returns the + * entry and leaves the input stream open positioned at the beginning of the + * bytes of that entry. Returns null if the entry could not be found. + */ + private ZipEntry findEntry(String name, ZipInputStream in) throws IOException { + ZipEntry current; + while ((current = in.getNextEntry()) != null) { + if (current.getName().equals(name)) { + return current; + } + } + return null; + } + + @Override + public IFileStore getChild(String name) { + return new ZipFileStore(rootStore, path.append(name)); + } + + @Override + public String getName() { + String name = path.lastSegment(); + return name == null ? "" : name; //$NON-NLS-1$ + } + + @Override + public IFileStore getParent() { + if (path.segmentCount() > 0) { + return new ZipFileStore(rootStore, path.removeLastSegments(1)); + } + // the root entry has no parent + return null; + } + + private String getPluginId() { + return FrameworkUtil.getBundle(this.getClass()).getSymbolicName(); + } + + public IPath getPath() { + return path; + } + + private boolean isNested() { + return this.rootStore instanceof ZipFileStore; + } + + @Override + public IFileStore mkdir(int options, IProgressMonitor monitor) throws CoreException { + URI zipUri; + try { + zipUri = new URI("jar:" + rootStore.toURI().toString() + "!/"); //$NON-NLS-1$ //$NON-NLS-2$ + } catch (URISyntaxException e) { + throw new CoreException(new Status(IStatus.ERROR, "org.eclipse.core.filesystem.zip", "Invalid ZIP file URI", e)); //$NON-NLS-1$ //$NON-NLS-2$ + } + + Map env = new HashMap<>(); + env.put("create", "false"); //$NON-NLS-1$ //$NON-NLS-2$ + + // Assuming the directory to create is represented by 'this.path' + try (FileSystem zipFs = FileSystems.newFileSystem(zipUri, env)) { + Path dirInZipPath = zipFs.getPath(this.path.toString()); + if (Files.notExists(dirInZipPath)) { + Files.createDirectories(dirInZipPath); + + // To ensure the directory is actually added to the ZIP, we + // might need to add a temporary file + // in this directory. This is a workaround and should be used + // with caution. + Path tempFileInDir = dirInZipPath.resolve(".keep"); //$NON-NLS-1$ + Files.createFile(tempFileInDir); + + // Immediately delete the temporary file after creation to just + // keep the directory + Files.delete(tempFileInDir); + } + } catch (IOException e) { + throw new CoreException(new Status(IStatus.ERROR, "org.eclipse.core.filesystem.zip", "Error creating directory in ZIP file", e)); //$NON-NLS-1$ //$NON-NLS-2$ + } + + // Return a file store representing the newly created directory. + return new ZipFileStore(rootStore, this.path); + } + + @Override + public void move(IFileStore destination, int options, IProgressMonitor monitor) throws CoreException { + if (!(destination instanceof ZipFileStore)) { + super.move(destination, options, monitor); + return; + } + ZipFileStore destZipFileStore = (ZipFileStore) destination; + + try (FileSystem srcFs = openZipFileSystem(); FileSystem destFs = destZipFileStore.openZipFileSystem()) { + Path srcPath = srcFs.getPath(this.path.toString()); + Path destPath = destFs.getPath(destZipFileStore.path.toString()); + + if (destPath.getParent() != null) { + Files.createDirectories(destPath.getParent()); + } + + if (Files.isDirectory(srcPath)) { + moveDirectory(srcPath, destPath, srcFs, destFs); + } else { + Files.move(srcPath, destPath, StandardCopyOption.REPLACE_EXISTING); + } + } catch (IOException | URISyntaxException e) { + throw new CoreException(new Status(IStatus.ERROR, "org.eclipse.core.filesystem.zip", "Error moving entry within ZIP", e)); //$NON-NLS-1$ //$NON-NLS-2$ + } + } + + private void moveDirectory(Path srcPath, Path destPath, FileSystem srcFs, FileSystem destFs) throws IOException { + // Ensure the destination directory structure is ready + if (destPath.getParent() != null) { + Files.createDirectories(destPath.getParent()); + } + + // Recursively move the contents + Files.walk(srcPath).forEach(source -> { + try { + Path destination = destPath.resolve(srcPath.relativize(source)); + if (Files.isDirectory(source)) { + if (!Files.exists(destination)) { + Files.createDirectories(destination); + } + } else { + Files.move(source, destination, StandardCopyOption.REPLACE_EXISTING); + } + } catch (IOException e) { + throw new RuntimeException("Failed to move files", e); //$NON-NLS-1$ + } + }); + + // Delete the source directory after moving its contents + Files.walk(srcPath).sorted(Comparator.reverseOrder()).forEach(pathToMove -> { + try { + Files.delete(pathToMove); + } catch (IOException e) { + throw new RuntimeException("Failed to delete original files after move", e); //$NON-NLS-1$ + } + }); + } + + @Override + public InputStream openInputStream(int options, IProgressMonitor monitor) throws CoreException { + try { + ZipInputStream in = new ZipInputStream(rootStore.openInputStream(EFS.NONE, monitor)); + ZipEntry entry = findEntry(path.toString(), in); + if (entry == null) { + throw new CoreException(Status.error("File not found: " + rootStore.toString())); //$NON-NLS-1$ + } + if (entry.isDirectory()) { + throw new CoreException(Status.error("Resource is not a file: " + rootStore.toString())); //$NON-NLS-1$ + } + return in; + } catch (IOException e) { + throw new CoreException(Status.error("Could not read file: " + rootStore.toString(), e)); //$NON-NLS-1$ + } + } + + @Override + public OutputStream openOutputStream(int options, IProgressMonitor monitor) { + // Creating a ByteArrayOutputStream to capture the data written to the + // OutputStream + return new ByteArrayOutputStream() { + @Override + public void close() throws IOException { + try (FileSystem zipFs = openZipFileSystem()) { + Path entryPath = zipFs.getPath(path.toString()); + // Ensure parent directories exist + Path parentPath = entryPath.getParent(); + if (parentPath != null) { + Files.createDirectories(parentPath); + } + // Write the ByteArrayOutputStream's data to the entry + // in the ZIP file + Files.write(entryPath, this.toByteArray(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); + + } catch (Exception e) { + throw new IOException("Failed to integrate data into ZIP file", e); //$NON-NLS-1$ + } + } + }; + } + + private FileSystem openZipFileSystem() throws URISyntaxException, IOException { + Map env = new HashMap<>(); + env.put("create", "false"); //$NON-NLS-1$ //$NON-NLS-2$ + URI nioURI = toNioURI(); + Path innerArchivePath = null; + + if (isNested()) { + ZipFileStore outerZipFileStore = (ZipFileStore) this.rootStore; + FileSystem outerFs = outerZipFileStore.openZipFileSystem(); + innerArchivePath = outerFs.getPath(outerZipFileStore.path.toString()); + nioURI = innerArchivePath.toUri(); + } + + try { + if (innerArchivePath != null) { + return FileSystems.newFileSystem(innerArchivePath, env); + } + return FileSystems.newFileSystem(nioURI, env); + } catch (FileSystemAlreadyExistsException e) { + return FileSystems.getFileSystem(nioURI); + } + } + + @Override + public void putInfo(IFileInfo info, int options, IProgressMonitor monitor) throws CoreException { + if (monitor != null) { + monitor.beginTask("Updating Zip Entry Information", 1); //$NON-NLS-1$ + } + try (FileSystem zipFs = openZipFileSystem()) { + Path filePath = zipFs.getPath(path.toString()); + // Check options for what information is requested to be updated + if ((options & EFS.SET_ATTRIBUTES) != 0) { + boolean isHidden = info.getAttribute(EFS.ATTRIBUTE_HIDDEN); + boolean isArchive = info.getAttribute(EFS.ATTRIBUTE_ARCHIVE); + + if (ZipFileSystem.getOS().startsWith("Windows")) { //$NON-NLS-1$ + Files.setAttribute(filePath, "dos:hidden", isHidden); //$NON-NLS-1$ + Files.setAttribute(filePath, "dos:archive", isArchive); //$NON-NLS-1$ + } + } + if ((options & EFS.SET_LAST_MODIFIED) != 0) { + FileTime lastModified = FileTime.fromMillis(info.getLastModified()); + Files.setLastModifiedTime(filePath, lastModified); + } + + } catch (Exception e) { + throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Error updating ZIP file entry information", e)); //$NON-NLS-1$ + } finally { + if (monitor != null) { + monitor.done(); + } + } + } + + @Override + public URI toURI() { + String scheme = ZipFileSystem.SCHEME_ZIP; + String pathString = path.makeAbsolute().toString(); + URI rootStoreURI = rootStore.toURI(); + String rootStoreScheme = rootStoreURI.getScheme(); + String rootStorePath = rootStoreURI.getPath(); + String rootStoreQuery = rootStoreScheme + ":" + rootStorePath; //$NON-NLS-1$ + try { + return new URI(scheme, null, pathString, rootStoreQuery, null); + } catch (URISyntaxException e) { + // should not happen + throw new RuntimeException(e); + } + } + + private URI toNioURI() throws URISyntaxException { + String nioScheme = "jar:"; //$NON-NLS-1$ + String rootPath = rootStore.toURI().toString(); + + String suffix = "!/"; //$NON-NLS-1$ + String ret = nioScheme + rootPath + suffix; + return new URI(ret); + } + +} diff --git a/resources/examples/org.eclipse.ui.examples.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileSystem.java b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileSystem.java similarity index 79% rename from resources/examples/org.eclipse.ui.examples.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileSystem.java rename to resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileSystem.java index 386fdda01ef..b030d13ddc7 100644 --- a/resources/examples/org.eclipse.ui.examples.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileSystem.java +++ b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileSystem.java @@ -21,6 +21,9 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; +/** + * @since 1.11 + */ public class ZipFileSystem extends FileSystem { /** * Scheme constant (value "zip") indicating the zip file system scheme. @@ -41,4 +44,22 @@ public IFileStore getStore(URI uri) { } return EFS.getNullFileSystem().getStore(uri); } + + /** + * Returns the current OS. This is equivalent to Platform.getOS(), but + * is tolerant of the platform runtime not being present. + */ + static String getOS() { + return System.getProperty("osgi.os", ""); //$NON-NLS-1$ //$NON-NLS-2$ + } + + @Override + public boolean canDelete() { + return true; + } + + @Override + public boolean canWrite() { + return true; + } } diff --git a/resources/bundles/org.eclipse.core.resources/plugin.xml b/resources/bundles/org.eclipse.core.resources/plugin.xml index 74b386a109c..9cdeba8cce4 100644 --- a/resources/bundles/org.eclipse.core.resources/plugin.xml +++ b/resources/bundles/org.eclipse.core.resources/plugin.xml @@ -203,6 +203,12 @@ class="org.eclipse.core.internal.propertytester.ResourceMappingPropertyTester" properties="projectPersistentProperty" id="org.eclipse.core.resources.mappingPropertyTester"/> + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - entries = new HashMap<>(); - String myName = path.toString(); - try (ZipInputStream in = new ZipInputStream(rootStore.openInputStream(EFS.NONE, monitor))) { - ZipEntry current; - while ((current = in.getNextEntry()) != null) { - final String currentPath = current.getName(); - if (isParent(myName, currentPath)) { - entries.put(currentPath, current); - } else if (isAncestor(myName, currentPath)) { - int myNameLength = myName.length() + 1; - int nameEnd = currentPath.indexOf('/', myNameLength); - String dirName = nameEnd == -1 ? currentPath : currentPath.substring(0, nameEnd + 1); - if (!entries.containsKey(dirName)) - entries.put(dirName, new ZipEntry(dirName)); - } - } - } catch (IOException e) { - throw new CoreException(Status.error("Could not read file: " + rootStore.toString(), e)); - } - return entries.values().toArray(new ZipEntry[entries.size()]); - } - - @Override - public IFileInfo[] childInfos(int options, IProgressMonitor monitor) throws CoreException { - ZipEntry[] entries = childEntries(monitor); - int entryCount = entries.length; - IFileInfo[] infos = new IFileInfo[entryCount]; - for (int i = 0; i < entryCount; i++) { - infos[i] = convertZipEntryToFileInfo(entries[i]); - } - return infos; - } - - @Override - public String[] childNames(int options, IProgressMonitor monitor) throws CoreException { - ZipEntry[] entries = childEntries(monitor); - int entryCount = entries.length; - String[] names = new String[entryCount]; - for (int i = 0; i < entryCount; i++) { - names[i] = computeName(entries[i]); - } - return names; - } - - /** - * Computes the simple file name for a given zip entry. - */ - private String computeName(ZipEntry entry) { - //the entry name is a relative path, with an optional trailing separator - //We need to strip off the trailing slash, and then take everything after the - //last separator as the name - String name = entry.getName(); - int end = name.length() - 1; - if (name.charAt(end) == '/') { - end--; - } - return name.substring(name.lastIndexOf('/', end) + 1, end + 1); - } - - /** - * Creates a file info object corresponding to a given zip entry - * - * @param entry the zip entry - * @return The file info for a zip entry - */ - private IFileInfo convertZipEntryToFileInfo(ZipEntry entry) { - FileInfo info = new FileInfo(computeName(entry)); - info.setLastModified(entry.getTime()); - info.setExists(true); - info.setDirectory(entry.isDirectory()); - info.setLength(entry.getSize()); - return info; - } - - @Override - public IFileInfo fetchInfo(int options, IProgressMonitor monitor) throws CoreException { - try (ZipInputStream in = new ZipInputStream(rootStore.openInputStream(EFS.NONE, monitor))) { - String myPath = path.toString(); - ZipEntry current; - while ((current = in.getNextEntry()) != null) { - String currentPath = current.getName(); - if (myPath.equals(currentPath)) { - return convertZipEntryToFileInfo(current); - } - //directories don't always have their own entry, but it is implied by the existence of a child - if (isAncestor(myPath, currentPath)) { - return createDirectoryInfo(getName()); - } - } - } catch (IOException e) { - throw new CoreException(Status.error("Could not read file: " + rootStore.toString(), e)); - } - //does not exist - return new FileInfo(getName()); - } - - /** - * @return A directory info for this file store - */ - private IFileInfo createDirectoryInfo(String name) { - FileInfo result = new FileInfo(name); - result.setExists(true); - result.setDirectory(true); - return result; - } - - /** - * Finds the zip entry with the given name in this zip file. Returns the - * entry and leaves the input stream open positioned at the beginning of - * the bytes of that entry. Returns null if the entry could not be found. - */ - private ZipEntry findEntry(String name, ZipInputStream in) throws IOException { - ZipEntry current; - while ((current = in.getNextEntry()) != null) { - if (current.getName().equals(name)) { - return current; - } - } - return null; - } - - @Override - public IFileStore getChild(String name) { - return new ZipFileStore(rootStore, path.append(name)); - } - - @Override - public String getName() { - String name = path.lastSegment(); - return name == null ? "" : name; //$NON-NLS-1$ - } - - @Override - public IFileStore getParent() { - if (path.segmentCount() > 0) { - return new ZipFileStore(rootStore, path.removeLastSegments(1)); - } - //the root entry has no parent - return null; - } - - /** - * Returns whether ancestor is a parent of child. - * @param ancestor the potential ancestor - * @param child the potential child - * @return true or false - */ - private boolean isAncestor(String ancestor, String child) { - //children will start with myName and have no child path - int ancestorLength = ancestor.length(); - if (ancestorLength == 0) { - return true; - } - return child.startsWith(ancestor) && child.length() > ancestorLength && child.charAt(ancestorLength) == '/'; - } - - /** - * Returns whether parent is the immediate parent of child. - * @param parent the potential parent - * @param child the potential child - * @return true or false - */ - private boolean isParent(String parent, String child) { - //children will start with myName and have no child path - int chop = parent.length() + 1; - return child.startsWith(parent) && child.length() > chop && child.substring(chop).indexOf('/') == -1; - } - - @Override - public InputStream openInputStream(int options, IProgressMonitor monitor) throws CoreException { - try (ZipInputStream in = new ZipInputStream(rootStore.openInputStream(EFS.NONE, monitor))) { - ZipEntry entry = findEntry(path.toString(), in); - if (entry == null) { - throw new CoreException(Status.error("File not found: " + rootStore.toString())); - } - if (entry.isDirectory()) { - throw new CoreException(Status.error("Resource is not a file: " + rootStore.toString())); - } - return in; - } catch (IOException e) { - throw new CoreException(Status.error("Could not read file: " + rootStore.toString(), e)); - } - } - - @Override - public URI toURI() { - try { - return new URI(ZipFileSystem.SCHEME_ZIP, null, path.makeAbsolute().toString(), rootStore.toURI().toString(), null); - } catch (URISyntaxException e) { - //should not happen - throw new RuntimeException(e); - } - } -} diff --git a/resources/examples/org.eclipse.ui.examples.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileSystemContributor.java b/resources/examples/org.eclipse.ui.examples.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileSystemContributor.java deleted file mode 100644 index d8516a8b60a..00000000000 --- a/resources/examples/org.eclipse.ui.examples.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileSystemContributor.java +++ /dev/null @@ -1,82 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2022 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ - -package org.eclipse.core.internal.filesystem.zip; - -import java.io.File; -import java.net.URI; -import java.net.URISyntaxException; - -import org.eclipse.swt.widgets.FileDialog; -import org.eclipse.swt.widgets.Shell; -import org.eclipse.ui.ide.fileSystem.FileSystemContributor; - -/** - * ZipFileSystemContributor is the zip example of a file system contributor. - */ -public class ZipFileSystemContributor extends FileSystemContributor { - - public ZipFileSystemContributor() { - super(); - } - - @Override - public URI getURI(String pathString) { - try { - if (pathString.startsWith(ZipFileSystem.SCHEME_ZIP)) - return new URI(pathString); - } catch (URISyntaxException e1) { - return null; - } - if (File.separatorChar != '/') - pathString = pathString.replace(File.separatorChar, '/'); - final int length = pathString.length(); - StringBuffer pathBuf = new StringBuffer(length + 1); - pathBuf.append("file:"); //$NON-NLS-1$ - // There must be a leading slash in a hierarchical URI - if (length > 0 && (pathString.charAt(0) != '/')) - pathBuf.append('/'); - // additional double-slash for UNC paths to distinguish from host - // separator - if (pathString.startsWith("//")) //$NON-NLS-1$ - pathBuf.append('/').append('/'); - pathBuf.append(pathString); - try { - //scheme, host, path, query, fragment - return new URI(ZipFileSystem.SCHEME_ZIP, null, "/", pathBuf.toString(), null); //$NON-NLS-1$ - } catch (URISyntaxException e) { - return null; - } - } - - /* (non-Javadoc) - * @see org.eclipse.ui.ide.fileSystem.FileSystemContributor#browseFileSystem(java.lang.String, org.eclipse.swt.widgets.Shell) - */ - @Override - public URI browseFileSystem(String initialPath, Shell shell) { - - FileDialog dialog = new FileDialog(shell); - - if (initialPath.length() > 0) - dialog.setFilterPath(initialPath); - - dialog.setFilterExtensions(new String[] {"*.zip"});//$NON-NLS-1$ - - String selectedFile = dialog.open(); - if (selectedFile == null) - return null; - return getURI(selectedFile); - } - -} diff --git a/resources/examples/org.eclipse.ui.examples.filesystem/src/org/eclipse/ui/examples/filesystem/CollapseZipHandler.java b/resources/examples/org.eclipse.ui.examples.filesystem/src/org/eclipse/ui/examples/filesystem/CollapseZipHandler.java deleted file mode 100644 index 9b75ba8bf0d..00000000000 --- a/resources/examples/org.eclipse.ui.examples.filesystem/src/org/eclipse/ui/examples/filesystem/CollapseZipHandler.java +++ /dev/null @@ -1,79 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2022 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Patrick Ziegler - Migration from a JFace Action to a Command Handler, - * in order to be used with the 'org.eclipse.ui.menus' - * extension point. - *******************************************************************************/ -package org.eclipse.ui.examples.filesystem; - -import java.net.URI; -import org.eclipse.core.commands.AbstractHandler; -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.core.filesystem.EFS; -import org.eclipse.core.filesystem.IFileStore; -import org.eclipse.core.filesystem.URIUtil; -import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IFolder; -import org.eclipse.core.resources.IResource; -import org.eclipse.core.runtime.IPath; -import org.eclipse.jface.dialogs.MessageDialog; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.jface.viewers.IStructuredSelection; -import org.eclipse.swt.widgets.Shell; -import org.eclipse.ui.handlers.HandlerUtil; - -public class CollapseZipHandler extends AbstractHandler { - - private void collapseZip(IFolder folder, Shell shell) { - try { - URI zipURI = new URI(folder.getLocationURI().getQuery()); - //check if the zip file is physically stored below the folder in the workspace - IFileStore parentStore = EFS.getStore(folder.getParent().getLocationURI()); - URI childURI = parentStore.getChild(folder.getName()).toURI(); - if (URIUtil.equals(zipURI, childURI)) { - //the zip file is in the workspace so just delete the link - // and refresh the parent to create the resource - folder.delete(IResource.NONE, null); - folder.getParent().refreshLocal(IResource.DEPTH_INFINITE, null); - } else { - //otherwise the zip file must be a linked resource - IFile file = folder.getParent().getFile(IPath.fromOSString(folder.getName())); - file.createLink(zipURI, IResource.REPLACE, null); - } - } catch (Exception e) { - MessageDialog.openError(shell, "Error", "Error opening zip file"); - e.printStackTrace(); - } - } - - @Override - public Object execute(ExecutionEvent event) throws ExecutionException { - Shell shell = HandlerUtil.getActiveShell(event); - ISelection selection = HandlerUtil.getCurrentSelection(event); - - if (!(selection instanceof IStructuredSelection)) { - return null; - } - - Object element = ((IStructuredSelection) selection).getFirstElement(); - - if (!(element instanceof IFolder)) { - return null; - } - - collapseZip((IFolder) element, shell); - return null; - } - -} diff --git a/resources/examples/org.eclipse.ui.examples.filesystem/src/org/eclipse/ui/examples/filesystem/ExpandZipHandler.java b/resources/examples/org.eclipse.ui.examples.filesystem/src/org/eclipse/ui/examples/filesystem/ExpandZipHandler.java deleted file mode 100644 index 7fde7131080..00000000000 --- a/resources/examples/org.eclipse.ui.examples.filesystem/src/org/eclipse/ui/examples/filesystem/ExpandZipHandler.java +++ /dev/null @@ -1,66 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2022 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Patrick Ziegler - Migration from a JFace Action to a Command Handler, - * in order to be used with the 'org.eclipse.ui.menus' - * extension point. - *******************************************************************************/ -package org.eclipse.ui.examples.filesystem; - -import java.net.URI; -import org.eclipse.core.commands.AbstractHandler; -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.core.internal.filesystem.zip.ZipFileSystem; -import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IFolder; -import org.eclipse.core.resources.IResource; -import org.eclipse.core.runtime.IPath; -import org.eclipse.jface.dialogs.MessageDialog; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.jface.viewers.IStructuredSelection; -import org.eclipse.swt.widgets.Shell; -import org.eclipse.ui.handlers.HandlerUtil; - -public class ExpandZipHandler extends AbstractHandler { - - private void expandZip(IFile file, Shell shell) { - try { - URI zipURI = new URI(ZipFileSystem.SCHEME_ZIP, null, "/", file.getLocationURI().toString(), null); - IFolder link = file.getParent().getFolder(IPath.fromOSString(file.getName())); - link.createLink(zipURI, IResource.REPLACE, null); - } catch (Exception e) { - MessageDialog.openError(shell, "Error", "Error opening zip file"); - e.printStackTrace(); - } - } - - @Override - public Object execute(ExecutionEvent event) throws ExecutionException { - Shell shell = HandlerUtil.getActiveShell(event); - ISelection selection = HandlerUtil.getCurrentSelection(event); - - if (!(selection instanceof IStructuredSelection)) { - return null; - } - - Object element = ((IStructuredSelection) selection).getFirstElement(); - - if (!(element instanceof IFile)) { - return null; - } - - expandZip((IFile) element, shell); - return null; - } - -} diff --git a/resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicText.zip b/resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicText.zip new file mode 100644 index 0000000000000000000000000000000000000000..b33117fa4057a19f7b86474b2e667ccddc7593c1 GIT binary patch literal 126 zcmWIWW@Zs#0D;x3Ln0Q-Xv**a*&xgT#388_C3+bqjd4n_9T$u{?90o)3pD4XSG#5hC$abw{<^RBtf!B#*;XnHXf&M@ykUNdIjHP*98y3ahl%5|15J_ zebNBslrJa4TAeujfGvH9w6WL?Xh-GZ*VgLA!bgSxhRVi_RySsTb}_K1d~9j;V{5h7 z0!U?KP^%+@BTxTI_BYy>vmh2KC#zaLS$)r;g0^L4TdOPCo@roR=C(KGjl!cMIVGIm K1!LN{j9CL5weZaV literal 0 HcmV?d00001 diff --git a/resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/Empty.zip b/resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/Empty.zip new file mode 100644 index 0000000000000000000000000000000000000000..15cb0ecb3e219d1701294bfdf0fe3f5cb5d208e7 GIT binary patch literal 22 NcmWIWW@Tf*000g10H*)| literal 0 HcmV?d00001 diff --git a/resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/Fake.zip b/resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/Fake.zip new file mode 100644 index 00000000000..945948d8236 --- /dev/null +++ b/resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/Fake.zip @@ -0,0 +1 @@ +THIS IS NOT A ZIP! \ No newline at end of file diff --git a/resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/NestedZipFileParent.zip b/resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/NestedZipFileParent.zip new file mode 100644 index 0000000000000000000000000000000000000000..6af194f1ce744b7ce672bbca26462f0d25c6f54a GIT binary patch literal 1478 zcmWIWW@Zs#0D z*GD#-Z*|>F37}dK769T9pdOd}?9$xSyb`^ViV{aBXBSsDcU~^f*&{i8Cey7!E)OzyTMKRa}~6s1GEJ^aH#ZnYb80VhjwJzE=T@BM9`c z;9^h!OEQ8&Yjb&m_pO_ZA0q6o-rG1APUEvdkOA2SH=tR_Hh{y4x>od}Spg11VyzHi zfTS#JAqox#aC*4{WFm(zD;r3T1qc%u85sV66(Z7#LFTfE#WI>QJV0>}HUnaWZXnAT z6i+4~!W2Z9fd~r_VF@A(4fI1&E5PZ}BQ+-{Um-ldC?`b`VSYj$s`(QAP#{hGug zipFileNames() { + return Stream.of(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + } + + static void ensureExists(IResource resource) throws CoreException, IOException { + switch (resource.getType()) { + case IResource.FILE: { + IFileStore fileStore = EFS.getStore(resource.getLocationURI()); + ensureExistsInFileSystem(fileStore); + ensureExistsInWorkspace((IFile) resource); + break; + } + case IResource.FOLDER: { + IFileStore fileStore = EFS.getStore(resource.getLocationURI()); + ensureExistsInFileSystem(fileStore); + ensureExistsInWorkspace((IFolder) resource); + break; + } + default: + throw new IllegalArgumentException("Unexpected value: " + resource.getType()); + } + } + + static void ensureDoesNotExist(IResource resource) throws CoreException, IOException { + switch (resource.getType()) { + case IResource.FILE: { + IFileStore fileStore = EFS.getStore(resource.getLocationURI()); + ensureDoesNotExistInFileSystem(fileStore); + ensureDoesNotExistInWorkspace((IFile) resource); + break; + } + case IResource.FOLDER: { + IFileStore fileStore = EFS.getStore(resource.getLocationURI()); + ensureDoesNotExistInFileSystem(fileStore); + ensureDoesNotExistInWorkspace((IFolder) resource); + break; + } + default: + throw new IllegalArgumentException("Unexpected value: " + resource.getType()); + } + } + + static void assertTextFileContent(IFile textFile, String expectedContent) throws IOException, CoreException { + try (InputStreamReader isr = new InputStreamReader(textFile.getContents()); + BufferedReader reader = new BufferedReader(isr)) { + String content = reader.readLine(); // Assuming the file has a single line with "Hello World!" + Assert.assertEquals("The content of " + textFile.getName() + " should be '" + expectedContent + "'", + expectedContent, content); + } + } + + private static void ensureDoesNotExistInFileSystem(IFileStore store) throws CoreException { + assertTrue("store was not properly deleted: " + store, !store.fetchInfo().exists()); + } + + private static void ensureExistsInFileSystem(IFileStore store) throws CoreException, IOException { + final IFileInfo info = store.fetchInfo(); + assertTrue("file info for store does not exist: " + store, info.exists()); + } + + private static void ensureDoesNotExistInWorkspace(IFile file) throws CoreException { + assertTrue("file was not properly deleted: " + file, !file.exists()); + } + + private static void ensureDoesNotExistInWorkspace(IFolder folder) throws CoreException { + assertTrue("folder was not properly deleted: " + folder, !folder.exists()); + } + + private static void ensureExistsInWorkspace(IFile file) throws CoreException, IOException { + assertTrue("file does not exist in workspace: " + file, file.exists()); + } + + private static void ensureExistsInWorkspace(IFolder folder) throws CoreException, IOException { + assertTrue("folder does not exist in workspace: " + folder, folder.exists()); + } + + static IProgressMonitor getMonitor() { + return new FussyProgressMonitor(); + } + + static void openZipFile(IFile file) throws URISyntaxException, CoreException, IOException { + ZipFileTransformer.openZipFile(file, new NullProgressMonitor(), false); + } + + static void openZipFileBackground(IFile file) throws URISyntaxException, CoreException, IOException { + ZipFileTransformer.openZipFile(file, new NullProgressMonitor(), true); + } + + static void closeZipFile(IFolder folder) throws Exception { + ZipFileTransformer.closeZipFile(folder); + } + + static void printContents(IContainer container, String indent) throws CoreException { + IResource[] members = container.members(); + for (IResource member : members) { + if (member instanceof IFile) { + System.out.println(indent + "File: " + member.getName()); + } else if (member instanceof IContainer) { // This can be IFolder or IProject + System.out.println(indent + "Folder: " + member.getName()); + printContents((IContainer) member, indent + " "); // Recursively print contents + } + } + } + +} diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/AutomatedResourceTests.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/AutomatedResourceTests.java index 38d3f7545fe..3c899db1ab2 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/AutomatedResourceTests.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/AutomatedResourceTests.java @@ -23,6 +23,7 @@ @Suite @SelectClasses({ // org.eclipse.core.tests.filesystem.AllFileSystemTests.class, // + org.eclipse.core.tests.filesystem.zip.AllZipFileSystemTests.class, // org.eclipse.core.tests.internal.alias.AllAliasTests.class, // org.eclipse.core.tests.internal.builders.AllBuilderTests.class, // org.eclipse.core.tests.internal.dtree.AllDtreeTests.class, // From e200d6164ece034b9c3f65a4098812a30db77276 Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Tue, 11 Jun 2024 16:06:20 +0200 Subject: [PATCH 02/35] remove progressMonitor and ProgressDialog The progress monitor used in ZipFileTransformer#openZipFile was necessary in previous versions because performance issues were present. now it is useless and is thus be removed. --- .../core/internal/resources/ResourceTree.java | 2 +- .../core/resources/ZipFileTransformer.java | 18 ++++++++---------- .../filesystem/zip/ZipFileSystemTestUtil.java | 5 ++--- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ResourceTree.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ResourceTree.java index 5c4405be44a..29d1c4d0856 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ResourceTree.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ResourceTree.java @@ -1017,7 +1017,7 @@ public void standardMoveFolder(IFolder source, IFolder destination, int flags, I IFile newDestination = destination.getParent().getFile(IPath.fromOSString(destination.getName())); newSource.move(newDestination.getFullPath(), false, null); if (!ZipFileUtil.isNested(destination.getLocationURI())) { - ZipFileTransformer.openZipFile(newDestination, monitor.slice(4), false); + ZipFileTransformer.openZipFile(newDestination, false); } return; } catch (URISyntaxException | CoreException e) { diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java index 950007bab40..ef142766592 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java @@ -22,10 +22,8 @@ import org.eclipse.core.filesystem.ZipFileUtil; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; -import org.eclipse.core.runtime.SubMonitor; /** * Utility class for opening and closing zip files. @@ -70,19 +68,20 @@ public static void closeZipFile(IFolder folder) throws URISyntaxException, CoreE * This method prevents opening linked zip files. zip files must be local to be * opened. Otherwise a CoreException is thrown. * - * @param file The file representing the zip file to open. - * @param monitor monitor indicating the completion progress + * @param file The file representing the zip file to open. + * @param backgroundRefresh A boolean indicating wether the zip file should be + * loaded in the background or in the foreground. When + * testing the boolean should be false. * */ - public static void openZipFile(IFile file, IProgressMonitor monitor, boolean backgroundRefresh) + public static void openZipFile(IFile file, boolean backgroundRefresh) throws URISyntaxException, CoreException { - SubMonitor subMonitor = SubMonitor.convert(monitor, 20); try (InputStream fis = file.getContents()) { ZipFileUtil.checkFileForZipHeader(fis); // Additional operations can continue here if header is correct } catch (IOException e) { // If the header is incorrect or there's an IO error, handle gracefully - throw new CoreException(new Status(IStatus.ERROR, "your.plugin.id", //$NON-NLS-1$ + throw new CoreException(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, "Failed to open ZIP file due to incorrect file header: " + file.getName(), e)); //$NON-NLS-1$ } @@ -99,12 +98,11 @@ public static void openZipFile(IFile file, IProgressMonitor monitor, boolean bac URI zipURI = new URI("zip", null, "/", file.getLocationURI().toString(), null); //$NON-NLS-1$ //$NON-NLS-2$ IFolder link = file.getParent().getFolder(IPath.fromOSString(file.getName())); - subMonitor.split(1); int flags = backgroundRefresh ? IResource.REPLACE | IResource.BACKGROUND_REFRESH : IResource.REPLACE; try { - link.createLink(zipURI, flags, subMonitor.split(19)); - link.refreshLocal(0, subMonitor); + link.createLink(zipURI, flags, null); + link.refreshLocal(0, null); } catch (CoreException e) { throw new CoreException( new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, "Zip File could not be opened")); //$NON-NLS-1$ diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestUtil.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestUtil.java index a8df732da80..07705f0a381 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestUtil.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestUtil.java @@ -29,7 +29,6 @@ import org.eclipse.core.resources.ZipFileTransformer; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.tests.harness.FussyProgressMonitor; import org.junit.Assert; @@ -116,11 +115,11 @@ static IProgressMonitor getMonitor() { } static void openZipFile(IFile file) throws URISyntaxException, CoreException, IOException { - ZipFileTransformer.openZipFile(file, new NullProgressMonitor(), false); + ZipFileTransformer.openZipFile(file, false); } static void openZipFileBackground(IFile file) throws URISyntaxException, CoreException, IOException { - ZipFileTransformer.openZipFile(file, new NullProgressMonitor(), true); + ZipFileTransformer.openZipFile(file, true); } static void closeZipFile(IFolder folder) throws Exception { From 6475ab0b0a1d927dcf461dc3f49856125522c580 Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Tue, 11 Jun 2024 16:24:21 +0200 Subject: [PATCH 03/35] remove parametrization for tests parametrization for tests is removed because it makes trouble building a good test name within build jobs --- .../core/tests/filesystem/zip/CloseTest.java | 13 +- .../core/tests/filesystem/zip/CopyTest.java | 66 +++++----- .../core/tests/filesystem/zip/CreateTest.java | 19 ++- .../core/tests/filesystem/zip/DeleteTest.java | 37 +++--- .../core/tests/filesystem/zip/MoveTest.java | 117 ++++++++---------- .../core/tests/filesystem/zip/RenameTest.java | 30 ++--- .../core/tests/filesystem/zip/SetupTest.java | 17 ++- .../filesystem/zip/ZipFileSystemTestUtil.java | 5 - 8 files changed, 135 insertions(+), 169 deletions(-) diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CloseTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CloseTest.java index b253f49d72f..663e2bbec5d 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CloseTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CloseTest.java @@ -19,8 +19,7 @@ import org.eclipse.core.resources.IFolder; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.api.Test; public class CloseTest { @@ -34,14 +33,14 @@ public void teardown() throws Exception { ZipFileSystemTestSetup.teardown(); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testCloseZipFile(String zipFileName) throws Exception { + @Test + public void testCloseZipFile() throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(zipFileName); + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); ensureExists(openedZipFile); ZipFileSystemTestUtil.closeZipFile(openedZipFile); - IFile zipFile = ZipFileSystemTestSetup.firstProject.getFile(zipFileName); + IFile zipFile = ZipFileSystemTestSetup.firstProject + .getFile(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); // Don't use Utility method ensureDoesNotExist because the fileStore is still // available after closing. The fileStore is the File itself in the local file // system that still exists after closing. diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CopyTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CopyTest.java index 9c59a41dd32..1bb294363a8 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CopyTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CopyTest.java @@ -23,8 +23,7 @@ import org.eclipse.core.resources.IFolder; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.api.Test; /** * @@ -41,27 +40,25 @@ public void teardown() throws Exception { ZipFileSystemTestSetup.teardown(); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testCopyZipFile(String zipFileName) throws Exception { + @Test + public void testCopyZipFile() throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(zipFileName); + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); ensureExists(openedZipFile); IFolder destinationFolder = ZipFileSystemTestSetup.firstProject.getFolder("Folder"); destinationFolder.create(true, true, getMonitor()); ensureExists(destinationFolder); IFolder copyDestination = ZipFileSystemTestSetup.firstProject - .getFolder("Folder" + "/" + zipFileName); + .getFolder("Folder" + "/" + ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); openedZipFile.copy(copyDestination.getFullPath(), true, getMonitor()); ensureExists(copyDestination); ensureExists(openedZipFile); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testCopyFileInsideOfZipFile(String zipFileName) throws Exception { + @Test + public void testCopyFileInsideOfZipFile() throws Exception { IFile textFile = ZipFileSystemTestSetup.firstProject.getFile( - zipFileName + "/" + ZipFileSystemTestSetup.TEXT_FILE_NAME); + ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME + "/" + ZipFileSystemTestSetup.TEXT_FILE_NAME); ensureExists(textFile); IFolder destinationFolder = ZipFileSystemTestSetup.firstProject.getFolder("Folder"); destinationFolder.create(true, true, getMonitor()); @@ -73,10 +70,10 @@ public void testCopyFileInsideOfZipFile(String zipFileName) throws Exception { ensureExists(textFile); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testCopyFolderInsideOfZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); + @Test + public void testCopyFolderInsideOfZipFile() throws Exception { + IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); ensureExists(openedZipFile); IFolder newFolder = ZipFileSystemTestSetup.firstProject.getFolder("NewFolder"); ensureDoesNotExist(newFolder); @@ -97,9 +94,8 @@ public void testCopyFolderInsideOfZipFile(String zipFileName) throws Exception { assertTextFileContent(textFile, "Foo"); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testCopyFileIntoZipFile(String zipFileName) throws Exception { + @Test + public void testCopyFileIntoZipFile() throws Exception { IFile textFile = ZipFileSystemTestSetup.firstProject.getFile("NewFile.txt"); ensureDoesNotExist(textFile); String text = "Foo"; @@ -108,16 +104,15 @@ public void testCopyFileIntoZipFile(String zipFileName) throws Exception { stream.close(); ensureExists(textFile); IFile copyDestination = ZipFileSystemTestSetup.firstProject - .getFile(zipFileName + "/" + "NewFile.txt"); + .getFile(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME + "/" + "NewFile.txt"); textFile.copy(copyDestination.getFullPath(), true, getMonitor()); ensureExists(copyDestination); ensureExists(textFile); assertTextFileContent(textFile, "Foo"); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testCopyFolderIntoZipFile(String zipFileName) throws Exception { + @Test + public void testCopyFolderIntoZipFile() throws Exception { IFile textFile = ZipFileSystemTestSetup.firstProject.getFile("NewFile.txt"); ensureDoesNotExist(textFile); String text = "Foo"; @@ -125,17 +120,18 @@ public void testCopyFolderIntoZipFile(String zipFileName) throws Exception { textFile.create(stream, true, getMonitor()); stream.close(); ensureExists(textFile); - IFile copyDestination = ZipFileSystemTestSetup.firstProject.getFile(zipFileName + "/" + "NewFile.txt"); + IFile copyDestination = ZipFileSystemTestSetup.firstProject + .getFile(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME + "/" + "NewFile.txt"); textFile.copy(copyDestination.getFullPath(), true, getMonitor()); ensureExists(copyDestination); ensureExists(textFile); assertTextFileContent(textFile, "Foo"); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testCopyFileFromOutsideOfZipFIleIntoFolderInZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); + @Test + public void testCopyFileFromOutsideOfZipFIleIntoFolderInZipFile() throws Exception { + IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); IFolder newFolder = openedZipFile.getFolder("NewFolder"); ensureDoesNotExist(newFolder); newFolder.create(false, true, getMonitor()); @@ -155,10 +151,10 @@ public void testCopyFileFromOutsideOfZipFIleIntoFolderInZipFile(String zipFileNa assertTextFileContent(textFile, "Foo"); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testCopyFolderIntoFolderInZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); + @Test + public void testCopyFolderIntoFolderInZipFile() throws Exception { + IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); IFolder firstNewFolder = openedZipFile.getFolder("FirstNewFolder"); ensureDoesNotExist(firstNewFolder); firstNewFolder.create(false, true, getMonitor()); @@ -182,10 +178,10 @@ public void testCopyFolderIntoFolderInZipFile(String zipFileName) throws Excepti assertTextFileContent(textFile, "Foo"); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testCopyFileFromOneFolderToOtherFolderInsideofZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); + @Test + public void testCopyFileFromOneFolderToOtherFolderInsideofZipFile() throws Exception { + IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); IFolder firstNewFolder = openedZipFile.getFolder("FirstNewFolder"); ensureDoesNotExist(firstNewFolder); firstNewFolder.create(false, true, getMonitor()); diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CreateTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CreateTest.java index 6279bb920cb..d6488dfb534 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CreateTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CreateTest.java @@ -23,8 +23,7 @@ import org.eclipse.core.resources.IFolder; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.api.Test; public class CreateTest { @@ -38,10 +37,10 @@ public void teardown() throws Exception { ZipFileSystemTestSetup.teardown(); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testCreateFileInsideOfZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); + @Test + public void testCreateFileInsideOfZipFile() throws Exception { + IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); IFile textFile = openedZipFile.getFile("NewFile.txt"); ensureDoesNotExist(textFile); String text = "Foo"; @@ -52,10 +51,10 @@ public void testCreateFileInsideOfZipFile(String zipFileName) throws Exception { assertTextFileContent(textFile, "Foo"); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testCreateFolderInsideOfZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); + @Test + public void testCreateFolderInsideOfZipFile() throws Exception { + IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); IFolder newFolder = openedZipFile.getFolder("NewFolder"); ensureDoesNotExist(newFolder); newFolder.create(false, true, getMonitor()); diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/DeleteTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/DeleteTest.java index 1b2736eba49..cb1d2b0a4bc 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/DeleteTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/DeleteTest.java @@ -23,8 +23,7 @@ import org.eclipse.core.runtime.CoreException; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.api.Test; public class DeleteTest { @@ -38,33 +37,32 @@ public void teardown() throws Exception { ZipFileSystemTestSetup.teardown(); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testDeleteZipFile(String zipFileName) throws CoreException, IOException { + @Test + public void testDeleteZipFile() throws CoreException, IOException { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(zipFileName); + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); ensureExists(openedZipFile); openedZipFile.delete(false, false, getMonitor()); ensureDoesNotExist(openedZipFile); - IFile zipFile = ZipFileSystemTestSetup.firstProject.getFile(zipFileName); + IFile zipFile = ZipFileSystemTestSetup.firstProject + .getFile(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); ensureDoesNotExist(zipFile); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testDeleteFileInsideOfZipFile(String zipFileName) throws CoreException, IOException { + @Test + public void testDeleteFileInsideOfZipFile() throws CoreException, IOException { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(zipFileName); + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); IFile textFile = openedZipFile.getFile(ZipFileSystemTestSetup.TEXT_FILE_NAME); ensureExists(textFile); textFile.delete(true, getMonitor()); ensureDoesNotExist(textFile); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testDeleteEmptyFolder(String zipFileName) throws CoreException, IOException { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); + @Test + public void testDeleteEmptyFolder() throws CoreException, IOException { + IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); IFolder folder = openedZipFile.getFolder("FolderToDelete"); ensureDoesNotExist(folder); folder.create(true, true, getMonitor()); @@ -73,11 +71,10 @@ public void testDeleteEmptyFolder(String zipFileName) throws CoreException, IOEx ensureDoesNotExist(folder); } - - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testDeleteFolderWithChildren(String zipFileName) throws CoreException, IOException { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); + @Test + public void testDeleteFolderWithChildren() throws CoreException, IOException { + IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); IFolder folder = openedZipFile.getFolder("FolderToDelete"); ensureDoesNotExist(folder); folder.create(true, true, getMonitor()); diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/MoveTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/MoveTest.java index 8f0951cb16b..445b44e9f2a 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/MoveTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/MoveTest.java @@ -25,8 +25,6 @@ import org.eclipse.core.runtime.CoreException; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.MethodSource; public class MoveTest { @@ -41,50 +39,47 @@ public void teardown() throws Exception { ZipFileSystemTestSetup.teardown(); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testMoveZipFileWithinProject(String zipFileName) throws CoreException, IOException { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); + public void testMoveZipFileWithinProject() throws CoreException, IOException { + IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); IFolder destinationFolder = ZipFileSystemTestSetup.firstProject.getFolder("destinationFolder"); destinationFolder.create(false, true, getMonitor()); IFolder destination = ZipFileSystemTestSetup.firstProject - .getFolder("destinationFolder/" + zipFileName); + .getFolder("destinationFolder/" + ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); openedZipFile.move(destination.getFullPath(), false, getMonitor()); IFolder newFolder = ZipFileSystemTestSetup.firstProject - .getFolder(destinationFolder.getName() + "/" + zipFileName); + .getFolder(destinationFolder.getName() + "/" + ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); ensureExists(newFolder); ensureDoesNotExist(openedZipFile); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testMoveZipFileToOtherProject(String zipFileName) throws CoreException, IOException { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); - IFolder destination = ZipFileSystemTestSetup.secondProject.getFolder(zipFileName); + public void testMoveZipFileToOtherProject() throws CoreException, IOException { + IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + IFolder destination = ZipFileSystemTestSetup.secondProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); openedZipFile.move(destination.getFullPath(), false, getMonitor()); - IFolder newFolder = ZipFileSystemTestSetup.secondProject.getFolder(zipFileName); + IFolder newFolder = ZipFileSystemTestSetup.secondProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); ensureExists(newFolder); ensureDoesNotExist(openedZipFile); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testMoveZipFileToOtherProjectFolder(String zipFileName) throws CoreException, IOException { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); + public void testMoveZipFileToOtherProjectFolder() throws CoreException, IOException { + IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); IFolder destinationFolder = ZipFileSystemTestSetup.secondProject.getFolder("destinationFolder"); destinationFolder.create(false, true, getMonitor()); IFolder destination = ZipFileSystemTestSetup.secondProject - .getFolder("destinationFolder/" + zipFileName); + .getFolder("destinationFolder/" + ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); openedZipFile.move(destination.getFullPath(), false, getMonitor()); IFolder newFolder = ZipFileSystemTestSetup.secondProject - .getFolder(destinationFolder.getName() + "/" + zipFileName); + .getFolder(destinationFolder.getName() + "/" + ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); ensureExists(newFolder); ensureDoesNotExist(openedZipFile); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testMoveFileIntoZipFile(String zipFileName) throws Exception { + public void testMoveFileIntoZipFile() throws Exception { IFile textFile = ZipFileSystemTestSetup.firstProject.getFile("NewFile.txt"); ensureDoesNotExist(textFile); String text = "Foo"; @@ -93,17 +88,16 @@ public void testMoveFileIntoZipFile(String zipFileName) throws Exception { stream.close(); ensureExists(textFile); IFile destinationFile = ZipFileSystemTestSetup.firstProject - .getFile(zipFileName + "/" + "NewFile.txt"); + .getFile(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME + "/" + "NewFile.txt"); textFile.move(destinationFile.getFullPath(), false, getMonitor()); ensureExists(destinationFile); assertTextFileContent(destinationFile, text); ensureDoesNotExist(textFile); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testMoveFolderIntoZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); + public void testMoveFolderIntoZipFile() throws Exception { + IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); IFolder destinationFolder = openedZipFile.getFolder("destinationFolder"); ensureDoesNotExist(destinationFolder); destinationFolder.create(false, true, getMonitor()); @@ -118,10 +112,9 @@ public void testMoveFolderIntoZipFile(String zipFileName) throws Exception { ensureExists(newFolderDestination); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testMoveFolderWithContentIntoZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); + public void testMoveFolderWithContentIntoZipFile() throws Exception { + IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); IFolder destinationFolder = openedZipFile.getFolder("destinationFolder"); ensureDoesNotExist(destinationFolder); destinationFolder.create(false, true, getMonitor()); @@ -143,11 +136,10 @@ public void testMoveFolderWithContentIntoZipFile(String zipFileName) throws Exce ensureExists(newFolderDestination); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testMoveFileFromZipFile(String zipFileName) throws Exception { + public void testMoveFileFromZipFile() throws Exception { IFile textFile = ZipFileSystemTestSetup.firstProject - .getFile(zipFileName + "/" + ZipFileSystemTestSetup.TEXT_FILE_NAME); + .getFile(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME + "/" + + ZipFileSystemTestSetup.TEXT_FILE_NAME); ensureExists(textFile); IFile destinationFile = ZipFileSystemTestSetup.firstProject.getFile(ZipFileSystemTestSetup.TEXT_FILE_NAME); textFile.move(destinationFile.getFullPath(), false, getMonitor()); @@ -156,10 +148,9 @@ public void testMoveFileFromZipFile(String zipFileName) throws Exception { ensureDoesNotExist(textFile); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testMoveFolderFromZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); + public void testMoveFolderFromZipFile() throws Exception { + IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); IFolder newFolder = openedZipFile.getFolder("NewFolder"); ensureDoesNotExist(newFolder); newFolder.create(false, true, getMonitor()); @@ -170,10 +161,9 @@ public void testMoveFolderFromZipFile(String zipFileName) throws Exception { ensureExists(folderDestination); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testMoveFolderWithContentFromZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); + public void testMoveFolderWithContentFromZipFile() throws Exception { + IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); IFolder newFolder = openedZipFile.getFolder("NewFolder"); ensureDoesNotExist(newFolder); newFolder.create(false, true, getMonitor()); @@ -191,12 +181,10 @@ public void testMoveFolderWithContentFromZipFile(String zipFileName) throws Exce ensureExists(folderDestination); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testMoveFolderWithContentFromZipFileIntoOtherZipFile(String zipFileName) throws Exception { - IFolder firstZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); + public void testMoveFolderWithContentFromZipFileIntoOtherZipFile() throws Exception { + IFolder firstZipFile = ZipFileSystemTestSetup.firstProject.getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); // create and open second ZipFile - String secondZipFileName = zipFileName.replace(".", "New."); + String secondZipFileName = ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME.replace(".", "New."); IFile secondZipFile = ZipFileSystemTestSetup.firstProject.getFile(secondZipFileName); ensureDoesNotExist(secondZipFile); ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.firstProject, secondZipFileName); @@ -224,11 +212,9 @@ public void testMoveFolderWithContentFromZipFileIntoOtherZipFile(String zipFileN ensureExists(movedTextFile); } - - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testMoveFileInsideOfZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); + public void testMoveFileInsideOfZipFile() throws Exception { + IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); IFolder destinationFolder = openedZipFile.getFolder("destinationFolder"); ensureDoesNotExist(destinationFolder); destinationFolder.create(false, true, getMonitor()); @@ -242,14 +228,11 @@ public void testMoveFileInsideOfZipFile(String zipFileName) throws Exception { ensureDoesNotExist(textFile); } - - - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testMoveZipFileIntoZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); + public void testMoveZipFileIntoZipFile() throws Exception { + IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); // create and open second ZipFile - String newZipFileName = zipFileName.replace(".", "New."); + String newZipFileName = ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME.replace(".", "New."); IFile newZipFile = ZipFileSystemTestSetup.firstProject.getFile(newZipFileName); ensureDoesNotExist(newZipFile); ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.firstProject, newZipFileName); @@ -271,11 +254,10 @@ public void testMoveZipFileIntoZipFile(String zipFileName) throws Exception { * refreshing the Workspace. This test checks if this specific error is handeled * correctly in RefreshLocalVisitor#visit() */ - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testMoveZipFileWithFolder(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); - String contentFolderPath = zipFileName + "/" + "Folder"; + public void testMoveZipFileWithFolder() throws Exception { + IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + String contentFolderPath = ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME + "/" + "Folder"; IFolder contentFolder = ZipFileSystemTestSetup.firstProject.getFolder(contentFolderPath); ensureDoesNotExist(contentFolder); contentFolder.create(false, true, getMonitor()); @@ -290,7 +272,8 @@ public void testMoveZipFileWithFolder(String zipFileName) throws Exception { ensureDoesNotExist(destinationFolder); destinationFolder.create(false, true, getMonitor()); ensureExists(destinationFolder); - IFolder zipFileDestination = ZipFileSystemTestSetup.firstProject.getFolder("destinationFolder/" + zipFileName); + IFolder zipFileDestination = ZipFileSystemTestSetup.firstProject + .getFolder("destinationFolder/" + ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); ensureDoesNotExist(zipFileDestination); openedZipFile.move(zipFileDestination.getFullPath(), false, getMonitor()); ensureExists(zipFileDestination); diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/RenameTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/RenameTest.java index 7cf04161e8b..549645cafe6 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/RenameTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/RenameTest.java @@ -20,8 +20,7 @@ import org.eclipse.core.resources.IFolder; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.api.Test; public class RenameTest { @@ -35,21 +34,22 @@ public void teardown() throws Exception { ZipFileSystemTestSetup.teardown(); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testRenameZipFile(String zipFileName) throws Exception { + @Test + public void testRenameZipFile() throws Exception { // IFolder is renamed by moving with the new path - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); - IFolder renamedOpenZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName + "Renamed"); + IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + IFolder renamedOpenZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME + "Renamed"); openedZipFile.move(renamedOpenZipFile.getFullPath(), false, getMonitor()); ensureExists(renamedOpenZipFile); ensureDoesNotExist(openedZipFile); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testRenameFileInsideOfZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); + @Test + public void testRenameFileInsideOfZipFile() throws Exception { + IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); IFile textFile = openedZipFile.getFile(ZipFileSystemTestSetup.TEXT_FILE_NAME); IFile renamedTextFile = openedZipFile.getFile(textFile.getName() + "Renamed"); textFile.move(renamedTextFile.getFullPath(), false, getMonitor()); @@ -57,10 +57,10 @@ public void testRenameFileInsideOfZipFile(String zipFileName) throws Exception { ensureDoesNotExist(textFile); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testRenameFolderInsideOfZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); + @Test + public void testRenameFolderInsideOfZipFile() throws Exception { + IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); IFolder folder = openedZipFile.getFolder("newFolder"); ensureDoesNotExist(folder); folder.create(false, true, getMonitor()); diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/SetupTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/SetupTest.java index e226cc6bd7f..f6a52f71c65 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/SetupTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/SetupTest.java @@ -19,8 +19,7 @@ import org.eclipse.core.resources.IFolder; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.api.Test; public class SetupTest { @@ -34,19 +33,17 @@ public void teardown() throws Exception { ZipFileSystemTestSetup.teardown(); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testZipFileInProject(String zipFileName) throws Exception { + @Test + public void testZipFileInProject() throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(zipFileName); + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); ensureExists(openedZipFile); } - @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") - public void testTextFileInZipFile(String zipFileName) throws Exception { + @Test + public void testTextFileInZipFile() throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(zipFileName); + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); IFile textFile = openedZipFile.getFile(ZipFileSystemTestSetup.TEXT_FILE_NAME); ensureExists(textFile); diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestUtil.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestUtil.java index 07705f0a381..6d58838ff27 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestUtil.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestUtil.java @@ -18,7 +18,6 @@ import java.io.IOException; import java.io.InputStreamReader; import java.net.URISyntaxException; -import java.util.stream.Stream; import org.eclipse.core.filesystem.EFS; import org.eclipse.core.filesystem.IFileInfo; import org.eclipse.core.filesystem.IFileStore; @@ -34,10 +33,6 @@ class ZipFileSystemTestUtil { - public static Stream zipFileNames() { - return Stream.of(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); - } - static void ensureExists(IResource resource) throws CoreException, IOException { switch (resource.getType()) { case IResource.FILE: { From c8c3666a38ddc0be0740cda99eacec0c8dceb4cb Mon Sep 17 00:00:00 2001 From: CodeLtDave Date: Tue, 11 Jun 2024 22:17:22 +0200 Subject: [PATCH 04/35] make static refactorings in ZipFileStore --- .../eclipse/core/internal/filesystem/zip/ZipFileStore.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java index 9fe5829f781..05321fb66ec 100644 --- a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java +++ b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java @@ -141,7 +141,7 @@ public String[] childNames(int options, IProgressMonitor monitor) throws CoreExc /** * Computes the simple file name for a given zip entry. */ - private String computeName(ZipEntry entry) { + private static String computeName(ZipEntry entry) { String name = entry.getName(); // removes "/" at the end if (name.endsWith("/")) { //$NON-NLS-1$ @@ -174,7 +174,7 @@ private IFileInfo convertToIFileInfo(Path zipEntryPath, BasicFileAttributes attr * @param entry the zip entry * @return The file info for a zip entry */ - private IFileInfo convertZipEntryToFileInfo(ZipEntry entry) { + private static IFileInfo convertZipEntryToFileInfo(ZipEntry entry) { FileInfo info = new FileInfo(computeName(entry)); if (entry.isDirectory()) { info.setLastModified(EFS.NONE); From 2b6bbb85c854705bfc24f7d946d0c7d90e75236b Mon Sep 17 00:00:00 2001 From: CodeLtDave Date: Tue, 11 Jun 2024 22:23:52 +0200 Subject: [PATCH 05/35] make use of getPluginID() --- .../core/internal/filesystem/zip/ZipFileStore.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java index 05321fb66ec..ce2c37a1111 100644 --- a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java +++ b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java @@ -254,7 +254,7 @@ public void delete(int options, IProgressMonitor monitor) throws CoreException { deleteRecursive(toDelete); } } catch (IOException | URISyntaxException e) { - throw new CoreException(new Status(IStatus.ERROR, "org.eclipse.core.filesystem.zip", "Error deleting file from zip: " + toDelete, e)); //$NON-NLS-1$ //$NON-NLS-2$ + throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Error deleting file from zip: " + toDelete, e)); //$NON-NLS-1$ } } @@ -344,7 +344,7 @@ public IFileStore mkdir(int options, IProgressMonitor monitor) throws CoreExcept try { zipUri = new URI("jar:" + rootStore.toURI().toString() + "!/"); //$NON-NLS-1$ //$NON-NLS-2$ } catch (URISyntaxException e) { - throw new CoreException(new Status(IStatus.ERROR, "org.eclipse.core.filesystem.zip", "Invalid ZIP file URI", e)); //$NON-NLS-1$ //$NON-NLS-2$ + throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Invalid ZIP file URI", e)); //$NON-NLS-1$ } Map env = new HashMap<>(); @@ -368,7 +368,7 @@ public IFileStore mkdir(int options, IProgressMonitor monitor) throws CoreExcept Files.delete(tempFileInDir); } } catch (IOException e) { - throw new CoreException(new Status(IStatus.ERROR, "org.eclipse.core.filesystem.zip", "Error creating directory in ZIP file", e)); //$NON-NLS-1$ //$NON-NLS-2$ + throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Error creating directory in ZIP file", e)); //$NON-NLS-1$ } // Return a file store representing the newly created directory. @@ -397,7 +397,7 @@ public void move(IFileStore destination, int options, IProgressMonitor monitor) Files.move(srcPath, destPath, StandardCopyOption.REPLACE_EXISTING); } } catch (IOException | URISyntaxException e) { - throw new CoreException(new Status(IStatus.ERROR, "org.eclipse.core.filesystem.zip", "Error moving entry within ZIP", e)); //$NON-NLS-1$ //$NON-NLS-2$ + throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Error moving entry within ZIP", e)); //$NON-NLS-1$ } } From 828d13953780a5dd25f36d9d63bead821a6ff120 Mon Sep 17 00:00:00 2001 From: CodeLtDave Date: Tue, 11 Jun 2024 22:31:14 +0200 Subject: [PATCH 06/35] remove tempFile creation when folder gets created --- .../core/internal/filesystem/zip/ZipFileStore.java | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java index ce2c37a1111..0f4ba14a5d9 100644 --- a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java +++ b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java @@ -355,17 +355,6 @@ public IFileStore mkdir(int options, IProgressMonitor monitor) throws CoreExcept Path dirInZipPath = zipFs.getPath(this.path.toString()); if (Files.notExists(dirInZipPath)) { Files.createDirectories(dirInZipPath); - - // To ensure the directory is actually added to the ZIP, we - // might need to add a temporary file - // in this directory. This is a workaround and should be used - // with caution. - Path tempFileInDir = dirInZipPath.resolve(".keep"); //$NON-NLS-1$ - Files.createFile(tempFileInDir); - - // Immediately delete the temporary file after creation to just - // keep the directory - Files.delete(tempFileInDir); } } catch (IOException e) { throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Error creating directory in ZIP file", e)); //$NON-NLS-1$ From 33c60118a6b0a14dbb2e31c84579b85199af96bb Mon Sep 17 00:00:00 2001 From: CodeLtDave Date: Tue, 11 Jun 2024 23:05:59 +0200 Subject: [PATCH 07/35] change checkFileForZipHeader with header check to canZipFileBeOpened with nio open check --- .../eclipse/core/filesystem/ZipFileUtil.java | 38 +++++++------------ .../core/resources/ZipFileTransformer.java | 10 +++-- .../core/tests/filesystem/zip/OpenTest.java | 13 +++---- 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/filesystem/ZipFileUtil.java b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/filesystem/ZipFileUtil.java index 4baad731c4c..30dd5fd4118 100644 --- a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/filesystem/ZipFileUtil.java +++ b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/filesystem/ZipFileUtil.java @@ -15,26 +15,17 @@ import java.io.IOException; import java.io.InputStream; import java.net.URI; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.util.Set; +import java.util.zip.ZipInputStream; import org.eclipse.core.internal.filesystem.zip.ZipFileStore; import org.eclipse.core.runtime.CoreException; /** - * Utility class to determine if a file is an archive based on file header information. - * This class checks for known file signatures to identify if a given file is a ZIP archive - * or a format based on ZIP, such as EPUB, JAR, ODF, and OOXML. + * Utility class to determine if a file is ZIP archive. * * @since 1.11 */ public class ZipFileUtil { - // Initializes known archive file signatures from Wikipedia's list of file signatures in the following order: - // 1. Standard ZIP file, 2. Empty archive, 3. Spanned archive - // (https://en.wikipedia.org/wiki/List_of_file_signatures) - private static final Set ARCHIVE_FILE_SIGNATURES = Set.of(0x504B0304, 0x504B0506, 0x504B0708); - /** * Determines if the given {@link IFileStore} represents an open ZIP file. * This can be used to check if operations on a ZIP file should be allowed or handled differently. @@ -84,25 +75,22 @@ public static boolean isNested(URI fileURI) { /** * Checks if the provided {@link InputStream} represents a ZIP archive - * by reading its first four bytes and comparing them against known ZIP file signatures. - * This method throws {@link IOException} if the file signature does not match any known ZIP archive signatures. + * by attempting to open it as a ZIP archive. + * This method throws {@link IOException} if the stream does not represent a valid ZIP archive. * * @param fis The {@link InputStream} of the file to check. - * @throws IOException If the file signature does not match known ZIP archive signatures + * @throws IOException If the stream does not represent a valid ZIP archive * or an I/O error occurs during reading from the stream. */ - public static void checkFileForZipHeader(InputStream fis) throws IOException { - byte[] bytes = new byte[4]; - if (fis.read(bytes) == bytes.length) { - ByteBuffer buffer = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN); - int header = buffer.getInt(); - - if (!ARCHIVE_FILE_SIGNATURES.contains(header)) { - throw new IOException("Invalid archive file signature."); // Throws IOException if header is not recognized //$NON-NLS-1$ + public static void canZipFileBeOpened(InputStream fis) throws IOException { + // Use ZipInputStream to try reading the InputStream as a ZIP file + try (ZipInputStream zipStream = new ZipInputStream(fis)) { + // Attempt to read the first entry from the zip stream + if (zipStream.getNextEntry() == null) { + // If there are no entries, then it might not be a ZIP file or it's empty + throw new IOException(); } - } else { - // Handle the case where not enough bytes are read - throw new IOException("Could not read enough data to check ZIP file header."); //$NON-NLS-1$ + // Successfully reading an entry implies it's likely a valid ZIP file } } } diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java index ef142766592..01fcf43541d 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java @@ -16,6 +16,7 @@ import java.io.InputStream; import java.net.URI; import java.net.URISyntaxException; +import java.util.zip.ZipException; import org.eclipse.core.filesystem.EFS; import org.eclipse.core.filesystem.IFileStore; import org.eclipse.core.filesystem.URIUtil; @@ -77,12 +78,15 @@ public static void closeZipFile(IFolder folder) throws URISyntaxException, CoreE public static void openZipFile(IFile file, boolean backgroundRefresh) throws URISyntaxException, CoreException { try (InputStream fis = file.getContents()) { - ZipFileUtil.checkFileForZipHeader(fis); + ZipFileUtil.canZipFileBeOpened(fis); // Additional operations can continue here if header is correct } catch (IOException e) { - // If the header is incorrect or there's an IO error, handle gracefully + if (e instanceof ZipException && e.getMessage().equals("encrypted ZIP entry not supported")) { //$NON-NLS-1$ + throw new CoreException(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, + "Opening encrypted ZIP files is not supported: " + file.getName(), e)); //$NON-NLS-1$ + } throw new CoreException(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, - "Failed to open ZIP file due to incorrect file header: " + file.getName(), e)); //$NON-NLS-1$ + "The file is either empty or doesn't represent a ZIP file: " + file.getName(), e)); //$NON-NLS-1$ } if (file.isLinked()) { diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/OpenTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/OpenTest.java index 9aa41556312..ad23c53b4ed 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/OpenTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/OpenTest.java @@ -15,7 +15,6 @@ import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.assertTextFileContent; import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.ensureDoesNotExist; import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.ensureExists; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -53,7 +52,8 @@ public void testOpenEmptyZipFile() throws IOException, CoreException, URISyntaxE ZipFileSystemTestUtil.openZipFile(zipFile); } catch (CoreException e) { ensureExists(zipFile); - assertEquals("Zip File has no children", e.getMessage()); + String expectedMessage = "The file is either empty or doesn't represent a ZIP file:"; + assertTrue(e.getMessage().contains(expectedMessage)); } } @@ -133,9 +133,8 @@ public void testOpenFakeZip() { ZipFileSystemTestUtil.openZipFile(fakeZipFile); fail("Expected an IOException due to incorrect file header."); } catch (CoreException e) { - String expectedMessage = "Failed to open ZIP file due to incorrect file header: " - + ZipFileSystemTestSetup.FAKE_ZIP_FILE_NAME; - assertTrue("Expected different error message", e.getMessage().contains(expectedMessage)); + String expectedMessage = "The file is either empty or doesn't represent a ZIP file"; + assertTrue(e.getMessage().contains(expectedMessage)); } catch (Exception e) { fail("Expected a CoreException, but got a different type of exception."); } @@ -153,8 +152,8 @@ public void testOpenPasswordProtectedZip() { ZipFileSystemTestUtil.openZipFile(passwordProtectedZipFile); fail("Expected an IOException due to password protection."); } catch (CoreException e) { - String expectedMessage = "Zip File could not be opened"; - assertTrue("Expected different error message", e.getMessage().contains(expectedMessage)); + String expectedMessage = "Opening encrypted ZIP files is not supported:"; + assertTrue("Expected message: " + expectedMessage, e.getMessage().contains(expectedMessage)); } catch (Exception e) { fail("Expected a CoreException, but got a different type of exception."); } From 891d7268ee43ccef7f7cabc47dafc6b2e8214920 Mon Sep 17 00:00:00 2001 From: CodeLtDave Date: Tue, 11 Jun 2024 23:14:19 +0200 Subject: [PATCH 08/35] extract the ZipEntryFileVisitor from childEntries in its own class --- .../filesystem/zip/ZipEntryFileVisitor.java | 48 +++++++++++++++++++ .../internal/filesystem/zip/ZipFileStore.java | 45 ++--------------- 2 files changed, 53 insertions(+), 40 deletions(-) create mode 100644 resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipEntryFileVisitor.java diff --git a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipEntryFileVisitor.java b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipEntryFileVisitor.java new file mode 100644 index 00000000000..b36fc66f999 --- /dev/null +++ b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipEntryFileVisitor.java @@ -0,0 +1,48 @@ +package org.eclipse.core.internal.filesystem.zip; + +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.ArrayList; +import java.util.List; +import java.util.zip.ZipEntry; + +public class ZipEntryFileVisitor extends SimpleFileVisitor { + private final Path zipRoot; + private final List entryList; + + public ZipEntryFileVisitor(Path zipRoot) { + this.zipRoot = zipRoot; + this.entryList = new ArrayList<>(); + } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { + String entryName = zipRoot.relativize(file).toString(); + if (!Files.isDirectory(file)) { + ZipEntry zipEntry = new ZipEntry(entryName); + zipEntry.setSize(attrs.size()); + zipEntry.setTime(attrs.lastModifiedTime().toMillis()); + zipEntry.setMethod(ZipEntry.DEFLATED); + entryList.add(zipEntry); + } else { + entryList.add(new ZipEntry(entryName + "/")); //$NON-NLS-1$ + } + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { + if (!dir.equals(zipRoot)) { + String dirName = zipRoot.relativize(dir).toString() + "/"; //$NON-NLS-1$ + entryList.add(new ZipEntry(dirName)); + } + return FileVisitResult.CONTINUE; + } + + public List getEntries() { + return entryList; + } +} diff --git a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java index 0f4ba14a5d9..44e1cb8f4b2 100644 --- a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java +++ b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java @@ -20,18 +20,13 @@ import java.nio.file.FileSystem; import java.nio.file.FileSystemAlreadyExistsException; import java.nio.file.FileSystems; -import java.nio.file.FileVisitOption; -import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.SimpleFileVisitor; import java.nio.file.StandardCopyOption; import java.nio.file.StandardOpenOption; import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.FileTime; -import java.util.ArrayList; import java.util.Comparator; -import java.util.EnumSet; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -75,47 +70,17 @@ public ZipFileStore(IFileStore rootStore, IPath path) { } private ZipEntry[] childEntries(IProgressMonitor monitor) throws CoreException { - List entryList = new ArrayList<>(); - String myName = path.toString(); - try (FileSystem zipFs = openZipFileSystem()) { - Path zipRoot = zipFs.getPath(myName); - Files.walkFileTree(zipRoot, EnumSet.noneOf(FileVisitOption.class), 1, new SimpleFileVisitor() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { - String entryName = zipRoot.relativize(file).toString(); - if (!Files.isDirectory(file)) { - // For files, read attributes and create ZipEntry - ZipEntry zipEntry = new ZipEntry(entryName); - zipEntry.setSize(attrs.size()); - zipEntry.setTime(attrs.lastModifiedTime().toMillis()); - // Compressed size is not directly available; method is set based on ZIP standard - zipEntry.setMethod(ZipEntry.DEFLATED); - entryList.add(zipEntry); - } else { - // For directories, simply add them with a trailing slash - entryList.add(new ZipEntry(entryName + "/")); //$NON-NLS-1$ - } - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { - // Include directories only if they are not the root directory - if (!dir.equals(zipRoot)) { - String dirName = zipRoot.relativize(dir).toString() + "/"; //$NON-NLS-1$ - entryList.add(new ZipEntry(dirName)); - } - return FileVisitResult.CONTINUE; - } - }); + Path zipRoot = zipFs.getPath(path.toString()); + ZipEntryFileVisitor visitor = new ZipEntryFileVisitor(zipRoot); + Files.walkFileTree(zipRoot, visitor); + return visitor.getEntries().toArray(new ZipEntry[0]); } catch (IOException | URISyntaxException e) { throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Error reading ZIP file", e)); //$NON-NLS-1$ } - - return entryList.toArray(new ZipEntry[0]); } + @Override public IFileInfo[] childInfos(int options, IProgressMonitor monitor) throws CoreException { ZipEntry[] entries = childEntries(monitor); From 86bce294a2e414ad86bb9868b7c014d84ce27230 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 12 Jun 2024 13:57:48 +0200 Subject: [PATCH 09/35] fix bug sub folders appearing on top level --- .../core/internal/filesystem/zip/ZipEntryFileVisitor.java | 1 + .../src/org/eclipse/core/tests/filesystem/zip/OpenTest.java | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipEntryFileVisitor.java b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipEntryFileVisitor.java index b36fc66f999..2211352acd0 100644 --- a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipEntryFileVisitor.java +++ b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipEntryFileVisitor.java @@ -38,6 +38,7 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { if (!dir.equals(zipRoot)) { String dirName = zipRoot.relativize(dir).toString() + "/"; //$NON-NLS-1$ entryList.add(new ZipEntry(dirName)); + return FileVisitResult.SKIP_SUBTREE; // Skip the subdirectories } return FileVisitResult.CONTINUE; } diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/OpenTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/OpenTest.java index ad23c53b4ed..b20f7776bb6 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/OpenTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/OpenTest.java @@ -116,6 +116,10 @@ public void testOpenDeepNestedTextFile() throws IOException, CoreException, URIS ensureExists(nestedFile); assertTextFileContent(nestedFile, "Hello World!"); + String nestedPathShouldFail = "sub2"; + IFolder nestedFileShouldFail = openedNestedZipFileParent.getFolder(nestedPathShouldFail); + ensureDoesNotExist(nestedFileShouldFail); + String deepNestedPath = "sub1/sub2/sub3/sub4/sub5/sub6/sub8/sub9/sub10/Text.txt"; IFile deepNestedFile = openedNestedZipFileParent.getFile(deepNestedPath); ensureExists(deepNestedFile); From b4f4f5c05fb22b5a9e38860d10176219536456fe Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Tue, 18 Jun 2024 17:14:47 +0200 Subject: [PATCH 10/35] New test for copy bug --- .../core/tests/filesystem/zip/CloseTest.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CloseTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CloseTest.java index 663e2bbec5d..31a6bab30d7 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CloseTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CloseTest.java @@ -48,4 +48,31 @@ public void testCloseZipFile() throws Exception { ensureExists(zipFile); } + /* + * Test for a bug that breaks the opened zip file underneath the zip file that + * is closing. The zip file underneath converts to a linked file but the local + * file in the project is deleted so the linked file has no target. + */ + @Test + public void testCloseZipFileWithZipFileUnderneath() throws Exception { + IFolder firstOpenedZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + ensureExists(firstOpenedZipFile); + String secondZipFileName = ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME.replace(".", "New."); + ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.firstProject, secondZipFileName); + IFile secondZipFile = ZipFileSystemTestSetup.firstProject.getFile(secondZipFileName); + ZipFileSystemTestUtil.openZipFile(secondZipFile); + IFolder secondOpenedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(secondZipFileName); + ensureExists(secondOpenedZipFile); + + ZipFileSystemTestUtil.closeZipFile(firstOpenedZipFile); + IFile zipFile = ZipFileSystemTestSetup.firstProject + .getFile(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + // Don't use Utility method ensureDoesNotExist because the fileStore is still + // available after closing. The fileStore is the File itself in the local file + // system that still exists after closing. + assertTrue("folder was not properly deleted: " + firstOpenedZipFile, !firstOpenedZipFile.exists()); + ensureExists(zipFile); + ensureExists(secondOpenedZipFile); + } } From aae77240b50bd7888d9388f400e34aa84a6deef0 Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Mon, 24 Jun 2024 09:37:25 +0200 Subject: [PATCH 11/35] temporary workaround for gender issues temporary workaround for CloseTest#testCloseZipFileWithZipFileUnderneath --- .../eclipse/core/internal/localstore/RefreshLocalVisitor.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/RefreshLocalVisitor.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/RefreshLocalVisitor.java index 0fb70969856..dbaf3ce2c0d 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/RefreshLocalVisitor.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/RefreshLocalVisitor.java @@ -14,6 +14,7 @@ *******************************************************************************/ package org.eclipse.core.internal.localstore; +import org.eclipse.core.filesystem.ZipFileUtil; import org.eclipse.core.internal.resources.Container; import org.eclipse.core.internal.resources.File; import org.eclipse.core.internal.resources.Folder; @@ -247,7 +248,7 @@ protected boolean synchronizeGender(UnifiedTreeNode node, Resource target) throw return false; } } else { - if (!node.isFolder()) { + if (!node.isFolder() && !ZipFileUtil.isOpenZipFile(target.getStore())) { folderToFile(node, target); resourceChanged = true; return false; From f03138bae208bd62ee0de908eed3fe1f3dc0b54b Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Tue, 25 Jun 2024 14:10:27 +0200 Subject: [PATCH 12/35] fix MoveTest --- .../core/tests/filesystem/zip/MoveTest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/MoveTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/MoveTest.java index 445b44e9f2a..56363daebc8 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/MoveTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/MoveTest.java @@ -25,6 +25,7 @@ import org.eclipse.core.runtime.CoreException; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class MoveTest { @@ -39,6 +40,7 @@ public void teardown() throws Exception { ZipFileSystemTestSetup.teardown(); } + @Test public void testMoveZipFileWithinProject() throws CoreException, IOException { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); @@ -53,6 +55,7 @@ public void testMoveZipFileWithinProject() throws CoreException, IOException { ensureDoesNotExist(openedZipFile); } + @Test public void testMoveZipFileToOtherProject() throws CoreException, IOException { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); @@ -65,6 +68,7 @@ public void testMoveZipFileToOtherProject() throws CoreException, IOException { ensureDoesNotExist(openedZipFile); } + @Test public void testMoveZipFileToOtherProjectFolder() throws CoreException, IOException { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); @@ -79,6 +83,7 @@ public void testMoveZipFileToOtherProjectFolder() throws CoreException, IOExcept ensureDoesNotExist(openedZipFile); } + @Test public void testMoveFileIntoZipFile() throws Exception { IFile textFile = ZipFileSystemTestSetup.firstProject.getFile("NewFile.txt"); ensureDoesNotExist(textFile); @@ -95,6 +100,7 @@ public void testMoveFileIntoZipFile() throws Exception { ensureDoesNotExist(textFile); } + @Test public void testMoveFolderIntoZipFile() throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); @@ -112,6 +118,7 @@ public void testMoveFolderIntoZipFile() throws Exception { ensureExists(newFolderDestination); } + @Test public void testMoveFolderWithContentIntoZipFile() throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); @@ -136,6 +143,7 @@ public void testMoveFolderWithContentIntoZipFile() throws Exception { ensureExists(newFolderDestination); } + @Test public void testMoveFileFromZipFile() throws Exception { IFile textFile = ZipFileSystemTestSetup.firstProject .getFile(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME + "/" @@ -148,6 +156,7 @@ public void testMoveFileFromZipFile() throws Exception { ensureDoesNotExist(textFile); } + @Test public void testMoveFolderFromZipFile() throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); @@ -161,6 +170,7 @@ public void testMoveFolderFromZipFile() throws Exception { ensureExists(folderDestination); } + @Test public void testMoveFolderWithContentFromZipFile() throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); @@ -181,6 +191,7 @@ public void testMoveFolderWithContentFromZipFile() throws Exception { ensureExists(folderDestination); } + @Test public void testMoveFolderWithContentFromZipFileIntoOtherZipFile() throws Exception { IFolder firstZipFile = ZipFileSystemTestSetup.firstProject.getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); // create and open second ZipFile @@ -212,6 +223,7 @@ public void testMoveFolderWithContentFromZipFileIntoOtherZipFile() throws Except ensureExists(movedTextFile); } + @Test public void testMoveFileInsideOfZipFile() throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); @@ -228,6 +240,7 @@ public void testMoveFileInsideOfZipFile() throws Exception { ensureDoesNotExist(textFile); } + @Test public void testMoveZipFileIntoZipFile() throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); @@ -254,6 +267,7 @@ public void testMoveZipFileIntoZipFile() throws Exception { * refreshing the Workspace. This test checks if this specific error is handeled * correctly in RefreshLocalVisitor#visit() */ + @Test public void testMoveZipFileWithFolder() throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); From d1bd0799acbdad4e4169b68bc9bbe940108d6715 Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Tue, 25 Jun 2024 14:10:40 +0200 Subject: [PATCH 13/35] new test concurrencyTest --- .../tests/filesystem/AllFileSystemTests.java | 2 + .../tests/filesystem/zip/ConcurrencyTest.java | 98 +++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ConcurrencyTest.java diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/AllFileSystemTests.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/AllFileSystemTests.java index f577a29b017..f7d86c2654e 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/AllFileSystemTests.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/AllFileSystemTests.java @@ -14,6 +14,7 @@ *******************************************************************************/ package org.eclipse.core.tests.filesystem; +import org.eclipse.core.tests.filesystem.zip.ConcurrencyTest; import org.junit.platform.suite.api.SelectClasses; import org.junit.platform.suite.api.Suite; @@ -30,6 +31,7 @@ PutInfoTest.class, // SymlinkTest.class, // URIUtilTest.class, // + ConcurrencyTest.class, // }) public class AllFileSystemTests { } diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ConcurrencyTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ConcurrencyTest.java new file mode 100644 index 00000000000..9d488cce3cb --- /dev/null +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ConcurrencyTest.java @@ -0,0 +1,98 @@ +/******************************************************************************* + * Copyright (c) 2024 Vector Informatik GmbH and others. + * + * This program and the accompanying materials are made available under the terms of the Eclipse + * Public License 2.0 which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: Vector Informatik GmbH - initial API and implementation + *******************************************************************************/ + +package org.eclipse.core.tests.filesystem.zip; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +import java.net.URI; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.atomic.AtomicReference; +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.filesystem.IFileStore; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.runtime.CoreException; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class ConcurrencyTest { + + @BeforeEach + public void setup() throws Exception { + ZipFileSystemTestSetup.defaultSetup(); + } + + @AfterEach + public void teardown() throws Exception { + ZipFileSystemTestSetup.teardown(); + } + + @Test + public void testFetchInfoWithMultipleThreads() throws Exception { + IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + ZipFileSystemTestUtil.ensureExists(openedZipFile); + URI zipFileURI = openedZipFile.getLocationURI(); + IFileStore zipFileStore = EFS.getStore(zipFileURI); + + int totalThreadCount = 10; + CountDownLatch startLatch = new CountDownLatch(1); // Latch to start all threads simultaneously + CountDownLatch doneLatch = new CountDownLatch(totalThreadCount); // Latch to wait for all threads to finish + ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(totalThreadCount); + + // Shared exception reference to propagate exceptions from threads to the main + // thread + AtomicReference exceptionReference = new AtomicReference<>(); + + // Submit tasks to the executor + for (int i = 0; i < totalThreadCount; i++) { + executor.submit(() -> { + try { + startLatch.await(); // Wait for the signal to start + // Perform various tasks on ZipFileStore + for (int j = 0; j <= 10; j++) { + zipFileStore.childInfos(0, ZipFileSystemTestUtil.getMonitor()); + assertTrue(zipFileStore.fetchInfo().exists(), "File system should exist"); + } + } catch (InterruptedException e) { + e.printStackTrace(); + fail("Thread was interrupted"); + Thread.currentThread().interrupt(); + } catch (CoreException e) { + // Propagate CoreException to the main thread + exceptionReference.set(e); + } finally { + doneLatch.countDown(); + } + }); + } + + // Start all threads + startLatch.countDown(); + + // Wait for all tasks to complete + doneLatch.await(); + + executor.shutdown(); + + // Check if any exception was thrown by the threads + if (exceptionReference.get() != null) { + throw exceptionReference.get(); + } + + assertTrue(executor.getCompletedTaskCount() == totalThreadCount, "All tasks should complete successfully"); + } +} From 2f78e29cee637e10b203f6e2d7389734e41b2179 Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Mon, 24 Jun 2024 11:26:31 +0200 Subject: [PATCH 14/35] Introduce Lock mechanism to ZipFileStore This change fixes various problems regarding ClosedZipFileSystemException, NoZipFileSystemFoundException and ZipFileSystemAlreadyExistsException --- .../internal/filesystem/zip/ZipFileStore.java | 76 +++++++++++-------- 1 file changed, 45 insertions(+), 31 deletions(-) diff --git a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java index 44e1cb8f4b2..08cc3118908 100644 --- a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java +++ b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java @@ -18,7 +18,7 @@ import java.net.URI; import java.net.URISyntaxException; import java.nio.file.FileSystem; -import java.nio.file.FileSystemAlreadyExistsException; +import java.nio.file.FileSystemNotFoundException; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; @@ -26,10 +26,12 @@ import java.nio.file.StandardOpenOption; import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.FileTime; +import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.locks.ReentrantLock; import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.zip.ZipEntry; @@ -51,6 +53,7 @@ * @since 1.11 */ public class ZipFileStore extends FileStore { + private static final Map uriLockMap = Collections.synchronizedMap(new HashMap<>()); /** * The path of this store within the zip file. */ @@ -77,6 +80,8 @@ private ZipEntry[] childEntries(IProgressMonitor monitor) throws CoreException { return visitor.getEntries().toArray(new ZipEntry[0]); } catch (IOException | URISyntaxException e) { throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Error reading ZIP file", e)); //$NON-NLS-1$ + } finally { + unlock(); } } @@ -184,6 +189,8 @@ protected void copyDirectory(IFileInfo sourceInfo, IFileStore destination, int o }); } catch (IOException | URISyntaxException e) { throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Error copying directory within ZIP", e)); //$NON-NLS-1$ + } finally { + unlock(); } } @@ -207,6 +214,8 @@ protected void copyFile(IFileInfo sourceInfo, IFileStore destination, int option Files.copy(sourcePath, destPath, StandardCopyOption.REPLACE_EXISTING); } catch (IOException | URISyntaxException e) { throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Error copying file within ZIP", e)); //$NON-NLS-1$ + } finally { + unlock(); } } @@ -220,6 +229,8 @@ public void delete(int options, IProgressMonitor monitor) throws CoreException { } } catch (IOException | URISyntaxException e) { throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Error deleting file from zip: " + toDelete, e)); //$NON-NLS-1$ + } finally { + unlock(); } } @@ -248,6 +259,8 @@ public IFileInfo fetchInfo(int options, IProgressMonitor monitor) throws CoreExc } } catch (IOException | URISyntaxException e) { throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Error accessing ZIP file", e)); //$NON-NLS-1$ + } finally { + unlock(); } // Correctly set up FileInfo before returning @@ -299,30 +312,18 @@ public IPath getPath() { return path; } - private boolean isNested() { - return this.rootStore instanceof ZipFileStore; - } - @Override public IFileStore mkdir(int options, IProgressMonitor monitor) throws CoreException { - URI zipUri; - try { - zipUri = new URI("jar:" + rootStore.toURI().toString() + "!/"); //$NON-NLS-1$ //$NON-NLS-2$ - } catch (URISyntaxException e) { - throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Invalid ZIP file URI", e)); //$NON-NLS-1$ - } - - Map env = new HashMap<>(); - env.put("create", "false"); //$NON-NLS-1$ //$NON-NLS-2$ - // Assuming the directory to create is represented by 'this.path' - try (FileSystem zipFs = FileSystems.newFileSystem(zipUri, env)) { + try (FileSystem zipFs = openZipFileSystem()) { Path dirInZipPath = zipFs.getPath(this.path.toString()); if (Files.notExists(dirInZipPath)) { Files.createDirectories(dirInZipPath); } - } catch (IOException e) { + } catch (IOException | URISyntaxException e) { throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Error creating directory in ZIP file", e)); //$NON-NLS-1$ + } finally { + unlock(); } // Return a file store representing the newly created directory. @@ -352,6 +353,8 @@ public void move(IFileStore destination, int options, IProgressMonitor monitor) } } catch (IOException | URISyntaxException e) { throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Error moving entry within ZIP", e)); //$NON-NLS-1$ + } finally { + unlock(); } } @@ -424,31 +427,31 @@ public void close() throws IOException { } catch (Exception e) { throw new IOException("Failed to integrate data into ZIP file", e); //$NON-NLS-1$ + } finally { + try { + unlock(); + } catch (CoreException e) { + throw new IOException("Error accessing ZIP file", e); //$NON-NLS-1$ + } } } }; } + private static ReentrantLock getLockForURI(URI uri) { + return uriLockMap.computeIfAbsent(uri, k -> new ReentrantLock()); + } + private FileSystem openZipFileSystem() throws URISyntaxException, IOException { Map env = new HashMap<>(); env.put("create", "false"); //$NON-NLS-1$ //$NON-NLS-2$ URI nioURI = toNioURI(); - Path innerArchivePath = null; - - if (isNested()) { - ZipFileStore outerZipFileStore = (ZipFileStore) this.rootStore; - FileSystem outerFs = outerZipFileStore.openZipFileSystem(); - innerArchivePath = outerFs.getPath(outerZipFileStore.path.toString()); - nioURI = innerArchivePath.toUri(); - } - + ReentrantLock lock = getLockForURI(nioURI); + lock.lock(); try { - if (innerArchivePath != null) { - return FileSystems.newFileSystem(innerArchivePath, env); - } - return FileSystems.newFileSystem(nioURI, env); - } catch (FileSystemAlreadyExistsException e) { return FileSystems.getFileSystem(nioURI); + } catch (FileSystemNotFoundException e) { + return FileSystems.newFileSystem(nioURI, env); } } @@ -477,6 +480,7 @@ public void putInfo(IFileInfo info, int options, IProgressMonitor monitor) throw } catch (Exception e) { throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Error updating ZIP file entry information", e)); //$NON-NLS-1$ } finally { + unlock(); if (monitor != null) { monitor.done(); } @@ -508,4 +512,14 @@ private URI toNioURI() throws URISyntaxException { return new URI(ret); } + void unlock() throws CoreException { + try { + ReentrantLock lock = getLockForURI(toNioURI()); + if (lock.isHeldByCurrentThread()) { + lock.unlock(); + } + } catch (URISyntaxException e) { + throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Error accessing ZIP file", e)); //$NON-NLS-1$ + } + } } From 1c30966ab592a75494aaaf1ba3a68756004868b1 Mon Sep 17 00:00:00 2001 From: David Date: Mon, 8 Jul 2024 18:31:00 +0200 Subject: [PATCH 15/35] introduce Virtual Zip Folder --- .../.project | 22 ++++ .../core/internal/resources/ResourceTree.java | 106 +++++++---------- .../internal/resources/VirtualZipFolder.java | 109 ++++++++++++++++++ .../core/internal/resources/Workspace.java | 7 +- 4 files changed, 181 insertions(+), 63 deletions(-) create mode 100644 resources/bundles/org.eclipse.core.resources.win32.x86_64/.project create mode 100644 resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/VirtualZipFolder.java diff --git a/resources/bundles/org.eclipse.core.resources.win32.x86_64/.project b/resources/bundles/org.eclipse.core.resources.win32.x86_64/.project new file mode 100644 index 00000000000..fe2c03b97cf --- /dev/null +++ b/resources/bundles/org.eclipse.core.resources.win32.x86_64/.project @@ -0,0 +1,22 @@ + + + org.eclipse.core.resources.win32.x86_64 + + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + + org.eclipse.pde.PluginNature + + diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ResourceTree.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ResourceTree.java index 29d1c4d0856..9e4da89e21c 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ResourceTree.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ResourceTree.java @@ -15,15 +15,34 @@ package org.eclipse.core.internal.resources; import java.net.URI; -import org.eclipse.core.filesystem.*; -import java.net.URISyntaxException; +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.filesystem.IFileInfo; +import org.eclipse.core.filesystem.IFileStore; +import org.eclipse.core.filesystem.IFileSystem; import org.eclipse.core.filesystem.URIUtil; import org.eclipse.core.internal.localstore.FileSystemResourceManager; import org.eclipse.core.internal.properties.IPropertyManager; -import org.eclipse.core.internal.utils.*; -import org.eclipse.core.resources.*; +import org.eclipse.core.internal.utils.BitMask; +import org.eclipse.core.internal.utils.Messages; +import org.eclipse.core.internal.utils.Policy; +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IProjectDescription; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IResourceStatus; +import org.eclipse.core.resources.IResourceVisitor; +import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.resources.team.IResourceTree; -import org.eclipse.core.runtime.*; +import org.eclipse.core.runtime.Assert; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.MultiStatus; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.core.runtime.jobs.ILock; import org.eclipse.osgi.util.NLS; @@ -346,49 +365,29 @@ private boolean internalDeleteFolder(IFolder folder, int flags, IProgressMonitor if (!folder.exists()) return true; - IFileStore fileStore = localManager.getStore(folder); - if (ZipFileUtil.isOpenZipFile(fileStore)) { - try { - // folder is opened zip file - deletedFolder(folder); - // if the IResource.CLOSE_ZIP_FILE flag is set, the file should not be deleted - // in the file system. - if ((flags & (IResource.CLOSE_ZIP_FILE)) != 0) { - return true; - } - IFile file = folder.getParent().getFile(IPath.fromOSString(folder.getName())); - IFileStore parentStore = localManager.getStore(file); - parentStore.delete(EFS.NONE, Policy.subMonitorFor(monitor, Policy.totalWork / 4)); - return true; - } catch (CoreException e) { - failed(e.getStatus()); - } - } else { - // Don't delete contents if this is a linked resource - if (folder.isLinked()) { - deletedFolder(folder); - return true; - } - - // If the folder doesn't exist on disk then update the tree and return. - - if (!fileStore.fetchInfo().exists()) { - deletedFolder(folder); - return true; - } + // Don't delete contents if this is a linked resource + if (folder.isLinked()) { + deletedFolder(folder); + return true; + } - try { - // this will delete local and workspace - localManager.delete(folder, flags, Policy.subMonitorFor(monitor, Policy.totalWork)); - } catch (CoreException ce) { - message = NLS.bind(Messages.localstore_couldnotDelete, folder.getFullPath()); - IStatus status = new ResourceStatus(IStatus.ERROR, IResourceStatus.FAILED_DELETE_LOCAL, - folder.getFullPath(), message, ce); - failed(status); - return false; - } + // If the folder doesn't exist on disk then update the tree and return. + IFileStore fileStore = localManager.getStore(folder); + if (!fileStore.fetchInfo().exists()) { + deletedFolder(folder); return true; } + + try { + // this will delete local and workspace + localManager.delete(folder, flags, Policy.subMonitorFor(monitor, Policy.totalWork)); + } catch (CoreException ce) { + message = NLS.bind(Messages.localstore_couldnotDelete, folder.getFullPath()); + IStatus status = new ResourceStatus(IStatus.ERROR, IResourceStatus.FAILED_DELETE_LOCAL, + folder.getFullPath(), message, ce); + failed(status); + return false; + } return true; } @@ -1010,23 +1009,6 @@ public void standardMoveFolder(IFolder source, IFolder destination, int flags, I if (!source.exists() || destination.exists() || !destination.getParent().isAccessible()) throw new IllegalArgumentException(); - if (ZipFileUtil.isOpenZipFile(source.getLocationURI())) { - try { - ZipFileTransformer.closeZipFile(source); - IFile newSource = source.getParent().getFile(IPath.fromOSString(source.getName())); - IFile newDestination = destination.getParent().getFile(IPath.fromOSString(destination.getName())); - newSource.move(newDestination.getFullPath(), false, null); - if (!ZipFileUtil.isNested(destination.getLocationURI())) { - ZipFileTransformer.openZipFile(newDestination, false); - } - return; - } catch (URISyntaxException | CoreException e) { - message = NLS.bind(Messages.localstore_couldNotMove, source); - IStatus status = new ResourceStatus(IStatus.ERROR, source.getFullPath(), message, e); - failed(status); - } - } - // Check to see if we are synchronized with the local file system. If we are in // sync then we can // short circuit this method and do a file system only move. Otherwise we have diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/VirtualZipFolder.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/VirtualZipFolder.java new file mode 100644 index 00000000000..e318619ae6a --- /dev/null +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/VirtualZipFolder.java @@ -0,0 +1,109 @@ +/******************************************************************************* + * Copyright (c) 2024 IBM Corporation and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.core.internal.resources; + +import java.net.URISyntaxException; +import org.eclipse.core.filesystem.ZipFileUtil; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.resources.ZipFileTransformer; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Status; + +/** + * + */ +public class VirtualZipFolder extends Folder { + + public VirtualZipFolder(IPath path, Workspace container) { + super(path, container); + } + + @Override + public void copy(IPath destination, int updateFlags, IProgressMonitor monitor) throws CoreException { + try { + // Close the current ZIP file + ZipFileTransformer.closeZipFile(this); + + // Get the file representing the closed ZIP + IFile closedZipFile = this.getParent().getFile(new Path(this.getName())); + + // Copy the closed ZIP file to the destination + closedZipFile.copy(destination, updateFlags, monitor); + + // Get the copied ZIP file at the new destination + IFile copiedZipFile = ResourcesPlugin.getWorkspace().getRoot().getFile(destination); + + // If the destination is not a nested ZIP, open the copied ZIP file + if (!ZipFileUtil.isInsideOpenZipFile(copiedZipFile.getLocationURI())) { + ZipFileTransformer.openZipFile(copiedZipFile, false); + } + + // Reopen the original ZIP file + if (!ZipFileUtil.isInsideOpenZipFile(closedZipFile.getLocationURI())) { + ZipFileTransformer.openZipFile(closedZipFile, false); + } + } catch (URISyntaxException e) { + throw new CoreException( + new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, "Error copying ZIP file", e)); //$NON-NLS-1$ + } + } + + @Override + public void delete(int updateFlags, IProgressMonitor monitor) throws CoreException { + try { + if ((updateFlags & IResource.CLOSE_ZIP_FILE) != 0) { + super.delete(updateFlags, monitor); + return; + } + + ZipFileTransformer.closeZipFile(this); + IFile closedZipFile = this.getParent().getFile(IPath.fromOSString(this.getName())); + closedZipFile.delete(updateFlags, monitor); + } catch (URISyntaxException e) { + throw new CoreException( + new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, "Error deleting ZIP file", e)); //$NON-NLS-1$ + } + } + + @Override + public void move(IPath destination, int updateFlags, IProgressMonitor monitor) throws CoreException { + try { + // Close the current ZIP file + ZipFileTransformer.closeZipFile(this); + + // Get the file representing the closed ZIP + IFile closedZipFile = this.getParent().getFile(new Path(this.getName())); + + // Move the closed ZIP file to the destination + closedZipFile.move(destination, updateFlags, monitor); + + // Get the moved ZIP file at the new destination + IFile movedZipFile = ResourcesPlugin.getWorkspace().getRoot().getFile(destination); + + // If the destination is not a nested ZIP, open the moved ZIP file + if (!ZipFileUtil.isInsideOpenZipFile(movedZipFile.getLocationURI())) { + ZipFileTransformer.openZipFile(movedZipFile, false); + } + } catch (URISyntaxException e) { + throw new CoreException( + new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, "Error moving ZIP file", e)); //$NON-NLS-1$ + } + } +} diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/Workspace.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/Workspace.java index f5a7d456616..75444de2789 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/Workspace.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/Workspace.java @@ -52,6 +52,7 @@ import org.eclipse.core.filesystem.EFS; import org.eclipse.core.filesystem.IFileStore; import org.eclipse.core.filesystem.URIUtil; +import org.eclipse.core.filesystem.ZipFileUtil; import org.eclipse.core.internal.events.BuildManager; import org.eclipse.core.internal.events.ILifecycleListener; import org.eclipse.core.internal.events.LifecycleEvent; @@ -2229,7 +2230,11 @@ public Resource newResource(IPath path, int type) { message = "Path must include project and resource name: " + path.toString(); //$NON-NLS-1$ Assert.isLegal(false, message); } - return new Folder(path.makeAbsolute(), this); + Folder folder = new Folder(path.makeAbsolute(), this); + if (ZipFileUtil.isOpenZipFile(folder.getStore())) { + return new VirtualZipFolder(path.makeAbsolute(), this); + } + return folder; case IResource.FILE : if (path.segmentCount() < ICoreConstants.MINIMUM_FILE_SEGMENT_LENGTH) { message = "Path must include project and resource name: " + path.toString(); //$NON-NLS-1$ From 10b8ac93666982a8336ba26fa04676db1eb3a0a0 Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Mon, 29 Jul 2024 09:13:37 +0200 Subject: [PATCH 16/35] fix deadlock --- .../eclipse/core/internal/filesystem/zip/ZipFileStore.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java index 08cc3118908..5c57113321a 100644 --- a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java +++ b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java @@ -447,7 +447,9 @@ private FileSystem openZipFileSystem() throws URISyntaxException, IOException { env.put("create", "false"); //$NON-NLS-1$ //$NON-NLS-2$ URI nioURI = toNioURI(); ReentrantLock lock = getLockForURI(nioURI); - lock.lock(); + if (!lock.isHeldByCurrentThread()) { + lock.lock(); + } try { return FileSystems.getFileSystem(nioURI); } catch (FileSystemNotFoundException e) { From e260557b99f5ff0f4aaf9ade686898677f256054 Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Tue, 30 Jul 2024 08:46:16 +0200 Subject: [PATCH 17/35] delete implicit line The default value of create is false --- .../org/eclipse/core/internal/filesystem/zip/ZipFileStore.java | 1 - 1 file changed, 1 deletion(-) diff --git a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java index 5c57113321a..cfb5b7ef6d4 100644 --- a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java +++ b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java @@ -444,7 +444,6 @@ private static ReentrantLock getLockForURI(URI uri) { private FileSystem openZipFileSystem() throws URISyntaxException, IOException { Map env = new HashMap<>(); - env.put("create", "false"); //$NON-NLS-1$ //$NON-NLS-2$ URI nioURI = toNioURI(); ReentrantLock lock = getLockForURI(nioURI); if (!lock.isHeldByCurrentThread()) { From c632ff5cf113e7506fa2ddb54bed7528e853c20a Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Wed, 31 Jul 2024 08:46:37 +0200 Subject: [PATCH 18/35] Add parametrization to test .jar and .war files --- .../resources/ZipFileSystem/BasicText.jar | Bin 0 -> 442 bytes .../resources/ZipFileSystem/BasicText.war | Bin 0 -> 442 bytes .../resources/ZipFileSystem/BasicTextNew.jar | Bin 0 -> 442 bytes .../resources/ZipFileSystem/BasicTextNew.war | Bin 0 -> 442 bytes .../core/tests/filesystem/zip/CloseTest.java | 23 ++-- .../core/tests/filesystem/zip/CopyTest.java | 61 ++++++---- .../core/tests/filesystem/zip/CreateTest.java | 17 +-- .../core/tests/filesystem/zip/DeleteTest.java | 33 ++--- .../core/tests/filesystem/zip/MoveTest.java | 114 ++++++++++-------- .../core/tests/filesystem/zip/RenameTest.java | 26 ++-- .../core/tests/filesystem/zip/SetupTest.java | 17 +-- .../zip/ZipFileSystemTestSetup.java | 5 +- .../filesystem/zip/ZipFileSystemTestUtil.java | 7 ++ 13 files changed, 177 insertions(+), 126 deletions(-) create mode 100644 resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicText.jar create mode 100644 resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicText.war create mode 100644 resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicTextNew.jar create mode 100644 resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicTextNew.war diff --git a/resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicText.jar b/resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicText.jar new file mode 100644 index 0000000000000000000000000000000000000000..b35136e36b29492cca7afe175bacefc17f860cae GIT binary patch literal 442 zcmWIWW@h1HVBlb2P<8zu!GHuf8CV#6T|*poJ^kGD|D9rB2mmS-Vc_84z)&gz)CO1T z>*(j{<{BKL=j-;__snS@Z(Y5MyxzK6=gyqp9At3C_`%a6JuhD!Pv48BtF{CgFm=6< z)XcDWrWW?}`AgO3&$TL^slLwqRP?ck5ojYj$E$b0&rhJ0)UZ8gaHu* y$W8+V0Rm_PnQ*O00TSSiFahKRWQT#gfB*(j{<{BKL=j-;__snS@Z(Y5MyxzK6=gyqp9At3C_`%a6JuhD!Pv48BtF{CgFm=6< z)XcDWrWW?}`AgO3&$TL^slLwqRP?ck5ojYj$E$b0&rhJ0)UZ8gaHu* y$W8+V0Rm_PnQ*O00TSSiFahKRWQT#gfB<) literal 0 HcmV?d00001 diff --git a/resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicTextNew.jar b/resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicTextNew.jar new file mode 100644 index 0000000000000000000000000000000000000000..b35136e36b29492cca7afe175bacefc17f860cae GIT binary patch literal 442 zcmWIWW@h1HVBlb2P<8zu!GHuf8CV#6T|*poJ^kGD|D9rB2mmS-Vc_84z)&gz)CO1T z>*(j{<{BKL=j-;__snS@Z(Y5MyxzK6=gyqp9At3C_`%a6JuhD!Pv48BtF{CgFm=6< z)XcDWrWW?}`AgO3&$TL^slLwqRP?ck5ojYj$E$b0&rhJ0)UZ8gaHu* y$W8+V0Rm_PnQ*O00TSSiFahKRWQT#gfB*(j{<{BKL=j-;__snS@Z(Y5MyxzK6=gyqp9At3C_`%a6JuhD!Pv48BtF{CgFm=6< z)XcDWrWW?}`AgO3&$TL^slLwqRP?ck5ojYj$E$b0&rhJ0)UZ8gaHu* y$W8+V0Rm_PnQ*O00TSSiFahKRWQT#gfB<) literal 0 HcmV?d00001 diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CloseTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CloseTest.java index 31a6bab30d7..580de52b9dc 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CloseTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CloseTest.java @@ -19,7 +19,8 @@ import org.eclipse.core.resources.IFolder; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; public class CloseTest { @@ -33,14 +34,15 @@ public void teardown() throws Exception { ZipFileSystemTestSetup.teardown(); } - @Test - public void testCloseZipFile() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testCloseZipFile(String zipFileName) throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); ensureExists(openedZipFile); ZipFileSystemTestUtil.closeZipFile(openedZipFile); IFile zipFile = ZipFileSystemTestSetup.firstProject - .getFile(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFile(zipFileName); // Don't use Utility method ensureDoesNotExist because the fileStore is still // available after closing. The fileStore is the File itself in the local file // system that still exists after closing. @@ -53,12 +55,13 @@ public void testCloseZipFile() throws Exception { * is closing. The zip file underneath converts to a linked file but the local * file in the project is deleted so the linked file has no target. */ - @Test - public void testCloseZipFileWithZipFileUnderneath() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testCloseZipFileWithZipFileUnderneath(String zipFileName) throws Exception { IFolder firstOpenedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); ensureExists(firstOpenedZipFile); - String secondZipFileName = ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME.replace(".", "New."); + String secondZipFileName = zipFileName.replace(".", "New."); ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.firstProject, secondZipFileName); IFile secondZipFile = ZipFileSystemTestSetup.firstProject.getFile(secondZipFileName); ZipFileSystemTestUtil.openZipFile(secondZipFile); @@ -67,7 +70,7 @@ public void testCloseZipFileWithZipFileUnderneath() throws Exception { ZipFileSystemTestUtil.closeZipFile(firstOpenedZipFile); IFile zipFile = ZipFileSystemTestSetup.firstProject - .getFile(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFile(zipFileName); // Don't use Utility method ensureDoesNotExist because the fileStore is still // available after closing. The fileStore is the File itself in the local file // system that still exists after closing. diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CopyTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CopyTest.java index 1bb294363a8..cabee325724 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CopyTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CopyTest.java @@ -23,7 +23,8 @@ import org.eclipse.core.resources.IFolder; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** * @@ -40,25 +41,27 @@ public void teardown() throws Exception { ZipFileSystemTestSetup.teardown(); } - @Test - public void testCopyZipFile() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testCopyZipFile(String zipFileName) throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); ensureExists(openedZipFile); IFolder destinationFolder = ZipFileSystemTestSetup.firstProject.getFolder("Folder"); destinationFolder.create(true, true, getMonitor()); ensureExists(destinationFolder); IFolder copyDestination = ZipFileSystemTestSetup.firstProject - .getFolder("Folder" + "/" + ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder("Folder" + "/" + zipFileName); openedZipFile.copy(copyDestination.getFullPath(), true, getMonitor()); ensureExists(copyDestination); ensureExists(openedZipFile); } - @Test - public void testCopyFileInsideOfZipFile() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testCopyFileInsideOfZipFile(String zipFileName) throws Exception { IFile textFile = ZipFileSystemTestSetup.firstProject.getFile( - ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME + "/" + ZipFileSystemTestSetup.TEXT_FILE_NAME); + zipFileName + "/" + ZipFileSystemTestSetup.TEXT_FILE_NAME); ensureExists(textFile); IFolder destinationFolder = ZipFileSystemTestSetup.firstProject.getFolder("Folder"); destinationFolder.create(true, true, getMonitor()); @@ -70,10 +73,11 @@ public void testCopyFileInsideOfZipFile() throws Exception { ensureExists(textFile); } - @Test - public void testCopyFolderInsideOfZipFile() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testCopyFolderInsideOfZipFile(String zipFileName) throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); ensureExists(openedZipFile); IFolder newFolder = ZipFileSystemTestSetup.firstProject.getFolder("NewFolder"); ensureDoesNotExist(newFolder); @@ -94,8 +98,9 @@ public void testCopyFolderInsideOfZipFile() throws Exception { assertTextFileContent(textFile, "Foo"); } - @Test - public void testCopyFileIntoZipFile() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testCopyFileIntoZipFile(String zipFileName) throws Exception { IFile textFile = ZipFileSystemTestSetup.firstProject.getFile("NewFile.txt"); ensureDoesNotExist(textFile); String text = "Foo"; @@ -104,15 +109,16 @@ public void testCopyFileIntoZipFile() throws Exception { stream.close(); ensureExists(textFile); IFile copyDestination = ZipFileSystemTestSetup.firstProject - .getFile(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME + "/" + "NewFile.txt"); + .getFile(zipFileName + "/" + "NewFile.txt"); textFile.copy(copyDestination.getFullPath(), true, getMonitor()); ensureExists(copyDestination); ensureExists(textFile); assertTextFileContent(textFile, "Foo"); } - @Test - public void testCopyFolderIntoZipFile() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testCopyFolderIntoZipFile(String zipFileName) throws Exception { IFile textFile = ZipFileSystemTestSetup.firstProject.getFile("NewFile.txt"); ensureDoesNotExist(textFile); String text = "Foo"; @@ -121,17 +127,18 @@ public void testCopyFolderIntoZipFile() throws Exception { stream.close(); ensureExists(textFile); IFile copyDestination = ZipFileSystemTestSetup.firstProject - .getFile(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME + "/" + "NewFile.txt"); + .getFile(zipFileName + "/" + "NewFile.txt"); textFile.copy(copyDestination.getFullPath(), true, getMonitor()); ensureExists(copyDestination); ensureExists(textFile); assertTextFileContent(textFile, "Foo"); } - @Test - public void testCopyFileFromOutsideOfZipFIleIntoFolderInZipFile() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testCopyFileFromOutsideOfZipFIleIntoFolderInZipFile(String zipFileName) throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); IFolder newFolder = openedZipFile.getFolder("NewFolder"); ensureDoesNotExist(newFolder); newFolder.create(false, true, getMonitor()); @@ -151,10 +158,11 @@ public void testCopyFileFromOutsideOfZipFIleIntoFolderInZipFile() throws Excepti assertTextFileContent(textFile, "Foo"); } - @Test - public void testCopyFolderIntoFolderInZipFile() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testCopyFolderIntoFolderInZipFile(String zipFileName) throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); IFolder firstNewFolder = openedZipFile.getFolder("FirstNewFolder"); ensureDoesNotExist(firstNewFolder); firstNewFolder.create(false, true, getMonitor()); @@ -178,10 +186,11 @@ public void testCopyFolderIntoFolderInZipFile() throws Exception { assertTextFileContent(textFile, "Foo"); } - @Test - public void testCopyFileFromOneFolderToOtherFolderInsideofZipFile() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testCopyFileFromOneFolderToOtherFolderInsideofZipFile(String zipFileName) throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); IFolder firstNewFolder = openedZipFile.getFolder("FirstNewFolder"); ensureDoesNotExist(firstNewFolder); firstNewFolder.create(false, true, getMonitor()); diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CreateTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CreateTest.java index d6488dfb534..ade07691430 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CreateTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CreateTest.java @@ -23,7 +23,8 @@ import org.eclipse.core.resources.IFolder; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; public class CreateTest { @@ -37,10 +38,11 @@ public void teardown() throws Exception { ZipFileSystemTestSetup.teardown(); } - @Test - public void testCreateFileInsideOfZipFile() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testCreateFileInsideOfZipFile(String zipFileName) throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); IFile textFile = openedZipFile.getFile("NewFile.txt"); ensureDoesNotExist(textFile); String text = "Foo"; @@ -51,10 +53,11 @@ public void testCreateFileInsideOfZipFile() throws Exception { assertTextFileContent(textFile, "Foo"); } - @Test - public void testCreateFolderInsideOfZipFile() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testCreateFolderInsideOfZipFile(String zipFileName) throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); IFolder newFolder = openedZipFile.getFolder("NewFolder"); ensureDoesNotExist(newFolder); newFolder.create(false, true, getMonitor()); diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/DeleteTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/DeleteTest.java index cb1d2b0a4bc..b4c72c726a0 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/DeleteTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/DeleteTest.java @@ -23,7 +23,8 @@ import org.eclipse.core.runtime.CoreException; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; public class DeleteTest { @@ -37,32 +38,35 @@ public void teardown() throws Exception { ZipFileSystemTestSetup.teardown(); } - @Test - public void testDeleteZipFile() throws CoreException, IOException { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testDeleteZipFile(String zipFileName) throws CoreException, IOException { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); ensureExists(openedZipFile); openedZipFile.delete(false, false, getMonitor()); ensureDoesNotExist(openedZipFile); IFile zipFile = ZipFileSystemTestSetup.firstProject - .getFile(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFile(zipFileName); ensureDoesNotExist(zipFile); } - @Test - public void testDeleteFileInsideOfZipFile() throws CoreException, IOException { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testDeleteFileInsideOfZipFile(String zipFileName) throws CoreException, IOException { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); IFile textFile = openedZipFile.getFile(ZipFileSystemTestSetup.TEXT_FILE_NAME); ensureExists(textFile); textFile.delete(true, getMonitor()); ensureDoesNotExist(textFile); } - @Test - public void testDeleteEmptyFolder() throws CoreException, IOException { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testDeleteEmptyFolder(String zipFileName) throws CoreException, IOException { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); IFolder folder = openedZipFile.getFolder("FolderToDelete"); ensureDoesNotExist(folder); folder.create(true, true, getMonitor()); @@ -71,10 +75,11 @@ public void testDeleteEmptyFolder() throws CoreException, IOException { ensureDoesNotExist(folder); } - @Test - public void testDeleteFolderWithChildren() throws CoreException, IOException { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testDeleteFolderWithChildren(String zipFileName) throws CoreException, IOException { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); IFolder folder = openedZipFile.getFolder("FolderToDelete"); ensureDoesNotExist(folder); folder.create(true, true, getMonitor()); diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/MoveTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/MoveTest.java index 56363daebc8..0425da4939f 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/MoveTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/MoveTest.java @@ -25,7 +25,8 @@ import org.eclipse.core.runtime.CoreException; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; public class MoveTest { @@ -40,51 +41,55 @@ public void teardown() throws Exception { ZipFileSystemTestSetup.teardown(); } - @Test - public void testMoveZipFileWithinProject() throws CoreException, IOException { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testMoveZipFileWithinProject(String zipFileName) throws CoreException, IOException { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); IFolder destinationFolder = ZipFileSystemTestSetup.firstProject.getFolder("destinationFolder"); destinationFolder.create(false, true, getMonitor()); IFolder destination = ZipFileSystemTestSetup.firstProject - .getFolder("destinationFolder/" + ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder("destinationFolder/" + zipFileName); openedZipFile.move(destination.getFullPath(), false, getMonitor()); IFolder newFolder = ZipFileSystemTestSetup.firstProject - .getFolder(destinationFolder.getName() + "/" + ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(destinationFolder.getName() + "/" + zipFileName); ensureExists(newFolder); ensureDoesNotExist(openedZipFile); } - @Test - public void testMoveZipFileToOtherProject() throws CoreException, IOException { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testMoveZipFileToOtherProject(String zipFileName) throws CoreException, IOException { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); IFolder destination = ZipFileSystemTestSetup.secondProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); openedZipFile.move(destination.getFullPath(), false, getMonitor()); IFolder newFolder = ZipFileSystemTestSetup.secondProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); ensureExists(newFolder); ensureDoesNotExist(openedZipFile); } - @Test - public void testMoveZipFileToOtherProjectFolder() throws CoreException, IOException { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testMoveZipFileToOtherProjectFolder(String zipFileName) throws CoreException, IOException { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); IFolder destinationFolder = ZipFileSystemTestSetup.secondProject.getFolder("destinationFolder"); destinationFolder.create(false, true, getMonitor()); IFolder destination = ZipFileSystemTestSetup.secondProject - .getFolder("destinationFolder/" + ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder("destinationFolder/" + zipFileName); openedZipFile.move(destination.getFullPath(), false, getMonitor()); IFolder newFolder = ZipFileSystemTestSetup.secondProject - .getFolder(destinationFolder.getName() + "/" + ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(destinationFolder.getName() + "/" + zipFileName); ensureExists(newFolder); ensureDoesNotExist(openedZipFile); } - @Test - public void testMoveFileIntoZipFile() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testMoveFileIntoZipFile(String zipFileName) throws Exception { IFile textFile = ZipFileSystemTestSetup.firstProject.getFile("NewFile.txt"); ensureDoesNotExist(textFile); String text = "Foo"; @@ -93,17 +98,18 @@ public void testMoveFileIntoZipFile() throws Exception { stream.close(); ensureExists(textFile); IFile destinationFile = ZipFileSystemTestSetup.firstProject - .getFile(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME + "/" + "NewFile.txt"); + .getFile(zipFileName + "/" + "NewFile.txt"); textFile.move(destinationFile.getFullPath(), false, getMonitor()); ensureExists(destinationFile); assertTextFileContent(destinationFile, text); ensureDoesNotExist(textFile); } - @Test - public void testMoveFolderIntoZipFile() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testMoveFolderIntoZipFile(String zipFileName) throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); IFolder destinationFolder = openedZipFile.getFolder("destinationFolder"); ensureDoesNotExist(destinationFolder); destinationFolder.create(false, true, getMonitor()); @@ -118,10 +124,11 @@ public void testMoveFolderIntoZipFile() throws Exception { ensureExists(newFolderDestination); } - @Test - public void testMoveFolderWithContentIntoZipFile() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testMoveFolderWithContentIntoZipFile(String zipFileName) throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); IFolder destinationFolder = openedZipFile.getFolder("destinationFolder"); ensureDoesNotExist(destinationFolder); destinationFolder.create(false, true, getMonitor()); @@ -143,10 +150,11 @@ public void testMoveFolderWithContentIntoZipFile() throws Exception { ensureExists(newFolderDestination); } - @Test - public void testMoveFileFromZipFile() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testMoveFileFromZipFile(String zipFileName) throws Exception { IFile textFile = ZipFileSystemTestSetup.firstProject - .getFile(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME + "/" + .getFile(zipFileName + "/" + ZipFileSystemTestSetup.TEXT_FILE_NAME); ensureExists(textFile); IFile destinationFile = ZipFileSystemTestSetup.firstProject.getFile(ZipFileSystemTestSetup.TEXT_FILE_NAME); @@ -156,10 +164,11 @@ public void testMoveFileFromZipFile() throws Exception { ensureDoesNotExist(textFile); } - @Test - public void testMoveFolderFromZipFile() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testMoveFolderFromZipFile(String zipFileName) throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); IFolder newFolder = openedZipFile.getFolder("NewFolder"); ensureDoesNotExist(newFolder); newFolder.create(false, true, getMonitor()); @@ -170,10 +179,11 @@ public void testMoveFolderFromZipFile() throws Exception { ensureExists(folderDestination); } - @Test - public void testMoveFolderWithContentFromZipFile() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testMoveFolderWithContentFromZipFile(String zipFileName) throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); IFolder newFolder = openedZipFile.getFolder("NewFolder"); ensureDoesNotExist(newFolder); newFolder.create(false, true, getMonitor()); @@ -191,11 +201,12 @@ public void testMoveFolderWithContentFromZipFile() throws Exception { ensureExists(folderDestination); } - @Test - public void testMoveFolderWithContentFromZipFileIntoOtherZipFile() throws Exception { - IFolder firstZipFile = ZipFileSystemTestSetup.firstProject.getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testMoveFolderWithContentFromZipFileIntoOtherZipFile(String zipFileName) throws Exception { + IFolder firstZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); // create and open second ZipFile - String secondZipFileName = ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME.replace(".", "New."); + String secondZipFileName = zipFileName.replace(".", "New."); IFile secondZipFile = ZipFileSystemTestSetup.firstProject.getFile(secondZipFileName); ensureDoesNotExist(secondZipFile); ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.firstProject, secondZipFileName); @@ -223,10 +234,11 @@ public void testMoveFolderWithContentFromZipFileIntoOtherZipFile() throws Except ensureExists(movedTextFile); } - @Test - public void testMoveFileInsideOfZipFile() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testMoveFileInsideOfZipFile(String zipFileName) throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); IFolder destinationFolder = openedZipFile.getFolder("destinationFolder"); ensureDoesNotExist(destinationFolder); destinationFolder.create(false, true, getMonitor()); @@ -240,12 +252,13 @@ public void testMoveFileInsideOfZipFile() throws Exception { ensureDoesNotExist(textFile); } - @Test - public void testMoveZipFileIntoZipFile() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testMoveZipFileIntoZipFile(String zipFileName) throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); // create and open second ZipFile - String newZipFileName = ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME.replace(".", "New."); + String newZipFileName = zipFileName.replace(".", "New."); IFile newZipFile = ZipFileSystemTestSetup.firstProject.getFile(newZipFileName); ensureDoesNotExist(newZipFile); ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.firstProject, newZipFileName); @@ -267,11 +280,12 @@ public void testMoveZipFileIntoZipFile() throws Exception { * refreshing the Workspace. This test checks if this specific error is handeled * correctly in RefreshLocalVisitor#visit() */ - @Test - public void testMoveZipFileWithFolder() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testMoveZipFileWithFolder(String zipFileName) throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); - String contentFolderPath = ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME + "/" + "Folder"; + .getFolder(zipFileName); + String contentFolderPath = zipFileName + "/" + "Folder"; IFolder contentFolder = ZipFileSystemTestSetup.firstProject.getFolder(contentFolderPath); ensureDoesNotExist(contentFolder); contentFolder.create(false, true, getMonitor()); @@ -287,7 +301,7 @@ public void testMoveZipFileWithFolder() throws Exception { destinationFolder.create(false, true, getMonitor()); ensureExists(destinationFolder); IFolder zipFileDestination = ZipFileSystemTestSetup.firstProject - .getFolder("destinationFolder/" + ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder("destinationFolder/" + zipFileName); ensureDoesNotExist(zipFileDestination); openedZipFile.move(zipFileDestination.getFullPath(), false, getMonitor()); ensureExists(zipFileDestination); diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/RenameTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/RenameTest.java index 549645cafe6..616e291e847 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/RenameTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/RenameTest.java @@ -20,7 +20,8 @@ import org.eclipse.core.resources.IFolder; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; public class RenameTest { @@ -34,22 +35,24 @@ public void teardown() throws Exception { ZipFileSystemTestSetup.teardown(); } - @Test - public void testRenameZipFile() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testRenameZipFile(String zipFileName) throws Exception { // IFolder is renamed by moving with the new path IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); IFolder renamedOpenZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME + "Renamed"); + .getFolder(zipFileName + "Renamed"); openedZipFile.move(renamedOpenZipFile.getFullPath(), false, getMonitor()); ensureExists(renamedOpenZipFile); ensureDoesNotExist(openedZipFile); } - @Test - public void testRenameFileInsideOfZipFile() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testRenameFileInsideOfZipFile(String zipFileName) throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); IFile textFile = openedZipFile.getFile(ZipFileSystemTestSetup.TEXT_FILE_NAME); IFile renamedTextFile = openedZipFile.getFile(textFile.getName() + "Renamed"); textFile.move(renamedTextFile.getFullPath(), false, getMonitor()); @@ -57,10 +60,11 @@ public void testRenameFileInsideOfZipFile() throws Exception { ensureDoesNotExist(textFile); } - @Test - public void testRenameFolderInsideOfZipFile() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testRenameFolderInsideOfZipFile(String zipFileName) throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); IFolder folder = openedZipFile.getFolder("newFolder"); ensureDoesNotExist(folder); folder.create(false, true, getMonitor()); diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/SetupTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/SetupTest.java index f6a52f71c65..e226cc6bd7f 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/SetupTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/SetupTest.java @@ -19,7 +19,8 @@ import org.eclipse.core.resources.IFolder; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; public class SetupTest { @@ -33,17 +34,19 @@ public void teardown() throws Exception { ZipFileSystemTestSetup.teardown(); } - @Test - public void testZipFileInProject() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testZipFileInProject(String zipFileName) throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); ensureExists(openedZipFile); } - @Test - public void testTextFileInZipFile() throws Exception { + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testTextFileInZipFile(String zipFileName) throws Exception { IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); + .getFolder(zipFileName); IFile textFile = openedZipFile.getFile(ZipFileSystemTestSetup.TEXT_FILE_NAME); ensureExists(textFile); diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestSetup.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestSetup.java index 20fdc41b1d9..0df65bbb50d 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestSetup.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestSetup.java @@ -39,6 +39,8 @@ class ZipFileSystemTestSetup { static final String SECOND_PROJECT_NAME = "SecondProject"; static final String ZIP_FILE_VIRTUAL_FOLDER_NAME = "BasicText.zip"; // Assuming the ZIP is represented as this // folder + static final String JAR_FILE_VIRTUAL_FOLDER_NAME = "BasicText.jar"; + static final String WAR_FILE_VIRTUAL_FOLDER_NAME = "BasicText.war"; static final String EMPTY_ZIP_FILE_NAME = "Empty.zip"; static final String NESTED_ZIP_FILE_PARENT_NAME = "NestedZipFileParent.zip"; static final String NESTED_ZIP_FILE_CHILD_NAME = "NestedZipFileChild.zip"; @@ -51,7 +53,8 @@ class ZipFileSystemTestSetup { static IProgressMonitor progressMonitor = new NullProgressMonitor(); static void defaultSetup() throws Exception { - String[] defaultZipFileNames = { ZIP_FILE_VIRTUAL_FOLDER_NAME }; + String[] defaultZipFileNames = { ZIP_FILE_VIRTUAL_FOLDER_NAME, JAR_FILE_VIRTUAL_FOLDER_NAME, + WAR_FILE_VIRTUAL_FOLDER_NAME }; setup(defaultZipFileNames); } diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestUtil.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestUtil.java index 6d58838ff27..17165f214b9 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestUtil.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestUtil.java @@ -18,6 +18,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.net.URISyntaxException; +import java.util.stream.Stream; import org.eclipse.core.filesystem.EFS; import org.eclipse.core.filesystem.IFileInfo; import org.eclipse.core.filesystem.IFileStore; @@ -33,6 +34,12 @@ class ZipFileSystemTestUtil { + public static Stream zipFileNames() { + return Stream.of(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME, + ZipFileSystemTestSetup.JAR_FILE_VIRTUAL_FOLDER_NAME, + ZipFileSystemTestSetup.WAR_FILE_VIRTUAL_FOLDER_NAME); + } + static void ensureExists(IResource resource) throws CoreException, IOException { switch (resource.getType()) { case IResource.FILE: { From 8b6d7d07d664b618ef786d9dbacc8dffc26f963a Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Wed, 31 Jul 2024 09:10:55 +0200 Subject: [PATCH 19/35] Allow WAR and JAR files All ZIP-based zip files like zip, jar and war can be put on the classpath of the project. To avoid UI bugs, only zip files can be opened that are not added to the project's classpath. This commit introduces a check for the classpath. --- .../propertytester/ZipFilePropertyTester.java | 4 +- .../core/resources/ZipFileTransformer.java | 49 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/propertytester/ZipFilePropertyTester.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/propertytester/ZipFilePropertyTester.java index 7160df98f29..14909c7aed3 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/propertytester/ZipFilePropertyTester.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/propertytester/ZipFilePropertyTester.java @@ -24,7 +24,9 @@ public class ZipFilePropertyTester extends ResourcePropertyTester { /** Enum representing allowed file extensions for zip files. */ private enum ZipFileExtensions { - ZIP("zip"); //$NON-NLS-1$ + ZIP("zip"), //$NON-NLS-1$ + JAR("jar"), //$NON-NLS-1$ + WAR("war"); //$NON-NLS-1$ private final String value; diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java index 01fcf43541d..cc6c27997c3 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java @@ -17,6 +17,9 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.zip.ZipException; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; import org.eclipse.core.filesystem.EFS; import org.eclipse.core.filesystem.IFileStore; import org.eclipse.core.filesystem.URIUtil; @@ -25,6 +28,10 @@ import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; /** * Utility class for opening and closing zip files. @@ -99,6 +106,11 @@ public static void openZipFile(IFile file, boolean backgroundRefresh) "Nested ZIP files are not allowed to be opened: " + file.getName())); //$NON-NLS-1$ } + if (isOnClasspath(file)) { + throw new CoreException(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, + "ZIP files on classpath are not allowed to be opened: " + file.getName())); //$NON-NLS-1$ + } + URI zipURI = new URI("zip", null, "/", file.getLocationURI().toString(), null); //$NON-NLS-1$ //$NON-NLS-2$ IFolder link = file.getParent().getFolder(IPath.fromOSString(file.getName())); @@ -112,4 +124,41 @@ public static void openZipFile(IFile file, boolean backgroundRefresh) new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, "Zip File could not be opened")); //$NON-NLS-1$ } } + + /** + * Checks if the given file is on the project's classpath. + * + * @param file The file to check. + * @return true if the file is on the classpath; false otherwise. + * @throws CoreException + */ + private static boolean isOnClasspath(IFile file) throws CoreException { + IProject project = file.getProject(); + IFile classpathFile = project.getFile(".classpath"); //$NON-NLS-1$ + if (!classpathFile.exists()) { + return false; + } + try (InputStream inputStream = classpathFile.getContents()) { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document document = builder.parse(inputStream); + NodeList classpathEntries = document.getElementsByTagName("classpathentry"); //$NON-NLS-1$ + + for (int i = 0; i < classpathEntries.getLength(); i++) { + Element classpathEntry = (Element) classpathEntries.item(i); + String kind = classpathEntry.getAttribute("kind"); //$NON-NLS-1$ + String path = classpathEntry.getAttribute("path"); //$NON-NLS-1$ + + if ("lib".equals(kind) || "src".equals(kind)) { //$NON-NLS-1$//$NON-NLS-2$ + if (file.exists() && file.getLocation().toString().contains(path)) { + return true; + } + } + } + } catch (IOException | CoreException | ParserConfigurationException | SAXException e) { + throw new CoreException(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, + "ZIP files on classpath are not allowed to be opened: " + file.getName())); //$NON-NLS-1$ + } + return false; + } } From 5cae0baed3dcfbbccbd83771e8e81a75b5a52121 Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Thu, 1 Aug 2024 07:59:15 +0200 Subject: [PATCH 20/35] remove Todo --- .../src/org/eclipse/core/filesystem/ZipFileUtil.java | 1 - 1 file changed, 1 deletion(-) diff --git a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/filesystem/ZipFileUtil.java b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/filesystem/ZipFileUtil.java index 30dd5fd4118..788eb2701d4 100644 --- a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/filesystem/ZipFileUtil.java +++ b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/filesystem/ZipFileUtil.java @@ -47,7 +47,6 @@ public static boolean isInsideOpenZipFile(URI locationURI) { return isInsideOpenZipFile(store); } - //TODO Implement this method public static boolean isOpenZipFile(IFileStore store) { if (isInsideOpenZipFile(store)) { ZipFileStore zipStore = (ZipFileStore) store; From 87e0a69c88f0be4e39e4c2149864c0ea76bd3b73 Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Thu, 1 Aug 2024 11:32:36 +0200 Subject: [PATCH 21/35] remove code from RefreshLocalVisitor --- .../core/internal/localstore/RefreshLocalVisitor.java | 3 +-- .../eclipse/core/internal/resources/VirtualZipFolder.java | 5 +++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/RefreshLocalVisitor.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/RefreshLocalVisitor.java index dbaf3ce2c0d..0fb70969856 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/RefreshLocalVisitor.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/RefreshLocalVisitor.java @@ -14,7 +14,6 @@ *******************************************************************************/ package org.eclipse.core.internal.localstore; -import org.eclipse.core.filesystem.ZipFileUtil; import org.eclipse.core.internal.resources.Container; import org.eclipse.core.internal.resources.File; import org.eclipse.core.internal.resources.Folder; @@ -248,7 +247,7 @@ protected boolean synchronizeGender(UnifiedTreeNode node, Resource target) throw return false; } } else { - if (!node.isFolder() && !ZipFileUtil.isOpenZipFile(target.getStore())) { + if (!node.isFolder()) { folderToFile(node, target); resourceChanged = true; return false; diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/VirtualZipFolder.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/VirtualZipFolder.java index e318619ae6a..49f2d946acf 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/VirtualZipFolder.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/VirtualZipFolder.java @@ -106,4 +106,9 @@ public void move(IPath destination, int updateFlags, IProgressMonitor monitor) t new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, "Error moving ZIP file", e)); //$NON-NLS-1$ } } + + @Override + public IFile changeToFile() { + return this.getParent().getFile(new Path(this.getName())); + } } From 6e1efddbf1b98b16fcbd0c6de1b0d5e6f722ebc4 Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Thu, 1 Aug 2024 14:27:50 +0200 Subject: [PATCH 22/35] new test for move bug --- .../core/tests/filesystem/zip/MoveTest.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/MoveTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/MoveTest.java index 0425da4939f..e3f608ba702 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/MoveTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/MoveTest.java @@ -234,6 +234,47 @@ public void testMoveFolderWithContentFromZipFileIntoOtherZipFile(String zipFileN ensureExists(movedTextFile); } + @ParameterizedTest + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + public void testMoveFolderWithContentFromZipFileIntoOtherZipFileTwice(String zipFileName) throws Exception { + IFolder firstZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); + // create and open second ZipFile + String secondZipFileName = zipFileName.replace(".", "New."); + IFile secondZipFile = ZipFileSystemTestSetup.firstProject.getFile(secondZipFileName); + ensureDoesNotExist(secondZipFile); + ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.firstProject, secondZipFileName); + ensureExists(secondZipFile); + ZipFileSystemTestUtil.openZipFile(secondZipFile); + IFolder openedSecondZipFile = ZipFileSystemTestSetup.firstProject.getFolder(secondZipFileName); + ensureExists(openedSecondZipFile); + + IFolder newFolder = firstZipFile.getFolder("NewFolder"); + ensureDoesNotExist(newFolder); + newFolder.create(false, true, getMonitor()); + ensureExists(newFolder); + IFile textFile = newFolder.getFile("NewFile.txt"); + ensureDoesNotExist(textFile); + String text = "Foo"; + InputStream stream = new ByteArrayInputStream(text.getBytes()); + textFile.create(stream, false, getMonitor()); + stream.close(); + ensureExists(textFile); + IFolder movedFolderDestination = openedSecondZipFile.getFolder("NewFolder"); + newFolder.move(movedFolderDestination.getFullPath(), false, getMonitor()); + ensureDoesNotExist(newFolder); + ensureExists(movedFolderDestination); + IFile movedTextFile = movedFolderDestination.getFile("NewFile.txt"); + ensureExists(movedTextFile); + + // Move second time + IFolder originDestination = newFolder; + movedFolderDestination.move(originDestination.getFullPath(), false, getMonitor()); + ensureDoesNotExist(movedFolderDestination); + ensureExists(originDestination); + movedTextFile = originDestination.getFile("NewFile.txt"); + ensureExists(movedTextFile); + } + @ParameterizedTest @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") public void testMoveFileInsideOfZipFile(String zipFileName) throws Exception { From da1b006fb9224e901dae4dba7930567cc92fb98d Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Mon, 5 Aug 2024 10:10:12 +0200 Subject: [PATCH 23/35] Revert "new test concurrencyTest" This reverts commit cc797ed405db5f0baaf23186f8521b41ad84e452. --- .../tests/filesystem/AllFileSystemTests.java | 2 - .../tests/filesystem/zip/ConcurrencyTest.java | 98 ------------------- 2 files changed, 100 deletions(-) delete mode 100644 resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ConcurrencyTest.java diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/AllFileSystemTests.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/AllFileSystemTests.java index f7d86c2654e..f577a29b017 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/AllFileSystemTests.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/AllFileSystemTests.java @@ -14,7 +14,6 @@ *******************************************************************************/ package org.eclipse.core.tests.filesystem; -import org.eclipse.core.tests.filesystem.zip.ConcurrencyTest; import org.junit.platform.suite.api.SelectClasses; import org.junit.platform.suite.api.Suite; @@ -31,7 +30,6 @@ PutInfoTest.class, // SymlinkTest.class, // URIUtilTest.class, // - ConcurrencyTest.class, // }) public class AllFileSystemTests { } diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ConcurrencyTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ConcurrencyTest.java deleted file mode 100644 index 9d488cce3cb..00000000000 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ConcurrencyTest.java +++ /dev/null @@ -1,98 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2024 Vector Informatik GmbH and others. - * - * This program and the accompanying materials are made available under the terms of the Eclipse - * Public License 2.0 which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: Vector Informatik GmbH - initial API and implementation - *******************************************************************************/ - -package org.eclipse.core.tests.filesystem.zip; - -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; - -import java.net.URI; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.Executors; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.atomic.AtomicReference; -import org.eclipse.core.filesystem.EFS; -import org.eclipse.core.filesystem.IFileStore; -import org.eclipse.core.resources.IFolder; -import org.eclipse.core.runtime.CoreException; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -public class ConcurrencyTest { - - @BeforeEach - public void setup() throws Exception { - ZipFileSystemTestSetup.defaultSetup(); - } - - @AfterEach - public void teardown() throws Exception { - ZipFileSystemTestSetup.teardown(); - } - - @Test - public void testFetchInfoWithMultipleThreads() throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject - .getFolder(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME); - ZipFileSystemTestUtil.ensureExists(openedZipFile); - URI zipFileURI = openedZipFile.getLocationURI(); - IFileStore zipFileStore = EFS.getStore(zipFileURI); - - int totalThreadCount = 10; - CountDownLatch startLatch = new CountDownLatch(1); // Latch to start all threads simultaneously - CountDownLatch doneLatch = new CountDownLatch(totalThreadCount); // Latch to wait for all threads to finish - ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(totalThreadCount); - - // Shared exception reference to propagate exceptions from threads to the main - // thread - AtomicReference exceptionReference = new AtomicReference<>(); - - // Submit tasks to the executor - for (int i = 0; i < totalThreadCount; i++) { - executor.submit(() -> { - try { - startLatch.await(); // Wait for the signal to start - // Perform various tasks on ZipFileStore - for (int j = 0; j <= 10; j++) { - zipFileStore.childInfos(0, ZipFileSystemTestUtil.getMonitor()); - assertTrue(zipFileStore.fetchInfo().exists(), "File system should exist"); - } - } catch (InterruptedException e) { - e.printStackTrace(); - fail("Thread was interrupted"); - Thread.currentThread().interrupt(); - } catch (CoreException e) { - // Propagate CoreException to the main thread - exceptionReference.set(e); - } finally { - doneLatch.countDown(); - } - }); - } - - // Start all threads - startLatch.countDown(); - - // Wait for all tasks to complete - doneLatch.await(); - - executor.shutdown(); - - // Check if any exception was thrown by the threads - if (exceptionReference.get() != null) { - throw exceptionReference.get(); - } - - assertTrue(executor.getCompletedTaskCount() == totalThreadCount, "All tasks should complete successfully"); - } -} From 129fc4b9f36ab14694a38009efcf8637f7210956 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 7 Aug 2024 16:40:37 +0200 Subject: [PATCH 24/35] fix move folder twice error --- .../eclipse/core/internal/filesystem/zip/ZipFileStore.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java index cfb5b7ef6d4..832bbf9175c 100644 --- a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java +++ b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java @@ -349,7 +349,7 @@ public void move(IFileStore destination, int options, IProgressMonitor monitor) if (Files.isDirectory(srcPath)) { moveDirectory(srcPath, destPath, srcFs, destFs); } else { - Files.move(srcPath, destPath, StandardCopyOption.REPLACE_EXISTING); + Files.move(srcPath, destPath, StandardCopyOption.COPY_ATTRIBUTES); } } catch (IOException | URISyntaxException e) { throw new CoreException(new Status(IStatus.ERROR, getPluginId(), "Error moving entry within ZIP", e)); //$NON-NLS-1$ @@ -373,7 +373,7 @@ private void moveDirectory(Path srcPath, Path destPath, FileSystem srcFs, FileSy Files.createDirectories(destination); } } else { - Files.move(source, destination, StandardCopyOption.REPLACE_EXISTING); + Files.move(source, destination, StandardCopyOption.COPY_ATTRIBUTES); } } catch (IOException e) { throw new RuntimeException("Failed to move files", e); //$NON-NLS-1$ From c37d5e80b0416f54ed9a2b33b636e5aa4b8836df Mon Sep 17 00:00:00 2001 From: David Date: Wed, 7 Aug 2024 16:59:03 +0200 Subject: [PATCH 25/35] revert accidental changes --- .../.project | 2 +- .../core/internal/resources/ResourceTree.java | 39 ++++--------------- 2 files changed, 9 insertions(+), 32 deletions(-) rename resources/bundles/{org.eclipse.core.resources.win32.x86_64 => org.eclipse.core.filesystem.win32.x86_64}/.project (89%) diff --git a/resources/bundles/org.eclipse.core.resources.win32.x86_64/.project b/resources/bundles/org.eclipse.core.filesystem.win32.x86_64/.project similarity index 89% rename from resources/bundles/org.eclipse.core.resources.win32.x86_64/.project rename to resources/bundles/org.eclipse.core.filesystem.win32.x86_64/.project index fe2c03b97cf..affc913b876 100644 --- a/resources/bundles/org.eclipse.core.resources.win32.x86_64/.project +++ b/resources/bundles/org.eclipse.core.filesystem.win32.x86_64/.project @@ -1,6 +1,6 @@ - org.eclipse.core.resources.win32.x86_64 + org.eclipse.core.filesystem.win32.x86_64 diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ResourceTree.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ResourceTree.java index 9e4da89e21c..d60f0ea05dd 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ResourceTree.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ResourceTree.java @@ -15,34 +15,14 @@ package org.eclipse.core.internal.resources; import java.net.URI; -import org.eclipse.core.filesystem.EFS; -import org.eclipse.core.filesystem.IFileInfo; -import org.eclipse.core.filesystem.IFileStore; -import org.eclipse.core.filesystem.IFileSystem; +import org.eclipse.core.filesystem.*; import org.eclipse.core.filesystem.URIUtil; import org.eclipse.core.internal.localstore.FileSystemResourceManager; import org.eclipse.core.internal.properties.IPropertyManager; -import org.eclipse.core.internal.utils.BitMask; -import org.eclipse.core.internal.utils.Messages; -import org.eclipse.core.internal.utils.Policy; -import org.eclipse.core.resources.IContainer; -import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IFolder; -import org.eclipse.core.resources.IProject; -import org.eclipse.core.resources.IProjectDescription; -import org.eclipse.core.resources.IResource; -import org.eclipse.core.resources.IResourceStatus; -import org.eclipse.core.resources.IResourceVisitor; -import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.internal.utils.*; +import org.eclipse.core.resources.*; import org.eclipse.core.resources.team.IResourceTree; -import org.eclipse.core.runtime.Assert; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.MultiStatus; -import org.eclipse.core.runtime.NullProgressMonitor; -import org.eclipse.core.runtime.OperationCanceledException; +import org.eclipse.core.runtime.*; import org.eclipse.core.runtime.jobs.ILock; import org.eclipse.osgi.util.NLS; @@ -379,12 +359,11 @@ private boolean internalDeleteFolder(IFolder folder, int flags, IProgressMonitor } try { - // this will delete local and workspace + //this will delete local and workspace localManager.delete(folder, flags, Policy.subMonitorFor(monitor, Policy.totalWork)); } catch (CoreException ce) { message = NLS.bind(Messages.localstore_couldnotDelete, folder.getFullPath()); - IStatus status = new ResourceStatus(IStatus.ERROR, IResourceStatus.FAILED_DELETE_LOCAL, - folder.getFullPath(), message, ce); + IStatus status = new ResourceStatus(IStatus.ERROR, IResourceStatus.FAILED_DELETE_LOCAL, folder.getFullPath(), message, ce); failed(status); return false; } @@ -1009,10 +988,8 @@ public void standardMoveFolder(IFolder source, IFolder destination, int flags, I if (!source.exists() || destination.exists() || !destination.getParent().isAccessible()) throw new IllegalArgumentException(); - // Check to see if we are synchronized with the local file system. If we are in - // sync then we can - // short circuit this method and do a file system only move. Otherwise we have - // to recursively + // Check to see if we are synchronized with the local file system. If we are in sync then we can + // short circuit this method and do a file system only move. Otherwise we have to recursively // try and move all resources, doing it in a best-effort manner. boolean force = (flags & IResource.FORCE) != 0; if (!force && !isSynchronized(source, IResource.DEPTH_INFINITE)) { From 551edba4aa2ce65961d0b3b64fef3d95a7ace0b3 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 7 Aug 2024 18:48:31 +0200 Subject: [PATCH 26/35] show in FileSystem for VirtualZipFolders --- .../eclipse/core/internal/resources/VirtualZipFolder.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/VirtualZipFolder.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/VirtualZipFolder.java index 49f2d946acf..ec4c9f52eb7 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/VirtualZipFolder.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/VirtualZipFolder.java @@ -107,6 +107,13 @@ public void move(IPath destination, int updateFlags, IProgressMonitor monitor) t } } + @Override + public IPath getLocation() { + String nameSegment = path.lastSegment(); + IPath parentPath = this.getParent().getLocation(); + return parentPath.append(nameSegment); + } + @Override public IFile changeToFile() { return this.getParent().getFile(new Path(this.getName())); From 5e86e76fc42f1705358c25b30d9cb550cb41a604 Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Mon, 12 Aug 2024 11:40:51 +0200 Subject: [PATCH 27/35] Revert "Allow WAR and JAR files" This reverts commit 9e3d212ff948337eedca7d782a21ad1075b58545. --- .../propertytester/ZipFilePropertyTester.java | 4 +- .../core/resources/ZipFileTransformer.java | 49 ------------------- 2 files changed, 1 insertion(+), 52 deletions(-) diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/propertytester/ZipFilePropertyTester.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/propertytester/ZipFilePropertyTester.java index 14909c7aed3..7160df98f29 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/propertytester/ZipFilePropertyTester.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/propertytester/ZipFilePropertyTester.java @@ -24,9 +24,7 @@ public class ZipFilePropertyTester extends ResourcePropertyTester { /** Enum representing allowed file extensions for zip files. */ private enum ZipFileExtensions { - ZIP("zip"), //$NON-NLS-1$ - JAR("jar"), //$NON-NLS-1$ - WAR("war"); //$NON-NLS-1$ + ZIP("zip"); //$NON-NLS-1$ private final String value; diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java index cc6c27997c3..01fcf43541d 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java @@ -17,9 +17,6 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.zip.ZipException; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; import org.eclipse.core.filesystem.EFS; import org.eclipse.core.filesystem.IFileStore; import org.eclipse.core.filesystem.URIUtil; @@ -28,10 +25,6 @@ import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; /** * Utility class for opening and closing zip files. @@ -106,11 +99,6 @@ public static void openZipFile(IFile file, boolean backgroundRefresh) "Nested ZIP files are not allowed to be opened: " + file.getName())); //$NON-NLS-1$ } - if (isOnClasspath(file)) { - throw new CoreException(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, - "ZIP files on classpath are not allowed to be opened: " + file.getName())); //$NON-NLS-1$ - } - URI zipURI = new URI("zip", null, "/", file.getLocationURI().toString(), null); //$NON-NLS-1$ //$NON-NLS-2$ IFolder link = file.getParent().getFolder(IPath.fromOSString(file.getName())); @@ -124,41 +112,4 @@ public static void openZipFile(IFile file, boolean backgroundRefresh) new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, "Zip File could not be opened")); //$NON-NLS-1$ } } - - /** - * Checks if the given file is on the project's classpath. - * - * @param file The file to check. - * @return true if the file is on the classpath; false otherwise. - * @throws CoreException - */ - private static boolean isOnClasspath(IFile file) throws CoreException { - IProject project = file.getProject(); - IFile classpathFile = project.getFile(".classpath"); //$NON-NLS-1$ - if (!classpathFile.exists()) { - return false; - } - try (InputStream inputStream = classpathFile.getContents()) { - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - DocumentBuilder builder = factory.newDocumentBuilder(); - Document document = builder.parse(inputStream); - NodeList classpathEntries = document.getElementsByTagName("classpathentry"); //$NON-NLS-1$ - - for (int i = 0; i < classpathEntries.getLength(); i++) { - Element classpathEntry = (Element) classpathEntries.item(i); - String kind = classpathEntry.getAttribute("kind"); //$NON-NLS-1$ - String path = classpathEntry.getAttribute("path"); //$NON-NLS-1$ - - if ("lib".equals(kind) || "src".equals(kind)) { //$NON-NLS-1$//$NON-NLS-2$ - if (file.exists() && file.getLocation().toString().contains(path)) { - return true; - } - } - } - } catch (IOException | CoreException | ParserConfigurationException | SAXException e) { - throw new CoreException(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, - "ZIP files on classpath are not allowed to be opened: " + file.getName())); //$NON-NLS-1$ - } - return false; - } } From 585d6031e2dc604e438a8f0b21a8a05f8cf7c414 Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Mon, 12 Aug 2024 11:37:37 +0200 Subject: [PATCH 28/35] make opening zip files atomic to make sure that clients like JDT do not have a behaviour change or even errors the code for opening zip files is encapsulated in a IWorkspaceRunnable. This allows opening zip files on the class path. --- .../propertytester/ZipFilePropertyTester.java | 4 +- .../core/resources/ZipFileTransformer.java | 53 +++++++++++-------- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/propertytester/ZipFilePropertyTester.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/propertytester/ZipFilePropertyTester.java index 7160df98f29..14909c7aed3 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/propertytester/ZipFilePropertyTester.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/propertytester/ZipFilePropertyTester.java @@ -24,7 +24,9 @@ public class ZipFilePropertyTester extends ResourcePropertyTester { /** Enum representing allowed file extensions for zip files. */ private enum ZipFileExtensions { - ZIP("zip"); //$NON-NLS-1$ + ZIP("zip"), //$NON-NLS-1$ + JAR("jar"), //$NON-NLS-1$ + WAR("war"); //$NON-NLS-1$ private final String value; diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java index 01fcf43541d..235262ddd0e 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java @@ -21,10 +21,12 @@ import org.eclipse.core.filesystem.IFileStore; import org.eclipse.core.filesystem.URIUtil; import org.eclipse.core.filesystem.ZipFileUtil; +import org.eclipse.core.internal.resources.Workspace; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.ISchedulingRule; /** * Utility class for opening and closing zip files. @@ -44,12 +46,13 @@ public class ZipFileTransformer { * */ public static void closeZipFile(IFolder folder) throws URISyntaxException, CoreException { + IProject project = folder.getProject(); URI zipURI = new URI(folder.getLocationURI().getQuery()); IFileStore parentStore = EFS.getStore(folder.getParent().getLocationURI()); URI childURI = parentStore.getChild(folder.getName()).toURI(); if (URIUtil.equals(zipURI, childURI)) { folder.delete(IResource.CLOSE_ZIP_FILE, null); - folder.getProject().refreshLocal(IResource.DEPTH_INFINITE, null); + project.refreshLocal(IResource.DEPTH_INFINITE, null); } else { throw new CoreException(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, "Closing of Zip File " + folder.getName() //$NON-NLS-1$ @@ -76,18 +79,10 @@ public static void closeZipFile(IFolder folder) throws URISyntaxException, CoreE * */ public static void openZipFile(IFile file, boolean backgroundRefresh) - throws URISyntaxException, CoreException { - try (InputStream fis = file.getContents()) { - ZipFileUtil.canZipFileBeOpened(fis); - // Additional operations can continue here if header is correct - } catch (IOException e) { - if (e instanceof ZipException && e.getMessage().equals("encrypted ZIP entry not supported")) { //$NON-NLS-1$ - throw new CoreException(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, - "Opening encrypted ZIP files is not supported: " + file.getName(), e)); //$NON-NLS-1$ - } - throw new CoreException(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, - "The file is either empty or doesn't represent a ZIP file: " + file.getName(), e)); //$NON-NLS-1$ - } + throws CoreException { + Workspace workspace = ((Workspace) file.getWorkspace()); + IProject project = file.getProject(); + final ISchedulingRule rule = workspace.getRuleFactory().createRule(project); if (file.isLinked()) { throw new CoreException(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, @@ -99,17 +94,29 @@ public static void openZipFile(IFile file, boolean backgroundRefresh) "Nested ZIP files are not allowed to be opened: " + file.getName())); //$NON-NLS-1$ } + IWorkspaceRunnable runnable = monitor -> { + try (InputStream fis = file.getContents()) { + ZipFileUtil.canZipFileBeOpened(fis); + // Additional operations can continue here if header is correct + URI zipURI = new URI("zip", null, "/", file.getLocationURI().toString(), null); //$NON-NLS-1$ //$NON-NLS-2$ + IFolder link = file.getParent().getFolder(IPath.fromOSString(file.getName())); + int flags = backgroundRefresh ? IResource.REPLACE | IResource.BACKGROUND_REFRESH : IResource.REPLACE; - URI zipURI = new URI("zip", null, "/", file.getLocationURI().toString(), null); //$NON-NLS-1$ //$NON-NLS-2$ - IFolder link = file.getParent().getFolder(IPath.fromOSString(file.getName())); - int flags = backgroundRefresh ? IResource.REPLACE | IResource.BACKGROUND_REFRESH : IResource.REPLACE; + link.createLink(zipURI, flags, monitor); + project.refreshLocal(IResource.DEPTH_INFINITE, monitor); + } catch (IOException e) { + if (e instanceof ZipException && e.getMessage().equals("encrypted ZIP entry not supported")) { //$NON-NLS-1$ + throw new CoreException(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, + "Opening encrypted ZIP files is not supported: " + file.getName(), e)); //$NON-NLS-1$ + } + throw new CoreException(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, + "The file is either empty or doesn't represent a ZIP file: " + file.getName(), e)); //$NON-NLS-1$ + } catch (CoreException | URISyntaxException e) { + throw new CoreException( + new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, "Zip File could not be opened")); //$NON-NLS-1$ + } + }; - try { - link.createLink(zipURI, flags, null); - link.refreshLocal(0, null); - } catch (CoreException e) { - throw new CoreException( - new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, "Zip File could not be opened")); //$NON-NLS-1$ - } + workspace.run(runnable, rule, IWorkspace.AVOID_UPDATE, null); } } From 484451952a3fb4e25c54beb15244a356cec95640 Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Tue, 13 Aug 2024 13:07:41 +0200 Subject: [PATCH 29/35] add null check for locationURI --- .../src/org/eclipse/core/filesystem/ZipFileUtil.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/filesystem/ZipFileUtil.java b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/filesystem/ZipFileUtil.java index 788eb2701d4..a98e963cda2 100644 --- a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/filesystem/ZipFileUtil.java +++ b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/filesystem/ZipFileUtil.java @@ -40,7 +40,11 @@ public static boolean isInsideOpenZipFile(IFileStore store) { public static boolean isInsideOpenZipFile(URI locationURI) { IFileStore store; try { - store = EFS.getStore(locationURI); + if (locationURI != null) { + store = EFS.getStore(locationURI); + } else { + return false; + } } catch (CoreException e) { return false; } From 12c7a946ef0e4917d590b8bc0d794aeb242d46e1 Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Tue, 13 Aug 2024 13:07:24 +0200 Subject: [PATCH 30/35] implement double click open for zip files --- .../plugin.properties | 1 + .../org.eclipse.core.resources/plugin.xml | 9 +++++ .../content/ZipFileContentDescriber.java | 36 +++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 runtime/bundles/org.eclipse.core.contenttype/src/org/eclipse/core/runtime/content/ZipFileContentDescriber.java diff --git a/resources/bundles/org.eclipse.core.resources/plugin.properties b/resources/bundles/org.eclipse.core.resources/plugin.properties index 09190635c2e..7e662951051 100644 --- a/resources/bundles/org.eclipse.core.resources/plugin.properties +++ b/resources/bundles/org.eclipse.core.resources/plugin.properties @@ -26,6 +26,7 @@ filterMatchers=Filter Matchers preferencesExtPtName=Resource Preferences resourceModelName=File System Resources variableProviders=Variable Providers +zipfileContentTypeName = Zip File markerName = Marker problemName = Problem diff --git a/resources/bundles/org.eclipse.core.resources/plugin.xml b/resources/bundles/org.eclipse.core.resources/plugin.xml index 9cdeba8cce4..33ad123d686 100644 --- a/resources/bundles/org.eclipse.core.resources/plugin.xml +++ b/resources/bundles/org.eclipse.core.resources/plugin.xml @@ -41,6 +41,15 @@ id="preferences" base-type="org.eclipse.core.runtime.properties"/> + + + + + Date: Thu, 15 Aug 2024 13:48:17 +0200 Subject: [PATCH 31/35] Refactor tests --- .../{BasicTextNew.jar => BasicText2.jar} | Bin .../{BasicTextNew.war => BasicText2.war} | Bin .../{BasicTextNew.zip => BasicText2.zip} | Bin .../core/tests/filesystem/zip/CloseTest.java | 41 +- .../core/tests/filesystem/zip/CopyTest.java | 223 +++++------ .../core/tests/filesystem/zip/CreateTest.java | 33 +- .../core/tests/filesystem/zip/DeleteTest.java | 65 ++-- .../core/tests/filesystem/zip/MoveTest.java | 358 +++++++++--------- .../core/tests/filesystem/zip/OpenTest.java | 85 ++--- .../core/tests/filesystem/zip/RenameTest.java | 45 +-- .../core/tests/filesystem/zip/SetupTest.java | 18 +- .../zip/ZipFileSystemTestSetup.java | 150 +++----- .../filesystem/zip/ZipFileSystemTestUtil.java | 143 ------- 13 files changed, 456 insertions(+), 705 deletions(-) rename resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/{BasicTextNew.jar => BasicText2.jar} (100%) rename resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/{BasicTextNew.war => BasicText2.war} (100%) rename resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/{BasicTextNew.zip => BasicText2.zip} (100%) delete mode 100644 resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestUtil.java diff --git a/resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicTextNew.jar b/resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicText2.jar similarity index 100% rename from resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicTextNew.jar rename to resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicText2.jar diff --git a/resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicTextNew.war b/resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicText2.war similarity index 100% rename from resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicTextNew.war rename to resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicText2.war diff --git a/resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicTextNew.zip b/resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicText2.zip similarity index 100% rename from resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicTextNew.zip rename to resources/tests/org.eclipse.core.tests.resources/resources/ZipFileSystem/BasicText2.zip diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CloseTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CloseTest.java index 580de52b9dc..33de38cb5e5 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CloseTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CloseTest.java @@ -12,11 +12,11 @@ package org.eclipse.core.tests.filesystem.zip; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.ensureExists; import static org.junit.Assert.assertTrue; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.ZipFileTransformer; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.params.ParameterizedTest; @@ -26,7 +26,7 @@ public class CloseTest { @BeforeEach public void setup() throws Exception { - ZipFileSystemTestSetup.defaultSetup(); + ZipFileSystemTestSetup.setup(); } @AfterEach @@ -35,19 +35,19 @@ public void teardown() throws Exception { } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testCloseZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); - ensureExists(openedZipFile); - ZipFileSystemTestUtil.closeZipFile(openedZipFile); - IFile zipFile = ZipFileSystemTestSetup.firstProject + ZipFileSystemTestSetup.ensureExists(openedZipFile); + ZipFileTransformer.closeZipFile(openedZipFile); + IFile zipFile = ZipFileSystemTestSetup.projects.get(0) .getFile(zipFileName); // Don't use Utility method ensureDoesNotExist because the fileStore is still // available after closing. The fileStore is the File itself in the local file // system that still exists after closing. assertTrue("folder was not properly deleted: " + openedZipFile, !openedZipFile.exists()); - ensureExists(zipFile); + ZipFileSystemTestSetup.ensureExists(zipFile); } /* @@ -56,26 +56,23 @@ public void testCloseZipFile(String zipFileName) throws Exception { * file in the project is deleted so the linked file has no target. */ @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testCloseZipFileWithZipFileUnderneath(String zipFileName) throws Exception { - IFolder firstOpenedZipFile = ZipFileSystemTestSetup.firstProject + IFolder firstZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); - ensureExists(firstOpenedZipFile); - String secondZipFileName = zipFileName.replace(".", "New."); - ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.firstProject, secondZipFileName); - IFile secondZipFile = ZipFileSystemTestSetup.firstProject.getFile(secondZipFileName); - ZipFileSystemTestUtil.openZipFile(secondZipFile); - IFolder secondOpenedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(secondZipFileName); - ensureExists(secondOpenedZipFile); + String secondZipFileName = zipFileName.replace(".", "2."); + ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.projects.get(0), secondZipFileName); + ZipFileTransformer.openZipFile(ZipFileSystemTestSetup.projects.get(0).getFile(secondZipFileName), true); + IFolder secondZipFile = ZipFileSystemTestSetup.projects.get(0).getFolder(secondZipFileName); - ZipFileSystemTestUtil.closeZipFile(firstOpenedZipFile); - IFile zipFile = ZipFileSystemTestSetup.firstProject + ZipFileTransformer.closeZipFile(firstZipFile); + IFile closedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFile(zipFileName); // Don't use Utility method ensureDoesNotExist because the fileStore is still // available after closing. The fileStore is the File itself in the local file // system that still exists after closing. - assertTrue("folder was not properly deleted: " + firstOpenedZipFile, !firstOpenedZipFile.exists()); - ensureExists(zipFile); - ensureExists(secondOpenedZipFile); + assertTrue("folder was not properly deleted: " + firstZipFile, !firstZipFile.exists()); + ZipFileSystemTestSetup.ensureExists(closedZipFile); + ZipFileSystemTestSetup.ensureExists(secondZipFile); } } diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CopyTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CopyTest.java index cabee325724..aa184b3c409 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CopyTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CopyTest.java @@ -12,15 +12,10 @@ package org.eclipse.core.tests.filesystem.zip; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.assertTextFileContent; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.ensureDoesNotExist; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.ensureExists; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.getMonitor; - import java.io.ByteArrayInputStream; -import java.io.InputStream; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; +import org.eclipse.core.runtime.NullProgressMonitor; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.params.ParameterizedTest; @@ -33,7 +28,7 @@ public class CopyTest { @BeforeEach public void setup() throws Exception { - ZipFileSystemTestSetup.defaultSetup(); + ZipFileSystemTestSetup.setup(); } @AfterEach @@ -42,175 +37,151 @@ public void teardown() throws Exception { } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testCopyZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); - ensureExists(openedZipFile); - IFolder destinationFolder = ZipFileSystemTestSetup.firstProject.getFolder("Folder"); - destinationFolder.create(true, true, getMonitor()); - ensureExists(destinationFolder); - IFolder copyDestination = ZipFileSystemTestSetup.firstProject + ZipFileSystemTestSetup.ensureExists(openedZipFile); + IFolder destinationFolder = ZipFileSystemTestSetup.projects.get(0).getFolder("Folder"); + destinationFolder.create(true, true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(destinationFolder); + IFolder copyDestination = ZipFileSystemTestSetup.projects.get(0) .getFolder("Folder" + "/" + zipFileName); - openedZipFile.copy(copyDestination.getFullPath(), true, getMonitor()); - ensureExists(copyDestination); - ensureExists(openedZipFile); + openedZipFile.copy(copyDestination.getFullPath(), true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(copyDestination); + ZipFileSystemTestSetup.ensureExists(openedZipFile); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testCopyFileInsideOfZipFile(String zipFileName) throws Exception { - IFile textFile = ZipFileSystemTestSetup.firstProject.getFile( + IFile textFile = ZipFileSystemTestSetup.projects.get(0).getFile( zipFileName + "/" + ZipFileSystemTestSetup.TEXT_FILE_NAME); - ensureExists(textFile); - IFolder destinationFolder = ZipFileSystemTestSetup.firstProject.getFolder("Folder"); - destinationFolder.create(true, true, getMonitor()); - ensureExists(destinationFolder); - IFile copyDestination = ZipFileSystemTestSetup.firstProject + ZipFileSystemTestSetup.ensureExists(textFile); + IFolder destinationFolder = ZipFileSystemTestSetup.projects.get(0).getFolder("Folder"); + destinationFolder.create(true, true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(destinationFolder); + IFile copyDestination = ZipFileSystemTestSetup.projects.get(0) .getFile("Folder" + "/" + ZipFileSystemTestSetup.TEXT_FILE_NAME); - textFile.copy(copyDestination.getFullPath(), true, getMonitor()); - ensureExists(copyDestination); - ensureExists(textFile); + textFile.copy(copyDestination.getFullPath(), true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(copyDestination); + ZipFileSystemTestSetup.ensureExists(textFile); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testCopyFolderInsideOfZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); - ensureExists(openedZipFile); - IFolder newFolder = ZipFileSystemTestSetup.firstProject.getFolder("NewFolder"); - ensureDoesNotExist(newFolder); - newFolder.create(false, true, getMonitor()); - ensureExists(newFolder); + ZipFileSystemTestSetup.ensureExists(openedZipFile); + IFolder newFolder = ZipFileSystemTestSetup.projects.get(0).getFolder("NewFolder"); + ZipFileSystemTestSetup.ensureDoesNotExist(newFolder); + newFolder.create(false, true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(newFolder); IFile textFile = newFolder.getFile("NewFile.txt"); - ensureDoesNotExist(textFile); - String text = "Foo"; - InputStream stream = new ByteArrayInputStream(text.getBytes()); - textFile.create(stream, true, getMonitor()); - stream.close(); - ensureExists(textFile); + ZipFileSystemTestSetup.ensureDoesNotExist(textFile); + textFile.create(new ByteArrayInputStream(new byte[0]), true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(textFile); IFolder copyDestination = openedZipFile.getFolder("NewFolder"); - ensureDoesNotExist(copyDestination); - newFolder.copy(copyDestination.getFullPath(), true, getMonitor()); - ensureExists(copyDestination); - ensureExists(newFolder); - assertTextFileContent(textFile, "Foo"); + ZipFileSystemTestSetup.ensureDoesNotExist(copyDestination); + newFolder.copy(copyDestination.getFullPath(), true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(copyDestination); + ZipFileSystemTestSetup.ensureExists(newFolder); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testCopyFileIntoZipFile(String zipFileName) throws Exception { - IFile textFile = ZipFileSystemTestSetup.firstProject.getFile("NewFile.txt"); - ensureDoesNotExist(textFile); - String text = "Foo"; - InputStream stream = new ByteArrayInputStream(text.getBytes()); - textFile.create(stream, true, getMonitor()); - stream.close(); - ensureExists(textFile); - IFile copyDestination = ZipFileSystemTestSetup.firstProject + IFile textFile = ZipFileSystemTestSetup.projects.get(0).getFile("NewFile.txt"); + ZipFileSystemTestSetup.ensureDoesNotExist(textFile); + textFile.create(new ByteArrayInputStream(new byte[0]), true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(textFile); + IFile copyDestination = ZipFileSystemTestSetup.projects.get(0) .getFile(zipFileName + "/" + "NewFile.txt"); - textFile.copy(copyDestination.getFullPath(), true, getMonitor()); - ensureExists(copyDestination); - ensureExists(textFile); - assertTextFileContent(textFile, "Foo"); + textFile.copy(copyDestination.getFullPath(), true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(copyDestination); + ZipFileSystemTestSetup.ensureExists(textFile); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testCopyFolderIntoZipFile(String zipFileName) throws Exception { - IFile textFile = ZipFileSystemTestSetup.firstProject.getFile("NewFile.txt"); - ensureDoesNotExist(textFile); - String text = "Foo"; - InputStream stream = new ByteArrayInputStream(text.getBytes()); - textFile.create(stream, true, getMonitor()); - stream.close(); - ensureExists(textFile); - IFile copyDestination = ZipFileSystemTestSetup.firstProject + IFile textFile = ZipFileSystemTestSetup.projects.get(0).getFile("NewFile.txt"); + ZipFileSystemTestSetup.ensureDoesNotExist(textFile); + textFile.create(new ByteArrayInputStream(new byte[0]), true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(textFile); + IFile copyDestination = ZipFileSystemTestSetup.projects.get(0) .getFile(zipFileName + "/" + "NewFile.txt"); - textFile.copy(copyDestination.getFullPath(), true, getMonitor()); - ensureExists(copyDestination); - ensureExists(textFile); - assertTextFileContent(textFile, "Foo"); + textFile.copy(copyDestination.getFullPath(), true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(copyDestination); + ZipFileSystemTestSetup.ensureExists(textFile); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testCopyFileFromOutsideOfZipFIleIntoFolderInZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); IFolder newFolder = openedZipFile.getFolder("NewFolder"); - ensureDoesNotExist(newFolder); - newFolder.create(false, true, getMonitor()); - ensureExists(newFolder); - IFile textFile = ZipFileSystemTestSetup.firstProject.getFile("NewFile.txt"); - ensureDoesNotExist(textFile); - String text = "Foo"; - InputStream stream = new ByteArrayInputStream(text.getBytes()); - textFile.create(stream, true, getMonitor()); - stream.close(); - ensureExists(textFile); + ZipFileSystemTestSetup.ensureDoesNotExist(newFolder); + newFolder.create(false, true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(newFolder); + IFile textFile = ZipFileSystemTestSetup.projects.get(0).getFile("NewFile.txt"); + ZipFileSystemTestSetup.ensureDoesNotExist(textFile); + textFile.create(new ByteArrayInputStream(new byte[0]), true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(textFile); IFile copyDestination = newFolder.getFile("NewFile.txt"); - ensureDoesNotExist(copyDestination); - textFile.copy(copyDestination.getFullPath(), true, getMonitor()); - ensureExists(copyDestination); - ensureExists(textFile); - assertTextFileContent(textFile, "Foo"); + ZipFileSystemTestSetup.ensureDoesNotExist(copyDestination); + textFile.copy(copyDestination.getFullPath(), true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(copyDestination); + ZipFileSystemTestSetup.ensureExists(textFile); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testCopyFolderIntoFolderInZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); IFolder firstNewFolder = openedZipFile.getFolder("FirstNewFolder"); - ensureDoesNotExist(firstNewFolder); - firstNewFolder.create(false, true, getMonitor()); - ensureExists(firstNewFolder); + ZipFileSystemTestSetup.ensureDoesNotExist(firstNewFolder); + firstNewFolder.create(false, true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(firstNewFolder); IFolder secondNewFolder = openedZipFile.getFolder("SecondNewFolder"); - ensureDoesNotExist(secondNewFolder); - secondNewFolder.create(false, true, getMonitor()); - ensureExists(secondNewFolder); + ZipFileSystemTestSetup.ensureDoesNotExist(secondNewFolder); + secondNewFolder.create(false, true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(secondNewFolder); IFile textFile = firstNewFolder.getFile("NewFile.txt"); - ensureDoesNotExist(textFile); - String text = "Foo"; - try (InputStream stream = new ByteArrayInputStream(text.getBytes())) { - textFile.create(stream, true, getMonitor()); - } - ensureExists(textFile); + ZipFileSystemTestSetup.ensureDoesNotExist(textFile); + textFile.create(new ByteArrayInputStream(new byte[0]), true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(textFile); IFolder copyDestination = secondNewFolder.getFolder("FirstNewFolder"); - ensureDoesNotExist(copyDestination); - firstNewFolder.copy(copyDestination.getFullPath(), true, getMonitor()); - ensureExists(copyDestination); - ensureExists(firstNewFolder); - assertTextFileContent(textFile, "Foo"); + ZipFileSystemTestSetup.ensureDoesNotExist(copyDestination); + firstNewFolder.copy(copyDestination.getFullPath(), true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(copyDestination); + ZipFileSystemTestSetup.ensureExists(firstNewFolder); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testCopyFileFromOneFolderToOtherFolderInsideofZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); IFolder firstNewFolder = openedZipFile.getFolder("FirstNewFolder"); - ensureDoesNotExist(firstNewFolder); - firstNewFolder.create(false, true, getMonitor()); - ensureExists(firstNewFolder); + ZipFileSystemTestSetup.ensureDoesNotExist(firstNewFolder); + firstNewFolder.create(false, true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(firstNewFolder); IFolder secondNewFolder = openedZipFile.getFolder("SecondNewFolder"); - ensureDoesNotExist(secondNewFolder); - secondNewFolder.create(false, true, getMonitor()); - ensureExists(secondNewFolder); + ZipFileSystemTestSetup.ensureDoesNotExist(secondNewFolder); + secondNewFolder.create(false, true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(secondNewFolder); IFile textFile = firstNewFolder.getFile("NewFile.txt"); - ensureDoesNotExist(textFile); - String text = "Foo"; - try (InputStream stream = new ByteArrayInputStream(text.getBytes())) { - textFile.create(stream, true, getMonitor()); - } - ensureExists(textFile); + ZipFileSystemTestSetup.ensureDoesNotExist(textFile); + textFile.create(new ByteArrayInputStream(new byte[0]), true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(textFile); IFile copyDestination = secondNewFolder.getFile("NewFile.txt"); - ensureDoesNotExist(copyDestination); - textFile.copy(copyDestination.getFullPath(), true, getMonitor()); - ensureExists(copyDestination); - ensureExists(textFile); - assertTextFileContent(textFile, "Foo"); + ZipFileSystemTestSetup.ensureDoesNotExist(copyDestination); + textFile.copy(copyDestination.getFullPath(), true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(copyDestination); + ZipFileSystemTestSetup.ensureExists(textFile); } } diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CreateTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CreateTest.java index ade07691430..13e2daeb3e7 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CreateTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/CreateTest.java @@ -12,15 +12,10 @@ package org.eclipse.core.tests.filesystem.zip; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.assertTextFileContent; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.ensureDoesNotExist; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.ensureExists; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.getMonitor; - import java.io.ByteArrayInputStream; -import java.io.InputStream; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; +import org.eclipse.core.runtime.NullProgressMonitor; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.params.ParameterizedTest; @@ -30,7 +25,7 @@ public class CreateTest { @BeforeEach public void setup() throws Exception { - ZipFileSystemTestSetup.defaultSetup(); + ZipFileSystemTestSetup.setup(); } @AfterEach @@ -39,28 +34,24 @@ public void teardown() throws Exception { } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testCreateFileInsideOfZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); IFile textFile = openedZipFile.getFile("NewFile.txt"); - ensureDoesNotExist(textFile); - String text = "Foo"; - InputStream stream = new ByteArrayInputStream(text.getBytes()); - textFile.create(stream, true, getMonitor()); - stream.close(); - ensureExists(textFile); - assertTextFileContent(textFile, "Foo"); + ZipFileSystemTestSetup.ensureDoesNotExist(textFile); + textFile.create(new ByteArrayInputStream(new byte[0]), true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(textFile); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testCreateFolderInsideOfZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); IFolder newFolder = openedZipFile.getFolder("NewFolder"); - ensureDoesNotExist(newFolder); - newFolder.create(false, true, getMonitor()); - ensureExists(newFolder); + ZipFileSystemTestSetup.ensureDoesNotExist(newFolder); + newFolder.create(false, true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(newFolder); } } diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/DeleteTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/DeleteTest.java index b4c72c726a0..714cd07b41a 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/DeleteTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/DeleteTest.java @@ -12,15 +12,12 @@ package org.eclipse.core.tests.filesystem.zip; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.ensureDoesNotExist; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.ensureExists; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.getMonitor; - import java.io.ByteArrayInputStream; import java.io.IOException; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.params.ParameterizedTest; @@ -30,7 +27,7 @@ public class DeleteTest { @BeforeEach public void setup() throws Exception { - ZipFileSystemTestSetup.defaultSetup(); + ZipFileSystemTestSetup.setup(); } @AfterEach @@ -39,56 +36,56 @@ public void teardown() throws Exception { } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testDeleteZipFile(String zipFileName) throws CoreException, IOException { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); - ensureExists(openedZipFile); - openedZipFile.delete(false, false, getMonitor()); - ensureDoesNotExist(openedZipFile); - IFile zipFile = ZipFileSystemTestSetup.firstProject + ZipFileSystemTestSetup.ensureExists(openedZipFile); + openedZipFile.delete(false, false, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureDoesNotExist(openedZipFile); + IFile zipFile = ZipFileSystemTestSetup.projects.get(0) .getFile(zipFileName); - ensureDoesNotExist(zipFile); + ZipFileSystemTestSetup.ensureDoesNotExist(zipFile); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testDeleteFileInsideOfZipFile(String zipFileName) throws CoreException, IOException { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); IFile textFile = openedZipFile.getFile(ZipFileSystemTestSetup.TEXT_FILE_NAME); - ensureExists(textFile); - textFile.delete(true, getMonitor()); - ensureDoesNotExist(textFile); + ZipFileSystemTestSetup.ensureExists(textFile); + textFile.delete(true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureDoesNotExist(textFile); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testDeleteEmptyFolder(String zipFileName) throws CoreException, IOException { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); IFolder folder = openedZipFile.getFolder("FolderToDelete"); - ensureDoesNotExist(folder); - folder.create(true, true, getMonitor()); - ensureExists(folder); - folder.delete(true, getMonitor()); - ensureDoesNotExist(folder); + ZipFileSystemTestSetup.ensureDoesNotExist(folder); + folder.create(true, true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(folder); + folder.delete(true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureDoesNotExist(folder); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testDeleteFolderWithChildren(String zipFileName) throws CoreException, IOException { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); IFolder folder = openedZipFile.getFolder("FolderToDelete"); - ensureDoesNotExist(folder); - folder.create(true, true, getMonitor()); - ensureExists(folder); + ZipFileSystemTestSetup.ensureDoesNotExist(folder); + folder.create(true, true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(folder); IFile textFile = folder.getFile(ZipFileSystemTestSetup.TEXT_FILE_NAME); - textFile.create(new ByteArrayInputStream("Hello World!".getBytes()), true, getMonitor()); - ensureExists(textFile); - folder.delete(true, getMonitor()); - ensureDoesNotExist(folder); - ensureDoesNotExist(textFile); + textFile.create(new ByteArrayInputStream("Hello World!".getBytes()), true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(textFile); + folder.delete(true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureDoesNotExist(folder); + ZipFileSystemTestSetup.ensureDoesNotExist(textFile); } } diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/MoveTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/MoveTest.java index e3f608ba702..d5236c515e9 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/MoveTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/MoveTest.java @@ -12,17 +12,14 @@ package org.eclipse.core.tests.filesystem.zip; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.assertTextFileContent; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.ensureDoesNotExist; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.ensureExists; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.getMonitor; - import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.ZipFileTransformer; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.params.ParameterizedTest; @@ -33,7 +30,7 @@ public class MoveTest { @BeforeEach public void setup() throws Exception { - ZipFileSystemTestSetup.setupWithTwoProjects(); + ZipFileSystemTestSetup.setup(); } @AfterEach @@ -42,276 +39,261 @@ public void teardown() throws Exception { } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testMoveZipFileWithinProject(String zipFileName) throws CoreException, IOException { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); - IFolder destinationFolder = ZipFileSystemTestSetup.firstProject.getFolder("destinationFolder"); - destinationFolder.create(false, true, getMonitor()); - IFolder destination = ZipFileSystemTestSetup.firstProject + IFolder destinationFolder = ZipFileSystemTestSetup.projects.get(0).getFolder("destinationFolder"); + destinationFolder.create(false, true, new NullProgressMonitor()); + IFolder destination = ZipFileSystemTestSetup.projects.get(0) .getFolder("destinationFolder/" + zipFileName); - openedZipFile.move(destination.getFullPath(), false, getMonitor()); - IFolder newFolder = ZipFileSystemTestSetup.firstProject + openedZipFile.move(destination.getFullPath(), false, new NullProgressMonitor()); + IFolder newFolder = ZipFileSystemTestSetup.projects.get(0) .getFolder(destinationFolder.getName() + "/" + zipFileName); - ensureExists(newFolder); - ensureDoesNotExist(openedZipFile); + ZipFileSystemTestSetup.ensureExists(newFolder); + ZipFileSystemTestSetup.ensureDoesNotExist(openedZipFile); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testMoveZipFileToOtherProject(String zipFileName) throws CoreException, IOException { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); - IFolder destination = ZipFileSystemTestSetup.secondProject + IFolder destination = ZipFileSystemTestSetup.projects.get(1) .getFolder(zipFileName); - openedZipFile.move(destination.getFullPath(), false, getMonitor()); - IFolder newFolder = ZipFileSystemTestSetup.secondProject + destination.delete(true, new NullProgressMonitor()); + openedZipFile.move(destination.getFullPath(), false, new NullProgressMonitor()); + IFolder newFolder = ZipFileSystemTestSetup.projects.get(1) .getFolder(zipFileName); - ensureExists(newFolder); - ensureDoesNotExist(openedZipFile); + ZipFileSystemTestSetup.ensureExists(newFolder); + ZipFileSystemTestSetup.ensureDoesNotExist(openedZipFile); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testMoveZipFileToOtherProjectFolder(String zipFileName) throws CoreException, IOException { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); - IFolder destinationFolder = ZipFileSystemTestSetup.secondProject.getFolder("destinationFolder"); - destinationFolder.create(false, true, getMonitor()); - IFolder destination = ZipFileSystemTestSetup.secondProject + IFolder destinationFolder = ZipFileSystemTestSetup.projects.get(1).getFolder("destinationFolder"); + destinationFolder.create(false, true, new NullProgressMonitor()); + IFolder destination = ZipFileSystemTestSetup.projects.get(1) .getFolder("destinationFolder/" + zipFileName); - openedZipFile.move(destination.getFullPath(), false, getMonitor()); - IFolder newFolder = ZipFileSystemTestSetup.secondProject + openedZipFile.move(destination.getFullPath(), false, new NullProgressMonitor()); + IFolder newFolder = ZipFileSystemTestSetup.projects.get(1) .getFolder(destinationFolder.getName() + "/" + zipFileName); - ensureExists(newFolder); - ensureDoesNotExist(openedZipFile); + ZipFileSystemTestSetup.ensureExists(newFolder); + ZipFileSystemTestSetup.ensureDoesNotExist(openedZipFile); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testMoveFileIntoZipFile(String zipFileName) throws Exception { - IFile textFile = ZipFileSystemTestSetup.firstProject.getFile("NewFile.txt"); - ensureDoesNotExist(textFile); + IFile textFile = ZipFileSystemTestSetup.projects.get(0).getFile("NewFile.txt"); + ZipFileSystemTestSetup.ensureDoesNotExist(textFile); String text = "Foo"; InputStream stream = new ByteArrayInputStream(text.getBytes()); - textFile.create(stream, false, getMonitor()); + textFile.create(stream, false, new NullProgressMonitor()); stream.close(); - ensureExists(textFile); - IFile destinationFile = ZipFileSystemTestSetup.firstProject + ZipFileSystemTestSetup.ensureExists(textFile); + IFile destinationFile = ZipFileSystemTestSetup.projects.get(0) .getFile(zipFileName + "/" + "NewFile.txt"); - textFile.move(destinationFile.getFullPath(), false, getMonitor()); - ensureExists(destinationFile); - assertTextFileContent(destinationFile, text); - ensureDoesNotExist(textFile); + textFile.move(destinationFile.getFullPath(), false, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(destinationFile); + ZipFileSystemTestSetup.ensureDoesNotExist(textFile); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testMoveFolderIntoZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); IFolder destinationFolder = openedZipFile.getFolder("destinationFolder"); - ensureDoesNotExist(destinationFolder); - destinationFolder.create(false, true, getMonitor()); - ensureExists(destinationFolder); - IFolder newFolder = ZipFileSystemTestSetup.firstProject.getFolder("NewFolder"); - ensureDoesNotExist(newFolder); - newFolder.create(false, true, getMonitor()); - ensureExists(newFolder); + ZipFileSystemTestSetup.ensureDoesNotExist(destinationFolder); + destinationFolder.create(false, true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(destinationFolder); + IFolder newFolder = ZipFileSystemTestSetup.projects.get(0).getFolder("NewFolder"); + ZipFileSystemTestSetup.ensureDoesNotExist(newFolder); + newFolder.create(false, true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(newFolder); IFolder newFolderDestination = destinationFolder.getFolder("NewFolder"); - newFolder.move(newFolderDestination.getFullPath(), false, getMonitor()); - ensureDoesNotExist(newFolder); - ensureExists(newFolderDestination); + newFolder.move(newFolderDestination.getFullPath(), false, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureDoesNotExist(newFolder); + ZipFileSystemTestSetup.ensureExists(newFolderDestination); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testMoveFolderWithContentIntoZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); IFolder destinationFolder = openedZipFile.getFolder("destinationFolder"); - ensureDoesNotExist(destinationFolder); - destinationFolder.create(false, true, getMonitor()); - ensureExists(destinationFolder); - IFolder newFolder = ZipFileSystemTestSetup.firstProject.getFolder("NewFolder"); - ensureDoesNotExist(newFolder); - newFolder.create(false, true, getMonitor()); - ensureExists(newFolder); + ZipFileSystemTestSetup.ensureDoesNotExist(destinationFolder); + destinationFolder.create(false, true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(destinationFolder); + IFolder newFolder = ZipFileSystemTestSetup.projects.get(0).getFolder("NewFolder"); + ZipFileSystemTestSetup.ensureDoesNotExist(newFolder); + newFolder.create(false, true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(newFolder); IFile textFile = newFolder.getFile("NewFile.txt"); - ensureDoesNotExist(textFile); + ZipFileSystemTestSetup.ensureDoesNotExist(textFile); String text = "Foo"; InputStream stream = new ByteArrayInputStream(text.getBytes()); - textFile.create(stream, false, getMonitor()); + textFile.create(stream, false, new NullProgressMonitor()); stream.close(); - ensureExists(textFile); + ZipFileSystemTestSetup.ensureExists(textFile); IFolder newFolderDestination = destinationFolder.getFolder("NewFolder"); - newFolder.move(newFolderDestination.getFullPath(), false, getMonitor()); - ensureDoesNotExist(newFolder); - ensureExists(newFolderDestination); + newFolder.move(newFolderDestination.getFullPath(), false, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureDoesNotExist(newFolder); + ZipFileSystemTestSetup.ensureExists(newFolderDestination); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testMoveFileFromZipFile(String zipFileName) throws Exception { - IFile textFile = ZipFileSystemTestSetup.firstProject + IFile textFile = ZipFileSystemTestSetup.projects.get(0) .getFile(zipFileName + "/" + ZipFileSystemTestSetup.TEXT_FILE_NAME); - ensureExists(textFile); - IFile destinationFile = ZipFileSystemTestSetup.firstProject.getFile(ZipFileSystemTestSetup.TEXT_FILE_NAME); - textFile.move(destinationFile.getFullPath(), false, getMonitor()); - ensureExists(destinationFile); - assertTextFileContent(destinationFile, "Hello World!"); - ensureDoesNotExist(textFile); + ZipFileSystemTestSetup.ensureExists(textFile); + IFile destinationFile = ZipFileSystemTestSetup.projects.get(0).getFile(ZipFileSystemTestSetup.TEXT_FILE_NAME); + textFile.move(destinationFile.getFullPath(), false, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(destinationFile); + ZipFileSystemTestSetup.ensureDoesNotExist(textFile); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testMoveFolderFromZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); IFolder newFolder = openedZipFile.getFolder("NewFolder"); - ensureDoesNotExist(newFolder); - newFolder.create(false, true, getMonitor()); - ensureExists(newFolder); - IFolder folderDestination = ZipFileSystemTestSetup.firstProject.getFolder("NewFolder"); - newFolder.move(folderDestination.getFullPath(), false, getMonitor()); - ensureDoesNotExist(newFolder); - ensureExists(folderDestination); + ZipFileSystemTestSetup.ensureDoesNotExist(newFolder); + newFolder.create(false, true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(newFolder); + IFolder folderDestination = ZipFileSystemTestSetup.projects.get(0).getFolder("NewFolder"); + newFolder.move(folderDestination.getFullPath(), false, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureDoesNotExist(newFolder); + ZipFileSystemTestSetup.ensureExists(folderDestination); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testMoveFolderWithContentFromZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); IFolder newFolder = openedZipFile.getFolder("NewFolder"); - ensureDoesNotExist(newFolder); - newFolder.create(false, true, getMonitor()); - ensureExists(newFolder); + ZipFileSystemTestSetup.ensureDoesNotExist(newFolder); + newFolder.create(false, true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(newFolder); IFile textFile = newFolder.getFile("NewFile.txt"); - ensureDoesNotExist(textFile); + ZipFileSystemTestSetup.ensureDoesNotExist(textFile); String text = "Foo"; InputStream stream = new ByteArrayInputStream(text.getBytes()); - textFile.create(stream, false, getMonitor()); + textFile.create(stream, false, new NullProgressMonitor()); stream.close(); - ensureExists(textFile); - IFolder folderDestination = ZipFileSystemTestSetup.firstProject.getFolder("NewFolder"); - newFolder.move(folderDestination.getFullPath(), false, getMonitor()); - ensureDoesNotExist(newFolder); - ensureExists(folderDestination); + ZipFileSystemTestSetup.ensureExists(textFile); + IFolder folderDestination = ZipFileSystemTestSetup.projects.get(0).getFolder("NewFolder"); + newFolder.move(folderDestination.getFullPath(), false, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureDoesNotExist(newFolder); + ZipFileSystemTestSetup.ensureExists(folderDestination); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testMoveFolderWithContentFromZipFileIntoOtherZipFile(String zipFileName) throws Exception { - IFolder firstZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); - // create and open second ZipFile - String secondZipFileName = zipFileName.replace(".", "New."); - IFile secondZipFile = ZipFileSystemTestSetup.firstProject.getFile(secondZipFileName); - ensureDoesNotExist(secondZipFile); - ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.firstProject, secondZipFileName); - ensureExists(secondZipFile); - ZipFileSystemTestUtil.openZipFile(secondZipFile); - IFolder openedSecondZipFile = ZipFileSystemTestSetup.firstProject.getFolder(secondZipFileName); - ensureExists(openedSecondZipFile); + IFolder firstZipFile = ZipFileSystemTestSetup.projects.get(0).getFolder(zipFileName); + String secondZipFileName = zipFileName.replace(".", "2."); + ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.projects.get(0), secondZipFileName); + ZipFileTransformer.openZipFile(ZipFileSystemTestSetup.projects.get(0).getFile(secondZipFileName), true); + IFolder secondZipFile = ZipFileSystemTestSetup.projects.get(0).getFolder(secondZipFileName); IFolder newFolder = firstZipFile.getFolder("NewFolder"); - ensureDoesNotExist(newFolder); - newFolder.create(false, true, getMonitor()); - ensureExists(newFolder); + ZipFileSystemTestSetup.ensureDoesNotExist(newFolder); + newFolder.create(false, true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(newFolder); IFile textFile = newFolder.getFile("NewFile.txt"); - ensureDoesNotExist(textFile); + ZipFileSystemTestSetup.ensureDoesNotExist(textFile); String text = "Foo"; InputStream stream = new ByteArrayInputStream(text.getBytes()); - textFile.create(stream, false, getMonitor()); + textFile.create(stream, false, new NullProgressMonitor()); stream.close(); - ensureExists(textFile); - IFolder movedFolderDestination = openedSecondZipFile.getFolder("NewFolder"); - newFolder.move(movedFolderDestination.getFullPath(), false, getMonitor()); - ensureDoesNotExist(newFolder); - ensureExists(movedFolderDestination); + ZipFileSystemTestSetup.ensureExists(textFile); + IFolder movedFolderDestination = secondZipFile.getFolder("NewFolder"); + newFolder.move(movedFolderDestination.getFullPath(), false, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureDoesNotExist(newFolder); + ZipFileSystemTestSetup.ensureExists(movedFolderDestination); IFile movedTextFile = movedFolderDestination.getFile("NewFile.txt"); - ensureExists(movedTextFile); + ZipFileSystemTestSetup.ensureExists(movedTextFile); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testMoveFolderWithContentFromZipFileIntoOtherZipFileTwice(String zipFileName) throws Exception { - IFolder firstZipFile = ZipFileSystemTestSetup.firstProject.getFolder(zipFileName); - // create and open second ZipFile - String secondZipFileName = zipFileName.replace(".", "New."); - IFile secondZipFile = ZipFileSystemTestSetup.firstProject.getFile(secondZipFileName); - ensureDoesNotExist(secondZipFile); - ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.firstProject, secondZipFileName); - ensureExists(secondZipFile); - ZipFileSystemTestUtil.openZipFile(secondZipFile); - IFolder openedSecondZipFile = ZipFileSystemTestSetup.firstProject.getFolder(secondZipFileName); - ensureExists(openedSecondZipFile); + IFolder firstZipFile = ZipFileSystemTestSetup.projects.get(0).getFolder(zipFileName); + String secondZipFileName = zipFileName.replace(".", "2."); + ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.projects.get(0), secondZipFileName); + ZipFileTransformer.openZipFile(ZipFileSystemTestSetup.projects.get(0).getFile(secondZipFileName), true); + IFolder secondZipFile = ZipFileSystemTestSetup.projects.get(0).getFolder(secondZipFileName); IFolder newFolder = firstZipFile.getFolder("NewFolder"); - ensureDoesNotExist(newFolder); - newFolder.create(false, true, getMonitor()); - ensureExists(newFolder); + ZipFileSystemTestSetup.ensureDoesNotExist(newFolder); + newFolder.create(false, true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(newFolder); IFile textFile = newFolder.getFile("NewFile.txt"); - ensureDoesNotExist(textFile); + ZipFileSystemTestSetup.ensureDoesNotExist(textFile); String text = "Foo"; InputStream stream = new ByteArrayInputStream(text.getBytes()); - textFile.create(stream, false, getMonitor()); + textFile.create(stream, false, new NullProgressMonitor()); stream.close(); - ensureExists(textFile); - IFolder movedFolderDestination = openedSecondZipFile.getFolder("NewFolder"); - newFolder.move(movedFolderDestination.getFullPath(), false, getMonitor()); - ensureDoesNotExist(newFolder); - ensureExists(movedFolderDestination); + ZipFileSystemTestSetup.ensureExists(textFile); + IFolder movedFolderDestination = secondZipFile.getFolder("NewFolder"); + newFolder.move(movedFolderDestination.getFullPath(), false, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureDoesNotExist(newFolder); + ZipFileSystemTestSetup.ensureExists(movedFolderDestination); IFile movedTextFile = movedFolderDestination.getFile("NewFile.txt"); - ensureExists(movedTextFile); + ZipFileSystemTestSetup.ensureExists(movedTextFile); // Move second time IFolder originDestination = newFolder; - movedFolderDestination.move(originDestination.getFullPath(), false, getMonitor()); - ensureDoesNotExist(movedFolderDestination); - ensureExists(originDestination); + movedFolderDestination.move(originDestination.getFullPath(), false, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureDoesNotExist(movedFolderDestination); + ZipFileSystemTestSetup.ensureExists(originDestination); movedTextFile = originDestination.getFile("NewFile.txt"); - ensureExists(movedTextFile); + ZipFileSystemTestSetup.ensureExists(movedTextFile); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testMoveFileInsideOfZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); IFolder destinationFolder = openedZipFile.getFolder("destinationFolder"); - ensureDoesNotExist(destinationFolder); - destinationFolder.create(false, true, getMonitor()); - ensureExists(destinationFolder); + ZipFileSystemTestSetup.ensureDoesNotExist(destinationFolder); + destinationFolder.create(false, true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(destinationFolder); IFile textFile = openedZipFile.getFile(ZipFileSystemTestSetup.TEXT_FILE_NAME); - ensureExists(textFile); + ZipFileSystemTestSetup.ensureExists(textFile); IFile fileDestination = destinationFolder.getFile(ZipFileSystemTestSetup.TEXT_FILE_NAME); - ensureDoesNotExist(fileDestination); - textFile.move(fileDestination.getFullPath(), false, getMonitor()); - ensureExists(fileDestination); - ensureDoesNotExist(textFile); + ZipFileSystemTestSetup.ensureDoesNotExist(fileDestination); + textFile.move(fileDestination.getFullPath(), false, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(fileDestination); + ZipFileSystemTestSetup.ensureDoesNotExist(textFile); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testMoveZipFileIntoZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder firstZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); - // create and open second ZipFile - String newZipFileName = zipFileName.replace(".", "New."); - IFile newZipFile = ZipFileSystemTestSetup.firstProject.getFile(newZipFileName); - ensureDoesNotExist(newZipFile); - ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.firstProject, newZipFileName); - ensureExists(newZipFile); - ZipFileSystemTestUtil.openZipFile(newZipFile); - IFolder newOpenedZipFile = ZipFileSystemTestSetup.firstProject.getFolder(newZipFileName); - ensureExists(newOpenedZipFile); + String secondZipFileName = zipFileName.replace(".", "2."); + ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.projects.get(0), secondZipFileName); + ZipFileTransformer.openZipFile(ZipFileSystemTestSetup.projects.get(0).getFile(secondZipFileName), true); + IFolder secondZipFile = ZipFileSystemTestSetup.projects.get(0).getFolder(secondZipFileName); + // move second ZipFile into first ZipFile - IFile newOpenedZipFileDestination = openedZipFile.getFile(newZipFileName); - newOpenedZipFile.move(newOpenedZipFileDestination.getFullPath(), false, getMonitor()); - ensureExists(newOpenedZipFileDestination); - ensureDoesNotExist(newOpenedZipFile); + IFile secondZipFileDestination = firstZipFile.getFile(secondZipFileName); + secondZipFile.move(secondZipFileDestination.getFullPath(), false, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(secondZipFileDestination); + ZipFileSystemTestSetup.ensureDoesNotExist(secondZipFile); } /** @@ -322,29 +304,29 @@ public void testMoveZipFileIntoZipFile(String zipFileName) throws Exception { * correctly in RefreshLocalVisitor#visit() */ @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testMoveZipFileWithFolder(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); String contentFolderPath = zipFileName + "/" + "Folder"; - IFolder contentFolder = ZipFileSystemTestSetup.firstProject.getFolder(contentFolderPath); - ensureDoesNotExist(contentFolder); - contentFolder.create(false, true, getMonitor()); - ensureExists(contentFolder); + IFolder contentFolder = ZipFileSystemTestSetup.projects.get(0).getFolder(contentFolderPath); + ZipFileSystemTestSetup.ensureDoesNotExist(contentFolder); + contentFolder.create(false, true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(contentFolder); String text = "Foo"; InputStream stream = new ByteArrayInputStream(text.getBytes()); - IFile textFile = ZipFileSystemTestSetup.firstProject.getFile(contentFolderPath + "/" + "textFile"); - ensureDoesNotExist(textFile); - textFile.create(stream, false, getMonitor()); - ensureExists(textFile); - IFolder destinationFolder = ZipFileSystemTestSetup.firstProject.getFolder("destinationFolder"); - ensureDoesNotExist(destinationFolder); - destinationFolder.create(false, true, getMonitor()); - ensureExists(destinationFolder); - IFolder zipFileDestination = ZipFileSystemTestSetup.firstProject + IFile textFile = ZipFileSystemTestSetup.projects.get(0).getFile(contentFolderPath + "/" + "textFile"); + ZipFileSystemTestSetup.ensureDoesNotExist(textFile); + textFile.create(stream, false, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(textFile); + IFolder destinationFolder = ZipFileSystemTestSetup.projects.get(0).getFolder("destinationFolder"); + ZipFileSystemTestSetup.ensureDoesNotExist(destinationFolder); + destinationFolder.create(false, true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(destinationFolder); + IFolder zipFileDestination = ZipFileSystemTestSetup.projects.get(0) .getFolder("destinationFolder/" + zipFileName); - ensureDoesNotExist(zipFileDestination); - openedZipFile.move(zipFileDestination.getFullPath(), false, getMonitor()); - ensureExists(zipFileDestination); + ZipFileSystemTestSetup.ensureDoesNotExist(zipFileDestination); + openedZipFile.move(zipFileDestination.getFullPath(), false, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(zipFileDestination); } } diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/OpenTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/OpenTest.java index b20f7776bb6..e5cc8f08bf3 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/OpenTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/OpenTest.java @@ -12,9 +12,6 @@ package org.eclipse.core.tests.filesystem.zip; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.assertTextFileContent; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.ensureDoesNotExist; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.ensureExists; import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -23,6 +20,7 @@ import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.ZipFileTransformer; import org.eclipse.core.runtime.CoreException; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -32,7 +30,7 @@ public class OpenTest { @BeforeEach public void setup() throws Exception { - ZipFileSystemTestSetup.setup(new String[] {}); + ZipFileSystemTestSetup.setup(); } @AfterEach @@ -42,16 +40,16 @@ public void teardown() throws Exception { @Test public void testOpenEmptyZipFile() throws IOException, CoreException, URISyntaxException { - ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.firstProject, + ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.projects.get(0), ZipFileSystemTestSetup.EMPTY_ZIP_FILE_NAME); - IProject project = ZipFileSystemTestSetup.firstProject; + IProject project = ZipFileSystemTestSetup.projects.get(0); IFile zipFile = project.getFile(ZipFileSystemTestSetup.EMPTY_ZIP_FILE_NAME); - ensureExists(zipFile); + ZipFileSystemTestSetup.ensureExists(zipFile); try { - ZipFileSystemTestUtil.openZipFile(zipFile); + ZipFileTransformer.openZipFile(zipFile, true); } catch (CoreException e) { - ensureExists(zipFile); + ZipFileSystemTestSetup.ensureExists(zipFile); String expectedMessage = "The file is either empty or doesn't represent a ZIP file:"; assertTrue(e.getMessage().contains(expectedMessage)); } @@ -59,82 +57,81 @@ public void testOpenEmptyZipFile() throws IOException, CoreException, URISyntaxE @Test public void testOpenNestedZipFileParent() throws IOException, CoreException, URISyntaxException { - ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.firstProject, + ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.projects.get(0), ZipFileSystemTestSetup.NESTED_ZIP_FILE_PARENT_NAME); - IFile nestedZipFileParent = ZipFileSystemTestSetup.firstProject + IFile nestedZipFileParent = ZipFileSystemTestSetup.projects.get(0) .getFile(ZipFileSystemTestSetup.NESTED_ZIP_FILE_PARENT_NAME); - ensureExists(nestedZipFileParent); - ZipFileSystemTestUtil.openZipFile(nestedZipFileParent); - IFolder openedNestedZipFileParent = ZipFileSystemTestSetup.firstProject + ZipFileSystemTestSetup.ensureExists(nestedZipFileParent); + ZipFileTransformer.openZipFile(nestedZipFileParent, true); + IFolder openedNestedZipFileParent = ZipFileSystemTestSetup.projects.get(0) .getFolder(ZipFileSystemTestSetup.NESTED_ZIP_FILE_PARENT_NAME); - ensureExists(openedNestedZipFileParent); + ZipFileSystemTestSetup.ensureExists(openedNestedZipFileParent); } @Test public void testOpenNestedZipFileChild() throws IOException, CoreException, URISyntaxException { - ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.firstProject, + ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.projects.get(0), ZipFileSystemTestSetup.NESTED_ZIP_FILE_PARENT_NAME); - IFile nestedZipFileParent = ZipFileSystemTestSetup.firstProject + IFile nestedZipFileParent = ZipFileSystemTestSetup.projects.get(0) .getFile(ZipFileSystemTestSetup.NESTED_ZIP_FILE_PARENT_NAME); - ensureExists(nestedZipFileParent); - ZipFileSystemTestUtil.openZipFile(nestedZipFileParent); - IFolder openedNestedZipFileParent = ZipFileSystemTestSetup.firstProject + ZipFileSystemTestSetup.ensureExists(nestedZipFileParent); + ZipFileTransformer.openZipFile(nestedZipFileParent, true); + IFolder openedNestedZipFileParent = ZipFileSystemTestSetup.projects.get(0) .getFolder(ZipFileSystemTestSetup.NESTED_ZIP_FILE_PARENT_NAME); - ensureExists(openedNestedZipFileParent); + ZipFileSystemTestSetup.ensureExists(openedNestedZipFileParent); IFile nestedZipFileChild = openedNestedZipFileParent.getFile(ZipFileSystemTestSetup.NESTED_ZIP_FILE_CHILD_NAME); - ensureExists(nestedZipFileChild); + ZipFileSystemTestSetup.ensureExists(nestedZipFileChild); // Attempt to open the nested ZIP file and expect an exception try { - ZipFileSystemTestUtil.openZipFile(nestedZipFileChild); + ZipFileTransformer.openZipFile(nestedZipFileChild, true); fail("Expected a CoreException to be thrown when opening a nested ZIP file"); } catch (CoreException e) { // Verify that the expected exception was thrown assertTrue("Expected CoreException to be thrown when opening a nested ZIP file", e.getMessage().contains("Nested ZIP files are not allowed to be opened")); - IFolder openedNestedZipFileChild = ZipFileSystemTestSetup.firstProject + IFolder openedNestedZipFileChild = ZipFileSystemTestSetup.projects.get(0) .getFolder(ZipFileSystemTestSetup.NESTED_ZIP_FILE_CHILD_NAME); - ensureDoesNotExist(openedNestedZipFileChild); + ZipFileSystemTestSetup.ensureDoesNotExist(openedNestedZipFileChild); } } @Test public void testOpenDeepNestedTextFile() throws IOException, CoreException, URISyntaxException { - ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.firstProject, + ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.projects.get(0), ZipFileSystemTestSetup.NESTED_ZIP_FILE_PARENT_NAME); - IFile nestedZipFileParent = ZipFileSystemTestSetup.firstProject + IFile nestedZipFileParent = ZipFileSystemTestSetup.projects.get(0) .getFile(ZipFileSystemTestSetup.NESTED_ZIP_FILE_PARENT_NAME); - ensureExists(nestedZipFileParent); - ZipFileSystemTestUtil.openZipFile(nestedZipFileParent); - IFolder openedNestedZipFileParent = ZipFileSystemTestSetup.firstProject + ZipFileSystemTestSetup.ensureExists(nestedZipFileParent); + ZipFileTransformer.openZipFile(nestedZipFileParent, true); + IFolder openedNestedZipFileParent = ZipFileSystemTestSetup.projects.get(0) .getFolder(ZipFileSystemTestSetup.NESTED_ZIP_FILE_PARENT_NAME); - ensureExists(openedNestedZipFileParent); + ZipFileSystemTestSetup.ensureExists(openedNestedZipFileParent); String nestedPath = "sub1/Text.txt"; IFile nestedFile = openedNestedZipFileParent.getFile(nestedPath); - ensureExists(nestedFile); - assertTextFileContent(nestedFile, "Hello World!"); + ZipFileSystemTestSetup.ensureExists(nestedFile); String nestedPathShouldFail = "sub2"; IFolder nestedFileShouldFail = openedNestedZipFileParent.getFolder(nestedPathShouldFail); - ensureDoesNotExist(nestedFileShouldFail); + ZipFileSystemTestSetup.ensureDoesNotExist(nestedFileShouldFail); String deepNestedPath = "sub1/sub2/sub3/sub4/sub5/sub6/sub8/sub9/sub10/Text.txt"; IFile deepNestedFile = openedNestedZipFileParent.getFile(deepNestedPath); - ensureExists(deepNestedFile); - assertTextFileContent(deepNestedFile, "Hello World!"); + ZipFileSystemTestSetup.ensureExists(deepNestedFile); } @Test public void testOpenFakeZip() { try { - ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.firstProject, + ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.projects.get(0), ZipFileSystemTestSetup.FAKE_ZIP_FILE_NAME); - IFile fakeZipFile = ZipFileSystemTestSetup.firstProject.getFile(ZipFileSystemTestSetup.FAKE_ZIP_FILE_NAME); - ensureExists(fakeZipFile); + IFile fakeZipFile = ZipFileSystemTestSetup.projects.get(0) + .getFile(ZipFileSystemTestSetup.FAKE_ZIP_FILE_NAME); + ZipFileSystemTestSetup.ensureExists(fakeZipFile); - ZipFileSystemTestUtil.openZipFile(fakeZipFile); + ZipFileTransformer.openZipFile(fakeZipFile, true); fail("Expected an IOException due to incorrect file header."); } catch (CoreException e) { String expectedMessage = "The file is either empty or doesn't represent a ZIP file"; @@ -147,13 +144,13 @@ public void testOpenFakeZip() { @Test public void testOpenPasswordProtectedZip() { try { - ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.firstProject, + ZipFileSystemTestSetup.copyZipFileIntoProject(ZipFileSystemTestSetup.projects.get(0), ZipFileSystemTestSetup.PASSWORD_PROTECTED_ZIP_FILE_NAME); - IFile passwordProtectedZipFile = ZipFileSystemTestSetup.firstProject + IFile passwordProtectedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFile(ZipFileSystemTestSetup.PASSWORD_PROTECTED_ZIP_FILE_NAME); - ensureExists(passwordProtectedZipFile); + ZipFileSystemTestSetup.ensureExists(passwordProtectedZipFile); - ZipFileSystemTestUtil.openZipFile(passwordProtectedZipFile); + ZipFileTransformer.openZipFile(passwordProtectedZipFile, true); fail("Expected an IOException due to password protection."); } catch (CoreException e) { String expectedMessage = "Opening encrypted ZIP files is not supported:"; diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/RenameTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/RenameTest.java index 616e291e847..5a8852e3fe6 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/RenameTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/RenameTest.java @@ -12,12 +12,9 @@ package org.eclipse.core.tests.filesystem.zip; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.ensureDoesNotExist; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.ensureExists; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.getMonitor; - import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; +import org.eclipse.core.runtime.NullProgressMonitor; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.params.ParameterizedTest; @@ -27,7 +24,7 @@ public class RenameTest { @BeforeEach public void setup() throws Exception { - ZipFileSystemTestSetup.defaultSetup(); + ZipFileSystemTestSetup.setup(); } @AfterEach @@ -36,42 +33,42 @@ public void teardown() throws Exception { } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testRenameZipFile(String zipFileName) throws Exception { // IFolder is renamed by moving with the new path - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); - IFolder renamedOpenZipFile = ZipFileSystemTestSetup.firstProject + IFolder renamedOpenZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName + "Renamed"); - openedZipFile.move(renamedOpenZipFile.getFullPath(), false, getMonitor()); - ensureExists(renamedOpenZipFile); - ensureDoesNotExist(openedZipFile); + openedZipFile.move(renamedOpenZipFile.getFullPath(), false, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(renamedOpenZipFile); + ZipFileSystemTestSetup.ensureDoesNotExist(openedZipFile); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testRenameFileInsideOfZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); IFile textFile = openedZipFile.getFile(ZipFileSystemTestSetup.TEXT_FILE_NAME); IFile renamedTextFile = openedZipFile.getFile(textFile.getName() + "Renamed"); - textFile.move(renamedTextFile.getFullPath(), false, getMonitor()); - ensureExists(renamedTextFile); - ensureDoesNotExist(textFile); + textFile.move(renamedTextFile.getFullPath(), false, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(renamedTextFile); + ZipFileSystemTestSetup.ensureDoesNotExist(textFile); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testRenameFolderInsideOfZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); IFolder folder = openedZipFile.getFolder("newFolder"); - ensureDoesNotExist(folder); - folder.create(false, true, getMonitor()); - ensureExists(folder); + ZipFileSystemTestSetup.ensureDoesNotExist(folder); + folder.create(false, true, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(folder); IFolder renamedFolder = openedZipFile.getFolder(folder.getName() + "Renamed"); - folder.move(renamedFolder.getFullPath(), false, getMonitor()); - ensureExists(renamedFolder); - ensureDoesNotExist(folder); + folder.move(renamedFolder.getFullPath(), false, new NullProgressMonitor()); + ZipFileSystemTestSetup.ensureExists(renamedFolder); + ZipFileSystemTestSetup.ensureDoesNotExist(folder); } } diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/SetupTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/SetupTest.java index e226cc6bd7f..30a13b86625 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/SetupTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/SetupTest.java @@ -12,9 +12,6 @@ package org.eclipse.core.tests.filesystem.zip; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.assertTextFileContent; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.ensureExists; - import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; import org.junit.jupiter.api.AfterEach; @@ -26,7 +23,7 @@ public class SetupTest { @BeforeEach public void setup() throws Exception { - ZipFileSystemTestSetup.defaultSetup(); + ZipFileSystemTestSetup.setup(); } @AfterEach @@ -35,21 +32,20 @@ public void teardown() throws Exception { } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testZipFileInProject(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); - ensureExists(openedZipFile); + ZipFileSystemTestSetup.ensureExists(openedZipFile); } @ParameterizedTest - @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil#zipFileNames") + @MethodSource("org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestSetup#zipFileNames") public void testTextFileInZipFile(String zipFileName) throws Exception { - IFolder openedZipFile = ZipFileSystemTestSetup.firstProject + IFolder openedZipFile = ZipFileSystemTestSetup.projects.get(0) .getFolder(zipFileName); IFile textFile = openedZipFile.getFile(ZipFileSystemTestSetup.TEXT_FILE_NAME); - ensureExists(textFile); - assertTextFileContent(textFile, "Hello World!"); + ZipFileSystemTestSetup.ensureExists(textFile); } } diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestSetup.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestSetup.java index 0df65bbb50d..15c20fde553 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestSetup.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestSetup.java @@ -12,78 +12,73 @@ package org.eclipse.core.tests.filesystem.zip; -import static org.eclipse.core.tests.filesystem.zip.ZipFileSystemTestUtil.ensureExists; +import static org.junit.Assert.assertTrue; import java.io.IOException; -import java.net.URI; import java.net.URISyntaxException; 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.ArrayList; +import java.util.List; +import java.util.stream.Stream; +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.filesystem.IFileStore; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.resources.ZipFileTransformer; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.FileLocator; -import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Platform; class ZipFileSystemTestSetup { + static final String ZIP_FILE_NAME = "BasicText.zip"; + static final String JAR_FILE_NAME = "BasicText.jar"; + static final String WAR_FILE_NAME = "BasicText.war"; - static final String FIRST_PROJECT_NAME = "TestProject"; - static final String SECOND_PROJECT_NAME = "SecondProject"; - static final String ZIP_FILE_VIRTUAL_FOLDER_NAME = "BasicText.zip"; // Assuming the ZIP is represented as this - // folder - static final String JAR_FILE_VIRTUAL_FOLDER_NAME = "BasicText.jar"; - static final String WAR_FILE_VIRTUAL_FOLDER_NAME = "BasicText.war"; static final String EMPTY_ZIP_FILE_NAME = "Empty.zip"; + static final String FAKE_ZIP_FILE_NAME = "Fake.zip"; + static final String PASSWORD_PROTECTED_ZIP_FILE_NAME = "PasswordProtected.zip"; static final String NESTED_ZIP_FILE_PARENT_NAME = "NestedZipFileParent.zip"; static final String NESTED_ZIP_FILE_CHILD_NAME = "NestedZipFileChild.zip"; - static final String TEXT_FILE_NAME = "Text.txt"; static final String DEEP_NESTED_ZIP_FILE_NAME = "DeepNested.zip"; - static final String FAKE_ZIP_FILE_NAME = "Fake.zip"; - static final String PASSWORD_PROTECTED_ZIP_FILE_NAME = "PasswordProtected.zip"; - static IProject firstProject; - static IProject secondProject; - static IProgressMonitor progressMonitor = new NullProgressMonitor(); - - static void defaultSetup() throws Exception { - String[] defaultZipFileNames = { ZIP_FILE_VIRTUAL_FOLDER_NAME, JAR_FILE_VIRTUAL_FOLDER_NAME, - WAR_FILE_VIRTUAL_FOLDER_NAME }; - setup(defaultZipFileNames); - } - static void setup(String[] zipFileNames) throws Exception { - firstProject = createProject(FIRST_PROJECT_NAME); - refreshProject(firstProject); - for (String zipFileName : zipFileNames) { - copyZipFileIntoProject(firstProject, zipFileName); - refreshProject(firstProject); - ZipFileSystemTestUtil.openZipFile(firstProject.getFile(zipFileName)); - } + static final String TEXT_FILE_NAME = "Text.txt"; + + static List projects = new ArrayList<>(); + static List zipFileNames = List.of(ZIP_FILE_NAME, JAR_FILE_NAME, WAR_FILE_NAME); + + public static Stream zipFileNames() { + return zipFileNames.stream(); } - static void setupWithTwoProjects() throws Exception { - defaultSetup(); - secondProject = createProject(SECOND_PROJECT_NAME); - refreshProject(secondProject); - refreshEntireWorkspace(); + static void setup() throws Exception { + for (int i = 0; i <= 1; i++) { + projects.add(createProject("Project" + i)); + for (String zipFileName : zipFileNames) { + copyZipFileIntoProject(projects.get(i), zipFileName); + ZipFileTransformer.openZipFile(projects.get(i).getFile(zipFileName), true); + } + } } static void teardown() throws Exception { - deleteProject(firstProject); - deleteProject(secondProject); + deleteProjects(); } - private static void deleteProject(IProject project) throws CoreException { - if (project != null && project.exists()) { - project.delete(true, true, progressMonitor); - project = null; + private static void deleteProjects() throws CoreException { + for (IProject project : projects) { + if (project != null && project.exists()) { + project.delete(true, true, new NullProgressMonitor()); + project = null; + } } } @@ -92,71 +87,42 @@ static IProject createProject(String projectName) throws CoreException { IProject project = workspace.getRoot().getProject(projectName); if (!project.exists()) { - project.create(progressMonitor); + project.create(new NullProgressMonitor()); } - project.open(progressMonitor); + project.open(new NullProgressMonitor()); return project; } - private static void refreshProject(IProject project) { - try { - if (project.exists() && project.isOpen()) { - // Refreshing the specific project - project.refreshLocal(IResource.DEPTH_INFINITE, null); - } - } catch (CoreException e) { - e.printStackTrace(); - } - } - - public static void refreshEntireWorkspace() { - IWorkspace workspace = ResourcesPlugin.getWorkspace(); - try { - // IResource.DEPTH_INFINITE will cause all resources in the workspace to be - // refreshed. - workspace.getRoot().refreshLocal(IResource.DEPTH_INFINITE, null); - } catch (CoreException e) { - e.printStackTrace(); - } - } - static void copyZipFileIntoProject(IProject project, String zipFileName) throws IOException, CoreException { - // Resolve the source file URL from the plugin bundle - URL zipFileUrl = Platform.getBundle("org.eclipse.core.tests.resources") - .getEntry("resources/ZipFileSystem/" + zipFileName); - // Ensure proper conversion from URL to URI to Path - URL resolvedURL = FileLocator.resolve(zipFileUrl); // Resolves any redirection or bundling - java.nio.file.Path sourcePath; try { - // Convert URL to URI to Path correctly handling spaces and special characters - URI resolvedURI = resolvedURL.toURI(); - sourcePath = Paths.get(resolvedURI); + URL zipFileUrl = Platform.getBundle("org.eclipse.core.tests.resources") + .getEntry("resources/ZipFileSystem/" + zipFileName); + Path sourcePath = Paths.get(FileLocator.resolve(zipFileUrl).toURI()); + Path targetPath = project.getLocation().append(zipFileName).toFile().toPath(); + Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING); + project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor()); } catch (URISyntaxException e) { throw new IOException("Failed to resolve URI for the ZIP file", e); } + } - // Determine the target location within the project - java.nio.file.Path targetPath = Paths.get(project.getLocation().toOSString(), zipFileName); + static void ensureExistence(IResource resource, boolean shouldExist) throws CoreException, IOException { + IFileStore fileStore = EFS.getStore(resource.getLocationURI()); + boolean fileStoreExists = fileStore.fetchInfo().exists(); + assertTrue("File store existence check failed for: " + fileStore, fileStoreExists == shouldExist); - // Copy the file using java.nio.file.Files - Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING); + if (resource instanceof IFile file) { + assertTrue("File existence check failed for: " + file, file.exists() == shouldExist); + } else if (resource instanceof IFolder folder) { + assertTrue("Folder existence check failed for: " + folder, folder.exists() == shouldExist); + } + } - // Refresh the project to make Eclipse aware of the new file - project.refreshLocal(IResource.DEPTH_INFINITE, null); + static void ensureExists(IResource resource) throws CoreException, IOException { + ensureExistence(resource, true); } - static void copyAndOpenNestedZipFileIntoProject() throws IOException, CoreException, URISyntaxException { - copyZipFileIntoProject(firstProject, NESTED_ZIP_FILE_PARENT_NAME); - IFile nestedZipFileParent = firstProject.getFile(NESTED_ZIP_FILE_PARENT_NAME); - ensureExists(nestedZipFileParent); - ZipFileSystemTestUtil.openZipFile(nestedZipFileParent); - IFolder openedNestedZipFileParent = firstProject.getFolder(NESTED_ZIP_FILE_PARENT_NAME); - ensureExists(openedNestedZipFileParent); - IFile nestedZipFileChild = openedNestedZipFileParent.getFile(NESTED_ZIP_FILE_CHILD_NAME); - ensureExists(nestedZipFileChild); - ZipFileSystemTestUtil.openZipFile(nestedZipFileChild); - IFolder openedNestedZipFileChild = openedNestedZipFileParent - .getFolder(NESTED_ZIP_FILE_CHILD_NAME); - ensureExists(openedNestedZipFileChild); + static void ensureDoesNotExist(IResource resource) throws CoreException, IOException { + ensureExistence(resource, false); } } diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestUtil.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestUtil.java deleted file mode 100644 index 17165f214b9..00000000000 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/filesystem/zip/ZipFileSystemTestUtil.java +++ /dev/null @@ -1,143 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2024 Vector Informatik GmbH and others. - * - * This program and the accompanying materials are made available under the terms of the Eclipse - * Public License 2.0 which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: Vector Informatik GmbH - initial API and implementation - *******************************************************************************/ - -package org.eclipse.core.tests.filesystem.zip; - -import static org.junit.Assert.assertTrue; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.URISyntaxException; -import java.util.stream.Stream; -import org.eclipse.core.filesystem.EFS; -import org.eclipse.core.filesystem.IFileInfo; -import org.eclipse.core.filesystem.IFileStore; -import org.eclipse.core.resources.IContainer; -import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IFolder; -import org.eclipse.core.resources.IResource; -import org.eclipse.core.resources.ZipFileTransformer; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.tests.harness.FussyProgressMonitor; -import org.junit.Assert; - -class ZipFileSystemTestUtil { - - public static Stream zipFileNames() { - return Stream.of(ZipFileSystemTestSetup.ZIP_FILE_VIRTUAL_FOLDER_NAME, - ZipFileSystemTestSetup.JAR_FILE_VIRTUAL_FOLDER_NAME, - ZipFileSystemTestSetup.WAR_FILE_VIRTUAL_FOLDER_NAME); - } - - static void ensureExists(IResource resource) throws CoreException, IOException { - switch (resource.getType()) { - case IResource.FILE: { - IFileStore fileStore = EFS.getStore(resource.getLocationURI()); - ensureExistsInFileSystem(fileStore); - ensureExistsInWorkspace((IFile) resource); - break; - } - case IResource.FOLDER: { - IFileStore fileStore = EFS.getStore(resource.getLocationURI()); - ensureExistsInFileSystem(fileStore); - ensureExistsInWorkspace((IFolder) resource); - break; - } - default: - throw new IllegalArgumentException("Unexpected value: " + resource.getType()); - } - } - - static void ensureDoesNotExist(IResource resource) throws CoreException, IOException { - switch (resource.getType()) { - case IResource.FILE: { - IFileStore fileStore = EFS.getStore(resource.getLocationURI()); - ensureDoesNotExistInFileSystem(fileStore); - ensureDoesNotExistInWorkspace((IFile) resource); - break; - } - case IResource.FOLDER: { - IFileStore fileStore = EFS.getStore(resource.getLocationURI()); - ensureDoesNotExistInFileSystem(fileStore); - ensureDoesNotExistInWorkspace((IFolder) resource); - break; - } - default: - throw new IllegalArgumentException("Unexpected value: " + resource.getType()); - } - } - - static void assertTextFileContent(IFile textFile, String expectedContent) throws IOException, CoreException { - try (InputStreamReader isr = new InputStreamReader(textFile.getContents()); - BufferedReader reader = new BufferedReader(isr)) { - String content = reader.readLine(); // Assuming the file has a single line with "Hello World!" - Assert.assertEquals("The content of " + textFile.getName() + " should be '" + expectedContent + "'", - expectedContent, content); - } - } - - private static void ensureDoesNotExistInFileSystem(IFileStore store) throws CoreException { - assertTrue("store was not properly deleted: " + store, !store.fetchInfo().exists()); - } - - private static void ensureExistsInFileSystem(IFileStore store) throws CoreException, IOException { - final IFileInfo info = store.fetchInfo(); - assertTrue("file info for store does not exist: " + store, info.exists()); - } - - private static void ensureDoesNotExistInWorkspace(IFile file) throws CoreException { - assertTrue("file was not properly deleted: " + file, !file.exists()); - } - - private static void ensureDoesNotExistInWorkspace(IFolder folder) throws CoreException { - assertTrue("folder was not properly deleted: " + folder, !folder.exists()); - } - - private static void ensureExistsInWorkspace(IFile file) throws CoreException, IOException { - assertTrue("file does not exist in workspace: " + file, file.exists()); - } - - private static void ensureExistsInWorkspace(IFolder folder) throws CoreException, IOException { - assertTrue("folder does not exist in workspace: " + folder, folder.exists()); - } - - static IProgressMonitor getMonitor() { - return new FussyProgressMonitor(); - } - - static void openZipFile(IFile file) throws URISyntaxException, CoreException, IOException { - ZipFileTransformer.openZipFile(file, false); - } - - static void openZipFileBackground(IFile file) throws URISyntaxException, CoreException, IOException { - ZipFileTransformer.openZipFile(file, true); - } - - static void closeZipFile(IFolder folder) throws Exception { - ZipFileTransformer.closeZipFile(folder); - } - - static void printContents(IContainer container, String indent) throws CoreException { - IResource[] members = container.members(); - for (IResource member : members) { - if (member instanceof IFile) { - System.out.println(indent + "File: " + member.getName()); - } else if (member instanceof IContainer) { // This can be IFolder or IProject - System.out.println(indent + "Folder: " + member.getName()); - printContents((IContainer) member, indent + " "); // Recursively print contents - } - } - } - -} From 662e26cd523432ab562ea6959d3a81344fa2b3b6 Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Mon, 19 Aug 2024 16:57:15 +0200 Subject: [PATCH 32/35] add proper viewer refresh --- .../core/resources/ZipFileTransformer.java | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java index 235262ddd0e..2dab9638c6b 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java @@ -46,18 +46,29 @@ public class ZipFileTransformer { * */ public static void closeZipFile(IFolder folder) throws URISyntaxException, CoreException { + Workspace workspace = ((Workspace) folder.getWorkspace()); IProject project = folder.getProject(); - URI zipURI = new URI(folder.getLocationURI().getQuery()); - IFileStore parentStore = EFS.getStore(folder.getParent().getLocationURI()); - URI childURI = parentStore.getChild(folder.getName()).toURI(); - if (URIUtil.equals(zipURI, childURI)) { - folder.delete(IResource.CLOSE_ZIP_FILE, null); - project.refreshLocal(IResource.DEPTH_INFINITE, null); - } else { - throw new CoreException(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, - "Closing of Zip File " + folder.getName() //$NON-NLS-1$ - + " failed because the Zip File is not local.")); //$NON-NLS-1$ - } + final ISchedulingRule rule = workspace.getRuleFactory().createRule(project); + IWorkspaceRunnable runnable = monitor -> { + try { + URI zipURI = new URI(folder.getLocationURI().getQuery()); + + IFileStore parentStore = EFS.getStore(folder.getParent().getLocationURI()); + URI childURI = parentStore.getChild(folder.getName()).toURI(); + if (URIUtil.equals(zipURI, childURI)) { + folder.delete(IResource.CLOSE_ZIP_FILE, null); + project.refreshLocal(IResource.DEPTH_INFINITE, null); + } else { + throw new CoreException(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, + "Closing of Zip File " + folder.getName() //$NON-NLS-1$ + + " failed because the Zip File is not local.")); //$NON-NLS-1$ + } + } catch (URISyntaxException e) { + throw new CoreException(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, e.getMessage())); + } + }; + workspace.run(runnable, rule, IWorkspace.AVOID_UPDATE, null); + project.refreshLocal(IResource.DEPTH_INFINITE, null); } /** @@ -118,5 +129,6 @@ public static void openZipFile(IFile file, boolean backgroundRefresh) }; workspace.run(runnable, rule, IWorkspace.AVOID_UPDATE, null); + project.refreshLocal(IResource.DEPTH_INFINITE, null); } } From 4202dea0636d383b43b7324968d9e0c2dd5c0215 Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Tue, 20 Aug 2024 12:22:21 +0200 Subject: [PATCH 33/35] atomic zip file open working --- .../core/resources/ZipFileTransformer.java | 42 +++++++------------ 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java index 2dab9638c6b..8a1649674b6 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ZipFileTransformer.java @@ -21,6 +21,7 @@ import org.eclipse.core.filesystem.IFileStore; import org.eclipse.core.filesystem.URIUtil; import org.eclipse.core.filesystem.ZipFileUtil; +import org.eclipse.core.internal.resources.Resource; import org.eclipse.core.internal.resources.Workspace; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; @@ -46,29 +47,19 @@ public class ZipFileTransformer { * */ public static void closeZipFile(IFolder folder) throws URISyntaxException, CoreException { - Workspace workspace = ((Workspace) folder.getWorkspace()); IProject project = folder.getProject(); - final ISchedulingRule rule = workspace.getRuleFactory().createRule(project); - IWorkspaceRunnable runnable = monitor -> { - try { - URI zipURI = new URI(folder.getLocationURI().getQuery()); + URI zipURI = new URI(folder.getLocationURI().getQuery()); + IFileStore parentStore = EFS.getStore(folder.getParent().getLocationURI()); + URI childURI = parentStore.getChild(folder.getName()).toURI(); - IFileStore parentStore = EFS.getStore(folder.getParent().getLocationURI()); - URI childURI = parentStore.getChild(folder.getName()).toURI(); - if (URIUtil.equals(zipURI, childURI)) { - folder.delete(IResource.CLOSE_ZIP_FILE, null); - project.refreshLocal(IResource.DEPTH_INFINITE, null); - } else { - throw new CoreException(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, - "Closing of Zip File " + folder.getName() //$NON-NLS-1$ - + " failed because the Zip File is not local.")); //$NON-NLS-1$ - } - } catch (URISyntaxException e) { - throw new CoreException(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, e.getMessage())); - } - }; - workspace.run(runnable, rule, IWorkspace.AVOID_UPDATE, null); - project.refreshLocal(IResource.DEPTH_INFINITE, null); + if (URIUtil.equals(zipURI, childURI)) { + folder.delete(IResource.CLOSE_ZIP_FILE, null); + project.refreshLocal(IResource.DEPTH_INFINITE, null); + } else { + throw new CoreException(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, + "Closing of Zip File " + folder.getName() //$NON-NLS-1$ + + " failed because the Zip File is not local.")); //$NON-NLS-1$ + } } /** @@ -108,12 +99,11 @@ public static void openZipFile(IFile file, boolean backgroundRefresh) IWorkspaceRunnable runnable = monitor -> { try (InputStream fis = file.getContents()) { ZipFileUtil.canZipFileBeOpened(fis); - // Additional operations can continue here if header is correct URI zipURI = new URI("zip", null, "/", file.getLocationURI().toString(), null); //$NON-NLS-1$ //$NON-NLS-2$ IFolder link = file.getParent().getFolder(IPath.fromOSString(file.getName())); - int flags = backgroundRefresh ? IResource.REPLACE | IResource.BACKGROUND_REFRESH : IResource.REPLACE; - - link.createLink(zipURI, flags, monitor); + ((Resource) file).deleteResource(false, null); + workspace.broadcastPostChange(); + link.createLink(zipURI, IResource.BACKGROUND_REFRESH, monitor); project.refreshLocal(IResource.DEPTH_INFINITE, monitor); } catch (IOException e) { if (e instanceof ZipException && e.getMessage().equals("encrypted ZIP entry not supported")) { //$NON-NLS-1$ @@ -127,8 +117,6 @@ public static void openZipFile(IFile file, boolean backgroundRefresh) new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, "Zip File could not be opened")); //$NON-NLS-1$ } }; - workspace.run(runnable, rule, IWorkspace.AVOID_UPDATE, null); - project.refreshLocal(IResource.DEPTH_INFINITE, null); } } From 56817fbcdcb261f6bca6a0ed0aa106e4a30a6d3d Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Fri, 23 Aug 2024 14:59:47 +0200 Subject: [PATCH 34/35] Refactor --- .../eclipse/core/filesystem/ZipFileUtil.java | 20 +++++---- .../internal/filesystem/zip/ZipFileStore.java | 45 ++++++++++--------- .../internal/resources/VirtualZipFolder.java | 26 +++-------- .../content/ZipFileContentDescriber.java | 1 - 4 files changed, 40 insertions(+), 52 deletions(-) diff --git a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/filesystem/ZipFileUtil.java b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/filesystem/ZipFileUtil.java index a98e963cda2..e87ca9b7140 100644 --- a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/filesystem/ZipFileUtil.java +++ b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/filesystem/ZipFileUtil.java @@ -20,19 +20,13 @@ import org.eclipse.core.runtime.CoreException; /** - * Utility class to determine if a file is ZIP archive. + * Utility class for zip files. * * @since 1.11 */ public class ZipFileUtil { - /** - * Determines if the given {@link IFileStore} represents an open ZIP file. - * This can be used to check if operations on a ZIP file should be allowed or handled differently. - * - * @param store The file store to check. - * @return true if the store is an instance of {@link ZipFileStore}, false otherwise. - */ + public static boolean isInsideOpenZipFile(IFileStore store) { return store instanceof ZipFileStore; } @@ -51,6 +45,13 @@ public static boolean isInsideOpenZipFile(URI locationURI) { return isInsideOpenZipFile(store); } + /** + * Determines if the given {@link IFileStore} represents an open ZIP file. + * This can be used to check if operations on a ZIP file should be allowed or handled differently. + * + * @param store The file store to check. + * @return true if the store is an instance of {@link ZipFileStore}, false otherwise. + */ public static boolean isOpenZipFile(IFileStore store) { if (isInsideOpenZipFile(store)) { ZipFileStore zipStore = (ZipFileStore) store; @@ -59,6 +60,9 @@ public static boolean isOpenZipFile(IFileStore store) { return false; } + /** + * @see ZipFileUtil#isOpenZipFile(IFileStore) + */ public static boolean isOpenZipFile(URI locationURI) { IFileStore store; try { diff --git a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java index 832bbf9175c..7bf55c54a76 100644 --- a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java +++ b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/zip/ZipFileStore.java @@ -1,14 +1,15 @@ /******************************************************************************* - * Copyright (c) 2022 IBM Corporation and others. + * Copyright (c) 2024 Vector Informatik GmbH and others. * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which accompanies this distribution, - * and is available at https://www.eclipse.org/legal/epl-2.0/ + * This program and the accompanying materials are made available under the terms of the Eclipse + * Public License 2.0 which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ * * SPDX-License-Identifier: EPL-2.0 * - * Contributors: IBM Corporation - initial API and implementation + * Contributors: Vector Informatik GmbH - initial API and implementation *******************************************************************************/ + package org.eclipse.core.internal.filesystem.zip; import java.io.ByteArrayOutputStream; @@ -53,7 +54,19 @@ * @since 1.11 */ public class ZipFileStore extends FileStore { + + /** + * A thread-safe map that associates each zip file's URI with a corresponding {@link ReentrantLock}. + *

+ * This map is used to ensure that each zip file is accessed by only one thread at a time, preventing + * concurrent access issues. The keys in the map are {@link URI} objects representing the zip files, and + * the values are {@link ReentrantLock} objects that are used to control access to the corresponding zip file. + * The map itself is wrapped with {@link Collections#synchronizedMap(Map)} to ensure thread safety + * when accessing the map. + *

+ */ private static final Map uriLockMap = Collections.synchronizedMap(new HashMap<>()); + /** * The path of this store within the zip file. */ @@ -64,9 +77,6 @@ public class ZipFileStore extends FileStore { */ private final IFileStore rootStore; - /** - * Creates a new zip file store. - */ public ZipFileStore(IFileStore rootStore, IPath path) { this.rootStore = rootStore; this.path = path.makeRelative(); @@ -85,7 +95,6 @@ private ZipEntry[] childEntries(IProgressMonitor monitor) throws CoreException { } } - @Override public IFileInfo[] childInfos(int options, IProgressMonitor monitor) throws CoreException { ZipEntry[] entries = childEntries(monitor); @@ -108,16 +117,13 @@ public String[] childNames(int options, IProgressMonitor monitor) throws CoreExc return names; } - /** - * Computes the simple file name for a given zip entry. - */ private static String computeName(ZipEntry entry) { String name = entry.getName(); // removes "/" at the end if (name.endsWith("/")) { //$NON-NLS-1$ name = name.substring(0, name.length() - 1); } - // creates last segment after last / + int lastIndex = name.lastIndexOf('/'); if (lastIndex != -1) { @@ -138,12 +144,6 @@ private IFileInfo convertToIFileInfo(Path zipEntryPath, BasicFileAttributes attr return info; } - /** - * Creates a file info object corresponding to a given zip entry - * - * @param entry the zip entry - * @return The file info for a zip entry - */ private static IFileInfo convertZipEntryToFileInfo(ZipEntry entry) { FileInfo info = new FileInfo(computeName(entry)); if (entry.isDirectory()) { @@ -308,6 +308,9 @@ private String getPluginId() { return FrameworkUtil.getBundle(this.getClass()).getSymbolicName(); } + /** + * Returns the path of this file store. + */ public IPath getPath() { return path; } @@ -424,7 +427,6 @@ public void close() throws IOException { // Write the ByteArrayOutputStream's data to the entry // in the ZIP file Files.write(entryPath, this.toByteArray(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); - } catch (Exception e) { throw new IOException("Failed to integrate data into ZIP file", e); //$NON-NLS-1$ } finally { @@ -499,7 +501,6 @@ public URI toURI() { try { return new URI(scheme, null, pathString, rootStoreQuery, null); } catch (URISyntaxException e) { - // should not happen throw new RuntimeException(e); } } @@ -513,7 +514,7 @@ private URI toNioURI() throws URISyntaxException { return new URI(ret); } - void unlock() throws CoreException { + private void unlock() throws CoreException { try { ReentrantLock lock = getLockForURI(toNioURI()); if (lock.isHeldByCurrentThread()) { diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/VirtualZipFolder.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/VirtualZipFolder.java index ec4c9f52eb7..de73ce40c8f 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/VirtualZipFolder.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/VirtualZipFolder.java @@ -1,16 +1,15 @@ /******************************************************************************* - * Copyright (c) 2024 IBM Corporation and others. + * Copyright (c) 2024 Vector Informatik GmbH and others. * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at + * This program and the accompanying materials are made available under the terms of the Eclipse + * Public License 2.0 which accompanies this distribution, and is available at * https://www.eclipse.org/legal/epl-2.0/ * * SPDX-License-Identifier: EPL-2.0 * - * Contributors: - * IBM Corporation - initial API and implementation + * Contributors: Vector Informatik GmbH - initial API and implementation *******************************************************************************/ + package org.eclipse.core.internal.resources; import java.net.URISyntaxException; @@ -38,23 +37,15 @@ public VirtualZipFolder(IPath path, Workspace container) { @Override public void copy(IPath destination, int updateFlags, IProgressMonitor monitor) throws CoreException { try { - // Close the current ZIP file ZipFileTransformer.closeZipFile(this); - - // Get the file representing the closed ZIP IFile closedZipFile = this.getParent().getFile(new Path(this.getName())); - - // Copy the closed ZIP file to the destination closedZipFile.copy(destination, updateFlags, monitor); - - // Get the copied ZIP file at the new destination IFile copiedZipFile = ResourcesPlugin.getWorkspace().getRoot().getFile(destination); // If the destination is not a nested ZIP, open the copied ZIP file if (!ZipFileUtil.isInsideOpenZipFile(copiedZipFile.getLocationURI())) { ZipFileTransformer.openZipFile(copiedZipFile, false); } - // Reopen the original ZIP file if (!ZipFileUtil.isInsideOpenZipFile(closedZipFile.getLocationURI())) { ZipFileTransformer.openZipFile(closedZipFile, false); @@ -85,16 +76,9 @@ public void delete(int updateFlags, IProgressMonitor monitor) throws CoreExcepti @Override public void move(IPath destination, int updateFlags, IProgressMonitor monitor) throws CoreException { try { - // Close the current ZIP file ZipFileTransformer.closeZipFile(this); - - // Get the file representing the closed ZIP IFile closedZipFile = this.getParent().getFile(new Path(this.getName())); - - // Move the closed ZIP file to the destination closedZipFile.move(destination, updateFlags, monitor); - - // Get the moved ZIP file at the new destination IFile movedZipFile = ResourcesPlugin.getWorkspace().getRoot().getFile(destination); // If the destination is not a nested ZIP, open the moved ZIP file diff --git a/runtime/bundles/org.eclipse.core.contenttype/src/org/eclipse/core/runtime/content/ZipFileContentDescriber.java b/runtime/bundles/org.eclipse.core.contenttype/src/org/eclipse/core/runtime/content/ZipFileContentDescriber.java index ed1ef2b556f..ff1e9f88eee 100644 --- a/runtime/bundles/org.eclipse.core.contenttype/src/org/eclipse/core/runtime/content/ZipFileContentDescriber.java +++ b/runtime/bundles/org.eclipse.core.contenttype/src/org/eclipse/core/runtime/content/ZipFileContentDescriber.java @@ -17,7 +17,6 @@ /** * @since 3.10 - * */ public class ZipFileContentDescriber extends TextContentDescriber { From ba2cef4f5aa3017401bfec7c69fb7944c3be5f58 Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Mon, 26 Aug 2024 13:23:19 +0200 Subject: [PATCH 35/35] revert new file .project --- .../.project | 22 ------------------- 1 file changed, 22 deletions(-) delete mode 100644 resources/bundles/org.eclipse.core.filesystem.win32.x86_64/.project diff --git a/resources/bundles/org.eclipse.core.filesystem.win32.x86_64/.project b/resources/bundles/org.eclipse.core.filesystem.win32.x86_64/.project deleted file mode 100644 index affc913b876..00000000000 --- a/resources/bundles/org.eclipse.core.filesystem.win32.x86_64/.project +++ /dev/null @@ -1,22 +0,0 @@ - - - org.eclipse.core.filesystem.win32.x86_64 - - - - - - org.eclipse.pde.ManifestBuilder - - - - - org.eclipse.pde.SchemaBuilder - - - - - - org.eclipse.pde.PluginNature - -