/** * Convenience method to select an element from the model by its ID. If an * element with a matching ID is not found, <tt>null</tt> will be returned. * * @param id the ID. * @return the element. */ public static synchronized Element getElemnetByID(String id) { return (Element)generatedModel.selectSingleNode("//*[@id='" + id + "']"); }
protected ConfigSaveState updatePartial(String xmlPartial, final String md5) throws Exception { LOGGER.debug("[Config Save] Updating partial"); Document document = documentRoot(); Element root = document.getRootElement(); Element configElement = ((Element) root.selectSingleNode(getXpath())); List nodes = configElement.getParent().content(); int index = nodes.indexOf(configElement); LOGGER.debug("[Config Save] Converting to object"); Element newConfigElement = reader.read(new StringReader(xmlPartial)).getRootElement(); nodes.set(index, newConfigElement); return saveConfig(document.asXML(), md5); }
@Override protected void addDataElement(Element element, List<TestDataElement> testDataElements) { int id = Integer.parseInt( element.selectSingleNode( "id" ).getText() ); String type = element.selectSingleNode( "type" ).getText(); String wkt = element.selectSingleNode( "wkt" ).getText(); String sdo = element.selectSingleNode( "sdo" ).getText(); TestDataElement testDataElement = new SDOTestDataElement( id, type, wkt, sdo ); testDataElements.add( testDataElement ); }
protected void addDataElement(Element element, List<TestDataElement> testDataElements) { int id = Integer.parseInt( element.selectSingleNode( "id" ).getText() ); String type = element.selectSingleNode( "type" ).getText(); String wkt = element.selectSingleNode( "wkt" ).getText(); TestDataElement testDataElement = new TestDataElement( id, type, wkt ); testDataElements.add( testDataElement ); }
private void validateOutput(String output, boolean selected) throws DocumentException { SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals("select", rootElement.getName()); assertEquals("country", rootElement.attribute("name").getValue()); List children = rootElement.elements(); assertEquals("Incorrect number of children", 4, children.size()); Element e = (Element) rootElement.selectSingleNode("option[@value = 'UK']"); Attribute selectedAttr = e.attribute("selected"); if (selected) { assertTrue(selectedAttr != null && "selected".equals(selectedAttr.getValue())); } else { assertNull(selectedAttr); } }
@Test public void withoutItemsEnumBindTarget() throws Exception { BeanWithEnum testBean = new BeanWithEnum(); testBean.setTestEnum(TestEnum.VALUE_2); getPageContext().getRequest().setAttribute("testBean", testBean); this.tag.setPath("testEnum"); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = "<div>" + getOutput() + "</div>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals(2, rootElement.elements().size()); Node value1 = rootElement.selectSingleNode("//input[@value = 'VALUE_1']"); Node value2 = rootElement.selectSingleNode("//input[@value = 'VALUE_2']"); assertEquals("TestEnum: VALUE_1", rootElement.selectSingleNode("//label[@for = '" + value1.valueOf("@id") + "']").getText()); assertEquals("TestEnum: VALUE_2", rootElement.selectSingleNode("//label[@for = '" + value2.valueOf("@id") + "']").getText()); assertEquals(value2, rootElement.selectSingleNode("//input[@checked]")); }
private void assertStringArray() throws JspException, DocumentException { int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); assertTrue(output.startsWith("<select ")); assertTrue(output.endsWith("</select>")); SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals("select", rootElement.getName()); assertEquals("name", rootElement.attribute("name").getValue()); List children = rootElement.elements(); assertEquals("Incorrect number of children", 4, children.size()); Element e = (Element) rootElement.selectSingleNode("option[text() = 'Rob']"); assertEquals("Rob node not selected", "selected", e.attribute("selected").getValue()); }
@Test public void withoutItemsEnumBindTargetWithExplicitLabelsAndValues() throws Exception { BeanWithEnum testBean = new BeanWithEnum(); testBean.setTestEnum(TestEnum.VALUE_2); getPageContext().getRequest().setAttribute("testBean", testBean); this.tag.setPath("testEnum"); this.tag.setItemLabel("enumLabel"); this.tag.setItemValue("enumValue"); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = "<div>" + getOutput() + "</div>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals(2, rootElement.elements().size()); Node value1 = rootElement.selectSingleNode("//input[@value = 'Value: VALUE_1']"); Node value2 = rootElement.selectSingleNode("//input[@value = 'Value: VALUE_2']"); assertEquals("Label: VALUE_1", rootElement.selectSingleNode("//label[@for = '" + value1.valueOf("@id") + "']").getText()); assertEquals("Label: VALUE_2", rootElement.selectSingleNode("//label[@for = '" + value2.valueOf("@id") + "']").getText()); assertEquals(value2, rootElement.selectSingleNode("//input[@checked]")); }
@Test public void withIntegerArray() throws Exception { this.tag.setPath("someIntegerArray"); Integer[] array = new Integer[50]; for (int i = 0; i < array.length; i++) { array[i] = new Integer(i); } this.tag.setItems(array); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); output = "<doc>" + output + "</doc>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals(2, rootElement.elements().size()); Element selectElement = rootElement.element("select"); assertEquals("select", selectElement.getName()); assertEquals("someIntegerArray", selectElement.attribute("name").getValue()); List children = selectElement.elements(); assertEquals("Incorrect number of children", array.length, children.size()); Element e = (Element) selectElement.selectSingleNode("option[text() = '12']"); assertEquals("'12' node not selected", "selected", e.attribute("selected").getValue()); e = (Element) selectElement.selectSingleNode("option[text() = '34']"); assertEquals("'34' node not selected", "selected", e.attribute("selected").getValue()); }
@Test public void withMultiMap() throws Exception { Map someMap = new HashMap(); someMap.put("M", "Male"); someMap.put("F", "Female"); this.bean.setSomeMap(someMap); this.tag.setPath("someMap"); this.tag.setItems(getSexes()); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); output = "<doc>" + output + "</doc>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals(2, rootElement.elements().size()); Element selectElement = rootElement.element("select"); assertEquals("select", selectElement.getName()); assertEquals("someMap", selectElement.attribute("name").getValue()); List children = selectElement.elements(); assertEquals("Incorrect number of children", 2, children.size()); Element e = (Element) selectElement.selectSingleNode("option[@value = 'M']"); assertEquals("M node not selected", "selected", e.attribute("selected").getValue()); e = (Element) selectElement.selectSingleNode("option[@value = 'F']"); assertEquals("F node not selected", "selected", e.attribute("selected").getValue()); }
@Test public void withFloatCustom() throws Exception { PropertyEditor propertyEditor = new SimpleFloatEditor(); BeanPropertyBindingResult errors = new BeanPropertyBindingResult(getTestBean(), COMMAND_NAME); errors.getPropertyAccessor().registerCustomEditor(Float.class, propertyEditor); exposeBindingResult(errors); this.tag.setPath("myFloat"); Float[] array = new Float[] { new Float("12.30"), new Float("12.32"), new Float("12.34"), new Float("12.36"), new Float("12.38"), new Float("12.40"), new Float("12.42"), new Float("12.44"), new Float("12.46"), new Float("12.48") }; this.tag.setItems(array); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); assertTrue(output.startsWith("<select ")); assertTrue(output.endsWith("</select>")); SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals("select", rootElement.getName()); assertEquals("myFloat", rootElement.attribute("name").getValue()); List children = rootElement.elements(); assertEquals("Incorrect number of children", array.length, children.size()); Element e = (Element) rootElement.selectSingleNode("option[text() = '12.34f']"); assertEquals("'12.34' node not selected", "selected", e.attribute("selected").getValue()); e = (Element) rootElement.selectSingleNode("option[text() = '12.32f']"); assertNull("'12.32' node incorrectly selected", e.attribute("selected")); }
@Test public void withoutItemsEnumParent() throws Exception { BeanWithEnum testBean = new BeanWithEnum(); testBean.setTestEnum(TestEnum.VALUE_2); getPageContext().getRequest().setAttribute("testBean", testBean); this.selectTag.setPath("testBean.testEnum"); this.selectTag.doStartTag(); int result = this.tag.doStartTag(); assertEquals(BodyTag.SKIP_BODY, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); this.selectTag.doEndTag(); String output = getWriter().toString(); SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals(2, rootElement.elements().size()); Node value1 = rootElement.selectSingleNode("option[@value = 'VALUE_1']"); Node value2 = rootElement.selectSingleNode("option[@value = 'VALUE_2']"); assertEquals("TestEnum: VALUE_1", value1.getText()); assertEquals("TestEnum: VALUE_2", value2.getText()); assertEquals(value2, rootElement.selectSingleNode("option[@selected]")); }
@Test public void withMultiList() throws Exception { List list = new ArrayList(); list.add(Country.COUNTRY_UK); list.add(Country.COUNTRY_AT); this.bean.setSomeList(list); this.tag.setPath("someList"); this.tag.setItems(Country.getCountries()); this.tag.setItemValue("isoCode"); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); output = "<doc>" + output + "</doc>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals(2, rootElement.elements().size()); Element selectElement = rootElement.element("select"); assertEquals("select", selectElement.getName()); assertEquals("someList", selectElement.attribute("name").getValue()); List children = selectElement.elements(); assertEquals("Incorrect number of children", 4, children.size()); Element e = (Element) selectElement.selectSingleNode("option[@value = 'UK']"); assertEquals("UK node not selected", "selected", e.attribute("selected").getValue()); assertEquals("United Kingdom(UK)", e.getText()); e = (Element) selectElement.selectSingleNode("option[@value = 'AT']"); assertEquals("AT node not selected", "selected", e.attribute("selected").getValue()); assertEquals("Austria(AT)", e.getText()); }
@Test public void withoutItemsEnumParentWithExplicitLabelsAndValues() throws Exception { BeanWithEnum testBean = new BeanWithEnum(); testBean.setTestEnum(TestEnum.VALUE_2); getPageContext().getRequest().setAttribute("testBean", testBean); this.selectTag.setPath("testBean.testEnum"); this.tag.setItemLabel("enumLabel"); this.tag.setItemValue("enumValue"); this.selectTag.doStartTag(); int result = this.tag.doStartTag(); assertEquals(BodyTag.SKIP_BODY, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); this.selectTag.doEndTag(); String output = getWriter().toString(); SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals(2, rootElement.elements().size()); Node value1 = rootElement.selectSingleNode("option[@value = 'Value: VALUE_1']"); Node value2 = rootElement.selectSingleNode("option[@value = 'Value: VALUE_2']"); assertEquals("Label: VALUE_1", value1.getText()); assertEquals("Label: VALUE_2", value2.getText()); assertEquals(value2, rootElement.selectSingleNode("option[@selected]")); }
assertEquals("Incorrect number of children", 6, children.size()); Element element = (Element) rootElement.selectSingleNode("option[text() = '12.34f']"); assertNotNull("Option node should not be null", element); assertEquals("12.34 node not selected", "selected", element.attribute("selected").getValue()); assertNull("No id rendered", element.attribute("id")); element = (Element) rootElement.selectSingleNode("option[text() = '12.35f']"); assertNotNull("Option node should not be null", element); assertNull("12.35 node incorrectly selected", element.attribute("selected"));
assertEquals("Incorrect number of children", 4, children.size()); Element e = (Element) selectElement.selectSingleNode("option[@value = 'UK']"); assertEquals("UK node not selected", "selected", e.attribute("selected").getValue()); e = (Element) selectElement.selectSingleNode("option[@value = 'AT']"); assertEquals("AT node not selected", "selected", e.attribute("selected").getValue());
assertEquals("Incorrect number of children", 4, children.size()); Element e = (Element) selectElement.selectSingleNode("option[@value = 'UK']"); assertEquals("UK node not selected", "selected", e.attribute("selected").getValue()); assertEquals("United Kingdom", e.getText()); e = (Element) selectElement.selectSingleNode("option[@value = 'AT']"); assertEquals("AT node not selected", "selected", e.attribute("selected").getValue()); assertEquals("Austria", e.getText());
assertEquals("Incorrect number of children", 4, children.size()); Element e = (Element) selectElement.selectSingleNode("option[@value = 'UK']"); assertEquals("UK node not selected", "selected", e.attribute("selected").getValue()); assertEquals("United Kingdom", e.getText());
@Test public void withCollection() throws Exception { getPageContext().setAttribute( SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), "testBean.country", false)); this.tag.setItems(Country.getCountries()); this.tag.setItemValue("isoCode"); this.tag.setItemLabel("name"); this.tag.setId("myOption"); this.tag.setCssClass("myClass"); this.tag.setOnclick("CLICK"); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); output = "<doc>" + output + "</doc>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); List children = rootElement.elements(); assertEquals("Incorrect number of children", 4, children.size()); Element element = (Element) rootElement.selectSingleNode("option[@value = 'UK']"); assertEquals("UK node not selected", "selected", element.attribute("selected").getValue()); assertEquals("myOption3", element.attribute("id").getValue()); assertEquals("myClass", element.attribute("class").getValue()); assertEquals("CLICK", element.attribute("onclick").getValue()); }
assertEquals("Incorrect number of children", 4, children.size()); Element element = (Element) rootElement.selectSingleNode("option[@value = 'UK']"); assertEquals("UK node not selected", "selected", element.attribute("selected").getValue()); assertEquals("myOption3", element.attribute("id").getValue());