mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 00:15:44 +01:00
Resolve config files via resolve method of context provider
This commit is contained in:
@@ -21,10 +21,10 @@ import javax.xml.bind.annotation.XmlAccessType;
|
|||||||
import javax.xml.bind.annotation.XmlAccessorType;
|
import javax.xml.bind.annotation.XmlAccessorType;
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@@ -96,9 +96,8 @@ public class XmlGroupV1UpdateStep implements UpdateStep {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Optional<Path> determineV1File() {
|
private Optional<Path> determineV1File() {
|
||||||
Path configDirectory = determineConfigDirectory();
|
Path existingGroupsFile = resolveConfigFile("groups");
|
||||||
Path existingGroupsFile = configDirectory.resolve("groups" + StoreConstants.FILE_EXTENSION);
|
Path groupsV1File = resolveConfigFile("groupsV1");
|
||||||
Path groupsV1File = configDirectory.resolve("groupsV1" + StoreConstants.FILE_EXTENSION);
|
|
||||||
if (existingGroupsFile.toFile().exists()) {
|
if (existingGroupsFile.toFile().exists()) {
|
||||||
try {
|
try {
|
||||||
Files.move(existingGroupsFile, groupsV1File);
|
Files.move(existingGroupsFile, groupsV1File);
|
||||||
@@ -111,8 +110,11 @@ public class XmlGroupV1UpdateStep implements UpdateStep {
|
|||||||
return empty();
|
return empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Path determineConfigDirectory() {
|
private Path resolveConfigFile(String name) {
|
||||||
return new File(contextProvider.getBaseDirectory(), StoreConstants.CONFIG_DIRECTORY_NAME).toPath();
|
return contextProvider
|
||||||
|
.resolve(
|
||||||
|
Paths.get(StoreConstants.CONFIG_DIRECTORY_NAME).resolve(name + StoreConstants.FILE_EXTENSION)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@XmlAccessorType(XmlAccessType.FIELD)
|
@XmlAccessorType(XmlAccessType.FIELD)
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@@ -94,7 +95,7 @@ public class XmlRepositoryV1UpdateStep implements UpdateStep {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doUpdate() throws JAXBException {
|
public void doUpdate() throws JAXBException {
|
||||||
if (!determineV1File().exists()) {
|
if (!resolveV1File().exists()) {
|
||||||
LOG.info("no v1 repositories database file found");
|
LOG.info("no v1 repositories database file found");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -179,7 +180,7 @@ public class XmlRepositoryV1UpdateStep implements UpdateStep {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Optional<V1RepositoryDatabase> readV1Database(JAXBContext jaxbContext) throws JAXBException {
|
private Optional<V1RepositoryDatabase> readV1Database(JAXBContext jaxbContext) throws JAXBException {
|
||||||
Object unmarshal = jaxbContext.createUnmarshaller().unmarshal(determineV1File());
|
Object unmarshal = jaxbContext.createUnmarshaller().unmarshal(resolveV1File());
|
||||||
if (unmarshal instanceof V1RepositoryDatabase) {
|
if (unmarshal instanceof V1RepositoryDatabase) {
|
||||||
return of((V1RepositoryDatabase) unmarshal);
|
return of((V1RepositoryDatabase) unmarshal);
|
||||||
} else {
|
} else {
|
||||||
@@ -187,9 +188,11 @@ public class XmlRepositoryV1UpdateStep implements UpdateStep {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private File determineV1File() {
|
private File resolveV1File() {
|
||||||
File configDirectory = new File(contextProvider.getBaseDirectory(), StoreConstants.CONFIG_DIRECTORY_NAME);
|
return contextProvider
|
||||||
return new File(configDirectory, "repositories" + StoreConstants.FILE_EXTENSION);
|
.resolve(
|
||||||
|
Paths.get(StoreConstants.CONFIG_DIRECTORY_NAME).resolve("repositories" + StoreConstants.FILE_EXTENSION)
|
||||||
|
).toFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
@XmlAccessorType(XmlAccessType.FIELD)
|
@XmlAccessorType(XmlAccessType.FIELD)
|
||||||
|
|||||||
@@ -22,10 +22,10 @@ import javax.xml.bind.annotation.XmlAccessType;
|
|||||||
import javax.xml.bind.annotation.XmlAccessorType;
|
import javax.xml.bind.annotation.XmlAccessorType;
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@@ -107,9 +107,8 @@ public class XmlUserV1UpdateStep implements UpdateStep {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Optional<Path> determineV1File() {
|
private Optional<Path> determineV1File() {
|
||||||
Path configDirectory = determineConfigDirectory();
|
Path existingUsersFile = resolveConfigFile("users");
|
||||||
Path existingUsersFile = configDirectory.resolve("users" + StoreConstants.FILE_EXTENSION);
|
Path usersV1File = resolveConfigFile("usersV1");
|
||||||
Path usersV1File = configDirectory.resolve("usersV1" + StoreConstants.FILE_EXTENSION);
|
|
||||||
if (existingUsersFile.toFile().exists()) {
|
if (existingUsersFile.toFile().exists()) {
|
||||||
try {
|
try {
|
||||||
Files.move(existingUsersFile, usersV1File);
|
Files.move(existingUsersFile, usersV1File);
|
||||||
@@ -122,8 +121,11 @@ public class XmlUserV1UpdateStep implements UpdateStep {
|
|||||||
return empty();
|
return empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Path determineConfigDirectory() {
|
private Path resolveConfigFile(String name) {
|
||||||
return new File(contextProvider.getBaseDirectory(), StoreConstants.CONFIG_DIRECTORY_NAME).toPath();
|
return contextProvider
|
||||||
|
.resolve(
|
||||||
|
Paths.get(StoreConstants.CONFIG_DIRECTORY_NAME).resolve(name + StoreConstants.FILE_EXTENSION)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@XmlAccessorType(XmlAccessType.FIELD)
|
@XmlAccessorType(XmlAccessType.FIELD)
|
||||||
|
|||||||
@@ -15,7 +15,8 @@ import java.nio.file.Files;
|
|||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.lenient;
|
||||||
|
|
||||||
public class UpdateStepTestUtil {
|
public class UpdateStepTestUtil {
|
||||||
|
|
||||||
@@ -28,7 +29,8 @@ private final SCMContextProvider contextProvider;
|
|||||||
this.tempDir = tempDir;
|
this.tempDir = tempDir;
|
||||||
contextProvider = Mockito.mock(SCMContextProvider.class);
|
contextProvider = Mockito.mock(SCMContextProvider.class);
|
||||||
storeFactory = new JAXBConfigurationEntryStoreFactory(contextProvider, null, new DefaultKeyGenerator());
|
storeFactory = new JAXBConfigurationEntryStoreFactory(contextProvider, null, new DefaultKeyGenerator());
|
||||||
when(contextProvider.getBaseDirectory()).thenReturn(tempDir.toFile());
|
lenient().when(contextProvider.getBaseDirectory()).thenReturn(tempDir.toFile());
|
||||||
|
lenient().when(contextProvider.resolve(any())).thenAnswer(invocation -> tempDir.resolve(invocation.getArgument(0).toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public SCMContextProvider getContextProvider() {
|
public SCMContextProvider getContextProvider() {
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import org.mockito.ArgumentCaptor;
|
|||||||
import org.mockito.Captor;
|
import org.mockito.Captor;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
import sonia.scm.SCMContextProvider;
|
|
||||||
import sonia.scm.repository.Repository;
|
import sonia.scm.repository.Repository;
|
||||||
import sonia.scm.repository.RepositoryPermission;
|
import sonia.scm.repository.RepositoryPermission;
|
||||||
import sonia.scm.repository.xml.XmlRepositoryDAO;
|
import sonia.scm.repository.xml.XmlRepositoryDAO;
|
||||||
@@ -19,6 +18,7 @@ import sonia.scm.store.ConfigurationEntryStore;
|
|||||||
import sonia.scm.store.ConfigurationEntryStoreFactory;
|
import sonia.scm.store.ConfigurationEntryStoreFactory;
|
||||||
import sonia.scm.store.InMemoryConfigurationEntryStore;
|
import sonia.scm.store.InMemoryConfigurationEntryStore;
|
||||||
import sonia.scm.store.InMemoryConfigurationEntryStoreFactory;
|
import sonia.scm.store.InMemoryConfigurationEntryStoreFactory;
|
||||||
|
import sonia.scm.update.UpdateStepTestUtil;
|
||||||
|
|
||||||
import javax.xml.bind.JAXBException;
|
import javax.xml.bind.JAXBException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -46,8 +46,6 @@ class XmlRepositoryV1UpdateStepTest {
|
|||||||
|
|
||||||
Injector injectorMock = MigrationStrategyMock.init();
|
Injector injectorMock = MigrationStrategyMock.init();
|
||||||
|
|
||||||
@Mock
|
|
||||||
SCMContextProvider contextProvider;
|
|
||||||
@Mock
|
@Mock
|
||||||
XmlRepositoryDAO repositoryDAO;
|
XmlRepositoryDAO repositoryDAO;
|
||||||
@Mock
|
@Mock
|
||||||
@@ -60,17 +58,15 @@ class XmlRepositoryV1UpdateStepTest {
|
|||||||
@Captor
|
@Captor
|
||||||
ArgumentCaptor<Path> locationCaptor;
|
ArgumentCaptor<Path> locationCaptor;
|
||||||
|
|
||||||
|
UpdateStepTestUtil testUtil;
|
||||||
|
|
||||||
XmlRepositoryV1UpdateStep updateStep;
|
XmlRepositoryV1UpdateStep updateStep;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void mockScmHome(@TempDirectory.TempDir Path tempDir) {
|
void createUpdateStepFromMocks(@TempDirectory.TempDir Path tempDir) {
|
||||||
when(contextProvider.getBaseDirectory()).thenReturn(tempDir.toFile());
|
testUtil = new UpdateStepTestUtil(tempDir);
|
||||||
}
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
void createUpdateStepFromMocks() {
|
|
||||||
updateStep = new XmlRepositoryV1UpdateStep(
|
updateStep = new XmlRepositoryV1UpdateStep(
|
||||||
contextProvider,
|
testUtil.getContextProvider(),
|
||||||
repositoryDAO,
|
repositoryDAO,
|
||||||
migrationStrategyDao,
|
migrationStrategyDao,
|
||||||
injectorMock,
|
injectorMock,
|
||||||
|
|||||||
Reference in New Issue
Block a user