Java tutorial
/* * * * Copyright (C) 2007 Pingtel Corp., certain elements licensed under a Contributor Agreement. * Contributors retain copyright to elements licensed under a Contributor Agreement. * Licensed to the User under the LGPL license. * * */ package org.sipfoundry.sipxconfig.site; import java.util.List; import net.sourceforge.jwebunit.html.Cell; import net.sourceforge.jwebunit.html.Row; import net.sourceforge.jwebunit.html.Table; import net.sourceforge.jwebunit.junit.WebTestCase; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; /** * Support for testing screens (or parts of the screen) that display table with add, delete, move * etc. capability * */ public abstract class ListWebTestCase extends WebTestCase { /** * id of the link on the home page that needs to be pressed before every test to reset the * environment */ private final String m_resetLink; /** prefix for page components - callgroup:list is a table etc. */ private final String m_idPrefix; private final String m_pageLink; private String m_formName; private boolean m_hasDuplicate = true; private boolean m_hasMove = false; private boolean m_exactCheck = true; private boolean m_addLinkSubmit = false; private boolean m_pager = false; public ListWebTestCase(String pageLink, String resetLink, String idPrefix) { m_pageLink = pageLink; m_idPrefix = idPrefix; m_resetLink = resetLink; } @Override public void setUp() { getTestContext().setBaseUrl(SiteTestHelper.getBaseUrl()); SiteTestHelper.home(getTester()); // assumption true for list bridges at least SiteTestHelper.setScriptingEnabled(tester, false); clickLink(m_resetLink); clickLink(m_pageLink); } /** * Overrite to create an array of the form parameter names. * * These names will be set on the add form for the item. Corresponding values have to be * provided by getParamValues function. * */ protected abstract String[] getParamNames(); /** * Returns a set of values corresponding to set of names returned by the first function. * * @param i - to diversify the values - make it part of the value in some way to make tests * more robust. */ protected abstract String[] getParamValues(int i); /** * Returns the table row view of the tested item. * * By default table displays exactly the same values as entered on add form - override it to * change it. * * @param paramValues generated by getParamValues function * @return array of values that correspond to what we need to see in the table */ protected Object[] getExpectedTableRow(String[] paramValues) { return ArrayUtils.add(paramValues, 0, "unchecked"); } protected String buildId(String id) { return m_idPrefix + ":" + id; } protected String buildEditId(String id) { return StringUtils.chop(m_idPrefix) + ":" + id; } // common tests public void testDisplay() { SiteTestHelper.assertNoException(tester); assertFormPresent(); assertLinkPresent(buildId("add")); assertEquals(1 + (m_pager ? 1 : 0), SiteTestHelper.getRowCount(tester, getTableId())); assertButtonPresent(buildId("delete")); if (m_hasDuplicate) { assertButtonPresent(buildId("duplicate")); } if (m_hasMove) { assertButtonPresent(buildId("moveUp")); assertButtonPresent(buildId("moveDown")); } } public void testAdd() throws Exception { final int count = 5; Table expected = new Table(); for (int i = 0; i < count; i++) { String[] values = getParamValues(i); addItem(getParamNames(), values); Row row = new Row(getExpectedTableRow(values)); expected.appendRow(row); } int extraRows = 1 + (m_pager ? 1 : 0); assertEquals(count + extraRows, SiteTestHelper.getRowCount(tester, getTableId())); if (m_exactCheck) { assertTableRowsEqual(getTableId(), extraRows, expected); } else { assertTableRowsExist(getTableId(), expected); } } public void testEdit() throws Exception { String[] values = getParamValues(7); addItem(getParamNames(), values); // click on name - it should take us to the edit page clickLinkWithText(values[0]); String[] names = getParamNames(); for (int i = 0; i < names.length; i++) { assertTextFieldEquals(names[i], values[i]); } } public void testDelete() throws Exception { final int[] toBeRemoved = { 2, 4 }; final int count = 5; Table expected = new Table(); for (int i = 0; i < count; i++) { String[] values = getParamValues(i); addItem(getParamNames(), values); if (!ArrayUtils.contains(toBeRemoved, i)) { expected.appendRow(new Row(getExpectedTableRow(values))); } } if (m_formName != null) { tester.setWorkingForm(m_formName); } // remove 2nd and 4th for (int i = 0; i < toBeRemoved.length; i++) { SiteTestHelper.selectRow(tester, toBeRemoved[i], true); } clickDeleteButton(); SiteTestHelper.assertNoUserError(tester); SiteTestHelper.assertNoException(tester); int extraRows = 1 + (m_pager ? 1 : 0); assertEquals(count + extraRows - toBeRemoved.length, SiteTestHelper.getRowCount(tester, getTableId())); if (m_exactCheck) { assertTableRowsEqual(getTableId(), extraRows, expected); } } protected void clickDeleteButton() { clickButton(buildId("delete")); } protected final void addItem(String[] names, String[] values) throws Exception { SiteTestHelper.assertNoException(tester); assertEquals(names.length, values.length); clickAddLink(); SiteTestHelper.assertNoUserError(tester); setAddParams(names, values); clickButton("form:ok"); SiteTestHelper.assertNoUserError(tester); } protected void clickAddLink() throws Exception { String addLinkId = buildId("add"); if (m_addLinkSubmit) { if (!StringUtils.isEmpty(m_formName)) { tester.setWorkingForm(m_formName); } SiteTestHelper.clickSubmitLink(tester, addLinkId); } else { clickLink(addLinkId); } } private void assertTableRowsExist(String tableId, Table expected) { for (Row row : (List<Row>) expected.getRows()) { for (Cell cell : (List<Cell>) row.getCells()) { assertTextInTable(tableId, cell.getValue()); } } } /** * Overwrite this to set any additional params * * @param names * @param values */ protected void setAddParams(String[] names, String[] values) { for (int i = 0; i < names.length; i++) { setTextField(names[i], values[i]); } } public void setHasDuplicate(boolean hasDuplicate) { m_hasDuplicate = hasDuplicate; } public void setHasMove(boolean hasMove) { m_hasMove = hasMove; } public void setAddLinkSubmit(boolean addLinkSubmit) { m_addLinkSubmit = addLinkSubmit; } public void setExactCheck(boolean exactCheck) { m_exactCheck = exactCheck; } protected String getFormId() { return buildId("form"); } protected String getTableId() { return buildId("list"); } public boolean isPager() { return m_pager; } public void setPager(boolean pager) { m_pager = pager; } public String getFormName() { return m_formName; } public void setFormName(String formName) { m_formName = formName; } }