REPO-5624 Reimplement SchemaBootstrapTest (#435)

This commit is contained in:
Nana Insaidoo
2021-05-06 16:26:10 +01:00
committed by GitHub
parent d6ff933ce5
commit 45952e8e40

View File

@@ -33,23 +33,46 @@ import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.alfresco.repo.admin.patch.impl.SchemaUpgradeScriptPatch;
import org.alfresco.repo.domain.schema.SchemaBootstrap;
import org.alfresco.test_category.OwnJVMTestsCategory;
import org.alfresco.util.test.junitrules.ApplicationContextInit;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.RuleChain;
import org.junit.rules.TemporaryFolder;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@Category({OwnJVMTestsCategory.class})
public class SchemaBootstrapTest
{
private static final String BOOTSTRAP_TEST_CONTEXT = "classpath*:alfresco/dbscripts/test-bootstrap-context.xml";
private static final String MAIN_SCHEMA_REFERENCE_FILE = "classpath:alfresco/dbscripts/create/org.alfresco.repo.domain.dialect.PostgreSQLDialect/Schema-Reference-ALF.xml";
private static final String TEST_SCHEMA_REFERENCE_FILE = "classpath:alfresco/dbscripts/create/org.alfresco.repo.domain.dialect.PostgreSQLDialect/Test-Schema-Reference-ALF.xml";
private static final List<String> TEST_SCHEMA_REFERENCE_URLS = Arrays.asList(
"classpath:alfresco/dbscripts/create/${db.script.dialect}/Test-Schema-Reference-ALF.xml",
"classpath:alfresco/dbscripts/create/${db.script.dialect}/Schema-Reference-ACT.xml");
TEST_SCHEMA_REFERENCE_FILE,
"classpath:alfresco/dbscripts/create/org.alfresco.repo.domain.dialect.PostgreSQLDialect/Schema-Reference-ACT.xml");
private static ApplicationContextInit APP_CONTEXT_INIT = ApplicationContextInit.createStandardContextWithOverrides(BOOTSTRAP_TEST_CONTEXT);
@@ -59,6 +82,20 @@ public class SchemaBootstrapTest
private SchemaBootstrap schemaBootstrap;
private SchemaUpgradeScriptPatch optionalPatch;
@Rule
public TemporaryFolder testFolder = new TemporaryFolder();
@BeforeClass
public static void beforeClass() throws Exception
{
ResourcePatternResolver rpr = new PathMatchingResourcePatternResolver(SchemaBootstrapTest.class.getClassLoader());
Document schemaRefXML = loadXML(rpr.getResource(MAIN_SCHEMA_REFERENCE_FILE));
Node indexes = getIndexesNode(schemaRefXML);
indexes.appendChild(createTestIndex(schemaRefXML));
Resource testSchemaRef = rpr.getResource(TEST_SCHEMA_REFERENCE_FILE);
updateTestSchemaReferenceFile(testSchemaRef, schemaRefXML);
}
@Before
public void setUp() throws Exception
{
@@ -83,4 +120,47 @@ public class SchemaBootstrapTest
+ optionalPatch.getId() + " has been run"));
}
private static void updateTestSchemaReferenceFile(Resource testSchemaRef, Document newDoc) throws Exception
{
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
StreamResult result = new StreamResult(testSchemaRef.getFile());
DOMSource source = new DOMSource(newDoc);
transformer.transform(source, result);
}
private static Element createTestIndex(Document document)
{
Element testIndex = document.createElement("index");
testIndex.setAttribute("name", "idx_alf_node_test");
testIndex.setAttribute("unique", "false");
Element columnNames = document.createElement("columnnames");
for (String colName: Arrays.asList("acl_id", "audit_creator"))
{
Element columnName = document.createElement("columnname");
columnName.setNodeValue(colName);
columnNames.appendChild(columnName);
}
testIndex.appendChild(columnNames);
return testIndex;
}
private static Node getIndexesNode(Document document) throws XPathExpressionException
{
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("/schema/objects/table[@name='alf_node']/indexes");
Node indexes = (Node)expr.evaluate(document, XPathConstants.NODE);
return indexes;
}
private static Document loadXML(Resource resource) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
builder = factory.newDocumentBuilder();
return builder.parse(resource.getInputStream());
}
}