Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keyscope test for mapref #3193

Merged
merged 1 commit into from
Jan 26, 2019
Merged
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
37 changes: 28 additions & 9 deletions src/test/java/org/dita/dost/module/KeyrefModuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.dita.dost.util.Job.FileInfo.Builder;
import org.dita.dost.util.KeyDef;
import org.dita.dost.util.KeyScope;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;
Expand All @@ -30,18 +31,20 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static java.net.URI.create;
import static java.util.Collections.*;
import static org.dita.dost.TestUtils.assertXMLEqual;
import static org.dita.dost.TestUtils.createTempDir;
import static org.dita.dost.util.Constants.INPUT_DITAMAP_URI;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;

public class KeyrefModuleTest {

private static final File baseDir = TestUtils.getResourceDir(KeyrefModuleTest.class);
private static final URI inputMap = new File(baseDir, "xsrc" + File.separator + "test.ditamap").toURI();
private static final URI subMap = new File(baseDir, "xsrc" + File.separator + "submap.ditamap").toURI();

KeyrefModule module;

Expand All @@ -53,6 +56,13 @@ public void setUp() throws IOException {
final Job job = new Job(tempDir);
job.setInputDir(new File(baseDir, "xsrc").toURI());
job.setInputMap(URI.create("test.ditamap"));
job.add(new Job.FileInfo.Builder()
.uri(URI.create("submap.ditamap"))
.src(new File(baseDir, "xsrc" + File.separator + "submap.ditamap").toURI())
.result(new File(baseDir, "xsrc" + File.separator + "submap.ditamap").toURI())
.format("ditamap")
.hasKeyref(true)
.build());
job.add(new Job.FileInfo.Builder()
.uri(URI.create("topic.dita"))
.src(new File(baseDir, "xsrc" + File.separator + "topic.dita").toURI())
Expand Down Expand Up @@ -121,22 +131,31 @@ public void testRewriteScopeTargets() {
public void testWalkMap() throws ParserConfigurationException, IOException, SAXException {
final DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final Document act = b.parse(new File(baseDir, "src" + File.separator + "test.ditamap"));
final KeyScope childScope = new KeyScope("A", "A",
ImmutableMap.of(
"VAR", new KeyDef("VAR", null, "local", "dita", inputMap, null),
"A.VAR", new KeyDef("VAR", null, "local", "dita", inputMap, null)
),
EMPTY_LIST
);
final KeyScope keyScope =
new KeyScope("#root", null,
ImmutableMap.of(
"VAR", new KeyDef("VAR", null, "local", "dita", inputMap, null),
"A.VAR", new KeyDef("VAR", null, "local", "dita", inputMap, null)),
singletonList(
new KeyScope("A", "A",
ImmutableMap.of(
"VAR", new KeyDef("VAR", null, "local", "dita", inputMap, null),
"A.VAR", new KeyDef("VAR", null, "local", "dita", inputMap, null)),
EMPTY_LIST)
));
"A.VAR", new KeyDef("VAR", null, "local", "dita", inputMap, null)
),
singletonList(childScope)
);
final List<ResolveTask> res = new ArrayList<>();
module.walkMap(act.getDocumentElement(), keyScope, res);
final Document exp = b.parse(new File(baseDir, "exp" + File.separator + "test.ditamap"));

final Job.FileInfo subMapInfo = module.job.getFileInfo(subMap);
final Optional<ResolveTask> subMapTask = res.stream().filter(r -> r.in.equals(subMapInfo)).findFirst();

assertFalse(subMapTask.isPresent());
// assertEquals(subMapTask.get().scope, childScope);

assertXMLEqual(exp, act);
}

Expand Down
98 changes: 59 additions & 39 deletions src/test/java/org/dita/dost/writer/KeyrefPaserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package org.dita.dost.writer;

import static org.dita.dost.TestUtils.assertXMLEqual;
import static org.dita.dost.TestUtils.createTempDir;

import java.io.File;
import java.io.IOException;
Expand All @@ -30,10 +31,14 @@
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;

import org.apache.commons.io.IOUtils;
import org.dita.dost.reader.KeyrefReader;
import org.dita.dost.util.FileUtils;
import org.dita.dost.util.Job;
import org.dita.dost.util.KeyDef;
import org.dita.dost.util.KeyScope;
import org.junit.BeforeClass;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
Expand All @@ -49,76 +54,100 @@ public class KeyrefPaserTest {
private static final File srcDir = new File(resourceDir, "src");
private static final File expDir = new File(resourceDir, "exp");

private static KeyScope keyDefinition;
private KeyScope keyDefinition;
private File tempDir;

@Before
public void setUp() throws Exception {
tempDir = createTempDir(KeyrefPaserTest.class);
TestUtils.copy(srcDir, tempDir);

@BeforeClass
public static void setUp() throws Exception {
keyDefinition = readKeyMap(Paths.get("keys.ditamap"));
}

@After
public void tearDown() throws IOException {
TestUtils.forceDelete(tempDir);
}

@Test
public void testTopicWrite() throws Exception {
final KeyrefPaser parser = new KeyrefPaser();
parser.setLogger(new TestUtils.TestLogger());
parser.setJob(new Job(srcDir));
parser.setJob(new Job(tempDir));
parser.setKeyDefinition(keyDefinition);
parser.setCurrentFile(new File(srcDir, "a.xml").toURI());
parser.write(new File(srcDir, "a.xml"));
parser.setCurrentFile(new File(tempDir, "a.xml").toURI());
parser.write(new File(tempDir, "a.xml"));

assertXMLEqual(new InputSource(new File(expDir, "a.xml").toURI().toString()),
new InputSource(new File(srcDir, "a.xml").toURI().toString()));
new InputSource(new File(tempDir, "a.xml").toURI().toString()));
}

@Test
public void testFragment() throws Exception {
final KeyrefPaser parser = new KeyrefPaser();
parser.setLogger(new TestUtils.TestLogger());
parser.setJob(new Job(srcDir));
parser.setJob(new Job(tempDir));
parser.setKeyDefinition(keyDefinition);
parser.setCurrentFile(new File(srcDir, "id.xml").toURI());
parser.write(new File(srcDir, "id.xml"));
parser.setCurrentFile(new File(tempDir, "id.xml").toURI());
parser.write(new File(tempDir, "id.xml"));

assertXMLEqual(new InputSource(new File(expDir, "id.xml").toURI().toString()),
new InputSource(new File(srcDir, "id.xml").toURI().toString()));
new InputSource(new File(tempDir, "id.xml").toURI().toString()));
}

@Test
public void testFallback() throws Exception {
final KeyrefPaser parser = new KeyrefPaser();
parser.setLogger(new TestUtils.TestLogger());
parser.setJob(new Job(srcDir));
parser.setJob(new Job(tempDir));
parser.setKeyDefinition(keyDefinition);
parser.setCurrentFile(new File(srcDir, "fallback.xml").toURI());
parser.write(new File(srcDir, "fallback.xml"));
parser.setCurrentFile(new File(tempDir, "fallback.xml").toURI());
parser.write(new File(tempDir, "fallback.xml"));

assertXMLEqual(new InputSource(new File(expDir, "fallback.xml").toURI().toString()),
new InputSource(new File(srcDir, "fallback.xml").toURI().toString()));
new InputSource(new File(tempDir, "fallback.xml").toURI().toString()));
}

@Test
public void testMapWrite() throws Exception {
final KeyrefPaser parser = new KeyrefPaser();
parser.setLogger(new TestUtils.TestLogger());
parser.setJob(new Job(srcDir));
parser.setJob(new Job(tempDir));
parser.setKeyDefinition(keyDefinition);
parser.setCurrentFile(new File(srcDir, "b.ditamap").toURI());
parser.write(new File(srcDir, "b.ditamap"));
parser.setCurrentFile(new File(tempDir, "b.ditamap").toURI());
parser.write(new File(tempDir, "b.ditamap"));

assertXMLEqual(new InputSource(new File(expDir, "b.ditamap").toURI().toString()),
new InputSource(new File(srcDir, "b.ditamap").toURI().toString()));
new InputSource(new File(tempDir, "b.ditamap").toURI().toString()));
}

