/*
* Copyright 2013 Pascal Christoph (hbz)
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* 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 org.culturegraph.mf.xml;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
/**
* Extracts records from an xml file and writes single xml files into
* filesystem. The name of the xml files are generated by extracting a value
* using an xpath.
*
* @author Pascal Christoph (dr0i)
*
*/
public class XmlFilenameWriterTest {
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
private XmlFilenameWriter xmlFilenameWriter;
@Before
public void createSystemUnderTest() {
xmlFilenameWriter = new XmlFilenameWriter();
xmlFilenameWriter.setStartIndex(0);
xmlFilenameWriter.setEndIndex(1);
xmlFilenameWriter.setTarget(tempFolder.getRoot().getAbsolutePath());
}
@Test
public void testPicaXmlSplits() throws IOException {
xmlFilenameWriter.setProperty("xmldata/@id");
xmlFilenameWriter.startRecord("0");
xmlFilenameWriter.literal("Element", createXml("1"));
xmlFilenameWriter.endRecord();
xmlFilenameWriter.startRecord("1");
xmlFilenameWriter.literal("Element", createXml("2"));
xmlFilenameWriter.endRecord();
assertEquals(createXml("1"), readFile("1/1.xml"));
assertEquals(createXml("2"), readFile("2/2.xml"));
}
private String readFile(final String fileName) throws IOException {
final Path path = tempFolder.getRoot().toPath().resolve(fileName);
return new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
}
private String createXml(final String id) {
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><xmldata id=\"" + id +
"\" />";
}
}