13 require_once
'PU_testcase_dcp.php';
15 include_once
'FREEDOM/freedom_import_xml.php';
22 static private $runid = 0;
23 static private $workDir =
false;
28 parent::setUpBeforeClass();
29 self::createWorkDir();
34 parent::tearDownAfterClass();
35 $stat = stat(self::$workDir);
36 if ($stat[
'nlink'] <= 2) {
37 rmdir(self::$workDir);
47 $testDir = self::$workDir . DIRECTORY_SEPARATOR . self::$runid;
50 $src =
'DCPTEST' . DIRECTORY_SEPARATOR .
'Layout' . DIRECTORY_SEPARATOR .
$data[
'xml'];
51 $workingXML = $testDir . DIRECTORY_SEPARATOR .
$data[
'xml'];
52 $ret = copy($src, $workingXML);
54 throw new \Exception(sprintf(
"Could not copy '%s' to '%s'.", $src, $workingXML));
56 if (isset(
$data[
'xml_alter'])) {
58 if (isset(
$data[
'xml_alter_args'])) {
61 $ret = call_user_func(array(
64 ) , $workingXML,
$args);
66 throw new \Exception($this->errmsg);
70 if (isset(
$data[
'expect_error']) &&
$data[
'expect_error'] ===
true) {
73 $this->assertTrue(
false,
"XML Error not detected");
76 $this->assertNotEmpty($e->getMessage() , sprintf(
"splitXmlDocument did not returned with an expected error"));
84 if (!isset(
$data[
'produces'])) {
89 $file = $testDir . DIRECTORY_SEPARATOR .
$file;
90 $this->assertTrue(is_file($file) , sprintf(
"Required file '%s' has not been produced by splitXmlDocument.", $file));
93 foreach (
$data[
'produces'] as $file) {
94 $file = $testDir . DIRECTORY_SEPARATOR .
$file;
95 $this->assertTrue($this->isValidXML($file) , sprintf(
"Output file '%s' does not seems to be a valid XML file according to xmllint.", $file));
98 $this->rm_Rf($testDir);
100 private static function createWorkDir()
103 if (!is_dir($tmpdir)) {
104 throw new \Exception(sprintf(
"Invalid directory '%s'.", $tmpdir));
106 $tmpname = tempnam($tmpdir,
'PU_TEST_DCP_SPLITXMLDOCUMENT');
107 if ($tmpname ===
false) {
108 throw new \Exception(sprintf(
"Could not create temporary file in '%s'.", $tmpdir));
111 if (mkdir($tmpname, 0700) ===
false) {
112 throw new \Exception(sprintf(
"Could not create directory '%s'.", $tmpname));
114 self::$workDir = $tmpname;
117 private function isValidXML(
$file)
119 $cmd = sprintf(
"xmllint --sax %s | grep -c '^SAX\\.error' > /dev/null 2>&1", escapeshellarg(
$file));
132 private function addBigNode($xml,
$args = array())
134 $addNodeData = file_get_contents(
'DCPTEST' . DIRECTORY_SEPARATOR .
'Layout' . DIRECTORY_SEPARATOR .
'PU_data_dcp_splitxmldocument_bignode_template.xml');
135 if ($addNodeData ===
false) {
136 $this->errmsg = sprintf(
"Could not get content from XML file '%s'.",
"PU_data_dcp_splitxmldocument_bignode_template.xml");
139 $xmlData = file_get_contents($xml);
140 if ($xmlData ===
false) {
141 $this->errmsg = sprintf(
"Could not get content from XML file '%s'.", $xml);
147 $addNodeElmts = preg_split(
'/(@[a-zA-Z_][a-zA-Z0-9_]+@)/', $addNodeData, -1, PREG_SPLIT_DELIM_CAPTURE);
148 foreach ($addNodeElmts as & $el) {
150 if (!preg_match(
'/^@(?<var>[a-zA-Z_][a-zA-Z0-9_]+)@$/', $el, $m)) {
153 if ($m[
'var'] ==
'DATA') {
157 if (!isset(
$args[$m[
'var']])) {
160 $el =
$args[$m[
'var']];
167 if (!preg_match(
':^(?<top>.*)(?<bottom></documents>\s*)$:ms', $xmlData, $xmlElmts)) {
168 $this->errmsg = sprintf(
"Could not match XML document in XML file '%s'.", $xml);
171 $fh = fopen($xml,
'w');
173 $this->errmsg = sprintf(
"Could not open XML file '%s' for writing.", $xml);
177 $ret = fwrite($fh, $xmlElmts[
'top']);
181 foreach ($addNodeElmts as & $el) {
183 if (!preg_match(
'/^@DATA@$/', $el, $m)) {
190 $blockCount = floor(
$size / (3 * 1024));
191 $oneBlock = str_repeat(
"QUFB", 1024);
192 for ($i = 1; $i <= $blockCount; $i++) {
193 fwrite($fh, $oneBlock);
195 $remBytes =
$size - $blockCount * 3 * 1024;
196 if ($remBytes == 1024) {
197 fwrite($fh, str_repeat(
"QUFB", 341) .
"QQ==");
198 } elseif ($remBytes == 2048) {
199 fwrite($fh, str_repeat(
"QUFB", 682) .
"QUE=");
204 $ret = fwrite($fh, $xmlElmts[
'bottom']);
209 private function rm_Rf(
$dir)
212 if (
$type !=
'dir') {
220 while ((
$file = readdir($fh)) !==
false) {
237 'description' =>
'Small XML file',
238 'xml' =>
'PU_data_dcp_splitxmldocument.xml',
240 '00000PU_DATA_DCP_SPLITXMLDOCUMENT_1.xml',
241 '00001PU_DATA_DCP_SPLITXMLDOCUMENT_2.xml',
242 '00002PU_DATA_DCP_SPLITXMLDOCUMENT_3.xml'
248 'description' =>
'Big XML file',
249 'xml' =>
'PU_data_dcp_splitxmldocument.xml',
250 'xml_alter' =>
'addBigNode',
251 'xml_alter_args' => array(
252 'NAME' =>
'PU_DATA_DCP_SPLITXMLDOCUMENT_BIGNODE',
253 'TITLE' =>
'big.bin',
254 'size_in_MB' =>
'100'
257 '00000PU_DATA_DCP_SPLITXMLDOCUMENT_1.xml',
258 '00001PU_DATA_DCP_SPLITXMLDOCUMENT_2.xml',
259 '00002PU_DATA_DCP_SPLITXMLDOCUMENT_3.xml',
260 '00003PU_DATA_DCP_SPLITXMLDOCUMENT_BIGNODE.xml'
266 'description' =>
'Invalid root node',
267 'xml' =>
'PU_data_dcp_splitxmldocument_invalid_root_node.xml',
268 'expect_error' =>
true
if(substr($wsh, 0, 1)!= '/') $args
static setUpBeforeClass()
static tearDownAfterClass()
testExecuteSplitXmlDocument($data)
foreach($argv as $arg) $cmd
static splitXmlDocument($xmlfiles, $splitdir)