@Test
public void testUpLevelMapWrite() throws Exception {
final KeyrefPaser parser = new KeyrefPaser();
parser.setLogger(new TestUtils.TestLogger());
parser.setJob(new Job(srcDir));
parser.setJob(new Job(tempDir));
parser.setKeyDefinition(readKeyMap(Paths.get("subdir", "c.ditamap")));
parser.setCurrentFile(new File(srcDir, "subdir"+ File.separator +"c.ditamap").toURI());
parser.write(new File(srcDir, "subdir"+ File.separator +"c.ditamap"));
parser.setCurrentFile(new File(tempDir, "subdir"+ File.separator +"c.ditamap").toURI());
parser.write(new File(tempDir, "subdir"+ File.separator +"c.ditamap"));

assertXMLEqual(new InputSource(new File(expDir, "subdir"+ File.separator +"c.ditamap").toURI().toString()),
new InputSource(new File(srcDir, "subdir"+ File.separator +"c.ditamap").toURI().toString()));
new InputSource(new File(tempDir, "subdir"+ File.separator +"c.ditamap").toURI().toString()));
}

@Test
public void testMapWithKeyScopes() throws Exception {
final KeyrefPaser parser = new KeyrefPaser();
parser.setLogger(new TestUtils.TestLogger());
parser.setJob(new Job(tempDir));
parser.setKeyDefinition(keyDefinition);
parser.setCurrentFile(new File(tempDir, "d.ditamap").toURI());
parser.write(new File(tempDir, "d.ditamap"));

System.err.println(new File(expDir, "d.ditamap"));
System.err.println(new File(tempDir, "d.ditamap"));
assertXMLEqual(new InputSource(new File(expDir, "d.ditamap").toURI().toString()),
new InputSource(new File(tempDir, "d.ditamap").toURI().toString()));
}

