Skip to content

Commit

Permalink
Merge pull request kitodo#6079 from slub/replace_junit4_with_junit5_k…
Browse files Browse the repository at this point in the history
…itodo-dataeditor

[Kitodo-DataEditor] Replace Junit4 with Junit5
  • Loading branch information
solth authored May 23, 2024
2 parents aa0fc91 + 8193ddc commit 77888b7
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 237 deletions.
4 changes: 0 additions & 4 deletions Kitodo-DataEditor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,42 @@

package org.kitodo.dataeditor;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class DataEditorTest {

private URI xsltFile = Paths.get("src/test/resources/xslt/MetsModsGoobi_to_MetsKitodo.xsl").toUri();
private final DataEditor dataEditor = new DataEditor();
private final URI xsltFile = Paths.get("src/test/resources/xslt/MetsModsGoobi_to_MetsKitodo.xsl").toUri();
private static byte[] testMetaOldFormat;
private static byte[] testmetaUnsupportedFormat;
private static final String pathOfOldMetaFormat = "src/test/resources/testmetaOldFormat.xml";

@Before
@BeforeEach
public void saveFile() throws IOException {
File file = new File(pathOfOldMetaFormat);
testMetaOldFormat = IOUtils.toByteArray(file.toURI());
file = new File("src/test/resources/testmetaUnsupportedFormat.xml");
testmetaUnsupportedFormat = IOUtils.toByteArray(file.toURI());
}

@After
@AfterEach
public void revertFile() throws IOException {
IOUtils.write( testMetaOldFormat, Files.newOutputStream(Paths.get(pathOfOldMetaFormat)));
IOUtils.write( testmetaUnsupportedFormat, Files.newOutputStream(Paths.get("src/test/resources/testmetaUnsupportedFormat.xml")));
}

@Rule
public ExpectedException expectedException = ExpectedException.none();

private DataEditor dataEditor = new DataEditor();

@Test
public void shouldReadMetadata() throws IOException {
dataEditor.readData(Paths.get("src/test/resources/testmeta.xml").toUri(), xsltFile);
Expand All @@ -66,33 +63,42 @@ public void shouldReadOldMetadata() throws IOException {
}

@Test
public void shouldNotReadOldMetadataWithNotExistingXslt() throws IOException {
public void shouldNotReadOldMetadataWithNotExistingXslt() {
URI xsltFile = Paths.get("src/test/resources/xslt/not_existing.xsl").toUri();
expectedException.expect(IOException.class);
expectedException.expectMessage("Xslt file [" + xsltFile.getPath()
+ "] for transformation of goobi format metadata files was not found. Please check your local config!");
dataEditor.readData(Paths.get(pathOfOldMetaFormat).toUri(), xsltFile);
Exception exception = assertThrows(IOException.class,
() -> dataEditor.readData(Paths.get(pathOfOldMetaFormat).toUri(), xsltFile)
);

assertEquals("Xslt file [" + xsltFile.getPath() + "] for transformation of goobi format metadata files was not found. Please check your local config!",
exception.getMessage());
}

@Test
public void shouldNotReadInvalidMetadata() throws IOException {
expectedException.expect(IOException.class);
expectedException.expectMessage("Unable to read file");
dataEditor.readData(Paths.get("src/test/resources/testmetaInvalid.xml").toUri(), xsltFile);
public void shouldNotReadInvalidMetadata() {
Exception exception;
exception = assertThrows(IOException.class,
() -> dataEditor.readData(Paths.get("src/test/resources/testmetaInvalid.xml").toUri(), xsltFile)
);

assertEquals("Unable to read file", exception.getMessage());
}

@Test
public void shouldNotReadUnsupportedMetadata() throws IOException {
expectedException.expect(IOException.class);
expectedException.expectMessage("Can not read data because of not supported format!");
dataEditor.readData(Paths.get("src/test/resources/testmetaUnsupportedFormat.xml").toUri(), xsltFile);
public void shouldNotReadUnsupportedMetadata() {
Exception exception = assertThrows(IOException.class,
() -> dataEditor.readData(Paths.get("src/test/resources/testmetaUnsupportedFormat.xml").toUri(), xsltFile)
);

assertEquals("Can not read data because of not supported format!", exception.getMessage());
}

@Test
public void shouldNotReadMetadataOfNotExistingFile() throws IOException {
public void shouldNotReadMetadataOfNotExistingFile() {
URI notExistingUri = Paths.get("notExisting.xml").toUri();
expectedException.expect(IOException.class);
expectedException.expectMessage("File was not found: " + notExistingUri.getPath());
dataEditor.readData(notExistingUri, xsltFile);
Exception exception = assertThrows(IOException.class,
() -> dataEditor.readData(notExistingUri, xsltFile)
);

assertEquals("File was not found: " + notExistingUri.getPath(), exception.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

package org.kitodo.dataeditor;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.File;
import java.io.IOException;
import java.net.URI;
Expand All @@ -22,28 +24,27 @@
import javax.xml.transform.TransformerException;

import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.kitodo.dataformat.metskitodo.KitodoType;
import org.kitodo.dataformat.metskitodo.MetadataGroupType;
import org.kitodo.dataformat.metskitodo.MetadataType;
import org.kitodo.dataformat.metskitodo.Mets;

public class MetsKitodoConverterTest {
private URI xmlfile = Paths.get("src/test/resources/testmetaOldFormat.xml").toUri();
private URI xsltFile = Paths.get("src/test/resources/xslt/MetsModsGoobi_to_MetsKitodo.xsl").toUri();
private final URI xmlfile = Paths.get("src/test/resources/testmetaOldFormat.xml").toUri();
private final URI xsltFile = Paths.get("src/test/resources/xslt/MetsModsGoobi_to_MetsKitodo.xsl").toUri();
private static final String pathOfOldMetaFormat = "src/test/resources/testmetaOldFormat.xml";
private static byte[] testMetaOldFormat;

@Before
@BeforeEach
public void saveFile() throws IOException {
File file = new File("src/test/resources/testmetaOldFormat.xml");
testMetaOldFormat = IOUtils.toByteArray(file.toURI());
}

@After
@AfterEach
public void revertFile() throws IOException {
IOUtils.write( testMetaOldFormat, Files.newOutputStream(Paths.get(pathOfOldMetaFormat)));
}
Expand All @@ -55,18 +56,28 @@ public void shouldReadKitodoMetadataFormOldFormatFile() throws JAXBException, Tr
KitodoType kitodoType = (KitodoType) jaxbElement.getValue();

MetadataType metadataType = kitodoType.getMetadata().get(1);
Assert.assertEquals("Reading data of type 'name' out of kitodo format was not correct", "PublisherName",
metadataType.getName());
Assert.assertEquals("Reading content metadata out of kitodo format was not correct", "Test Publisher",
metadataType.getValue());
assertEquals("PublisherName",
metadataType.getName(),
"Reading data of type 'name' out of kitodo format was not correct");
assertEquals("Test Publisher",
metadataType.getValue(),
"Reading content metadata out of kitodo format was not correct");

MetadataGroupType metadataGroup = kitodoType.getMetadataGroup().get(0);
Assert.assertEquals("Converting of metadata group was wrong at name attribute","TypeOfResource", metadataGroup.getName());
Assert.assertEquals("Converting of metadata group was wrong at metadata child element","Handschrift", metadataGroup.getMetadata().get(0).getValue());
assertEquals("TypeOfResource",
metadataGroup.getName(),
"Converting of metadata group was wrong at name attribute");
assertEquals("Handschrift",
metadataGroup.getMetadata().get(0).getValue(),
"Converting of metadata group was wrong at metadata child element");

MetadataGroupType personMetadataGroup = kitodoType.getMetadataGroup().get(1);
Assert.assertEquals("Converting of person was wrong at name attribute","person", personMetadataGroup.getName());
Assert.assertEquals("Converting of person was wrong at metadata child element","FormerOwner", personMetadataGroup.getMetadata().get(0).getValue());
assertEquals("person",
personMetadataGroup.getName(),
"Converting of person was wrong at name attribute");
assertEquals("FormerOwner",
personMetadataGroup.getMetadata().get(0).getValue(),
"Converting of person was wrong at metadata child element");

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

package org.kitodo.dataeditor;

import org.junit.Assert;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class MetsKitodoNamespacePrefixMapperTest {
MetsKitodoNamespacePrefixMapper mapper = new MetsKitodoNamespacePrefixMapper();
Expand All @@ -23,10 +24,9 @@ public void shouldReturnPrefix() {
String preferredMetsPrefix = mapper.getPreferredPrefix("http://www.loc.gov/METS/", null, true);
String preferredXlinkPrefix = mapper.getPreferredPrefix("http://www.w3.org/1999/xlink", null, true);
String notExistingPrefix = mapper.getPreferredPrefix("http://not.existing", "return this", true);
Assert.assertEquals("Prefix mapper return the wrong prefix for kitodo uri", "kitodo", preferredKitodoPrefix);
Assert.assertEquals("Prefix mapper return the wrong prefix for mets uri", "mets", preferredMetsPrefix);
Assert.assertEquals("Prefix mapper return the wrong prefix for xlink uri", "xlink", preferredXlinkPrefix);
Assert.assertEquals("Prefix mapper return the wrong prefix for not existing uri", "return this",
notExistingPrefix);
assertEquals("kitodo", preferredKitodoPrefix, "Prefix mapper return the wrong prefix for kitodo uri");
assertEquals("mets", preferredMetsPrefix, "Prefix mapper return the wrong prefix for mets uri");
assertEquals("xlink", preferredXlinkPrefix, "Prefix mapper return the wrong prefix for xlink uri");
assertEquals("return this", notExistingPrefix, "Prefix mapper return the wrong prefix for not existing uri");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

package org.kitodo.dataeditor;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -19,48 +22,46 @@
import javax.xml.bind.JAXBException;

import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.kitodo.dataformat.metskitodo.Mets;

public class MetsKitodoValidatorTest {
private static byte[] testMetaOldFormat;
private static final String pathOfOldMetaFormat = "src/test/resources/testmetaOldFormat.xml";

@Before
@BeforeEach
public void saveFile() throws IOException {
File file = new File("src/test/resources/testmetaOldFormat.xml");
testMetaOldFormat = IOUtils.toByteArray(file.toURI());
}

@After
@AfterEach
public void revertFile() throws IOException {
IOUtils.write( testMetaOldFormat, Files.newOutputStream(Paths.get(pathOfOldMetaFormat)));
}

@Test
public void shouldCheckValidMetsObject() throws JAXBException, IOException {
Mets mets = MetsKitodoReader.readUriToMets(Paths.get("./src/test/resources/testmeta.xml").toUri());
Assert.assertTrue("Result of validation of Mets object was not true!",
MetsKitodoValidator.checkMetsKitodoFormatOfMets(mets));
assertTrue(MetsKitodoValidator.checkMetsKitodoFormatOfMets(mets),
"Result of validation of Mets object was not true!");
}

@Test
public void shouldCheckOldFormatMetsObject() throws JAXBException, IOException {
Mets mets = MetsKitodoReader.readUriToMets(Paths.get("./src/test/resources/testmetaOldFormat.xml").toUri());
Assert.assertFalse("Result of validation of Mets object was not false!",
MetsKitodoValidator.checkMetsKitodoFormatOfMets(mets));
assertFalse(MetsKitodoValidator.checkMetsKitodoFormatOfMets(mets),
"Result of validation of Mets object was not false!");
}

@Test
public void shouldMetsContainsMetadataAtMdSecIndex() throws JAXBException, IOException {
Mets mets = MetsKitodoReader.readUriToMets(Paths.get("./src/test/resources/testmeta.xml").toUri());
Assert.assertTrue("Result of checking if mets contains metadata at dmdSec index was wrong!",
MetsKitodoValidator.metsContainsMetadataAtDmdSecIndex(mets, 2));
Assert.assertFalse(
"Result of checking if mets contains metadata at dmdSec index which does not exist was wrong!",
MetsKitodoValidator.metsContainsMetadataAtDmdSecIndex(mets, 6));
assertTrue(MetsKitodoValidator.metsContainsMetadataAtDmdSecIndex(mets, 2),
"Result of checking if mets contains metadata at dmdSec index was wrong!");
assertFalse(MetsKitodoValidator.metsContainsMetadataAtDmdSecIndex(mets, 6),
"Result of checking if mets contains metadata at dmdSec index which does not exist was wrong!");
}
}
Loading

0 comments on commit 77888b7

Please sign in to comment.