Skip to content

Commit

Permalink
#4919 - Unable to import project with knowledge-based exported by INC…
Browse files Browse the repository at this point in the history
…EpTION 33.0 (again)

- Fixed issue and added test
  • Loading branch information
reckart committed Jul 2, 2024
1 parent 5a6454f commit 3aa7a23
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package de.tudarmstadt.ukp.clarin.webanno.api.export;

import static org.apache.commons.io.FilenameUtils.normalize;
import static org.apache.commons.lang3.StringUtils.isEmpty;
import static org.apache.commons.lang3.StringUtils.removeStart;

import java.io.IOException;
Expand Down Expand Up @@ -56,23 +57,30 @@ void importData(ProjectImportRequest aRequest, Project aProject, ExportedProject

static String normalizeEntryName(ZipEntry aEntry)
{
// Strip leading "/" that we had in ZIP files prior to 2.0.8 (bug #985)
var entryName = aEntry.toString();
if (entryName.startsWith("/")) {
entryName = entryName.substring(1);
var name = removeStart(normalize(aEntry.getName(), true), "/");

if (isEmpty(name)) {
return null;
}

return entryName;
return name;
}

static ZipEntry getEntry(ZipFile aFile, String aEntryName)
{
var entryName = new ZipEntry(removeStart(normalize(aEntryName, true), "/"));
var entry = aFile.getEntry(aEntryName);
var normalizedEntryName = normalize(aEntryName, true);

var entry = aFile.getEntry(normalizedEntryName);
if (entry != null) {
return entry;
}
return aFile.getEntry("/" + entryName);

if (normalizedEntryName.startsWith("/")) {
return aFile.getEntry(removeStart(normalizedEntryName, "/"));
}
else {
return aFile.getEntry("/" + normalizedEntryName);
}
}

static void writeEntry(ZipOutputStream aStage, String aEntryName,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Licensed to the Technische Universität Darmstadt under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The Technische Universität Darmstadt
* licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.tudarmstadt.ukp.clarin.webanno.api.export;

import static de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExporter.getEntry;
import static de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExporter.normalizeEntryName;
import static de.tudarmstadt.ukp.clarin.webanno.api.export.ProjectExporter.writeEntry;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.FileOutputStream;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

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

class ProjectExporterTest
{

@Test
void testGetEntry(@TempDir Path tempDir) throws Exception
{
var zipFile = tempDir.resolve("test.zip").toFile();

try (var fos = new FileOutputStream(zipFile); var zos = new ZipOutputStream(fos)) {
zos.putNextEntry(new ZipEntry("/foo.txt"));
zos.write("foo".getBytes(UTF_8));
zos.closeEntry();

zos.putNextEntry(new ZipEntry("bar.txt"));
zos.write("bar".getBytes(UTF_8));
zos.closeEntry();
}

try (var zip = new ZipFile(zipFile)) {
assertThat(getEntry(zip, "/foo.txt")) //
.isNotNull() //
.extracting(ZipEntry::getName) //
.isEqualTo("/foo.txt");

assertThat(getEntry(zip, "foo.txt")) //
.isNotNull() //
.extracting(ZipEntry::getName) //
.isEqualTo("/foo.txt");

assertThat(getEntry(zip, "/bar.txt")) //
.isNotNull() //
.extracting(ZipEntry::getName) //
.isEqualTo("bar.txt");

assertThat(getEntry(zip, "bar.txt")) //
.isNotNull() //
.extracting(ZipEntry::getName) //
.isEqualTo("bar.txt");

assertThat(getEntry(zip, "nonexistent.txt")) //
.isNull();
}
}

@Test
void testWriteEntry(@TempDir Path tempDir) throws Exception
{
var zipFile = tempDir.resolve("test.zip").toFile();

try (var fos = new FileOutputStream(zipFile); var zos = new ZipOutputStream(fos)) {
writeEntry(zos, "/foo.txt", os -> os.write("foo".getBytes(UTF_8)));
writeEntry(zos, "bar.txt", os -> os.write("bar".getBytes(UTF_8)));
}

try (var zf = new ZipFile(zipFile)) {
assertThat(zf.getEntry("foo.txt")) //
.isNotNull() //
.extracting(ZipEntry::getName) //
.isEqualTo("foo.txt");
assertThat(zf.getEntry("bar.txt")) //
.isNotNull() //
.extracting(ZipEntry::getName) //
.isEqualTo("bar.txt");
}
}

@Test
void testNormalizeEntryName()
{
// Test entry with leading slash
assertThat(normalizeEntryName(new ZipEntry("/foo.txt"))).isEqualTo("foo.txt");

// Test entry without leading slash
assertThat(normalizeEntryName(new ZipEntry("bar.txt"))).isEqualTo("bar.txt");

// Test entry with multiple leading slashes
assertThat(normalizeEntryName(new ZipEntry("///baz.txt"))).isNull();

// Test entry with no name
assertThat(normalizeEntryName(new ZipEntry(""))).isNull();
}
}

0 comments on commit 3aa7a23

Please sign in to comment.