@Test
Expand Down Expand Up @@ -158,26 +187,17 @@ private Document domToSax(final Document doc, final boolean retain) throws Trans
return (Document) r.getNode();
}

private static KeyScope readKeyMap(final Path map) throws Exception {
final URI keyMapFile = srcDir.toPath().resolve(map).toUri();
private KeyScope readKeyMap(final Path map) throws Exception {
KeyrefReader reader = new KeyrefReader();
final URI keyMapFile = tempDir.toPath().resolve(map).toUri();
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
final InputSource inputSource = new InputSource(keyMapFile.toString());
final DocumentBuilder documentBuilder = factory.newDocumentBuilder();
final Document document = documentBuilder.parse(inputSource);

final Map<String, Element> keys = new HashMap<>();
final NodeList keydefs = document.getElementsByTagName("keydef");
final Map<String, KeyDef> keymap = new HashMap<>();
for (int i = 0; i < keydefs.getLength(); i++) {
final Element elem = (Element) keydefs.item(i);
final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
doc.appendChild(doc.importNode(elem, true));
keys.put(elem.getAttribute("keys"), elem);
final KeyDef keyDef = new KeyDef(elem.getAttribute("keys"), new URI(elem.getAttribute("href")),
null, null, srcDir.toPath().resolve(map).toUri(), elem);
keymap.put(keyDef.keys, keyDef);
}
return new KeyScope(null, null, keymap, Collections.emptyList());
reader.read(keyMapFile, document);

return reader.getKeyDefinition();
}

}
3 changes: 3 additions & 0 deletions src/test/resources/KeyrefModuleTest/exp/test.ditamap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<topicref class="- map/topicref " href="topic.dita"/>
<topicref class="- map/topicref " href="topic.dita"/>
<topicgroup class="+ map/topicref mapgroup-d/topicgroup " keyscope="A">
<submap class="+ map/topicref mapgroup-d/topicgroup ditaot-d/submap " dita-ot:orig-class="+ map/topicref mapgroup-d/mapref " dita-ot:orig-format="ditamap" dita-ot:orig-href="submap.ditamap" dita-ot:submap-DITAArchVersion="1.3" dita-ot:submap-cascade="merge" dita-ot:submap-class="- map/map " dita-ot:submap-domains="(map mapgroup-d) (topic abbrev-d) (topic delay-d) a(props deliveryTarget) (map ditavalref-d) (map glossref-d) (topic hazard-d) (topic hi-d) (topic indexing-d) (topic markup-d) (topic pr-d) (topic relmgmt-d) (topic sw-d) (topic ui-d) (topic ut-d) (topic markup-d xml-d)" dita-ot:submap-title="Ditamap A">
<topicref class="- map/topicref " href="topic-1.dita"/>
</submap>
<topicref class="- map/topicref " href="topic-1.dita"/>
<topicref class="- map/topicref " href="topic-1.dita"/>
</topicgroup>
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/KeyrefModuleTest/src/submap.ditamap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<map title="Ditamap A" xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" xmlns:dita-ot="http://dita-ot.sourceforge.net/ns/201007/dita-ot" cascade="merge" class="- map/map " ditaarch:DITAArchVersion="1.3" domains="(map mapgroup-d) (topic abbrev-d) (topic delay-d) a(props deliveryTarget) (map ditavalref-d) (map glossref-d) (topic hazard-d) (topic hi-d) (topic indexing-d) (topic markup-d) (topic pr-d) (topic relmgmt-d) (topic sw-d) (topic ui-d) (topic ut-d) (topic markup-d xml-d)">
<topicref class="- map/topicref " href="topic.dita"/>
</map>
3 changes: 3 additions & 0 deletions src/test/resources/KeyrefModuleTest/src/test.ditamap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<topicref class="- map/topicref " href="topic.dita"/>
<topicref class="- map/topicref " href="topic.dita"/>
<topicgroup class="+ map/topicref mapgroup-d/topicgroup " keyscope="A">
<submap class="+ map/topicref mapgroup-d/topicgroup ditaot-d/submap " dita-ot:orig-class="+ map/topicref mapgroup-d/mapref " dita-ot:orig-format="ditamap" dita-ot:orig-href="submap.ditamap" dita-ot:submap-DITAArchVersion="1.3" dita-ot:submap-cascade="merge" dita-ot:submap-class="- map/map " dita-ot:submap-domains="(map mapgroup-d) (topic abbrev-d) (topic delay-d) a(props deliveryTarget) (map ditavalref-d) (map glossref-d) (topic hazard-d) (topic hi-d) (topic indexing-d) (topic markup-d) (topic pr-d) (topic relmgmt-d) (topic sw-d) (topic ui-d) (topic ut-d) (topic markup-d xml-d)" dita-ot:submap-title="Ditamap A">
<topicref class="- map/topicref " href="topic.dita"/>
</submap>
<topicref class="- map/topicref " href="topic.dita"/>
<topicref class="- map/topicref " href="topic.dita"/>
</topicgroup>
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/KeyrefPaserTest/exp/b.ditamap
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,5 @@
<topicref class="- map/topicref " href="a.xml" keyref="indextermref_link"/>
<navref class="- map/navref " keyref="navref_link" mapref="a.xml"/>
</topicgroup>
<topicref class="- map/topicref " keyref="copy" href="a.xml" copy-to="a-copy.xml"/>
<topicref class="- map/topicref " keyref="copy" href="a-copy.xml" copy-to="a-copy.xml"/>
</map>
28 changes: 28 additions & 0 deletions src/test/resources/KeyrefPaserTest/exp/d.ditamap
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<map xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" class="- map/map "
domains="(topic delay-d) (map mapgroup-d) (topic indexing-d) (map glossref-d) (topic hi-d) (topic ut-d) (topic hazard-d) (topic abbrev-d) (topic pr-d) (topic sw-d) (topic ui-d) "
ditaarch:DITAArchVersion="1.2">

