@Override protected GadgetSpec parse(String content, Query query) throws XmlException, GadgetException { // Allow BOM entity as first item on stream and ignore it: if (content.length() >= BOM_ENTITY.length() && content.substring(0, BOM_ENTITY.length()).equalsIgnoreCase(BOM_ENTITY)) { content = content.substring(BOM_ENTITY.length()); } Element element = XmlUtil.parse(content); return new GadgetSpec(query.getSpecUri(), element, content); } }
@Test(expected=XmlException.class) public void parseBadXmlThrows() throws XmlException { XmlUtil.parse("malformed xml"); } }
@Test(expected=XmlException.class) public void parseBadXmlThrows() throws XmlException { XmlUtil.parse("malformed xml"); } }
@Test(expected = SpecParserException.class) public void testHrefMalformed() throws Exception { // Unfortunately, this actually does URI validation rather than URL, so // most anything will pass. urn:isbn:0321146530 is valid here. String xml = "<Content type=\"url\" href=\"fobad@$%!fdf\"/>"; new View("dummy", Arrays.asList(XmlUtil.parse(xml)), SPEC_URL); }
@Test(expected = SpecParserException.class) public void testSocialPreloadWithoutSignOwner() throws Exception { // Signed, but not by owner when owner data is fetched String xml = "<Content href=\"http://example.org/proxied.php\" " + "authz=\"SIGNED\" sign_owner=\"false\">" + "<OwnerRequest xmlns=\"" + PipelinedData.OPENSOCIAL_NAMESPACE + "\" " + " key=\"key\"" + " fields=\"name,id\"" + "/></Content>"; new View("test", Arrays.asList(XmlUtil.parse(xml)), SPEC_URL); }
@Test public void testPreferredHeight() throws Exception { String content1 = "<Content type=\"html\" preferred_height=\"100\"/>"; String content2 = "<Content type=\"html\" preferred_height=\"300\"/>"; View view = new View("test", Arrays.asList(XmlUtil.parse(content1), XmlUtil.parse(content2)), SPEC_URL); assertEquals(300, view.getPreferredHeight()); }
@Test(expected = SpecParserException.class) public void tooManySpecs() throws Exception { String xml = "<app xmlns='" + ApplicationManifest.NAMESPACE + "'>" + "<gadget>" + " <label>production</label>" + " <version>1.0</version>" + " <spec>whoever</spec>" + " <spec>whatever</spec>" + "</gadget></app>"; new ApplicationManifest(BASE_URI, XmlUtil.parse(xml)); }
@Test(expected = SpecParserException.class) public void parseNoRel() throws Exception { String xml = "<Link href='foo'/>"; new LinkSpec(XmlUtil.parse(xml), SPEC_URL); }
@Test(expected = SpecParserException.class) public void parseNoHref() throws Exception { String xml = "<Link rel='bar'/>"; new LinkSpec(XmlUtil.parse(xml), SPEC_URL); }
@Test(expected = SpecParserException.class) public void malformedHrefThrows() throws Exception { String xml = "<Preload href='@$%@$%$%'/>"; new Preload(XmlUtil.parse(xml), SPEC_URL); } }
@Test public void testPersonToXml() throws Exception { String xml = XSDValidator.validateOpenSocial(beanXmlConverter.convertToString(johnDoe)); Element element = XmlUtil.parse(xml); Node id = element.getElementsByTagName("id").item(0); assertEquals(johnDoe.getId(), id.getTextContent()); }
@Test public void testActivityToXml() throws Exception { String xml = XSDValidator.validateOpenSocial(beanXmlConverter.convertToString(activity)); Element element = XmlUtil.parse(xml); Node id = element.getElementsByTagName("id").item(0); assertEquals(activity.getId(), id.getTextContent()); }
@BeforeClass public static void createDefaultLibrary() throws Exception { doc = XmlUtil.parse(LIB_MARKUP); lib = new XmlTemplateLibrary(Uri.parse("http://example.com/my"), doc, LIB_MARKUP); }
@Test public void nestedMessages() throws Exception { String msgName = "message name"; String msgValue = "message value"; String xml = "<Locale>" + "<msg name=\"" + msgName + "\">" + msgValue + "</msg>" + "</Locale>"; LocaleSpec locale = new LocaleSpec(XmlUtil.parse(xml), SPEC_URL); assertEquals(msgValue, locale.getMessageBundle().getMessages().get(msgName)); }
@Test public void testParseService_nodata() throws Exception { String xml = "<Service/>"; try { new OAuthService(XmlUtil.parse(xml), SPEC_URL); } catch (SpecParserException e) { assertEquals("/OAuth/Service/Request is required", e.getMessage()); } }
@Test public void authzSigned() throws Exception { String xml = "<Preload href='" + HREF + "' authz='signed'/>"; Preload preload = new Preload(XmlUtil.parse(xml), SPEC_URL); assertEquals(AuthType.SIGNED, preload.getAuthType()); }
@Test public void arbitraryAttributes() throws Exception { String xml = "<Preload href='" + HREF + "' foo='bar' yo='momma' sub='__MSG_preload__'/>"; Preload preload = new Preload(XmlUtil.parse(xml), SPEC_URL); Substitutions substituter = new Substitutions(); substituter.addSubstitution(Substitutions.Type.MESSAGE, "preload", "stuff"); Preload substituted = preload.substitute(substituter); assertEquals("bar", substituted.getAttributes().get("foo")); assertEquals("momma", substituted.getAttributes().get("yo")); assertEquals("stuff", substituted.getAttributes().get("sub")); }
@Test public void getLastNamedChildNode() throws Exception { Element root = XmlUtil.parse(XML); assertTrue(DomUtil.getLastNamedChildNode(root, "element") != null); assertEquals("two", DomUtil.getLastNamedChildNode(root, "element").getTextContent()); assertEquals("not real", DomUtil.getLastNamedChildNode(root, "other").getTextContent()); assertNull("Did not return null for missing element.", DomUtil.getLastNamedChildNode(root, "fake")); }
@Test public void testRequire() throws Exception { String xml = "<Require feature=\"foo\"/>"; Feature feature = new Feature(XmlUtil.parse(xml)); assertEquals("foo", feature.getName()); assertTrue(feature.getRequired()); }
@Test public void testSubstitutions() throws Exception { String xml = "<Icon>http://__MSG_domain__/icon.png</Icon>"; Substitutions substituter = new Substitutions(); substituter.addSubstitution(Substitutions.Type.MESSAGE, "domain", "example.org"); Icon icon = new Icon(XmlUtil.parse(xml)).substitute(substituter); assertEquals("http://example.org/icon.png", icon.getContent()); } }