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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -80,8 +79,7 @@ public class KitodoServiceLoader<T> {
private static final String JAR = "*.jar";
private static final String ERROR = "Classpath could not be accessed";

private static final Path SYSTEM_TEMP_FOLDER = FileSystems.getDefault()
.getPath(System.getProperty("java.io.tmpdir"));
private static final Path SYSTEM_TEMP_FOLDER = Paths.get(System.getProperty("java.io.tmpdir"));

private static final Logger logger = LogManager.getLogger(KitodoServiceLoader.class);

Expand Down Expand Up @@ -142,7 +140,7 @@ public List<T> loadModules() {
* they can be used in all frontend files
*/
private void loadBeans() {
Path moduleFolder = FileSystems.getDefault().getPath(modulePath);
Path moduleFolder = Paths.get(modulePath);
try (DirectoryStream<Path> stream = Files.newDirectoryStream(moduleFolder, JAR)) {
for (Path f : stream) {
try (JarFile jarFile = new JarFile(f.toString())) {
Expand Down Expand Up @@ -197,7 +195,7 @@ private void loadBeans() {
*/
private void loadFrontendFilesIntoCore() {

Path moduleFolder = FileSystems.getDefault().getPath(modulePath);
Path moduleFolder = Paths.get(modulePath);

try (DirectoryStream<Path> stream = Files.newDirectoryStream(moduleFolder, JAR)) {

Expand Down Expand Up @@ -382,7 +380,7 @@ private File findFile(String name, File folder) throws FileNotFoundException {
* earlier class loader created at an earlier time.</p>
*/
private void loadModulesIntoClasspath() {
Path moduleFolder = FileSystems.getDefault().getPath(modulePath);
Path moduleFolder = Paths.get(modulePath);

try (DirectoryStream<Path> stream = Files.newDirectoryStream(moduleFolder, JAR)) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@

import java.util.NoSuchElementException;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.kitodo.config.enums.ParameterAPI;
import org.mockito.MockedStatic;

public class KitodoConfigTest {

private static ParameterAPI NONE;
private static MockedStatic<ParameterAPI> mockedParameterAPI;

/**
* Init once before tests.
Expand All @@ -40,12 +43,17 @@ public static void init() {
NONE = mock(ParameterAPI.class);
doReturn(3).when(NONE).ordinal();

mockStatic(ParameterAPI.class);
mockedParameterAPI = mockStatic(ParameterAPI.class);
when(ParameterAPI.values())
.thenReturn(new ParameterAPI[] {ParameterAPI.DIR_MODULES, ParameterAPI.DIR_PROCESSES,
ParameterAPI.DIR_XML_CONFIG, NONE });
}

@AfterAll
public static void tearDown() {
mockedParameterAPI.close();
}

@Test
public void shouldGetStringParameterWithoutDefault() {
String param = KitodoConfig.getParameter(ParameterAPI.DIR_XML_CONFIG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand Down Expand Up @@ -43,8 +44,12 @@ public void saveFile() throws IOException {

@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")));
try (OutputStream out1 = Files.newOutputStream(Paths.get(pathOfOldMetaFormat))) {
IOUtils.write(testMetaOldFormat, out1);
}
try (OutputStream out2 = Files.newOutputStream(Paths.get("src/test/resources/testmetaUnsupportedFormat.xml"))) {
IOUtils.write(testmetaUnsupportedFormat, out2);
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand Down Expand Up @@ -46,7 +47,9 @@ public void saveFile() throws IOException {

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

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

Expand All @@ -39,7 +40,9 @@ public void saveFile() throws IOException {

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

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.net.URI;
import java.nio.file.Files;
Expand Down Expand Up @@ -65,7 +66,9 @@ public void saveFile() throws IOException {

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

@BeforeAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand Down Expand Up @@ -49,7 +50,9 @@ public void saveFile() throws IOException {

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

@BeforeAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,18 +297,19 @@ public void duplicateMetsFileDefinitionWithoutStrictFileIdCheck() throws IOExcep
public void duplicateMetsFileDefinitionWithStrictFileIdCheck() {
// mock access to KitodoConfig usage
PropertiesConfiguration propertiesConfiguration = Mockito.mock(PropertiesConfiguration.class);
MockedStatic<KitodoConfig> mockedConfig = Mockito.mockStatic(KitodoConfig.class);
mockedConfig.when(KitodoConfig::getConfig).thenReturn(propertiesConfiguration);
// mock getBoolean method call like in the main class
Mockito.when(propertiesConfiguration.getBoolean("useStrictMetsFileIdCheck", false)).thenReturn(true);

Exception exception = assertThrows(IllegalArgumentException.class,
() -> new MetsXmlElementAccess().read(
new FileInputStream("src/test/resources/meta_duplicate_file.xml")
)
);

assertEquals("Corrupt file: each METS file ID has to be unique but FILE_0001 is used multiple times!", exception.getMessage());
try (MockedStatic<KitodoConfig> mockedConfig = Mockito.mockStatic(KitodoConfig.class)) {
mockedConfig.when(KitodoConfig::getConfig).thenReturn(propertiesConfiguration);
// mock getBoolean method call like in the main class
Mockito.when(propertiesConfiguration.getBoolean("useStrictMetsFileIdCheck", false)).thenReturn(true);

Exception exception = assertThrows(IllegalArgumentException.class,
() -> new MetsXmlElementAccess().read(
new FileInputStream("src/test/resources/meta_duplicate_file.xml")
)
);

assertEquals("Corrupt file: each METS file ID has to be unique but FILE_0001 is used multiple times!", exception.getMessage());
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ URI createMetaDirectory(URI parentFolderUri, String directoryName) throws IOExce
logger.info("Metadata directory: {} already existed! No new directory was created", directoryName);
} else {
CommandService commandService = ServiceManager.getCommandService();
String path = FileSystems.getDefault()
.getPath(ConfigCore.getKitodoDataDirectory(), parentFolderUri.getRawPath(), directoryName)
String path = Paths.get(ConfigCore.getKitodoDataDirectory(), parentFolderUri.getRawPath(), directoryName)
.normalize().toAbsolutePath().toString();
List<String> commandParameter = Collections.singletonList(path);
File script = new File(ConfigCore.getParameter(ParameterCore.SCRIPT_CREATE_DIR_META));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.LinkedList;

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -48,19 +49,30 @@ public class MediaPartialFormTest {
PhysicalDivision physicalDivision;
PhysicalDivision physicalStructure;

private static MockedStatic<Ajax> ajaxMockedStatic;

/**
* Initialize test class.
*/
@BeforeAll
public static void initTestClass() {
// mock frontend update calls
Mockito.mockStatic(Ajax.class);
ajaxMockedStatic = Mockito.mockStatic(Ajax.class);
PrimeFaces primeFaces = mock(PrimeFaces.class);
MockedStatic<PrimeFaces> primefacesSingleton = Mockito.mockStatic(PrimeFaces.class);
primefacesSingleton.when(PrimeFaces::current).thenReturn(primeFaces);
}

/**
* Clean up static mocks.
*/
@AfterAll
public static void cleanupTestClass() {
if (ajaxMockedStatic != null) {
ajaxMockedStatic.close();
}
}

/**
* Initialize test function.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

Expand Down Expand Up @@ -45,7 +46,9 @@ public void saveFile() throws IOException {

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

@Test
Expand Down
Loading