<topicgroup class="+ map/topicref mapgroup-d/topicgroup " keyscope="KEY_SCOPE">
<topicref class="- map/topicref " format="dead" href="dead" keyref="ks_topicref_link" scope="external" type="dead"/>
<topicref class="- map/topicref " keyref="ks_author_link"/>
<topicref class="- map/topicref " keyref="ks_data_link"/>
<topicref class="- map/topicref " keyref="ks_data-about_link"/>
<topicref class="- map/topicref " keyref="ks_publisher_link"/>
<topicref class="- map/topicref " keyref="ks_source_link"/>
<topicref class="- map/topicref " keyref="ks_indexterm_link"/>
<topicref class="- map/topicref " keyref="ks_index-base_link"/>
<topicref class="- map/topicref " keyref="ks_indextermref_link"/>
<navref class="- map/navref " keyref="ks_navref_link"/>

<topicref class="- map/topicref " href="a.xml" keyref="topicref_link" type="dead"/>
<topicref class="- map/topicref " href="a.xml" keyref="author_link"/>
<topicref class="- map/topicref " href="a.xml" keyref="data_link"/>
<topicref class="- map/topicref " href="a.xml" keyref="data-about_link"/>
<topicref class="- map/topicref " href="a.xml" keyref="publisher_link"/>
<topicref class="- map/topicref " href="a.xml" keyref="source_link"/>
<topicref class="- map/topicref " href="a.xml" keyref="indexterm_link"/>
<topicref class="- map/topicref " href="a.xml" keyref="index-base_link"/>
<topicref class="- map/topicref " href="a.xml" keyref="indextermref_link"/>
<navref class="- map/navref " keyref="navref_link" mapref="a.xml"/>
</topicgroup>
</map>
30 changes: 30 additions & 0 deletions src/test/resources/KeyrefPaserTest/src/d.ditamap
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<map xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" class="- map/map "
domains="(topic delay-d) (map mapgroup-d) (topic indexing-d) (map glossref-d) (topic hi-d) (topic ut-d) (topic hazard-d) (topic abbrev-d) (topic pr-d) (topic sw-d) (topic ui-d) "
ditaarch:DITAArchVersion="1.2">
<topicgroup class="+ map/topicref mapgroup-d/topicgroup " keyscope="KEY_SCOPE">

