1 package org.unicode.cldr.unittest; 2 3 import java.util.HashSet; 4 import java.util.Iterator; 5 import java.util.LinkedHashSet; 6 import java.util.Map; 7 import java.util.Set; 8 9 import org.unicode.cldr.util.CLDRConfig; 10 import org.unicode.cldr.util.CLDRFile; 11 import org.unicode.cldr.util.CldrUtility; 12 import org.unicode.cldr.util.XMLSource; 13 import org.unicode.cldr.util.XPathParts.Comments; 14 15 import com.ibm.icu.dev.test.TestFmwk; 16 17 public class TestXMLSource extends TestFmwk { 18 public static class DummyXMLSource extends XMLSource { 19 Map<String, String> valueMap = CldrUtility.newConcurrentHashMap(); 20 21 @Override freeze()22 public XMLSource freeze() { 23 return null; 24 } 25 26 @Override putFullPathAtDPath(String distinguishingXPath, String fullxpath)27 public void putFullPathAtDPath(String distinguishingXPath, 28 String fullxpath) { 29 } 30 31 @Override putValueAtDPath(String distinguishingXPath, String value)32 public void putValueAtDPath(String distinguishingXPath, String value) { 33 valueMap.put(distinguishingXPath, value); 34 } 35 36 @Override removeValueAtDPath(String distinguishingXPath)37 public void removeValueAtDPath(String distinguishingXPath) { 38 } 39 40 @Override getValueAtDPath(String path)41 public String getValueAtDPath(String path) { 42 return valueMap.get(path); 43 } 44 45 @Override getFullPathAtDPath(String path)46 public String getFullPathAtDPath(String path) { 47 return null; 48 } 49 50 @Override getXpathComments()51 public Comments getXpathComments() { 52 return null; 53 } 54 55 @Override setXpathComments(Comments comments)56 public void setXpathComments(Comments comments) { 57 } 58 59 @Override iterator()60 public Iterator<String> iterator() { 61 return valueMap.keySet().iterator(); 62 } 63 64 @Override getPathsWithValue(String valueToMatch, String pathPrefix, Set<String> result)65 public void getPathsWithValue(String valueToMatch, String pathPrefix, 66 Set<String> result) { 67 } 68 } 69 main(String[] args)70 public static void main(String[] args) { 71 new TestXMLSource().run(args); 72 } 73 TestGetPathsWithValue()74 public void TestGetPathsWithValue() { 75 XMLSource source = new DummyXMLSource(); 76 source.putValueAtDPath("//ldml/foo", "x"); 77 source.putValueAtDPath("//ldml/foo[@alt=\"proposed5\"]", "x"); 78 source.putValueAtDPath("//ldml/foo[@alt=\"short\"]", "x"); 79 source.putValueAtDPath("//ldml/foo[@alt=\"short-proposed-x\"]", "x"); 80 source.putValueAtDPath( 81 "//ldml/foo[@alt=\"short-proposed-x\"][@type=\"wide\"]", "x"); 82 source.putValueAtDPath("//ldml/foo[@alt=\"short-x\"]", "x"); 83 84 Set<String> result = new HashSet<String>(); 85 source.getPathsWithValue("x", "//ldml/foo/bar", result); 86 assertEquals("no paths should be matched", 0, result.size()); 87 result.clear(); 88 89 String xpath = "//ldml/foo"; 90 source.getPathsWithValue("x", xpath, result); 91 assertEquals("Set matched but incorrect: " + result.toString(), 2, 92 result.size()); 93 assertTrue(xpath + " not found", result.contains(xpath)); 94 assertTrue("//ldml/foo[@alt=\"proposed5\"] not found", 95 result.contains("//ldml/foo[@alt=\"proposed5\"]")); 96 result.clear(); 97 98 xpath = "//ldml/foo[@alt=\"short\"]"; 99 source.getPathsWithValue("x", xpath, result); 100 assertEquals("Set matched but incorrect: " + result.toString(), 3, 101 result.size()); 102 assertTrue(xpath + " not found", result.contains(xpath)); 103 assertTrue("//ldml/foo[@alt=\"short-proposed-x\"] not found", 104 result.contains("//ldml/foo[@alt=\"short-proposed-x\"]")); 105 assertTrue( 106 "//ldml/foo[@alt=\"short-proposed-x\"][@type=\"wide\"] not found", 107 result.contains("//ldml/foo[@alt=\"short-proposed-x\"][@type=\"wide\"]")); 108 result.clear(); 109 110 xpath = "//ldml/foo[@alt=\"short-proposed\"]"; 111 source.getPathsWithValue("x", xpath, result); 112 assertEquals("Set matched but incorrect: " + result.toString(), 3, 113 result.size()); 114 assertTrue("//ldml/foo[@alt=\"short-proposed-x\"] not found", 115 result.contains("//ldml/foo[@alt=\"short-proposed-x\"]")); 116 assertTrue("//ldml/foo[@alt=\"short\"] not found", 117 result.contains("//ldml/foo[@alt=\"short\"]")); 118 assertTrue( 119 "//ldml/foo[@alt=\"short-proposed-x\"][@type=\"wide\"] not found", 120 result.contains("//ldml/foo[@alt=\"short-proposed-x\"][@type=\"wide\"]")); 121 result.clear(); 122 } 123 TestA()124 public void TestA() { 125 CLDRConfig testInfo = CLDRConfig.getInstance(); 126 CLDRFile file = testInfo.getEnglish(); 127 Set<String> result = new LinkedHashSet<String>(); 128 file.getPathsWithValue("ms", "", null, result); 129 for (String path : result) { 130 String value = file.getStringValue(path); 131 if (!value.contains("ms")) { 132 errln("bad paths:\t" + value + "\t" + path); 133 } 134 } 135 136 } 137 } 138