<!-- Test scoped keys -->
<topicref class="- map/topicref " format="dead" href="dead" keyref="ks_topicref_link" scope="external" type="dead"/>
<topicref class="- map/topicref " keyref="ks_author_link"/>
<topicref class="- map/topicref " keyref="ks_data_link"/>
<topicref class="- map/topicref " keyref="ks_data-about_link"/>
<topicref class="- map/topicref " keyref="ks_publisher_link"/>
<topicref class="- map/topicref " keyref="ks_source_link"/>
<topicref class="- map/topicref " keyref="ks_indexterm_link"/>
<topicref class="- map/topicref " keyref="ks_index-base_link"/>
<topicref class="- map/topicref " keyref="ks_indextermref_link"/>
<navref class="- map/navref " keyref="ks_navref_link"/>

<!-- Test non-scoped keys -->
<topicref class="- map/topicref " format="dead" href="dead" keyref="topicref_link" scope="external" type="dead"/>
<topicref class="- map/topicref " keyref="author_link"/>
<topicref class="- map/topicref " keyref="data_link"/>
<topicref class="- map/topicref " keyref="data-about_link"/>
<topicref class="- map/topicref " keyref="publisher_link"/>
<topicref class="- map/topicref " keyref="source_link"/>
<topicref class="- map/topicref " keyref="indexterm_link"/>
<topicref class="- map/topicref " keyref="index-base_link"/>
<topicref class="- map/topicref " keyref="indextermref_link"/>
<navref class="- map/navref " keyref="navref_link"/>
</topicgroup>
</map>
Loading