diff options
Diffstat (limited to 'tests/server-side/com/vaadin/data')
43 files changed, 0 insertions, 13083 deletions
diff --git a/tests/server-side/com/vaadin/data/util/AbstractBeanContainerTest.java b/tests/server-side/com/vaadin/data/util/AbstractBeanContainerTest.java deleted file mode 100644 index 3c74a45bd7..0000000000 --- a/tests/server-side/com/vaadin/data/util/AbstractBeanContainerTest.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.vaadin.data.util; - -import com.vaadin.data.util.AbstractBeanContainer; -import com.vaadin.data.util.BeanContainer; -import com.vaadin.data.util.BeanItemContainer; - -/** - * Automated test for {@link AbstractBeanContainer}. - * - * Only a limited subset of the functionality is tested here, the rest in tests - * of subclasses including {@link BeanItemContainer} and {@link BeanContainer}. - */ -public abstract class AbstractBeanContainerTest extends - AbstractInMemoryContainerTest { - - public static class Person { - private String name; - - public Person(String name) { - setName(name); - } - - public void setName(String name) { - this.name = name; - } - - public String getName() { - return name; - } - } - - public static class ClassName { - // field names match constants in parent test class - private String fullyQualifiedName; - private String simpleName; - private String reverseFullyQualifiedName; - private Integer idNumber; - - public ClassName(String fullyQualifiedName, Integer idNumber) { - this.fullyQualifiedName = fullyQualifiedName; - simpleName = AbstractContainerTest - .getSimpleName(fullyQualifiedName); - reverseFullyQualifiedName = reverse(fullyQualifiedName); - this.idNumber = idNumber; - } - - public String getFullyQualifiedName() { - return fullyQualifiedName; - } - - public void setFullyQualifiedName(String fullyQualifiedName) { - this.fullyQualifiedName = fullyQualifiedName; - } - - public String getSimpleName() { - return simpleName; - } - - public void setSimpleName(String simpleName) { - this.simpleName = simpleName; - } - - public String getReverseFullyQualifiedName() { - return reverseFullyQualifiedName; - } - - public void setReverseFullyQualifiedName( - String reverseFullyQualifiedName) { - this.reverseFullyQualifiedName = reverseFullyQualifiedName; - } - - public Integer getIdNumber() { - return idNumber; - } - - public void setIdNumber(Integer idNumber) { - this.idNumber = idNumber; - } - } - -} diff --git a/tests/server-side/com/vaadin/data/util/AbstractContainerTest.java b/tests/server-side/com/vaadin/data/util/AbstractContainerTest.java deleted file mode 100644 index b0151a6690..0000000000 --- a/tests/server-side/com/vaadin/data/util/AbstractContainerTest.java +++ /dev/null @@ -1,649 +0,0 @@ -package com.vaadin.data.util; - -import java.util.ArrayList; -import java.util.List; - -import junit.framework.Assert; -import junit.framework.TestCase; - -import com.vaadin.data.Container; -import com.vaadin.data.Container.Filterable; -import com.vaadin.data.Container.ItemSetChangeEvent; -import com.vaadin.data.Container.ItemSetChangeListener; -import com.vaadin.data.Container.Sortable; -import com.vaadin.data.Item; -import com.vaadin.data.util.filter.SimpleStringFilter; - -public abstract class AbstractContainerTest extends TestCase { - - /** - * Helper class for testing e.g. listeners expecting events to be fired. - */ - protected abstract static class AbstractEventCounter { - private int eventCount = 0; - private int lastAssertedEventCount = 0; - - /** - * Increment the event count. To be called by subclasses e.g. from a - * listener method. - */ - protected void increment() { - ++eventCount; - } - - /** - * Check that no one event has occurred since the previous assert call. - */ - public void assertNone() { - Assert.assertEquals(lastAssertedEventCount, eventCount); - } - - /** - * Check that exactly one event has occurred since the previous assert - * call. - */ - public void assertOnce() { - Assert.assertEquals(++lastAssertedEventCount, eventCount); - } - - /** - * Reset the counter and the expected count. - */ - public void reset() { - eventCount = 0; - lastAssertedEventCount = 0; - } - } - - /** - * Test class for counting item set change events and verifying they have - * been received. - */ - protected static class ItemSetChangeCounter extends AbstractEventCounter - implements ItemSetChangeListener { - - @Override - public void containerItemSetChange(ItemSetChangeEvent event) { - increment(); - } - - } - - // #6043: for items that have been filtered out, Container interface does - // not specify what to return from getItem() and getContainerProperty(), so - // need checkGetItemNull parameter for the test to be usable for most - // current containers - protected void validateContainer(Container container, - Object expectedFirstItemId, Object expectedLastItemId, - Object itemIdInSet, Object itemIdNotInSet, - boolean checkGetItemNull, int expectedSize) { - Container.Indexed indexed = null; - if (container instanceof Container.Indexed) { - indexed = (Container.Indexed) container; - } - - List<Object> itemIdList = new ArrayList<Object>(container.getItemIds()); - - // size() - assertEquals(expectedSize, container.size()); - assertEquals(expectedSize, itemIdList.size()); - - // first item, last item - Object first = itemIdList.get(0); - Object last = itemIdList.get(itemIdList.size() - 1); - - assertEquals(expectedFirstItemId, first); - assertEquals(expectedLastItemId, last); - - // containsId - assertFalse(container.containsId(itemIdNotInSet)); - assertTrue(container.containsId(itemIdInSet)); - - // getItem - if (checkGetItemNull) { - assertNull(container.getItem(itemIdNotInSet)); - } - assertNotNull(container.getItem(itemIdInSet)); - - // getContainerProperty - for (Object propId : container.getContainerPropertyIds()) { - if (checkGetItemNull) { - assertNull(container.getContainerProperty(itemIdNotInSet, - propId)); - } - assertNotNull(container.getContainerProperty(itemIdInSet, propId)); - } - - if (indexed != null) { - // firstItemId - assertEquals(first, indexed.firstItemId()); - - // lastItemId - assertEquals(last, indexed.lastItemId()); - - // nextItemId - assertEquals(itemIdList.get(1), indexed.nextItemId(first)); - - // prevItemId - assertEquals(itemIdList.get(itemIdList.size() - 2), - indexed.prevItemId(last)); - - // isFirstId - assertTrue(indexed.isFirstId(first)); - assertFalse(indexed.isFirstId(last)); - - // isLastId - assertTrue(indexed.isLastId(last)); - assertFalse(indexed.isLastId(first)); - - // indexOfId - assertEquals(0, indexed.indexOfId(first)); - assertEquals(expectedSize - 1, indexed.indexOfId(last)); - - // getIdByIndex - assertEquals(indexed.getIdByIndex(0), first); - assertEquals(indexed.getIdByIndex(expectedSize - 1), last); - - } - - } - - protected static final Object FULLY_QUALIFIED_NAME = "fullyQualifiedName"; - protected static final Object SIMPLE_NAME = "simpleName"; - protected static final Object REVERSE_FULLY_QUALIFIED_NAME = "reverseFullyQualifiedName"; - protected static final Object ID_NUMBER = "idNumber"; - - protected void testBasicContainerOperations(Container container) { - initializeContainer(container); - - // Basic container - validateContainer(container, sampleData[0], - sampleData[sampleData.length - 1], sampleData[10], "abc", true, - sampleData.length); - } - - protected void testContainerOrdered(Container.Ordered container) { - Object id = container.addItem(); - assertNotNull(id); - Item item = container.getItem(id); - assertNotNull(item); - - assertEquals(id, container.firstItemId()); - assertEquals(id, container.lastItemId()); - - // isFirstId - assertTrue(container.isFirstId(id)); - assertTrue(container.isFirstId(container.firstItemId())); - // isLastId - assertTrue(container.isLastId(id)); - assertTrue(container.isLastId(container.lastItemId())); - - // Add a new item before the first - // addItemAfter - Object newFirstId = container.addItemAfter(null); - assertNotNull(newFirstId); - assertNotNull(container.getItem(newFirstId)); - - // isFirstId - assertTrue(container.isFirstId(newFirstId)); - assertTrue(container.isFirstId(container.firstItemId())); - // isLastId - assertTrue(container.isLastId(id)); - assertTrue(container.isLastId(container.lastItemId())); - - // nextItemId - assertEquals(id, container.nextItemId(newFirstId)); - assertNull(container.nextItemId(id)); - assertNull(container.nextItemId("not-in-container")); - - // prevItemId - assertEquals(newFirstId, container.prevItemId(id)); - assertNull(container.prevItemId(newFirstId)); - assertNull(container.prevItemId("not-in-container")); - - // addItemAfter(Object) - Object newSecondItemId = container.addItemAfter(newFirstId); - // order is now: newFirstId, newSecondItemId, id - assertNotNull(newSecondItemId); - assertNotNull(container.getItem(newSecondItemId)); - assertEquals(id, container.nextItemId(newSecondItemId)); - assertEquals(newFirstId, container.prevItemId(newSecondItemId)); - - // addItemAfter(Object,Object) - String fourthId = "id of the fourth item"; - Item fourth = container.addItemAfter(newFirstId, fourthId); - // order is now: newFirstId, fourthId, newSecondItemId, id - assertNotNull(fourth); - assertEquals(fourth, container.getItem(fourthId)); - assertEquals(newSecondItemId, container.nextItemId(fourthId)); - assertEquals(newFirstId, container.prevItemId(fourthId)); - - // addItemAfter(Object,Object) - Object fifthId = new Object(); - Item fifth = container.addItemAfter(null, fifthId); - // order is now: fifthId, newFirstId, fourthId, newSecondItemId, id - assertNotNull(fifth); - assertEquals(fifth, container.getItem(fifthId)); - assertEquals(newFirstId, container.nextItemId(fifthId)); - assertNull(container.prevItemId(fifthId)); - - } - - protected void testContainerIndexed(Container.Indexed container, - Object itemId, int itemPosition, boolean testAddEmptyItemAt, - Object newItemId, boolean testAddItemAtWithId) { - initializeContainer(container); - - // indexOfId - Assert.assertEquals(itemPosition, container.indexOfId(itemId)); - - // getIdByIndex - Assert.assertEquals(itemId, container.getIdByIndex(itemPosition)); - - // addItemAt - if (testAddEmptyItemAt) { - Object addedId = container.addItemAt(itemPosition); - Assert.assertEquals(itemPosition, container.indexOfId(addedId)); - Assert.assertEquals(itemPosition + 1, container.indexOfId(itemId)); - Assert.assertEquals(addedId, container.getIdByIndex(itemPosition)); - Assert.assertEquals(itemId, - container.getIdByIndex(itemPosition + 1)); - - Object newFirstId = container.addItemAt(0); - Assert.assertEquals(0, container.indexOfId(newFirstId)); - Assert.assertEquals(itemPosition + 2, container.indexOfId(itemId)); - Assert.assertEquals(newFirstId, container.firstItemId()); - Assert.assertEquals(newFirstId, container.getIdByIndex(0)); - Assert.assertEquals(itemId, - container.getIdByIndex(itemPosition + 2)); - - Object newLastId = container.addItemAt(container.size()); - Assert.assertEquals(container.size() - 1, - container.indexOfId(newLastId)); - Assert.assertEquals(itemPosition + 2, container.indexOfId(itemId)); - Assert.assertEquals(newLastId, container.lastItemId()); - Assert.assertEquals(newLastId, - container.getIdByIndex(container.size() - 1)); - Assert.assertEquals(itemId, - container.getIdByIndex(itemPosition + 2)); - - Assert.assertTrue(container.removeItem(addedId)); - Assert.assertTrue(container.removeItem(newFirstId)); - Assert.assertTrue(container.removeItem(newLastId)); - - Assert.assertFalse( - "Removing non-existing item should indicate failure", - container.removeItem(addedId)); - } - - // addItemAt - if (testAddItemAtWithId) { - container.addItemAt(itemPosition, newItemId); - Assert.assertEquals(itemPosition, container.indexOfId(newItemId)); - Assert.assertEquals(itemPosition + 1, container.indexOfId(itemId)); - Assert.assertEquals(newItemId, container.getIdByIndex(itemPosition)); - Assert.assertEquals(itemId, - container.getIdByIndex(itemPosition + 1)); - Assert.assertTrue(container.removeItem(newItemId)); - Assert.assertFalse(container.containsId(newItemId)); - - container.addItemAt(0, newItemId); - Assert.assertEquals(0, container.indexOfId(newItemId)); - Assert.assertEquals(itemPosition + 1, container.indexOfId(itemId)); - Assert.assertEquals(newItemId, container.firstItemId()); - Assert.assertEquals(newItemId, container.getIdByIndex(0)); - Assert.assertEquals(itemId, - container.getIdByIndex(itemPosition + 1)); - Assert.assertTrue(container.removeItem(newItemId)); - Assert.assertFalse(container.containsId(newItemId)); - - container.addItemAt(container.size(), newItemId); - Assert.assertEquals(container.size() - 1, - container.indexOfId(newItemId)); - Assert.assertEquals(itemPosition, container.indexOfId(itemId)); - Assert.assertEquals(newItemId, container.lastItemId()); - Assert.assertEquals(newItemId, - container.getIdByIndex(container.size() - 1)); - Assert.assertEquals(itemId, container.getIdByIndex(itemPosition)); - Assert.assertTrue(container.removeItem(newItemId)); - Assert.assertFalse(container.containsId(newItemId)); - } - } - - protected void testContainerFiltering(Container.Filterable container) { - initializeContainer(container); - - // Filter by "contains ab" - container.addContainerFilter(new SimpleStringFilter( - FULLY_QUALIFIED_NAME, "ab", false, false)); - - validateContainer(container, "com.vaadin.data.BufferedValidatable", - "com.vaadin.ui.TabSheet", "com.vaadin.client.Focusable", - "com.vaadin.data.Buffered", isFilteredOutItemNull(), 20); - - // Filter by "contains da" (reversed as ad here) - container.removeAllContainerFilters(); - container.addContainerFilter(new SimpleStringFilter( - REVERSE_FULLY_QUALIFIED_NAME, "ad", false, false)); - - validateContainer(container, "com.vaadin.data.Buffered", - "com.vaadin.server.ComponentSizeValidator", - "com.vaadin.data.util.IndexedContainer", - "com.vaadin.client.ui.VUriFragmentUtility", - isFilteredOutItemNull(), 37); - } - - /** - * Override in subclasses to return false if the container getItem() method - * returns a non-null value for an item that has been filtered out. - * - * @return - */ - protected boolean isFilteredOutItemNull() { - return true; - } - - protected void testContainerSortingAndFiltering(Container.Sortable sortable) { - Filterable filterable = (Filterable) sortable; - - initializeContainer(sortable); - - // Filter by "contains ab" - filterable.addContainerFilter(new SimpleStringFilter( - FULLY_QUALIFIED_NAME, "ab", false, false)); - - // Must be able to sort based on PROP1 for this test - assertTrue(sortable.getSortableContainerPropertyIds().contains( - FULLY_QUALIFIED_NAME)); - - sortable.sort(new Object[] { FULLY_QUALIFIED_NAME }, - new boolean[] { true }); - - validateContainer(sortable, "com.vaadin.data.BufferedValidatable", - "com.vaadin.ui.TableFieldFactory", - "com.vaadin.ui.TableFieldFactory", - "com.vaadin.data.util.BeanItem", isFilteredOutItemNull(), 20); - } - - protected void testContainerSorting(Container.Filterable container) { - Container.Sortable sortable = (Sortable) container; - - initializeContainer(container); - - // Must be able to sort based on PROP1 for this test - assertTrue(sortable.getSortableContainerPropertyIds().contains( - FULLY_QUALIFIED_NAME)); - assertTrue(sortable.getSortableContainerPropertyIds().contains( - REVERSE_FULLY_QUALIFIED_NAME)); - - sortable.sort(new Object[] { FULLY_QUALIFIED_NAME }, - new boolean[] { true }); - - validateContainer(container, "com.vaadin.Application", - "org.vaadin.test.LastClass", - "com.vaadin.server.ApplicationResource", "blah", true, - sampleData.length); - - sortable.sort(new Object[] { REVERSE_FULLY_QUALIFIED_NAME }, - new boolean[] { true }); - - validateContainer(container, "com.vaadin.server.ApplicationPortlet2", - "com.vaadin.data.util.ObjectProperty", - "com.vaadin.ui.BaseFieldFactory", "blah", true, - sampleData.length); - - } - - protected void initializeContainer(Container container) { - Assert.assertTrue(container.removeAllItems()); - Object[] propertyIds = container.getContainerPropertyIds().toArray(); - for (Object propertyId : propertyIds) { - container.removeContainerProperty(propertyId); - } - - container.addContainerProperty(FULLY_QUALIFIED_NAME, String.class, ""); - container.addContainerProperty(SIMPLE_NAME, String.class, ""); - container.addContainerProperty(REVERSE_FULLY_QUALIFIED_NAME, - String.class, null); - container.addContainerProperty(ID_NUMBER, Integer.class, null); - - for (int i = 0; i < sampleData.length; i++) { - String id = sampleData[i]; - Item item = container.addItem(id); - - item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(sampleData[i]); - item.getItemProperty(SIMPLE_NAME).setValue( - getSimpleName(sampleData[i])); - item.getItemProperty(REVERSE_FULLY_QUALIFIED_NAME).setValue( - reverse(sampleData[i])); - item.getItemProperty(ID_NUMBER).setValue(i); - } - } - - protected static String getSimpleName(String name) { - if (name.contains(".")) { - return name.substring(name.lastIndexOf('.') + 1); - } else { - return name; - } - } - - protected static String reverse(String string) { - return new StringBuilder(string).reverse().toString(); - } - - protected final String[] sampleData = { - "com.vaadin.annotations.AutoGenerated", "com.vaadin.Application", - "com.vaadin.data.Buffered", "com.vaadin.data.BufferedValidatable", - "com.vaadin.data.Container", "com.vaadin.data.Item", - "com.vaadin.data.Property", "com.vaadin.data.util.BeanItem", - "com.vaadin.data.util.BeanItemContainer", - "com.vaadin.data.util.ContainerHierarchicalWrapper", - "com.vaadin.data.util.ContainerOrderedWrapper", - "com.vaadin.data.util.DefaultItemSorter", - "com.vaadin.data.util.FilesystemContainer", - "com.vaadin.data.util.Filter", - "com.vaadin.data.util.HierarchicalContainer", - "com.vaadin.data.util.IndexedContainer", - "com.vaadin.data.util.ItemSorter", - "com.vaadin.data.util.MethodProperty", - "com.vaadin.data.util.ObjectProperty", - "com.vaadin.data.util.PropertyFormatter", - "com.vaadin.data.util.PropertysetItem", - "com.vaadin.data.util.QueryContainer", - "com.vaadin.data.util.TextFileProperty", - "com.vaadin.data.Validatable", - "com.vaadin.data.validator.AbstractStringValidator", - "com.vaadin.data.validator.AbstractValidator", - "com.vaadin.data.validator.CompositeValidator", - "com.vaadin.data.validator.DoubleValidator", - "com.vaadin.data.validator.EmailValidator", - "com.vaadin.data.validator.IntegerValidator", - "com.vaadin.data.validator.NullValidator", - "com.vaadin.data.validator.RegexpValidator", - "com.vaadin.data.validator.StringLengthValidator", - "com.vaadin.data.Validator", "com.vaadin.event.Action", - "com.vaadin.event.ComponentEventListener", - "com.vaadin.event.EventRouter", "com.vaadin.event.FieldEvents", - "com.vaadin.event.ItemClickEvent", "com.vaadin.event.LayoutEvents", - "com.vaadin.event.ListenerMethod", - "com.vaadin.event.MethodEventSource", - "com.vaadin.event.MouseEvents", "com.vaadin.event.ShortcutAction", - "com.vaadin.launcher.DemoLauncher", - "com.vaadin.launcher.DevelopmentServerLauncher", - "com.vaadin.launcher.util.BrowserLauncher", - "com.vaadin.service.ApplicationContext", - "com.vaadin.service.FileTypeResolver", - "com.vaadin.server.ApplicationResource", - "com.vaadin.server.ClassResource", - "com.vaadin.server.CompositeErrorMessage", - "com.vaadin.server.DownloadStream", - "com.vaadin.server.ErrorMessage", - "com.vaadin.server.ExternalResource", - "com.vaadin.server.FileResource", - "com.vaadin.client.ApplicationConfiguration", - "com.vaadin.client.ApplicationConnection", - "com.vaadin.client.BrowserInfo", - "com.vaadin.client.ClientExceptionHandler", - "com.vaadin.client.ComponentDetail", - "com.vaadin.client.ComponentDetailMap", - "com.vaadin.client.ComponentLocator", "com.vaadin.client.Console", - "com.vaadin.client.Container", - "com.vaadin.client.ContainerResizedListener", - "com.vaadin.client.CSSRule", "com.vaadin.client.DateTimeService", - "com.vaadin.client.DefaultWidgetSet", - "com.vaadin.client.Focusable", - "com.vaadin.client.HistoryImplIEVaadin", - "com.vaadin.client.LocaleNotLoadedException", - "com.vaadin.client.LocaleService", - "com.vaadin.client.MouseEventDetails", - "com.vaadin.client.NullConsole", "com.vaadin.client.Paintable", - "com.vaadin.client.RenderInformation", - "com.vaadin.client.RenderSpace", - "com.vaadin.client.StyleConstants", - "com.vaadin.client.TooltipInfo", "com.vaadin.client.ui.Action", - "com.vaadin.client.ui.ActionOwner", - "com.vaadin.client.ui.AlignmentInfo", - "com.vaadin.client.ui.CalendarEntry", - "com.vaadin.client.ui.ClickEventHandler", - "com.vaadin.client.ui.Field", "com.vaadin.client.ui.Icon", - "com.vaadin.client.ui.layout.CellBasedLayout", - "com.vaadin.client.ui.layout.ChildComponentContainer", - "com.vaadin.client.ui.layout.Margins", - "com.vaadin.client.ui.LayoutClickEventHandler", - "com.vaadin.client.ui.MenuBar", "com.vaadin.client.ui.MenuItem", - "com.vaadin.client.ui.richtextarea.VRichTextArea", - "com.vaadin.client.ui.richtextarea.VRichTextToolbar", - "com.vaadin.client.ui.ShortcutActionHandler", - "com.vaadin.client.ui.SubPartAware", "com.vaadin.client.ui.Table", - "com.vaadin.client.ui.TreeAction", - "com.vaadin.client.ui.TreeImages", - "com.vaadin.client.ui.VAbsoluteLayout", - "com.vaadin.client.ui.VAccordion", "com.vaadin.client.ui.VButton", - "com.vaadin.client.ui.VCalendarPanel", - "com.vaadin.client.ui.VCheckBox", - "com.vaadin.client.ui.VContextMenu", - "com.vaadin.client.ui.VCssLayout", - "com.vaadin.client.ui.VCustomComponent", - "com.vaadin.client.ui.VCustomLayout", - "com.vaadin.client.ui.VDateField", - "com.vaadin.client.ui.VDateFieldCalendar", - "com.vaadin.client.ui.VEmbedded", - "com.vaadin.client.ui.VFilterSelect", "com.vaadin.client.ui.VForm", - "com.vaadin.client.ui.VFormLayout", - "com.vaadin.client.ui.VGridLayout", - "com.vaadin.client.ui.VHorizontalLayout", - "com.vaadin.client.ui.VLabel", "com.vaadin.client.ui.VLink", - "com.vaadin.client.ui.VListSelect", - "com.vaadin.client.ui.VMarginInfo", - "com.vaadin.client.ui.VMenuBar", - "com.vaadin.client.ui.VNativeButton", - "com.vaadin.client.ui.VNativeSelect", - "com.vaadin.client.ui.VNotification", - "com.vaadin.client.ui.VOptionGroup", - "com.vaadin.client.ui.VOptionGroupBase", - "com.vaadin.client.ui.VOrderedLayout", - "com.vaadin.client.ui.VOverlay", "com.vaadin.client.ui.VPanel", - "com.vaadin.client.ui.VPasswordField", - "com.vaadin.client.ui.VPopupCalendar", - "com.vaadin.client.ui.VPopupView", - "com.vaadin.client.ui.VProgressIndicator", - "com.vaadin.client.ui.VScrollTable", - "com.vaadin.client.ui.VSlider", "com.vaadin.client.ui.VSplitPanel", - "com.vaadin.client.ui.VSplitPanelHorizontal", - "com.vaadin.client.ui.VSplitPanelVertical", - "com.vaadin.client.ui.VTablePaging", - "com.vaadin.client.ui.VTabsheet", - "com.vaadin.client.ui.VTabsheetBase", - "com.vaadin.client.ui.VTabsheetPanel", - "com.vaadin.client.ui.VTextArea", - "com.vaadin.client.ui.VTextField", - "com.vaadin.client.ui.VTextualDate", "com.vaadin.client.ui.VTime", - "com.vaadin.client.ui.VTree", - "com.vaadin.client.ui.VTwinColSelect", - "com.vaadin.client.ui.VUnknownComponent", - "com.vaadin.client.ui.VUpload", - "com.vaadin.client.ui.VUriFragmentUtility", - "com.vaadin.client.ui.VVerticalLayout", - "com.vaadin.client.ui.VView", "com.vaadin.client.ui.VWindow", - "com.vaadin.client.UIDL", "com.vaadin.client.Util", - "com.vaadin.client.ValueMap", "com.vaadin.client.VCaption", - "com.vaadin.client.VCaptionWrapper", - "com.vaadin.client.VDebugConsole", - "com.vaadin.client.VErrorMessage", "com.vaadin.client.VTooltip", - "com.vaadin.client.VUIDLBrowser", "com.vaadin.client.WidgetMap", - "com.vaadin.client.WidgetSet", - "com.vaadin.server.AbstractApplicationPortlet", - "com.vaadin.server.AbstractApplicationServlet", - "com.vaadin.server.AbstractCommunicationManager", - "com.vaadin.server.AbstractWebApplicationContext", - "com.vaadin.server.ApplicationPortlet", - "com.vaadin.server.ApplicationPortlet2", - "com.vaadin.server.ApplicationRunnerServlet", - "com.vaadin.server.ApplicationServlet", - "com.vaadin.server.ChangeVariablesErrorEvent", - "com.vaadin.server.CommunicationManager", - "com.vaadin.server.ComponentSizeValidator", - "com.vaadin.server.Constants", - "com.vaadin.server.GAEApplicationServlet", - "com.vaadin.server.HttpServletRequestListener", - "com.vaadin.server.HttpUploadStream", - "com.vaadin.server.JsonPaintTarget", - "com.vaadin.server.PortletApplicationContext", - "com.vaadin.server.PortletApplicationContext2", - "com.vaadin.server.PortletCommunicationManager", - "com.vaadin.server.PortletRequestListener", - "com.vaadin.server.RestrictedRenderResponse", - "com.vaadin.server.SessionExpiredException", - "com.vaadin.server.SystemMessageException", - "com.vaadin.server.WebApplicationContext", - "com.vaadin.server.WebBrowser", - "com.vaadin.server.widgetsetutils.ClassPathExplorer", - "com.vaadin.server.widgetsetutils.WidgetMapGenerator", - "com.vaadin.server.widgetsetutils.WidgetSetBuilder", - "com.vaadin.server.KeyMapper", "com.vaadin.server.Paintable", - "com.vaadin.server.PaintException", - "com.vaadin.server.PaintTarget", - "com.vaadin.server.ParameterHandler", "com.vaadin.server.Resource", - "com.vaadin.server.Scrollable", "com.vaadin.server.Sizeable", - "com.vaadin.server.StreamResource", - "com.vaadin.server.SystemError", "com.vaadin.server.Terminal", - "com.vaadin.server.ThemeResource", - "com.vaadin.server.UploadStream", "com.vaadin.server.URIHandler", - "com.vaadin.server.UserError", "com.vaadin.server.VariableOwner", - "com.vaadin.tools.ReflectTools", - "com.vaadin.tools.WidgetsetCompiler", - "com.vaadin.ui.AbsoluteLayout", "com.vaadin.ui.AbstractComponent", - "com.vaadin.ui.AbstractComponentContainer", - "com.vaadin.ui.AbstractField", "com.vaadin.ui.AbstractLayout", - "com.vaadin.ui.AbstractOrderedLayout", - "com.vaadin.ui.AbstractSelect", "com.vaadin.ui.Accordion", - "com.vaadin.ui.Alignment", "com.vaadin.ui.AlignmentUtils", - "com.vaadin.ui.BaseFieldFactory", "com.vaadin.ui.Button", - "com.vaadin.ui.CheckBox", "com.vaadin.ui.ClientWidget", - "com.vaadin.ui.ComboBox", "com.vaadin.ui.Component", - "com.vaadin.ui.ComponentContainer", "com.vaadin.ui.CssLayout", - "com.vaadin.ui.CustomComponent", "com.vaadin.ui.CustomLayout", - "com.vaadin.ui.DateField", "com.vaadin.ui.DefaultFieldFactory", - "com.vaadin.ui.Embedded", "com.vaadin.ui.ExpandLayout", - "com.vaadin.ui.Field", "com.vaadin.ui.FieldFactory", - "com.vaadin.ui.Form", "com.vaadin.ui.FormFieldFactory", - "com.vaadin.ui.FormLayout", "com.vaadin.ui.GridLayout", - "com.vaadin.ui.HorizontalLayout", "com.vaadin.ui.InlineDateField", - "com.vaadin.ui.Label", "com.vaadin.ui.Layout", - "com.vaadin.ui.Link", "com.vaadin.ui.ListSelect", - "com.vaadin.ui.LoginForm", "com.vaadin.ui.MenuBar", - "com.vaadin.ui.NativeButton", "com.vaadin.ui.NativeSelect", - "com.vaadin.ui.OptionGroup", "com.vaadin.ui.OrderedLayout", - "com.vaadin.ui.Panel", "com.vaadin.ui.PopupDateField", - "com.vaadin.ui.PopupView", "com.vaadin.ui.ProgressIndicator", - "com.vaadin.ui.RichTextArea", "com.vaadin.ui.Select", - "com.vaadin.ui.Slider", "com.vaadin.ui.SplitPanel", - "com.vaadin.ui.Table", "com.vaadin.ui.TableFieldFactory", - "com.vaadin.ui.TabSheet", "com.vaadin.ui.TextField", - "com.vaadin.ui.Tree", "com.vaadin.ui.TwinColSelect", - "com.vaadin.ui.Upload", "com.vaadin.ui.UriFragmentUtility", - "com.vaadin.ui.VerticalLayout", "com.vaadin.ui.Window", - "com.vaadin.util.SerializerHelper", "org.vaadin.test.LastClass" }; - -} diff --git a/tests/server-side/com/vaadin/data/util/AbstractHierarchicalContainerTest.java b/tests/server-side/com/vaadin/data/util/AbstractHierarchicalContainerTest.java deleted file mode 100644 index 772cd250ce..0000000000 --- a/tests/server-side/com/vaadin/data/util/AbstractHierarchicalContainerTest.java +++ /dev/null @@ -1,256 +0,0 @@ -package com.vaadin.data.util; - -import java.util.Collection; - -import com.vaadin.data.Container; -import com.vaadin.data.Container.Hierarchical; -import com.vaadin.data.Container.Sortable; -import com.vaadin.data.Item; - -public abstract class AbstractHierarchicalContainerTest extends - AbstractContainerTest { - - /** - * @param container - * The container to validate - * @param expectedFirstItemId - * Expected first item id - * @param expectedLastItemId - * Expected last item id - * @param itemIdInSet - * An item id that is in the container - * @param itemIdNotInSet - * An item id that is not in the container - * @param checkGetItemNull - * true if getItem() should return null for itemIdNotInSet, false - * to skip the check (container.containsId() is checked in any - * case) - * @param expectedSize - * Expected number of items in the container. Not related to - * hierarchy. - * @param expectedTraversalSize - * Expected number of items found when traversing from the roots - * down to all available nodes. - * @param expectedRootSize - * Expected number of root items - * @param rootsHaveChildren - * true if all roots have children, false otherwise (skips some - * asserts) - */ - protected void validateHierarchicalContainer(Hierarchical container, - Object expectedFirstItemId, Object expectedLastItemId, - Object itemIdInSet, Object itemIdNotInSet, - boolean checkGetItemNull, int expectedSize, int expectedRootSize, - boolean rootsHaveChildren) { - - validateContainer(container, expectedFirstItemId, expectedLastItemId, - itemIdInSet, itemIdNotInSet, checkGetItemNull, expectedSize); - - // rootItemIds - Collection<?> rootIds = container.rootItemIds(); - assertEquals(expectedRootSize, rootIds.size()); - - for (Object rootId : rootIds) { - // All roots must be in container - assertTrue(container.containsId(rootId)); - - // All roots must have no parent - assertNull(container.getParent(rootId)); - - // all roots must be roots - assertTrue(container.isRoot(rootId)); - - if (rootsHaveChildren) { - // all roots have children allowed in this case - assertTrue(container.areChildrenAllowed(rootId)); - - // all roots have children in this case - Collection<?> children = container.getChildren(rootId); - assertNotNull(rootId + " should have children", children); - assertTrue(rootId + " should have children", - (children.size() > 0)); - // getParent - for (Object childId : children) { - assertEquals(container.getParent(childId), rootId); - } - - } - } - - // isRoot should return false for unknown items - assertFalse(container.isRoot(itemIdNotInSet)); - - // hasChildren should return false for unknown items - assertFalse(container.hasChildren(itemIdNotInSet)); - - // areChildrenAllowed should return false for unknown items - assertFalse(container.areChildrenAllowed(itemIdNotInSet)); - - // removeItem of unknown items should return false - assertFalse(container.removeItem(itemIdNotInSet)); - - assertEquals(expectedSize, countNodes(container)); - - validateHierarchy(container); - } - - private int countNodes(Hierarchical container) { - int totalNodes = 0; - for (Object rootId : container.rootItemIds()) { - totalNodes += countNodes(container, rootId); - } - - return totalNodes; - } - - private int countNodes(Hierarchical container, Object itemId) { - int nodes = 1; // This - Collection<?> children = container.getChildren(itemId); - if (children != null) { - for (Object id : children) { - nodes += countNodes(container, id); - } - } - - return nodes; - } - - private void validateHierarchy(Hierarchical container) { - for (Object rootId : container.rootItemIds()) { - validateHierarchy(container, rootId, null); - } - } - - private void validateHierarchy(Hierarchical container, Object itemId, - Object parentId) { - Collection<?> children = container.getChildren(itemId); - - // getParent - assertEquals(container.getParent(itemId), parentId); - - if (!container.areChildrenAllowed(itemId)) { - // If no children is allowed the item should have no children - assertFalse(container.hasChildren(itemId)); - assertTrue(children == null || children.size() == 0); - - return; - } - if (children != null) { - for (Object id : children) { - validateHierarchy(container, id, itemId); - } - } - } - - protected void testHierarchicalContainer(Container.Hierarchical container) { - initializeContainer(container); - - int packages = 21 + 3; - int expectedSize = sampleData.length + packages; - validateHierarchicalContainer(container, "com", - "org.vaadin.test.LastClass", - "com.vaadin.server.ApplicationResource", "blah", true, - expectedSize, 2, true); - - } - - protected void testHierarchicalSorting(Container.Hierarchical container) { - Container.Sortable sortable = (Sortable) container; - - initializeContainer(container); - - // Must be able to sort based on PROP1 and PROP2 for this test - assertTrue(sortable.getSortableContainerPropertyIds().contains( - FULLY_QUALIFIED_NAME)); - assertTrue(sortable.getSortableContainerPropertyIds().contains( - REVERSE_FULLY_QUALIFIED_NAME)); - - sortable.sort(new Object[] { FULLY_QUALIFIED_NAME }, - new boolean[] { true }); - - int packages = 21 + 3; - int expectedSize = sampleData.length + packages; - validateHierarchicalContainer(container, "com", - "org.vaadin.test.LastClass", - "com.vaadin.server.ApplicationResource", "blah", true, - expectedSize, 2, true); - - sortable.sort(new Object[] { REVERSE_FULLY_QUALIFIED_NAME }, - new boolean[] { true }); - - validateHierarchicalContainer(container, - "com.vaadin.server.ApplicationPortlet2", - "com.vaadin.data.util.ObjectProperty", - "com.vaadin.server.ApplicationResource", "blah", true, - expectedSize, 2, true); - - } - - protected void initializeContainer(Container.Hierarchical container) { - container.removeAllItems(); - Object[] propertyIds = container.getContainerPropertyIds().toArray(); - for (Object propertyId : propertyIds) { - container.removeContainerProperty(propertyId); - } - - container.addContainerProperty(FULLY_QUALIFIED_NAME, String.class, ""); - container.addContainerProperty(SIMPLE_NAME, String.class, ""); - container.addContainerProperty(REVERSE_FULLY_QUALIFIED_NAME, - String.class, null); - container.addContainerProperty(ID_NUMBER, Integer.class, null); - - for (int i = 0; i < sampleData.length; i++) { - String id = sampleData[i]; - - // Add path as parent - String paths[] = id.split("\\."); - String path = paths[0]; - // Adds "com" and other items multiple times so should return null - // for all but the first time - if (container.addItem(path) != null) { - assertTrue(container.setChildrenAllowed(path, false)); - Item item = container.getItem(path); - item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(path); - item.getItemProperty(SIMPLE_NAME).setValue(getSimpleName(path)); - item.getItemProperty(REVERSE_FULLY_QUALIFIED_NAME).setValue( - reverse(path)); - item.getItemProperty(ID_NUMBER).setValue(1); - } - for (int j = 1; j < paths.length; j++) { - String parent = path; - path = path + "." + paths[j]; - - // Adds "com" and other items multiple times so should return - // null for all but the first time - if (container.addItem(path) != null) { - assertTrue(container.setChildrenAllowed(path, false)); - - Item item = container.getItem(path); - item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(path); - item.getItemProperty(SIMPLE_NAME).setValue( - getSimpleName(path)); - item.getItemProperty(REVERSE_FULLY_QUALIFIED_NAME) - .setValue(reverse(path)); - item.getItemProperty(ID_NUMBER).setValue(1); - - } - assertTrue(container.setChildrenAllowed(parent, true)); - assertTrue( - "Failed to set " + parent + " as parent for " + path, - container.setParent(path, parent)); - } - - Item item = container.getItem(id); - assertNotNull(item); - String parent = id.substring(0, id.lastIndexOf('.')); - assertTrue(container.setParent(id, parent)); - item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(sampleData[i]); - item.getItemProperty(SIMPLE_NAME).setValue( - getSimpleName(sampleData[i])); - item.getItemProperty(REVERSE_FULLY_QUALIFIED_NAME).setValue( - reverse(sampleData[i])); - item.getItemProperty(ID_NUMBER).setValue(i % 2); - } - } - -} diff --git a/tests/server-side/com/vaadin/data/util/AbstractInMemoryContainerTest.java b/tests/server-side/com/vaadin/data/util/AbstractInMemoryContainerTest.java deleted file mode 100644 index bbad33c5bc..0000000000 --- a/tests/server-side/com/vaadin/data/util/AbstractInMemoryContainerTest.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.vaadin.data.util; - -public abstract class AbstractInMemoryContainerTest extends - AbstractContainerTest { - -} diff --git a/tests/server-side/com/vaadin/data/util/BeanContainerTest.java b/tests/server-side/com/vaadin/data/util/BeanContainerTest.java deleted file mode 100644 index 9037e303a8..0000000000 --- a/tests/server-side/com/vaadin/data/util/BeanContainerTest.java +++ /dev/null @@ -1,460 +0,0 @@ -package com.vaadin.data.util; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - -import junit.framework.Assert; - -import com.vaadin.data.Container; -import com.vaadin.data.Item; -import com.vaadin.data.util.AbstractBeanContainer.BeanIdResolver; - -public class BeanContainerTest extends AbstractBeanContainerTest { - - protected static class PersonNameResolver implements - BeanIdResolver<String, Person> { - - @Override - public String getIdForBean(Person bean) { - return bean != null ? bean.getName() : null; - } - - } - - protected static class NullResolver implements - BeanIdResolver<String, Person> { - - @Override - public String getIdForBean(Person bean) { - return null; - } - - } - - private Map<String, ClassName> nameToBean = new LinkedHashMap<String, ClassName>(); - - private BeanContainer<String, ClassName> getContainer() { - return new BeanContainer<String, ClassName>(ClassName.class); - } - - @Override - public void setUp() { - nameToBean.clear(); - - for (int i = 0; i < sampleData.length; i++) { - ClassName className = new ClassName(sampleData[i], i); - nameToBean.put(sampleData[i], className); - } - } - - @Override - @SuppressWarnings("unchecked") - protected void initializeContainer(Container container) { - BeanContainer<String, ClassName> beanItemContainer = (BeanContainer<String, ClassName>) container; - - beanItemContainer.removeAllItems(); - - for (Entry<String, ClassName> entry : nameToBean.entrySet()) { - beanItemContainer.addItem(entry.getKey(), entry.getValue()); - } - } - - @Override - protected boolean isFilteredOutItemNull() { - return false; - } - - public void testBasicOperations() { - testBasicContainerOperations(getContainer()); - } - - public void testFiltering() { - testContainerFiltering(getContainer()); - } - - public void testSorting() { - testContainerSorting(getContainer()); - } - - public void testSortingAndFiltering() { - testContainerSortingAndFiltering(getContainer()); - } - - // duplicated from parent class and modified - adding items to - // BeanContainer differs from other containers - public void testContainerOrdered() { - BeanContainer<String, String> container = new BeanContainer<String, String>( - String.class); - - String id = "test1"; - - Item item = container.addItem(id, "value"); - assertNotNull(item); - - assertEquals(id, container.firstItemId()); - assertEquals(id, container.lastItemId()); - - // isFirstId - assertTrue(container.isFirstId(id)); - assertTrue(container.isFirstId(container.firstItemId())); - // isLastId - assertTrue(container.isLastId(id)); - assertTrue(container.isLastId(container.lastItemId())); - - // Add a new item before the first - // addItemAfter - String newFirstId = "newFirst"; - item = container.addItemAfter(null, newFirstId, "newFirstValue"); - assertNotNull(item); - assertNotNull(container.getItem(newFirstId)); - - // isFirstId - assertTrue(container.isFirstId(newFirstId)); - assertTrue(container.isFirstId(container.firstItemId())); - // isLastId - assertTrue(container.isLastId(id)); - assertTrue(container.isLastId(container.lastItemId())); - - // nextItemId - assertEquals(id, container.nextItemId(newFirstId)); - assertNull(container.nextItemId(id)); - assertNull(container.nextItemId("not-in-container")); - - // prevItemId - assertEquals(newFirstId, container.prevItemId(id)); - assertNull(container.prevItemId(newFirstId)); - assertNull(container.prevItemId("not-in-container")); - - // addItemAfter(IDTYPE, IDTYPE, BT) - String newSecondItemId = "newSecond"; - item = container.addItemAfter(newFirstId, newSecondItemId, - "newSecondValue"); - // order is now: newFirstId, newSecondItemId, id - assertNotNull(item); - assertNotNull(container.getItem(newSecondItemId)); - assertEquals(id, container.nextItemId(newSecondItemId)); - assertEquals(newFirstId, container.prevItemId(newSecondItemId)); - - // addItemAfter(IDTYPE, IDTYPE, BT) - String fourthId = "id of the fourth item"; - Item fourth = container.addItemAfter(newFirstId, fourthId, - "fourthValue"); - // order is now: newFirstId, fourthId, newSecondItemId, id - assertNotNull(fourth); - assertEquals(fourth, container.getItem(fourthId)); - assertEquals(newSecondItemId, container.nextItemId(fourthId)); - assertEquals(newFirstId, container.prevItemId(fourthId)); - - // addItemAfter(IDTYPE, IDTYPE, BT) - String fifthId = "fifth"; - Item fifth = container.addItemAfter(null, fifthId, "fifthValue"); - // order is now: fifthId, newFirstId, fourthId, newSecondItemId, id - assertNotNull(fifth); - assertEquals(fifth, container.getItem(fifthId)); - assertEquals(newFirstId, container.nextItemId(fifthId)); - assertNull(container.prevItemId(fifthId)); - - } - - // TODO test Container.Indexed interface operation - testContainerIndexed()? - - public void testAddItemAt() { - BeanContainer<String, String> container = new BeanContainer<String, String>( - String.class); - - container.addItem("id1", "value1"); - // id1 - container.addItemAt(0, "id2", "value2"); - // id2, id1 - container.addItemAt(1, "id3", "value3"); - // id2, id3, id1 - container.addItemAt(container.size(), "id4", "value4"); - // id2, id3, id1, id4 - - assertNull(container.addItemAt(-1, "id5", "value5")); - assertNull(container.addItemAt(container.size() + 1, "id6", "value6")); - - assertEquals(4, container.size()); - assertEquals("id2", container.getIdByIndex(0)); - assertEquals("id3", container.getIdByIndex(1)); - assertEquals("id1", container.getIdByIndex(2)); - assertEquals("id4", container.getIdByIndex(3)); - } - - public void testUnsupportedMethods() { - BeanContainer<String, Person> container = new BeanContainer<String, Person>( - Person.class); - container.addItem("John", new Person("John")); - - try { - container.addItem(); - Assert.fail(); - } catch (UnsupportedOperationException e) { - // should get exception - } - - try { - container.addItem(null); - Assert.fail(); - } catch (UnsupportedOperationException e) { - // should get exception - } - - try { - container.addItemAfter(null, null); - Assert.fail(); - } catch (UnsupportedOperationException e) { - // should get exception - } - - try { - container.addItemAfter(new Person("Jane")); - Assert.fail(); - } catch (UnsupportedOperationException e) { - // should get exception - } - - try { - container.addItemAt(0); - Assert.fail(); - } catch (UnsupportedOperationException e) { - // should get exception - } - - try { - container.addItemAt(0, new Person("Jane")); - Assert.fail(); - } catch (UnsupportedOperationException e) { - // should get exception - } - - try { - container.addContainerProperty("lastName", String.class, ""); - Assert.fail(); - } catch (UnsupportedOperationException e) { - // should get exception - } - - assertEquals(1, container.size()); - } - - public void testRemoveContainerProperty() { - BeanContainer<String, Person> container = new BeanContainer<String, Person>( - Person.class); - container.setBeanIdResolver(new PersonNameResolver()); - container.addBean(new Person("John")); - - Assert.assertEquals("John", - container.getContainerProperty("John", "name").getValue()); - Assert.assertTrue(container.removeContainerProperty("name")); - Assert.assertNull(container.getContainerProperty("John", "name")); - - Assert.assertNotNull(container.getItem("John")); - // property removed also from item - Assert.assertNull(container.getItem("John").getItemProperty("name")); - } - - public void testAddNullBeans() { - BeanContainer<String, Person> container = new BeanContainer<String, Person>( - Person.class); - - assertNull(container.addItem("id1", null)); - assertNull(container.addItemAfter(null, "id2", null)); - assertNull(container.addItemAt(0, "id3", null)); - - assertEquals(0, container.size()); - } - - public void testAddNullId() { - BeanContainer<String, Person> container = new BeanContainer<String, Person>( - Person.class); - - Person john = new Person("John"); - - assertNull(container.addItem(null, john)); - assertNull(container.addItemAfter(null, null, john)); - assertNull(container.addItemAt(0, null, john)); - - assertEquals(0, container.size()); - } - - public void testEmptyContainer() { - BeanContainer<String, Person> container = new BeanContainer<String, Person>( - Person.class); - - assertNull(container.firstItemId()); - assertNull(container.lastItemId()); - - assertEquals(0, container.size()); - - // could test more about empty container - } - - public void testAddBeanWithoutResolver() { - BeanContainer<String, Person> container = new BeanContainer<String, Person>( - Person.class); - - try { - container.addBean(new Person("John")); - Assert.fail(); - } catch (IllegalStateException e) { - // should get exception - } - try { - container.addBeanAfter(null, new Person("Jane")); - Assert.fail(); - } catch (IllegalStateException e) { - // should get exception - } - try { - container.addBeanAt(0, new Person("Jack")); - Assert.fail(); - } catch (IllegalStateException e) { - // should get exception - } - try { - container - .addAll(Arrays.asList(new Person[] { new Person("Jack") })); - Assert.fail(); - } catch (IllegalStateException e) { - // should get exception - } - - assertEquals(0, container.size()); - } - - public void testAddAllWithNullItemId() { - BeanContainer<String, Person> container = new BeanContainer<String, Person>( - Person.class); - // resolver that returns null as item id - container - .setBeanIdResolver(new BeanIdResolver<String, AbstractBeanContainerTest.Person>() { - - @Override - public String getIdForBean(Person bean) { - return bean.getName(); - } - }); - - List<Person> persons = new ArrayList<Person>(); - persons.add(new Person("John")); - persons.add(new Person("Marc")); - persons.add(new Person(null)); - persons.add(new Person("foo")); - - try { - container.addAll(persons); - fail(); - } catch (IllegalArgumentException e) { - // should get exception - } - - container.removeAllItems(); - persons.remove(2); - container.addAll(persons); - assertEquals(3, container.size()); - } - - public void testAddBeanWithNullResolver() { - BeanContainer<String, Person> container = new BeanContainer<String, Person>( - Person.class); - // resolver that returns null as item id - container.setBeanIdResolver(new NullResolver()); - - try { - container.addBean(new Person("John")); - Assert.fail(); - } catch (IllegalArgumentException e) { - // should get exception - } - try { - container.addBeanAfter(null, new Person("Jane")); - Assert.fail(); - } catch (IllegalArgumentException e) { - // should get exception - } - try { - container.addBeanAt(0, new Person("Jack")); - Assert.fail(); - } catch (IllegalArgumentException e) { - // should get exception - } - - assertEquals(0, container.size()); - } - - public void testAddBeanWithResolver() { - BeanContainer<String, Person> container = new BeanContainer<String, Person>( - Person.class); - container.setBeanIdResolver(new PersonNameResolver()); - - assertNotNull(container.addBean(new Person("John"))); - assertNotNull(container.addBeanAfter(null, new Person("Jane"))); - assertNotNull(container.addBeanAt(0, new Person("Jack"))); - - container.addAll(Arrays.asList(new Person[] { new Person("Jill"), - new Person("Joe") })); - - assertTrue(container.containsId("John")); - assertTrue(container.containsId("Jane")); - assertTrue(container.containsId("Jack")); - assertTrue(container.containsId("Jill")); - assertTrue(container.containsId("Joe")); - assertEquals(3, container.indexOfId("Jill")); - assertEquals(4, container.indexOfId("Joe")); - assertEquals(5, container.size()); - } - - public void testAddNullBeansWithResolver() { - BeanContainer<String, Person> container = new BeanContainer<String, Person>( - Person.class); - container.setBeanIdResolver(new PersonNameResolver()); - - assertNull(container.addBean(null)); - assertNull(container.addBeanAfter(null, null)); - assertNull(container.addBeanAt(0, null)); - - assertEquals(0, container.size()); - } - - public void testAddBeanWithPropertyResolver() { - BeanContainer<String, Person> container = new BeanContainer<String, Person>( - Person.class); - container.setBeanIdProperty("name"); - - assertNotNull(container.addBean(new Person("John"))); - assertNotNull(container.addBeanAfter(null, new Person("Jane"))); - assertNotNull(container.addBeanAt(0, new Person("Jack"))); - - container.addAll(Arrays.asList(new Person[] { new Person("Jill"), - new Person("Joe") })); - - assertTrue(container.containsId("John")); - assertTrue(container.containsId("Jane")); - assertTrue(container.containsId("Jack")); - assertTrue(container.containsId("Jill")); - assertTrue(container.containsId("Joe")); - assertEquals(3, container.indexOfId("Jill")); - assertEquals(4, container.indexOfId("Joe")); - assertEquals(5, container.size()); - } - - public void testAddNestedContainerProperty() { - BeanContainer<String, NestedMethodPropertyTest.Person> container = new BeanContainer<String, NestedMethodPropertyTest.Person>( - NestedMethodPropertyTest.Person.class); - container.setBeanIdProperty("name"); - - container.addBean(new NestedMethodPropertyTest.Person("John", - new NestedMethodPropertyTest.Address("Ruukinkatu 2-4", 20540))); - - assertTrue(container.addNestedContainerProperty("address.street")); - assertEquals("Ruukinkatu 2-4", - container.getContainerProperty("John", "address.street") - .getValue()); - } - -} diff --git a/tests/server-side/com/vaadin/data/util/BeanItemContainerSortTest.java b/tests/server-side/com/vaadin/data/util/BeanItemContainerSortTest.java deleted file mode 100644 index 38f317fef1..0000000000 --- a/tests/server-side/com/vaadin/data/util/BeanItemContainerSortTest.java +++ /dev/null @@ -1,170 +0,0 @@ -package com.vaadin.data.util; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import junit.framework.Assert; - -import org.junit.Test; - -import com.vaadin.data.util.BeanItemContainer; -import com.vaadin.data.util.DefaultItemSorter; - -public class BeanItemContainerSortTest { - public class Person { - private String name; - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - - private int age; - - public void setName(String name) { - this.name = name; - } - - public String getName() { - return name; - } - } - - public class Parent extends Person { - private Set<Person> children = new HashSet<Person>(); - - public void setChildren(Set<Person> children) { - this.children = children; - } - - public Set<Person> getChildren() { - return children; - } - } - - String[] names = new String[] { "Antti", "Ville", "Sirkka", "Jaakko", - "Pekka", "John" }; - int[] ages = new int[] { 10, 20, 50, 12, 64, 67 }; - String[] sortedByAge = new String[] { names[0], names[3], names[1], - names[2], names[4], names[5] }; - - public BeanItemContainer<Person> getContainer() { - BeanItemContainer<Person> bc = new BeanItemContainer<Person>( - Person.class); - for (int i = 0; i < names.length; i++) { - Person p = new Person(); - p.setName(names[i]); - p.setAge(ages[i]); - bc.addBean(p); - } - return bc; - - } - - public BeanItemContainer<Parent> getParentContainer() { - BeanItemContainer<Parent> bc = new BeanItemContainer<Parent>( - Parent.class); - for (int i = 0; i < names.length; i++) { - Parent p = new Parent(); - p.setName(names[i]); - p.setAge(ages[i]); - bc.addBean(p); - } - return bc; - } - - @Test - public void testSort() { - testSort(true); - } - - public void testSort(boolean b) { - BeanItemContainer<Person> container = getContainer(); - container.sort(new Object[] { "name" }, new boolean[] { b }); - - List<String> asList = Arrays.asList(names); - Collections.sort(asList); - if (!b) { - Collections.reverse(asList); - } - - int i = 0; - for (String string : asList) { - Person idByIndex = container.getIdByIndex(i++); - Assert.assertTrue(container.containsId(idByIndex)); - Assert.assertEquals(string, idByIndex.getName()); - } - } - - @Test - public void testReverseSort() { - testSort(false); - } - - @Test - public void primitiveSorting() { - BeanItemContainer<Person> container = getContainer(); - container.sort(new Object[] { "age" }, new boolean[] { true }); - - int i = 0; - for (String string : sortedByAge) { - Person idByIndex = container.getIdByIndex(i++); - Assert.assertTrue(container.containsId(idByIndex)); - Assert.assertEquals(string, idByIndex.getName()); - } - } - - @Test - public void customSorting() { - BeanItemContainer<Person> container = getContainer(); - - // custom sorter using the reverse order - container.setItemSorter(new DefaultItemSorter() { - @Override - public int compare(Object o1, Object o2) { - return -super.compare(o1, o2); - } - }); - - container.sort(new Object[] { "age" }, new boolean[] { true }); - - int i = container.size() - 1; - for (String string : sortedByAge) { - Person idByIndex = container.getIdByIndex(i--); - Assert.assertTrue(container.containsId(idByIndex)); - Assert.assertEquals(string, idByIndex.getName()); - } - } - - @Test - public void testGetSortableProperties() { - BeanItemContainer<Person> container = getContainer(); - - Collection<?> sortablePropertyIds = container - .getSortableContainerPropertyIds(); - Assert.assertEquals(2, sortablePropertyIds.size()); - Assert.assertTrue(sortablePropertyIds.contains("name")); - Assert.assertTrue(sortablePropertyIds.contains("age")); - } - - @Test - public void testGetNonSortableProperties() { - BeanItemContainer<Parent> container = getParentContainer(); - - Assert.assertEquals(3, container.getContainerPropertyIds().size()); - - Collection<?> sortablePropertyIds = container - .getSortableContainerPropertyIds(); - Assert.assertEquals(2, sortablePropertyIds.size()); - Assert.assertTrue(sortablePropertyIds.contains("name")); - Assert.assertTrue(sortablePropertyIds.contains("age")); - } - -} diff --git a/tests/server-side/com/vaadin/data/util/BeanItemContainerTest.java b/tests/server-side/com/vaadin/data/util/BeanItemContainerTest.java deleted file mode 100644 index 29043b1670..0000000000 --- a/tests/server-side/com/vaadin/data/util/BeanItemContainerTest.java +++ /dev/null @@ -1,718 +0,0 @@ -package com.vaadin.data.util; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import junit.framework.Assert; - -import com.vaadin.data.Container; -import com.vaadin.data.Item; -import com.vaadin.data.util.BeanItemContainer; - -/** - * Test basic functionality of BeanItemContainer. - * - * Most sorting related tests are in {@link BeanItemContainerSortTest}. - */ -public class BeanItemContainerTest extends AbstractBeanContainerTest { - - // basics from the common container test - - private Map<String, ClassName> nameToBean = new LinkedHashMap<String, ClassName>(); - - private BeanItemContainer<ClassName> getContainer() { - return new BeanItemContainer<ClassName>(ClassName.class); - } - - @Override - public void setUp() { - nameToBean.clear(); - - for (int i = 0; i < sampleData.length; i++) { - ClassName className = new ClassName(sampleData[i], i); - nameToBean.put(sampleData[i], className); - } - } - - @Override - @SuppressWarnings("unchecked") - protected void initializeContainer(Container container) { - BeanItemContainer<ClassName> beanItemContainer = (BeanItemContainer<ClassName>) container; - - beanItemContainer.removeAllItems(); - - Iterator<ClassName> it = nameToBean.values().iterator(); - while (it.hasNext()) { - beanItemContainer.addBean(it.next()); - } - } - - @Override - protected void validateContainer(Container container, - Object expectedFirstItemId, Object expectedLastItemId, - Object itemIdInSet, Object itemIdNotInSet, - boolean checkGetItemNull, int expectedSize) { - Object notInSet = nameToBean.get(itemIdNotInSet); - if (notInSet == null && itemIdNotInSet != null) { - notInSet = new ClassName(String.valueOf(itemIdNotInSet), 9999); - } - super.validateContainer(container, nameToBean.get(expectedFirstItemId), - nameToBean.get(expectedLastItemId), - nameToBean.get(itemIdInSet), notInSet, checkGetItemNull, - expectedSize); - } - - @Override - protected boolean isFilteredOutItemNull() { - return false; - } - - public void testBasicOperations() { - testBasicContainerOperations(getContainer()); - } - - public void testFiltering() { - testContainerFiltering(getContainer()); - } - - public void testSorting() { - testContainerSorting(getContainer()); - } - - public void testSortingAndFiltering() { - testContainerSortingAndFiltering(getContainer()); - } - - // duplicated from parent class and modified - adding items to - // BeanItemContainer differs from other containers - public void testContainerOrdered() { - BeanItemContainer<String> container = new BeanItemContainer<String>( - String.class); - - String id = "test1"; - - Item item = container.addBean(id); - assertNotNull(item); - - assertEquals(id, container.firstItemId()); - assertEquals(id, container.lastItemId()); - - // isFirstId - assertTrue(container.isFirstId(id)); - assertTrue(container.isFirstId(container.firstItemId())); - // isLastId - assertTrue(container.isLastId(id)); - assertTrue(container.isLastId(container.lastItemId())); - - // Add a new item before the first - // addItemAfter - String newFirstId = "newFirst"; - item = container.addItemAfter(null, newFirstId); - assertNotNull(item); - assertNotNull(container.getItem(newFirstId)); - - // isFirstId - assertTrue(container.isFirstId(newFirstId)); - assertTrue(container.isFirstId(container.firstItemId())); - // isLastId - assertTrue(container.isLastId(id)); - assertTrue(container.isLastId(container.lastItemId())); - - // nextItemId - assertEquals(id, container.nextItemId(newFirstId)); - assertNull(container.nextItemId(id)); - assertNull(container.nextItemId("not-in-container")); - - // prevItemId - assertEquals(newFirstId, container.prevItemId(id)); - assertNull(container.prevItemId(newFirstId)); - assertNull(container.prevItemId("not-in-container")); - - // addItemAfter(Object) - String newSecondItemId = "newSecond"; - item = container.addItemAfter(newFirstId, newSecondItemId); - // order is now: newFirstId, newSecondItemId, id - assertNotNull(item); - assertNotNull(container.getItem(newSecondItemId)); - assertEquals(id, container.nextItemId(newSecondItemId)); - assertEquals(newFirstId, container.prevItemId(newSecondItemId)); - - // addItemAfter(Object,Object) - String fourthId = "id of the fourth item"; - Item fourth = container.addItemAfter(newFirstId, fourthId); - // order is now: newFirstId, fourthId, newSecondItemId, id - assertNotNull(fourth); - assertEquals(fourth, container.getItem(fourthId)); - assertEquals(newSecondItemId, container.nextItemId(fourthId)); - assertEquals(newFirstId, container.prevItemId(fourthId)); - - // addItemAfter(Object,Object) - Object fifthId = "fifth"; - Item fifth = container.addItemAfter(null, fifthId); - // order is now: fifthId, newFirstId, fourthId, newSecondItemId, id - assertNotNull(fifth); - assertEquals(fifth, container.getItem(fifthId)); - assertEquals(newFirstId, container.nextItemId(fifthId)); - assertNull(container.prevItemId(fifthId)); - - } - - public void testContainerIndexed() { - testContainerIndexed(getContainer(), nameToBean.get(sampleData[2]), 2, - false, new ClassName("org.vaadin.test.Test", 8888), true); - } - - @SuppressWarnings("deprecation") - public void testCollectionConstructors() { - List<ClassName> classNames = new ArrayList<ClassName>(); - classNames.add(new ClassName("a.b.c.Def", 1)); - classNames.add(new ClassName("a.b.c.Fed", 2)); - classNames.add(new ClassName("b.c.d.Def", 3)); - - // note that this constructor is problematic, users should use the - // version that - // takes the bean class as a parameter - BeanItemContainer<ClassName> container = new BeanItemContainer<ClassName>( - classNames); - - Assert.assertEquals(3, container.size()); - Assert.assertEquals(classNames.get(0), container.firstItemId()); - Assert.assertEquals(classNames.get(1), container.getIdByIndex(1)); - Assert.assertEquals(classNames.get(2), container.lastItemId()); - - BeanItemContainer<ClassName> container2 = new BeanItemContainer<ClassName>( - ClassName.class, classNames); - - Assert.assertEquals(3, container2.size()); - Assert.assertEquals(classNames.get(0), container2.firstItemId()); - Assert.assertEquals(classNames.get(1), container2.getIdByIndex(1)); - Assert.assertEquals(classNames.get(2), container2.lastItemId()); - } - - // this only applies to the collection constructor with no type parameter - @SuppressWarnings("deprecation") - public void testEmptyCollectionConstructor() { - try { - new BeanItemContainer<ClassName>((Collection<ClassName>) null); - Assert.fail("Initializing BeanItemContainer from a null collection should not work!"); - } catch (IllegalArgumentException e) { - // success - } - try { - new BeanItemContainer<ClassName>(new ArrayList<ClassName>()); - Assert.fail("Initializing BeanItemContainer from an empty collection should not work!"); - } catch (IllegalArgumentException e) { - // success - } - } - - public void testItemSetChangeListeners() { - BeanItemContainer<ClassName> container = getContainer(); - ItemSetChangeCounter counter = new ItemSetChangeCounter(); - container.addListener(counter); - - ClassName cn1 = new ClassName("com.example.Test", 1111); - ClassName cn2 = new ClassName("com.example.Test2", 2222); - - initializeContainer(container); - counter.reset(); - container.addBean(cn1); - counter.assertOnce(); - - initializeContainer(container); - counter.reset(); - container.addItem(cn1); - counter.assertOnce(); - // no notification if already in container - container.addItem(cn1); - counter.assertNone(); - container.addItem(cn2); - counter.assertOnce(); - - initializeContainer(container); - counter.reset(); - container.addItemAfter(null, cn1); - counter.assertOnce(); - Assert.assertEquals( - "com.example.Test", - container.getContainerProperty(container.firstItemId(), - FULLY_QUALIFIED_NAME).getValue()); - - initializeContainer(container); - counter.reset(); - container.addItemAfter(container.firstItemId(), cn1); - counter.assertOnce(); - Assert.assertEquals( - "com.example.Test", - container.getContainerProperty(container.getIdByIndex(1), - FULLY_QUALIFIED_NAME).getValue()); - - initializeContainer(container); - counter.reset(); - container.addItemAfter(container.lastItemId(), cn1); - counter.assertOnce(); - Assert.assertEquals( - "com.example.Test", - container.getContainerProperty(container.lastItemId(), - FULLY_QUALIFIED_NAME).getValue()); - - initializeContainer(container); - counter.reset(); - container.addItemAt(0, cn1); - counter.assertOnce(); - Assert.assertEquals( - "com.example.Test", - container.getContainerProperty(container.firstItemId(), - FULLY_QUALIFIED_NAME).getValue()); - - initializeContainer(container); - counter.reset(); - container.addItemAt(1, cn1); - counter.assertOnce(); - Assert.assertEquals( - "com.example.Test", - container.getContainerProperty(container.getIdByIndex(1), - FULLY_QUALIFIED_NAME).getValue()); - - initializeContainer(container); - counter.reset(); - container.addItemAt(container.size(), cn1); - counter.assertOnce(); - Assert.assertEquals( - "com.example.Test", - container.getContainerProperty(container.lastItemId(), - FULLY_QUALIFIED_NAME).getValue()); - - initializeContainer(container); - counter.reset(); - container.removeItem(nameToBean.get(sampleData[0])); - counter.assertOnce(); - - initializeContainer(container); - counter.reset(); - // no notification for removing a non-existing item - container.removeItem(cn1); - counter.assertNone(); - - initializeContainer(container); - counter.reset(); - container.removeAllItems(); - counter.assertOnce(); - // already empty - container.removeAllItems(); - counter.assertNone(); - - } - - public void testItemSetChangeListenersFiltering() { - BeanItemContainer<ClassName> container = getContainer(); - ItemSetChangeCounter counter = new ItemSetChangeCounter(); - container.addListener(counter); - - ClassName cn1 = new ClassName("com.example.Test", 1111); - ClassName cn2 = new ClassName("com.example.Test2", 2222); - ClassName other = new ClassName("com.example.Other", 3333); - - // simply adding or removing container filters should cause event - // (content changes) - - initializeContainer(container); - counter.reset(); - container.addContainerFilter(SIMPLE_NAME, "a", true, false); - counter.assertOnce(); - container.removeContainerFilters(SIMPLE_NAME); - counter.assertOnce(); - - initializeContainer(container); - counter.reset(); - container.addContainerFilter(SIMPLE_NAME, "a", true, false); - counter.assertOnce(); - container.removeAllContainerFilters(); - counter.assertOnce(); - - // perform operations while filtering container - - initializeContainer(container); - counter.reset(); - container.addContainerFilter(FULLY_QUALIFIED_NAME, "Test", true, false); - counter.assertOnce(); - - // passes filter - container.addBean(cn1); - counter.assertOnce(); - - // passes filter but already in the container - container.addBean(cn1); - counter.assertNone(); - - initializeContainer(container); - counter.reset(); - - // passes filter - container.addItem(cn1); - counter.assertOnce(); - // already in the container - container.addItem(cn1); - counter.assertNone(); - container.addItem(cn2); - counter.assertOnce(); - // does not pass filter - container.addItem(other); - counter.assertNone(); - - initializeContainer(container); - counter.reset(); - container.addItemAfter(null, cn1); - counter.assertOnce(); - Assert.assertEquals( - "com.example.Test", - container.getContainerProperty(container.firstItemId(), - FULLY_QUALIFIED_NAME).getValue()); - - initializeContainer(container); - counter.reset(); - container.addItemAfter(container.firstItemId(), cn1); - counter.assertOnce(); - Assert.assertEquals( - "com.example.Test", - container.getContainerProperty(container.getIdByIndex(1), - FULLY_QUALIFIED_NAME).getValue()); - - initializeContainer(container); - counter.reset(); - container.addItemAfter(container.lastItemId(), cn1); - counter.assertOnce(); - Assert.assertEquals( - "com.example.Test", - container.getContainerProperty(container.lastItemId(), - FULLY_QUALIFIED_NAME).getValue()); - - initializeContainer(container); - counter.reset(); - container.addItemAt(0, cn1); - counter.assertOnce(); - Assert.assertEquals( - "com.example.Test", - container.getContainerProperty(container.firstItemId(), - FULLY_QUALIFIED_NAME).getValue()); - - initializeContainer(container); - counter.reset(); - container.addItemAt(1, cn1); - counter.assertOnce(); - Assert.assertEquals( - "com.example.Test", - container.getContainerProperty(container.getIdByIndex(1), - FULLY_QUALIFIED_NAME).getValue()); - - initializeContainer(container); - counter.reset(); - container.addItemAt(container.size(), cn1); - counter.assertOnce(); - Assert.assertEquals( - "com.example.Test", - container.getContainerProperty(container.lastItemId(), - FULLY_QUALIFIED_NAME).getValue()); - - // does not pass filter - // note: testAddRemoveWhileFiltering() checks position for these after - // removing filter etc, here concentrating on listeners - - initializeContainer(container); - counter.reset(); - container.addItemAfter(null, other); - counter.assertNone(); - - initializeContainer(container); - counter.reset(); - container.addItemAfter(container.firstItemId(), other); - counter.assertNone(); - - initializeContainer(container); - counter.reset(); - container.addItemAfter(container.lastItemId(), other); - counter.assertNone(); - - initializeContainer(container); - counter.reset(); - container.addItemAt(0, other); - counter.assertNone(); - - initializeContainer(container); - counter.reset(); - container.addItemAt(1, other); - counter.assertNone(); - - initializeContainer(container); - counter.reset(); - container.addItemAt(container.size(), other); - counter.assertNone(); - - // passes filter - - initializeContainer(container); - counter.reset(); - container.addItem(cn1); - counter.assertOnce(); - container.removeItem(cn1); - counter.assertOnce(); - - // does not pass filter - - initializeContainer(container); - counter.reset(); - // not visible - container.removeItem(nameToBean.get(sampleData[0])); - counter.assertNone(); - - container.removeAllItems(); - counter.assertOnce(); - // no visible items - container.removeAllItems(); - counter.assertNone(); - } - - public void testAddRemoveWhileFiltering() { - BeanItemContainer<Person> container = new BeanItemContainer<Person>( - Person.class); - - Person john = new Person("John"); - Person jane = new Person("Jane"); - Person matthew = new Person("Matthew"); - - Person jack = new Person("Jack"); - Person michael = new Person("Michael"); - Person william = new Person("William"); - Person julia = new Person("Julia"); - Person george = new Person("George"); - Person mark = new Person("Mark"); - - container.addBean(john); - container.addBean(jane); - container.addBean(matthew); - - assertEquals(3, container.size()); - // john, jane, matthew - - container.addContainerFilter("name", "j", true, true); - - assertEquals(2, container.size()); - // john, jane, (matthew) - - // add a bean that passes the filter - container.addBean(jack); - assertEquals(3, container.size()); - assertEquals(jack, container.lastItemId()); - // john, jane, (matthew), jack - - // add beans that do not pass the filter - container.addBean(michael); - // john, jane, (matthew), jack, (michael) - container.addItemAfter(null, william); - // (william), john, jane, (matthew), jack, (michael) - - // add after an item that is shown - container.addItemAfter(john, george); - // (william), john, (george), jane, (matthew), jack, (michael) - assertEquals(3, container.size()); - assertEquals(john, container.firstItemId()); - - // add after an item that is not shown does nothing - container.addItemAfter(william, julia); - // (william), john, (george), jane, (matthew), jack, (michael) - assertEquals(3, container.size()); - assertEquals(john, container.firstItemId()); - - container.addItemAt(1, julia); - // (william), john, julia, (george), jane, (matthew), jack, (michael) - - container.addItemAt(2, mark); - // (william), john, julia, (mark), (george), jane, (matthew), jack, - // (michael) - - container.removeItem(matthew); - // (william), john, julia, (mark), (george), jane, jack, (michael) - - assertEquals(4, container.size()); - assertEquals(jack, container.lastItemId()); - - container.removeContainerFilters("name"); - - assertEquals(8, container.size()); - assertEquals(william, container.firstItemId()); - assertEquals(john, container.nextItemId(william)); - assertEquals(julia, container.nextItemId(john)); - assertEquals(mark, container.nextItemId(julia)); - assertEquals(george, container.nextItemId(mark)); - assertEquals(jane, container.nextItemId(george)); - assertEquals(jack, container.nextItemId(jane)); - assertEquals(michael, container.lastItemId()); - } - - public void testRefilterOnPropertyModification() { - BeanItemContainer<Person> container = new BeanItemContainer<Person>( - Person.class); - - Person john = new Person("John"); - Person jane = new Person("Jane"); - Person matthew = new Person("Matthew"); - - container.addBean(john); - container.addBean(jane); - container.addBean(matthew); - - assertEquals(3, container.size()); - // john, jane, matthew - - container.addContainerFilter("name", "j", true, true); - - assertEquals(2, container.size()); - // john, jane, (matthew) - - // #6053 currently, modification of an item that is not visible does not - // trigger refiltering - should it? - // matthew.setName("Julia"); - // assertEquals(3, container.size()); - // john, jane, julia - - john.setName("Mark"); - assertEquals(2, container.size()); - // (mark), jane, julia - - container.removeAllContainerFilters(); - - assertEquals(3, container.size()); - } - - public void testAddAll() { - BeanItemContainer<Person> container = new BeanItemContainer<Person>( - Person.class); - - Person john = new Person("John"); - Person jane = new Person("Jane"); - Person matthew = new Person("Matthew"); - - container.addBean(john); - container.addBean(jane); - container.addBean(matthew); - - assertEquals(3, container.size()); - // john, jane, matthew - - Person jack = new Person("Jack"); - Person michael = new Person("Michael"); - - // addAll - container.addAll(Arrays.asList(jack, michael)); - // john, jane, matthew, jack, michael - - assertEquals(5, container.size()); - assertEquals(jane, container.nextItemId(john)); - assertEquals(matthew, container.nextItemId(jane)); - assertEquals(jack, container.nextItemId(matthew)); - assertEquals(michael, container.nextItemId(jack)); - } - - public void testUnsupportedMethods() { - BeanItemContainer<Person> container = new BeanItemContainer<Person>( - Person.class); - container.addBean(new Person("John")); - - try { - container.addItem(); - Assert.fail(); - } catch (UnsupportedOperationException e) { - // should get exception - } - - try { - container.addItemAfter(new Person("Jane")); - Assert.fail(); - } catch (UnsupportedOperationException e) { - // should get exception - } - - try { - container.addItemAt(0); - Assert.fail(); - } catch (UnsupportedOperationException e) { - // should get exception - } - - try { - container.addContainerProperty("lastName", String.class, ""); - Assert.fail(); - } catch (UnsupportedOperationException e) { - // should get exception - } - - assertEquals(1, container.size()); - } - - public void testRemoveContainerProperty() { - BeanItemContainer<Person> container = new BeanItemContainer<Person>( - Person.class); - Person john = new Person("John"); - container.addBean(john); - - Assert.assertEquals("John", container - .getContainerProperty(john, "name").getValue()); - Assert.assertTrue(container.removeContainerProperty("name")); - Assert.assertNull(container.getContainerProperty(john, "name")); - - Assert.assertNotNull(container.getItem(john)); - // property removed also from item - Assert.assertNull(container.getItem(john).getItemProperty("name")); - } - - public void testAddNullBean() { - BeanItemContainer<Person> container = new BeanItemContainer<Person>( - Person.class); - Person john = new Person("John"); - container.addBean(john); - - assertNull(container.addItem(null)); - assertNull(container.addItemAfter(null, null)); - assertNull(container.addItemAfter(john, null)); - assertNull(container.addItemAt(0, null)); - - assertEquals(1, container.size()); - } - - public void testBeanIdResolver() { - BeanItemContainer<Person> container = new BeanItemContainer<Person>( - Person.class); - Person john = new Person("John"); - - assertSame(john, container.getBeanIdResolver().getIdForBean(john)); - } - - public void testNullBeanClass() { - try { - new BeanItemContainer<Object>((Class<Object>) null); - } catch (IllegalArgumentException e) { - // should get exception - } - } - - public void testAddNestedContainerProperty() { - BeanItemContainer<NestedMethodPropertyTest.Person> container = new BeanItemContainer<NestedMethodPropertyTest.Person>( - NestedMethodPropertyTest.Person.class); - - NestedMethodPropertyTest.Person john = new NestedMethodPropertyTest.Person( - "John", new NestedMethodPropertyTest.Address("Ruukinkatu 2-4", - 20540)); - container.addBean(john); - - assertTrue(container.addNestedContainerProperty("address.street")); - assertEquals("Ruukinkatu 2-4", - container.getContainerProperty(john, "address.street") - .getValue()); - } - -} diff --git a/tests/server-side/com/vaadin/data/util/BeanItemTest.java b/tests/server-side/com/vaadin/data/util/BeanItemTest.java deleted file mode 100644 index e1ad75796c..0000000000 --- a/tests/server-side/com/vaadin/data/util/BeanItemTest.java +++ /dev/null @@ -1,333 +0,0 @@ -package com.vaadin.data.util; - -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.Map; - -import junit.framework.Assert; -import junit.framework.TestCase; - -/** - * Test BeanItem specific features. - * - * Only public API is tested, not the methods with package visibility. - * - * See also {@link PropertySetItemTest}, which tests the base class. - */ -public class BeanItemTest extends TestCase { - - @SuppressWarnings("unused") - protected static class MySuperClass { - private int superPrivate = 1; - private int superPrivate2 = 2; - protected double superProtected = 3.0; - private double superProtected2 = 4.0; - public boolean superPublic = true; - private boolean superPublic2 = true; - - public int getSuperPrivate() { - return superPrivate; - } - - public void setSuperPrivate(int superPrivate) { - this.superPrivate = superPrivate; - } - - public double getSuperProtected() { - return superProtected; - } - - public void setSuperProtected(double superProtected) { - this.superProtected = superProtected; - } - - public boolean isSuperPublic() { - return superPublic; - } - - public void setSuperPublic(boolean superPublic) { - this.superPublic = superPublic; - } - - } - - protected static class MyClass extends MySuperClass { - private String name; - public int value = 123; - - public MyClass(String name) { - this.name = name; - } - - public void setName(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - public void setNoField(String name) { - } - - public String getNoField() { - return "no field backing this setter"; - } - - public String getName2() { - return name; - } - } - - protected static class MyClass2 extends MyClass { - public MyClass2(String name) { - super(name); - } - - @Override - public void setName(String name) { - super.setName(name + "2"); - } - - @Override - public String getName() { - return super.getName() + "2"; - } - - @Override - public String getName2() { - return super.getName(); - } - - public void setName2(String name) { - super.setName(name); - } - } - - protected static interface MySuperInterface { - public int getSuper1(); - - public void setSuper1(int i); - - public int getOverride(); - } - - protected static interface MySuperInterface2 { - public int getSuper2(); - } - - protected static interface MySubInterface extends MySuperInterface, - MySuperInterface2 { - public int getSub(); - - public void setSub(int i); - - @Override - public int getOverride(); - - public void setOverride(int i); - } - - public void testGetProperties() { - BeanItem<MySuperClass> item = new BeanItem<MySuperClass>( - new MySuperClass()); - - Collection<?> itemPropertyIds = item.getItemPropertyIds(); - Assert.assertEquals(3, itemPropertyIds.size()); - Assert.assertTrue(itemPropertyIds.contains("superPrivate")); - Assert.assertTrue(itemPropertyIds.contains("superProtected")); - Assert.assertTrue(itemPropertyIds.contains("superPublic")); - } - - public void testGetSuperClassProperties() { - BeanItem<MyClass> item = new BeanItem<MyClass>(new MyClass("bean1")); - - Collection<?> itemPropertyIds = item.getItemPropertyIds(); - Assert.assertEquals(6, itemPropertyIds.size()); - Assert.assertTrue(itemPropertyIds.contains("superPrivate")); - Assert.assertTrue(itemPropertyIds.contains("superProtected")); - Assert.assertTrue(itemPropertyIds.contains("superPublic")); - Assert.assertTrue(itemPropertyIds.contains("name")); - Assert.assertTrue(itemPropertyIds.contains("noField")); - Assert.assertTrue(itemPropertyIds.contains("name2")); - } - - public void testOverridingProperties() { - BeanItem<MyClass2> item = new BeanItem<MyClass2>(new MyClass2("bean2")); - - Collection<?> itemPropertyIds = item.getItemPropertyIds(); - Assert.assertEquals(6, itemPropertyIds.size()); - - Assert.assertTrue(MyClass2.class.equals(item.getBean().getClass())); - - // check that name2 accessed via MyClass2, not MyClass - Assert.assertFalse(item.getItemProperty("name2").isReadOnly()); - } - - public void testGetInterfaceProperties() throws SecurityException, - NoSuchMethodException, IllegalArgumentException, - IllegalAccessException, InvocationTargetException { - Method method = BeanItem.class.getDeclaredMethod( - "getPropertyDescriptors", Class.class); - method.setAccessible(true); - LinkedHashMap<String, VaadinPropertyDescriptor<Class>> propertyDescriptors = (LinkedHashMap<String, VaadinPropertyDescriptor<Class>>) method - .invoke(null, MySuperInterface.class); - - Assert.assertEquals(2, propertyDescriptors.size()); - Assert.assertTrue(propertyDescriptors.containsKey("super1")); - Assert.assertTrue(propertyDescriptors.containsKey("override")); - - MethodProperty<?> property = (MethodProperty<?>) propertyDescriptors - .get("override").createProperty(getClass()); - Assert.assertTrue(property.isReadOnly()); - } - - public void testGetSuperInterfaceProperties() throws SecurityException, - NoSuchMethodException, IllegalArgumentException, - IllegalAccessException, InvocationTargetException { - Method method = BeanItem.class.getDeclaredMethod( - "getPropertyDescriptors", Class.class); - method.setAccessible(true); - LinkedHashMap<String, VaadinPropertyDescriptor<Class>> propertyDescriptors = (LinkedHashMap<String, VaadinPropertyDescriptor<Class>>) method - .invoke(null, MySubInterface.class); - - Assert.assertEquals(4, propertyDescriptors.size()); - Assert.assertTrue(propertyDescriptors.containsKey("sub")); - Assert.assertTrue(propertyDescriptors.containsKey("super1")); - Assert.assertTrue(propertyDescriptors.containsKey("super2")); - Assert.assertTrue(propertyDescriptors.containsKey("override")); - - MethodProperty<?> property = (MethodProperty<?>) propertyDescriptors - .get("override").createProperty(getClass()); - Assert.assertFalse(property.isReadOnly()); - } - - public void testPropertyExplicitOrder() { - Collection<String> ids = new ArrayList<String>(); - ids.add("name"); - ids.add("superPublic"); - ids.add("name2"); - ids.add("noField"); - - BeanItem<MyClass> item = new BeanItem<MyClass>(new MyClass("bean1"), - ids); - - Iterator<?> it = item.getItemPropertyIds().iterator(); - Assert.assertEquals("name", it.next()); - Assert.assertEquals("superPublic", it.next()); - Assert.assertEquals("name2", it.next()); - Assert.assertEquals("noField", it.next()); - Assert.assertFalse(it.hasNext()); - } - - public void testPropertyExplicitOrder2() { - BeanItem<MyClass> item = new BeanItem<MyClass>(new MyClass("bean1"), - new String[] { "name", "superPublic", "name2", "noField" }); - - Iterator<?> it = item.getItemPropertyIds().iterator(); - Assert.assertEquals("name", it.next()); - Assert.assertEquals("superPublic", it.next()); - Assert.assertEquals("name2", it.next()); - Assert.assertEquals("noField", it.next()); - Assert.assertFalse(it.hasNext()); - } - - public void testPropertyBadPropertyName() { - Collection<String> ids = new ArrayList<String>(); - ids.add("name3"); - ids.add("name"); - - // currently silently ignores non-existent properties - BeanItem<MyClass> item = new BeanItem<MyClass>(new MyClass("bean1"), - ids); - - Iterator<?> it = item.getItemPropertyIds().iterator(); - Assert.assertEquals("name", it.next()); - Assert.assertFalse(it.hasNext()); - } - - public void testRemoveProperty() { - BeanItem<MyClass> item = new BeanItem<MyClass>(new MyClass("bean1")); - - Collection<?> itemPropertyIds = item.getItemPropertyIds(); - Assert.assertEquals(6, itemPropertyIds.size()); - - item.removeItemProperty("name2"); - Assert.assertEquals(5, itemPropertyIds.size()); - Assert.assertFalse(itemPropertyIds.contains("name2")); - } - - public void testRemoveSuperProperty() { - BeanItem<MyClass> item = new BeanItem<MyClass>(new MyClass("bean1")); - - Collection<?> itemPropertyIds = item.getItemPropertyIds(); - Assert.assertEquals(6, itemPropertyIds.size()); - - item.removeItemProperty("superPrivate"); - Assert.assertEquals(5, itemPropertyIds.size()); - Assert.assertFalse(itemPropertyIds.contains("superPrivate")); - } - - public void testPropertyTypes() { - BeanItem<MyClass> item = new BeanItem<MyClass>(new MyClass("bean1")); - - Assert.assertTrue(Integer.class.equals(item.getItemProperty( - "superPrivate").getType())); - Assert.assertTrue(Double.class.equals(item.getItemProperty( - "superProtected").getType())); - Assert.assertTrue(Boolean.class.equals(item.getItemProperty( - "superPublic").getType())); - Assert.assertTrue(String.class.equals(item.getItemProperty("name") - .getType())); - } - - public void testPropertyReadOnly() { - BeanItem<MyClass> item = new BeanItem<MyClass>(new MyClass("bean1")); - - Assert.assertFalse(item.getItemProperty("name").isReadOnly()); - Assert.assertTrue(item.getItemProperty("name2").isReadOnly()); - } - - public void testCustomProperties() throws Exception { - LinkedHashMap<String, VaadinPropertyDescriptor<MyClass>> propertyDescriptors = new LinkedHashMap<String, VaadinPropertyDescriptor<MyClass>>(); - propertyDescriptors.put( - "myname", - new MethodPropertyDescriptor<BeanItemTest.MyClass>("myname", - MyClass.class, MyClass.class - .getDeclaredMethod("getName"), MyClass.class - .getDeclaredMethod("setName", String.class))); - MyClass instance = new MyClass("bean1"); - Constructor<BeanItem> constructor = BeanItem.class - .getDeclaredConstructor(Object.class, Map.class); - constructor.setAccessible(true); - BeanItem<MyClass> item = constructor.newInstance(instance, - propertyDescriptors); - - Assert.assertEquals(1, item.getItemPropertyIds().size()); - Assert.assertEquals("bean1", item.getItemProperty("myname").getValue()); - } - - public void testAddRemoveProperty() throws Exception { - MethodPropertyDescriptor<BeanItemTest.MyClass> pd = new MethodPropertyDescriptor<BeanItemTest.MyClass>( - "myname", MyClass.class, - MyClass.class.getDeclaredMethod("getName"), - MyClass.class.getDeclaredMethod("setName", String.class)); - - BeanItem<MyClass> item = new BeanItem<MyClass>(new MyClass("bean1")); - - Assert.assertEquals(6, item.getItemPropertyIds().size()); - Assert.assertEquals(null, item.getItemProperty("myname")); - - item.addItemProperty("myname", pd.createProperty(item.getBean())); - Assert.assertEquals(7, item.getItemPropertyIds().size()); - Assert.assertEquals("bean1", item.getItemProperty("myname").getValue()); - item.removeItemProperty("myname"); - Assert.assertEquals(6, item.getItemPropertyIds().size()); - Assert.assertEquals(null, item.getItemProperty("myname")); - } -} diff --git a/tests/server-side/com/vaadin/data/util/FileSystemContainerTest.java b/tests/server-side/com/vaadin/data/util/FileSystemContainerTest.java deleted file mode 100644 index f4bb794e5c..0000000000 --- a/tests/server-side/com/vaadin/data/util/FileSystemContainerTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.vaadin.data.util; - -import java.io.File; - -import org.junit.Assert; -import org.junit.Test; - -public class FileSystemContainerTest { - - @Test - public void nonExistingDirectory() { - FilesystemContainer fsc = new FilesystemContainer(new File( - "/non/existing")); - Assert.assertTrue(fsc.getItemIds().isEmpty()); - } -} diff --git a/tests/server-side/com/vaadin/data/util/NestedMethodPropertyTest.java b/tests/server-side/com/vaadin/data/util/NestedMethodPropertyTest.java deleted file mode 100644 index 640ede8743..0000000000 --- a/tests/server-side/com/vaadin/data/util/NestedMethodPropertyTest.java +++ /dev/null @@ -1,327 +0,0 @@ -package com.vaadin.data.util; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.io.Serializable; - -import junit.framework.Assert; -import junit.framework.TestCase; - -public class NestedMethodPropertyTest extends TestCase { - - public static class Address implements Serializable { - private String street; - private int postalCodePrimitive; - private Integer postalCodeObject; - - public Address(String street, int postalCode) { - this.street = street; - postalCodePrimitive = postalCode; - postalCodeObject = postalCode; - } - - public void setStreet(String street) { - this.street = street; - } - - public String getStreet() { - return street; - } - - public void setPostalCodePrimitive(int postalCodePrimitive) { - this.postalCodePrimitive = postalCodePrimitive; - } - - public int getPostalCodePrimitive() { - return postalCodePrimitive; - } - - public void setPostalCodeObject(Integer postalCodeObject) { - this.postalCodeObject = postalCodeObject; - } - - public Integer getPostalCodeObject() { - return postalCodeObject; - } - - // read-only boolean property - public boolean isBoolean() { - return true; - } - } - - public static class Person implements Serializable { - private String name; - private Address address; - - public Person(String name, Address address) { - this.name = name; - this.address = address; - } - - public void setName(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - public void setAddress(Address address) { - this.address = address; - } - - public Address getAddress() { - return address; - } - } - - public static class Team implements Serializable { - private String name; - private Person manager; - - public Team(String name, Person manager) { - this.name = name; - this.manager = manager; - } - - public void setName(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - public void setManager(Person manager) { - this.manager = manager; - } - - public Person getManager() { - return manager; - } - } - - private Address oldMill; - private Person joonas; - private Team vaadin; - - @Override - public void setUp() { - oldMill = new Address("Ruukinkatu 2-4", 20540); - joonas = new Person("Joonas", oldMill); - vaadin = new Team("Vaadin", joonas); - } - - @Override - public void tearDown() { - vaadin = null; - joonas = null; - oldMill = null; - } - - public void testSingleLevelNestedSimpleProperty() { - NestedMethodProperty<String> nameProperty = new NestedMethodProperty<String>( - vaadin, "name"); - - Assert.assertEquals(String.class, nameProperty.getType()); - Assert.assertEquals("Vaadin", nameProperty.getValue()); - } - - public void testSingleLevelNestedObjectProperty() { - NestedMethodProperty<Person> managerProperty = new NestedMethodProperty<Person>( - vaadin, "manager"); - - Assert.assertEquals(Person.class, managerProperty.getType()); - Assert.assertEquals(joonas, managerProperty.getValue()); - } - - public void testMultiLevelNestedProperty() { - NestedMethodProperty<String> managerNameProperty = new NestedMethodProperty<String>( - vaadin, "manager.name"); - NestedMethodProperty<Address> addressProperty = new NestedMethodProperty<Address>( - vaadin, "manager.address"); - NestedMethodProperty<String> streetProperty = new NestedMethodProperty<String>( - vaadin, "manager.address.street"); - NestedMethodProperty<Integer> postalCodePrimitiveProperty = new NestedMethodProperty<Integer>( - vaadin, "manager.address.postalCodePrimitive"); - NestedMethodProperty<Integer> postalCodeObjectProperty = new NestedMethodProperty<Integer>( - vaadin, "manager.address.postalCodeObject"); - NestedMethodProperty<Boolean> booleanProperty = new NestedMethodProperty<Boolean>( - vaadin, "manager.address.boolean"); - - Assert.assertEquals(String.class, managerNameProperty.getType()); - Assert.assertEquals("Joonas", managerNameProperty.getValue()); - - Assert.assertEquals(Address.class, addressProperty.getType()); - Assert.assertEquals(oldMill, addressProperty.getValue()); - - Assert.assertEquals(String.class, streetProperty.getType()); - Assert.assertEquals("Ruukinkatu 2-4", streetProperty.getValue()); - - Assert.assertEquals(Integer.class, - postalCodePrimitiveProperty.getType()); - Assert.assertEquals(Integer.valueOf(20540), - postalCodePrimitiveProperty.getValue()); - - Assert.assertEquals(Integer.class, postalCodeObjectProperty.getType()); - Assert.assertEquals(Integer.valueOf(20540), - postalCodeObjectProperty.getValue()); - - Assert.assertEquals(Boolean.class, booleanProperty.getType()); - Assert.assertEquals(Boolean.TRUE, booleanProperty.getValue()); - } - - public void testEmptyPropertyName() { - try { - new NestedMethodProperty<Object>(vaadin, ""); - fail(); - } catch (IllegalArgumentException e) { - // should get exception - } - - try { - new NestedMethodProperty<Object>(vaadin, " "); - fail(); - } catch (IllegalArgumentException e) { - // should get exception - } - } - - public void testInvalidPropertyName() { - try { - new NestedMethodProperty<Object>(vaadin, "."); - fail(); - } catch (IllegalArgumentException e) { - // should get exception - } - try { - new NestedMethodProperty<Object>(vaadin, ".manager"); - fail(); - } catch (IllegalArgumentException e) { - // should get exception - } - try { - new NestedMethodProperty<Object>(vaadin, "manager."); - fail(); - } catch (IllegalArgumentException e) { - // should get exception - } - try { - new NestedMethodProperty<Object>(vaadin, "manager..name"); - fail(); - } catch (IllegalArgumentException e) { - // should get exception - } - } - - public void testInvalidNestedPropertyName() { - try { - new NestedMethodProperty<Object>(vaadin, "member"); - fail(); - } catch (IllegalArgumentException e) { - // should get exception - } - - try { - new NestedMethodProperty<Object>(vaadin, "manager.pet"); - fail(); - } catch (IllegalArgumentException e) { - // should get exception - } - - try { - new NestedMethodProperty<Object>(vaadin, "manager.address.city"); - fail(); - } catch (IllegalArgumentException e) { - // should get exception - } - } - - public void testNullNestedProperty() { - NestedMethodProperty<String> managerNameProperty = new NestedMethodProperty<String>( - vaadin, "manager.name"); - NestedMethodProperty<String> streetProperty = new NestedMethodProperty<String>( - vaadin, "manager.address.street"); - - joonas.setAddress(null); - try { - streetProperty.getValue(); - fail(); - } catch (Exception e) { - // should get exception - } - - vaadin.setManager(null); - try { - managerNameProperty.getValue(); - fail(); - } catch (Exception e) { - // should get exception - } - try { - streetProperty.getValue(); - fail(); - } catch (Exception e) { - // should get exception - } - - vaadin.setManager(joonas); - Assert.assertEquals("Joonas", managerNameProperty.getValue()); - } - - public void testMultiLevelNestedPropertySetValue() { - NestedMethodProperty<String> managerNameProperty = new NestedMethodProperty<String>( - vaadin, "manager.name"); - NestedMethodProperty<Address> addressProperty = new NestedMethodProperty<Address>( - vaadin, "manager.address"); - NestedMethodProperty<String> streetProperty = new NestedMethodProperty<String>( - vaadin, "manager.address.street"); - NestedMethodProperty<Integer> postalCodePrimitiveProperty = new NestedMethodProperty<Integer>( - vaadin, "manager.address.postalCodePrimitive"); - NestedMethodProperty<Integer> postalCodeObjectProperty = new NestedMethodProperty<Integer>( - vaadin, "manager.address.postalCodeObject"); - - managerNameProperty.setValue("Joonas L"); - Assert.assertEquals("Joonas L", joonas.getName()); - streetProperty.setValue("Ruukinkatu"); - Assert.assertEquals("Ruukinkatu", oldMill.getStreet()); - postalCodePrimitiveProperty.setValue(0); - postalCodeObjectProperty.setValue(1); - Assert.assertEquals(0, oldMill.getPostalCodePrimitive()); - Assert.assertEquals(Integer.valueOf(1), oldMill.getPostalCodeObject()); - - postalCodeObjectProperty.setValue(null); - Assert.assertNull(oldMill.getPostalCodeObject()); - - Address address2 = new Address("Other street", 12345); - addressProperty.setValue(address2); - Assert.assertEquals("Other street", streetProperty.getValue()); - } - - public void testSerialization() throws IOException, ClassNotFoundException { - NestedMethodProperty<String> streetProperty = new NestedMethodProperty<String>( - vaadin, "manager.address.street"); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - new ObjectOutputStream(baos).writeObject(streetProperty); - @SuppressWarnings("unchecked") - NestedMethodProperty<String> property2 = (NestedMethodProperty<String>) new ObjectInputStream( - new ByteArrayInputStream(baos.toByteArray())).readObject(); - - Assert.assertEquals("Ruukinkatu 2-4", property2.getValue()); - } - - public void testIsReadOnly() { - NestedMethodProperty<String> streetProperty = new NestedMethodProperty<String>( - vaadin, "manager.address.street"); - NestedMethodProperty<Boolean> booleanProperty = new NestedMethodProperty<Boolean>( - vaadin, "manager.address.boolean"); - - Assert.assertFalse(streetProperty.isReadOnly()); - Assert.assertTrue(booleanProperty.isReadOnly()); - } - -} diff --git a/tests/server-side/com/vaadin/data/util/ObjectPropertyTest.java b/tests/server-side/com/vaadin/data/util/ObjectPropertyTest.java deleted file mode 100644 index 99ca58ba42..0000000000 --- a/tests/server-side/com/vaadin/data/util/ObjectPropertyTest.java +++ /dev/null @@ -1,97 +0,0 @@ -package com.vaadin.data.util; - -import junit.framework.TestCase; - -import org.junit.Assert; - -public class ObjectPropertyTest extends TestCase { - - public static class TestSuperClass { - private String name; - - public TestSuperClass(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - @Override - public String toString() { - return getName(); - } - } - - public static class TestSubClass extends TestSuperClass { - public TestSubClass(String name) { - super("Subclass: " + name); - } - } - - private TestSuperClass super1 = new TestSuperClass("super1"); - private TestSubClass sub1 = new TestSubClass("sub1"); - - public void testSimple() { - ObjectProperty<TestSuperClass> prop1 = new ObjectProperty<TestSuperClass>( - super1, TestSuperClass.class); - Assert.assertEquals("super1", prop1.getValue().getName()); - prop1 = new ObjectProperty<TestSuperClass>(super1); - Assert.assertEquals("super1", prop1.getValue().getName()); - - ObjectProperty<TestSubClass> prop2 = new ObjectProperty<TestSubClass>( - sub1, TestSubClass.class); - Assert.assertEquals("Subclass: sub1", prop2.getValue().getName()); - prop2 = new ObjectProperty<TestSubClass>(sub1); - Assert.assertEquals("Subclass: sub1", prop2.getValue().getName()); - } - - public void testSetValueObjectSuper() { - ObjectProperty<TestSuperClass> prop = new ObjectProperty<TestSuperClass>( - super1, TestSuperClass.class); - Assert.assertEquals("super1", prop.getValue().getName()); - prop.setValue(new TestSuperClass("super2")); - Assert.assertEquals("super1", super1.getName()); - Assert.assertEquals("super2", prop.getValue().getName()); - } - - public void testSetValueObjectSub() { - ObjectProperty<TestSubClass> prop = new ObjectProperty<TestSubClass>( - sub1, TestSubClass.class); - Assert.assertEquals("Subclass: sub1", prop.getValue().getName()); - prop.setValue(new TestSubClass("sub2")); - Assert.assertEquals("Subclass: sub1", sub1.getName()); - Assert.assertEquals("Subclass: sub2", prop.getValue().getName()); - } - - public void testSetValueStringSuper() { - ObjectProperty<TestSuperClass> prop = new ObjectProperty<TestSuperClass>( - super1, TestSuperClass.class); - Assert.assertEquals("super1", prop.getValue().getName()); - prop.setValue(new TestSuperClass("super2")); - Assert.assertEquals("super1", super1.getName()); - Assert.assertEquals("super2", prop.getValue().getName()); - } - - public void testSetValueStringSub() { - ObjectProperty<TestSubClass> prop = new ObjectProperty<TestSubClass>( - sub1, TestSubClass.class); - Assert.assertEquals("Subclass: sub1", prop.getValue().getName()); - prop.setValue(new TestSubClass("sub2")); - Assert.assertEquals("Subclass: sub1", sub1.getName()); - Assert.assertEquals("Subclass: sub2", prop.getValue().getName()); - } - - public void testMixedGenerics() { - ObjectProperty<TestSuperClass> prop = new ObjectProperty<TestSuperClass>( - sub1); - Assert.assertEquals("Subclass: sub1", prop.getValue().getName()); - Assert.assertEquals(prop.getType(), TestSubClass.class); - // create correct subclass based on the runtime type of the instance - // given to ObjectProperty constructor, which is a subclass of the type - // parameter - prop.setValue(new TestSubClass("sub2")); - Assert.assertEquals("Subclass: sub2", prop.getValue().getName()); - } - -} diff --git a/tests/server-side/com/vaadin/data/util/PerformanceTestIndexedContainer.java b/tests/server-side/com/vaadin/data/util/PerformanceTestIndexedContainer.java deleted file mode 100644 index c4ed12efc0..0000000000 --- a/tests/server-side/com/vaadin/data/util/PerformanceTestIndexedContainer.java +++ /dev/null @@ -1,117 +0,0 @@ -package com.vaadin.data.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.SortedSet; -import java.util.TreeSet; - -import junit.framework.Assert; -import junit.framework.TestCase; - -import com.vaadin.data.util.IndexedContainer; - -public class PerformanceTestIndexedContainer extends TestCase { - - private static final int REPEATS = 10; - private final static int ITEMS = 50000; - private static final long ADD_ITEM_FAIL_THRESHOLD = 200; - // TODO should improve performance of these methods - private static final long ADD_ITEM_AT_FAIL_THRESHOLD = 5000; - private static final long ADD_ITEM_AFTER_FAIL_THRESHOLD = 5000; - private static final long ADD_ITEM_AFTER_LAST_FAIL_THRESHOLD = 5000; - private static final long ADD_ITEMS_CONSTRUCTOR_FAIL_THRESHOLD = 200; - - public void testAddItemPerformance() { - Collection<Long> times = new ArrayList<Long>(); - for (int j = 0; j < REPEATS; ++j) { - IndexedContainer c = new IndexedContainer(); - long start = System.currentTimeMillis(); - for (int i = 0; i < ITEMS; i++) { - c.addItem(); - } - times.add(System.currentTimeMillis() - start); - } - checkMedian(ITEMS, times, "IndexedContainer.addItem()", - ADD_ITEM_FAIL_THRESHOLD); - } - - public void testAddItemAtPerformance() { - Collection<Long> times = new ArrayList<Long>(); - for (int j = 0; j < REPEATS; ++j) { - IndexedContainer c = new IndexedContainer(); - long start = System.currentTimeMillis(); - for (int i = 0; i < ITEMS; i++) { - c.addItemAt(0); - } - times.add(System.currentTimeMillis() - start); - } - checkMedian(ITEMS, times, "IndexedContainer.addItemAt()", - ADD_ITEM_AT_FAIL_THRESHOLD); - } - - public void testAddItemAfterPerformance() { - Object initialId = "Item0"; - Collection<Long> times = new ArrayList<Long>(); - for (int j = 0; j < REPEATS; ++j) { - IndexedContainer c = new IndexedContainer(); - c.addItem(initialId); - long start = System.currentTimeMillis(); - for (int i = 0; i < ITEMS; i++) { - c.addItemAfter(initialId); - } - times.add(System.currentTimeMillis() - start); - } - checkMedian(ITEMS, times, "IndexedContainer.addItemAfter()", - ADD_ITEM_AFTER_FAIL_THRESHOLD); - } - - public void testAddItemAfterLastPerformance() { - // TODO running with less items because slow otherwise - Collection<Long> times = new ArrayList<Long>(); - for (int j = 0; j < REPEATS; ++j) { - IndexedContainer c = new IndexedContainer(); - c.addItem(); - long start = System.currentTimeMillis(); - for (int i = 0; i < ITEMS / 3; i++) { - c.addItemAfter(c.lastItemId()); - } - times.add(System.currentTimeMillis() - start); - } - checkMedian(ITEMS / 3, times, "IndexedContainer.addItemAfter(lastId)", - ADD_ITEM_AFTER_LAST_FAIL_THRESHOLD); - } - - public void testAddItemsConstructorPerformance() { - Collection<Object> items = new ArrayList<Object>(50000); - for (int i = 0; i < ITEMS; ++i) { - items.add(new Object()); - } - - SortedSet<Long> times = new TreeSet<Long>(); - for (int j = 0; j < REPEATS; ++j) { - long start = System.currentTimeMillis(); - new IndexedContainer(items); - times.add(System.currentTimeMillis() - start); - } - checkMedian(ITEMS, times, "IndexedContainer(Collection)", - ADD_ITEMS_CONSTRUCTOR_FAIL_THRESHOLD); - } - - private void checkMedian(int items, Collection<Long> times, - String methodName, long threshold) { - long median = median(times); - System.out.println(methodName + " timings (ms) for " + items - + " items: " + times); - Assert.assertTrue(methodName + " too slow, median time " + median - + "ms for " + items + " items", median <= threshold); - } - - private Long median(Collection<Long> times) { - ArrayList<Long> list = new ArrayList<Long>(times); - Collections.sort(list); - // not exact median in some cases, but good enough - return list.get(list.size() / 2); - } - -} diff --git a/tests/server-side/com/vaadin/data/util/PropertyDescriptorTest.java b/tests/server-side/com/vaadin/data/util/PropertyDescriptorTest.java deleted file mode 100644 index 14e70d76d4..0000000000 --- a/tests/server-side/com/vaadin/data/util/PropertyDescriptorTest.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.vaadin.data.util; - -import java.beans.Introspector; -import java.beans.PropertyDescriptor; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; - -import junit.framework.Assert; -import junit.framework.TestCase; - -import com.vaadin.data.Property; -import com.vaadin.data.util.NestedMethodPropertyTest.Person; - -public class PropertyDescriptorTest extends TestCase { - public void testMethodPropertyDescriptorSerialization() throws Exception { - PropertyDescriptor[] pds = Introspector.getBeanInfo(Person.class) - .getPropertyDescriptors(); - - MethodPropertyDescriptor<Person> descriptor = null; - - for (PropertyDescriptor pd : pds) { - if ("name".equals(pd.getName())) { - descriptor = new MethodPropertyDescriptor<Person>(pd.getName(), - String.class, pd.getReadMethod(), pd.getWriteMethod()); - break; - } - } - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - new ObjectOutputStream(baos).writeObject(descriptor); - @SuppressWarnings("unchecked") - VaadinPropertyDescriptor<Person> descriptor2 = (VaadinPropertyDescriptor<Person>) new ObjectInputStream( - new ByteArrayInputStream(baos.toByteArray())).readObject(); - - Property<?> property = descriptor2.createProperty(new Person("John", - null)); - Assert.assertEquals("John", property.getValue()); - } - - public void testNestedPropertyDescriptorSerialization() throws Exception { - NestedPropertyDescriptor<Person> pd = new NestedPropertyDescriptor<Person>( - "name", Person.class); - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - new ObjectOutputStream(baos).writeObject(pd); - @SuppressWarnings("unchecked") - VaadinPropertyDescriptor<Person> pd2 = (VaadinPropertyDescriptor<Person>) new ObjectInputStream( - new ByteArrayInputStream(baos.toByteArray())).readObject(); - - Property<?> property = pd2.createProperty(new Person("John", null)); - Assert.assertEquals("John", property.getValue()); - } -} diff --git a/tests/server-side/com/vaadin/data/util/PropertySetItemTest.java b/tests/server-side/com/vaadin/data/util/PropertySetItemTest.java deleted file mode 100644 index a3169332ec..0000000000 --- a/tests/server-side/com/vaadin/data/util/PropertySetItemTest.java +++ /dev/null @@ -1,405 +0,0 @@ -package com.vaadin.data.util; - -import java.util.Iterator; - -import junit.framework.Assert; -import junit.framework.TestCase; - -import org.easymock.EasyMock; - -import com.vaadin.data.Item.PropertySetChangeEvent; -import com.vaadin.data.Item.PropertySetChangeListener; - -public class PropertySetItemTest extends TestCase { - - private static final String ID1 = "id1"; - private static final String ID2 = "id2"; - private static final String ID3 = "id3"; - - private static final String VALUE1 = "value1"; - private static final String VALUE2 = "value2"; - private static final String VALUE3 = "value3"; - - private ObjectProperty<String> prop1; - private ObjectProperty<String> prop2; - private ObjectProperty<String> prop3; - - private PropertySetChangeListener propertySetListenerMock; - private PropertySetChangeListener propertySetListenerMock2; - - @Override - protected void setUp() throws Exception { - prop1 = new ObjectProperty<String>(VALUE1, String.class); - prop2 = new ObjectProperty<String>(VALUE2, String.class); - prop3 = new ObjectProperty<String>(VALUE3, String.class); - - propertySetListenerMock = EasyMock - .createStrictMock(PropertySetChangeListener.class); - propertySetListenerMock2 = EasyMock - .createMock(PropertySetChangeListener.class); - } - - @Override - protected void tearDown() throws Exception { - prop1 = null; - prop2 = null; - prop3 = null; - - propertySetListenerMock = null; - propertySetListenerMock2 = null; - } - - private PropertysetItem createPropertySetItem() { - return new PropertysetItem(); - } - - public void testEmptyItem() { - PropertysetItem item = createPropertySetItem(); - Assert.assertNotNull(item.getItemPropertyIds()); - Assert.assertEquals(0, item.getItemPropertyIds().size()); - } - - public void testGetProperty() { - PropertysetItem item = createPropertySetItem(); - - Assert.assertNull(item.getItemProperty(ID1)); - - item.addItemProperty(ID1, prop1); - - Assert.assertEquals(prop1, item.getItemProperty(ID1)); - Assert.assertNull(item.getItemProperty(ID2)); - } - - public void testAddSingleProperty() { - PropertysetItem item = createPropertySetItem(); - - item.addItemProperty(ID1, prop1); - Assert.assertEquals(1, item.getItemPropertyIds().size()); - Object firstValue = item.getItemPropertyIds().iterator().next(); - Assert.assertEquals(ID1, firstValue); - Assert.assertEquals(prop1, item.getItemProperty(ID1)); - } - - public void testAddMultipleProperties() { - PropertysetItem item = createPropertySetItem(); - - item.addItemProperty(ID1, prop1); - Assert.assertEquals(1, item.getItemPropertyIds().size()); - Assert.assertEquals(prop1, item.getItemProperty(ID1)); - - item.addItemProperty(ID2, prop2); - Assert.assertEquals(2, item.getItemPropertyIds().size()); - Assert.assertEquals(prop1, item.getItemProperty(ID1)); - Assert.assertEquals(prop2, item.getItemProperty(ID2)); - - item.addItemProperty(ID3, prop3); - Assert.assertEquals(3, item.getItemPropertyIds().size()); - } - - public void testAddedPropertyOrder() { - PropertysetItem item = createPropertySetItem(); - item.addItemProperty(ID1, prop1); - item.addItemProperty(ID2, prop2); - item.addItemProperty(ID3, prop3); - - Iterator<?> it = item.getItemPropertyIds().iterator(); - Assert.assertEquals(ID1, it.next()); - Assert.assertEquals(ID2, it.next()); - Assert.assertEquals(ID3, it.next()); - } - - public void testAddPropertyTwice() { - PropertysetItem item = createPropertySetItem(); - Assert.assertTrue(item.addItemProperty(ID1, prop1)); - Assert.assertFalse(item.addItemProperty(ID1, prop1)); - - Assert.assertEquals(1, item.getItemPropertyIds().size()); - Assert.assertEquals(prop1, item.getItemProperty(ID1)); - } - - public void testCannotChangeProperty() { - PropertysetItem item = createPropertySetItem(); - Assert.assertTrue(item.addItemProperty(ID1, prop1)); - - Assert.assertEquals(prop1, item.getItemProperty(ID1)); - - Assert.assertFalse(item.addItemProperty(ID1, prop2)); - - Assert.assertEquals(1, item.getItemPropertyIds().size()); - Assert.assertEquals(prop1, item.getItemProperty(ID1)); - } - - public void testRemoveProperty() { - PropertysetItem item = createPropertySetItem(); - item.addItemProperty(ID1, prop1); - item.removeItemProperty(ID1); - - Assert.assertEquals(0, item.getItemPropertyIds().size()); - Assert.assertNull(item.getItemProperty(ID1)); - } - - public void testRemovePropertyOrder() { - PropertysetItem item = createPropertySetItem(); - item.addItemProperty(ID1, prop1); - item.addItemProperty(ID2, prop2); - item.addItemProperty(ID3, prop3); - - item.removeItemProperty(ID2); - - Iterator<?> it = item.getItemPropertyIds().iterator(); - Assert.assertEquals(ID1, it.next()); - Assert.assertEquals(ID3, it.next()); - } - - public void testRemoveNonExistentListener() { - PropertysetItem item = createPropertySetItem(); - item.removeListener(propertySetListenerMock); - } - - public void testRemoveListenerTwice() { - PropertysetItem item = createPropertySetItem(); - item.addListener(propertySetListenerMock); - item.removeListener(propertySetListenerMock); - item.removeListener(propertySetListenerMock); - } - - public void testAddPropertyNotification() { - // exactly one notification each time - PropertysetItem item = createPropertySetItem(); - - // Expectations and start test - propertySetListenerMock.itemPropertySetChange(EasyMock - .isA(PropertySetChangeEvent.class)); - EasyMock.replay(propertySetListenerMock); - - // Add listener and add a property -> should end up in listener once - item.addListener(propertySetListenerMock); - item.addItemProperty(ID1, prop1); - - // Ensure listener was called once - EasyMock.verify(propertySetListenerMock); - - // Remove the listener -> should not end up in listener when adding a - // property - item.removeListener(propertySetListenerMock); - item.addItemProperty(ID2, prop2); - - // Ensure listener still has been called only once - EasyMock.verify(propertySetListenerMock); - } - - public void testRemovePropertyNotification() { - // exactly one notification each time - PropertysetItem item = createPropertySetItem(); - item.addItemProperty(ID1, prop1); - item.addItemProperty(ID2, prop2); - - // Expectations and start test - propertySetListenerMock.itemPropertySetChange(EasyMock - .isA(PropertySetChangeEvent.class)); - EasyMock.replay(propertySetListenerMock); - - // Add listener and add a property -> should end up in listener once - item.addListener(propertySetListenerMock); - item.removeItemProperty(ID1); - - // Ensure listener was called once - EasyMock.verify(propertySetListenerMock); - - // Remove the listener -> should not end up in listener - item.removeListener(propertySetListenerMock); - item.removeItemProperty(ID2); - - // Ensure listener still has been called only once - EasyMock.verify(propertySetListenerMock); - } - - public void testItemEqualsNull() { - PropertysetItem item = createPropertySetItem(); - - Assert.assertFalse(item.equals(null)); - } - - public void testEmptyItemEquals() { - PropertysetItem item1 = createPropertySetItem(); - PropertysetItem item2 = createPropertySetItem(); - - Assert.assertTrue(item1.equals(item2)); - } - - public void testItemEqualsSingleProperty() { - PropertysetItem item1 = createPropertySetItem(); - PropertysetItem item2 = createPropertySetItem(); - item2.addItemProperty(ID1, prop1); - PropertysetItem item3 = createPropertySetItem(); - item3.addItemProperty(ID1, prop1); - PropertysetItem item4 = createPropertySetItem(); - item4.addItemProperty(ID1, prop2); - PropertysetItem item5 = createPropertySetItem(); - item5.addItemProperty(ID2, prop2); - - Assert.assertFalse(item1.equals(item2)); - Assert.assertFalse(item1.equals(item3)); - Assert.assertFalse(item1.equals(item4)); - Assert.assertFalse(item1.equals(item5)); - - Assert.assertTrue(item2.equals(item3)); - Assert.assertFalse(item2.equals(item4)); - Assert.assertFalse(item2.equals(item5)); - - Assert.assertFalse(item3.equals(item4)); - Assert.assertFalse(item3.equals(item5)); - - Assert.assertFalse(item4.equals(item5)); - - Assert.assertFalse(item2.equals(item1)); - } - - public void testItemEqualsMultipleProperties() { - PropertysetItem item1 = createPropertySetItem(); - item1.addItemProperty(ID1, prop1); - - PropertysetItem item2 = createPropertySetItem(); - item2.addItemProperty(ID1, prop1); - item2.addItemProperty(ID2, prop2); - - PropertysetItem item3 = createPropertySetItem(); - item3.addItemProperty(ID1, prop1); - item3.addItemProperty(ID2, prop2); - - Assert.assertFalse(item1.equals(item2)); - - Assert.assertTrue(item2.equals(item3)); - } - - public void testItemEqualsPropertyOrder() { - PropertysetItem item1 = createPropertySetItem(); - item1.addItemProperty(ID1, prop1); - item1.addItemProperty(ID2, prop2); - - PropertysetItem item2 = createPropertySetItem(); - item2.addItemProperty(ID2, prop2); - item2.addItemProperty(ID1, prop1); - - Assert.assertFalse(item1.equals(item2)); - } - - public void testEqualsSingleListener() { - PropertysetItem item1 = createPropertySetItem(); - PropertysetItem item2 = createPropertySetItem(); - - item1.addListener(propertySetListenerMock); - - Assert.assertFalse(item1.equals(item2)); - Assert.assertFalse(item2.equals(item1)); - - item2.addListener(propertySetListenerMock); - - Assert.assertTrue(item1.equals(item2)); - Assert.assertTrue(item2.equals(item1)); - } - - public void testEqualsMultipleListeners() { - PropertysetItem item1 = createPropertySetItem(); - PropertysetItem item2 = createPropertySetItem(); - - item1.addListener(propertySetListenerMock); - item1.addListener(propertySetListenerMock2); - - item2.addListener(propertySetListenerMock); - - Assert.assertFalse(item1.equals(item2)); - Assert.assertFalse(item2.equals(item1)); - - item2.addListener(propertySetListenerMock2); - - Assert.assertTrue(item1.equals(item2)); - Assert.assertTrue(item2.equals(item1)); - } - - public void testEqualsAddRemoveListener() { - PropertysetItem item1 = createPropertySetItem(); - PropertysetItem item2 = createPropertySetItem(); - - item1.addListener(propertySetListenerMock); - item1.removeListener(propertySetListenerMock); - - Assert.assertTrue(item1.equals(item2)); - Assert.assertTrue(item2.equals(item1)); - } - - public void testItemHashCodeEmpty() { - PropertysetItem item1 = createPropertySetItem(); - PropertysetItem item2 = createPropertySetItem(); - - Assert.assertEquals(item1.hashCode(), item2.hashCode()); - } - - public void testItemHashCodeAddProperties() { - PropertysetItem item1 = createPropertySetItem(); - PropertysetItem item2 = createPropertySetItem(); - - Assert.assertEquals(item1.hashCode(), item2.hashCode()); - - item1.addItemProperty(ID1, prop1); - item1.addItemProperty(ID2, prop2); - // hashCodes can be equal even if items are different - - item2.addItemProperty(ID1, prop1); - item2.addItemProperty(ID2, prop2); - // but here hashCodes must be equal - Assert.assertEquals(item1.hashCode(), item2.hashCode()); - } - - public void testItemHashCodeAddListeners() { - PropertysetItem item1 = createPropertySetItem(); - PropertysetItem item2 = createPropertySetItem(); - - Assert.assertEquals(item1.hashCode(), item2.hashCode()); - - item1.addListener(propertySetListenerMock); - // hashCodes can be equal even if items are different - - item2.addListener(propertySetListenerMock); - // but here hashCodes must be equal - Assert.assertEquals(item1.hashCode(), item2.hashCode()); - } - - public void testItemHashCodeAddRemoveProperty() { - PropertysetItem item1 = createPropertySetItem(); - PropertysetItem item2 = createPropertySetItem(); - - item1.addItemProperty(ID1, prop1); - item1.removeItemProperty(ID1); - - Assert.assertEquals(item1.hashCode(), item2.hashCode()); - } - - public void testItemHashCodeAddRemoveListener() { - PropertysetItem item1 = createPropertySetItem(); - PropertysetItem item2 = createPropertySetItem(); - - item1.addListener(propertySetListenerMock); - item1.removeListener(propertySetListenerMock); - - Assert.assertEquals(item1.hashCode(), item2.hashCode()); - } - - public void testToString() { - // toString() behavior is specified in the class javadoc - PropertysetItem item = createPropertySetItem(); - - Assert.assertEquals("", item.toString()); - - item.addItemProperty(ID1, prop1); - - Assert.assertEquals(String.valueOf(prop1.getValue()), item.toString()); - - item.addItemProperty(ID2, prop2); - - Assert.assertEquals( - String.valueOf(prop1.getValue()) + " " - + String.valueOf(prop2.getValue()), item.toString()); - } - -} diff --git a/tests/server-side/com/vaadin/data/util/TestContainerHierarchicalWrapper.java b/tests/server-side/com/vaadin/data/util/TestContainerHierarchicalWrapper.java deleted file mode 100644 index ff6de50651..0000000000 --- a/tests/server-side/com/vaadin/data/util/TestContainerHierarchicalWrapper.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.vaadin.data.util; - -import java.util.Collection; - -public class TestContainerHierarchicalWrapper extends - AbstractHierarchicalContainerTest { - - public void testBasicOperations() { - testBasicContainerOperations(new ContainerHierarchicalWrapper( - new IndexedContainer())); - } - - public void testHierarchicalContainer() { - testHierarchicalContainer(new ContainerHierarchicalWrapper( - new IndexedContainer())); - } - - public void testRemoveSubtree() { - testRemoveHierarchicalWrapperSubtree(new ContainerHierarchicalWrapper( - new IndexedContainer())); - } - - protected void testRemoveHierarchicalWrapperSubtree( - ContainerHierarchicalWrapper container) { - initializeContainer(container); - - // remove root item - container.removeItemRecursively("org"); - - int packages = 21 + 3 - 3; - int expectedSize = sampleData.length + packages - 1; - - validateContainer(container, "com", "com.vaadin.util.SerializerHelper", - "com.vaadin.server.ApplicationResource", "blah", true, - expectedSize); - - // rootItemIds - Collection<?> rootIds = container.rootItemIds(); - assertEquals(1, rootIds.size()); - } - -} diff --git a/tests/server-side/com/vaadin/data/util/TestContainerSorting.java b/tests/server-side/com/vaadin/data/util/TestContainerSorting.java deleted file mode 100644 index 497699605a..0000000000 --- a/tests/server-side/com/vaadin/data/util/TestContainerSorting.java +++ /dev/null @@ -1,224 +0,0 @@ -package com.vaadin.data.util; - -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; - -import junit.framework.TestCase; - -import com.vaadin.data.Container; -import com.vaadin.data.Item; -import com.vaadin.tests.util.TestUtil; - -public class TestContainerSorting extends TestCase { - - private static final String ITEM_DATA_MINUS2_NULL = "Data -2 null"; - private static final String ITEM_DATA_MINUS2 = "Data -2"; - private static final String ITEM_DATA_MINUS1 = "Data -1"; - private static final String ITEM_DATA_MINUS1_NULL = "Data -1 null"; - private static final String ITEM_ANOTHER_NULL = "Another null"; - private static final String ITEM_STRING_2 = "String 2"; - private static final String ITEM_STRING_NULL2 = "String null"; - private static final String ITEM_STRING_1 = "String 1"; - - private static final String PROPERTY_INTEGER_NULL2 = "integer-null"; - private static final String PROPERTY_INTEGER_NOT_NULL = "integer-not-null"; - private static final String PROPERTY_STRING_NULL = "string-null"; - private static final String PROPERTY_STRING_ID = "string-not-null"; - - @Override - protected void setUp() throws Exception { - super.setUp(); - } - - public void testEmptyFilteredIndexedContainer() { - IndexedContainer ic = new IndexedContainer(); - - addProperties(ic); - populate(ic); - - ic.addContainerFilter(PROPERTY_STRING_ID, "aasdfasdfasdf", true, false); - ic.sort(new Object[] { PROPERTY_STRING_ID }, new boolean[] { true }); - - } - - public void testFilteredIndexedContainer() { - IndexedContainer ic = new IndexedContainer(); - - addProperties(ic); - populate(ic); - - ic.addContainerFilter(PROPERTY_STRING_ID, "a", true, false); - ic.sort(new Object[] { PROPERTY_STRING_ID }, new boolean[] { true }); - verifyOrder(ic, - new String[] { ITEM_ANOTHER_NULL, ITEM_DATA_MINUS1, - ITEM_DATA_MINUS1_NULL, ITEM_DATA_MINUS2, - ITEM_DATA_MINUS2_NULL, }); - } - - public void testIndexedContainer() { - IndexedContainer ic = new IndexedContainer(); - - addProperties(ic); - populate(ic); - - ic.sort(new Object[] { PROPERTY_STRING_ID }, new boolean[] { true }); - verifyOrder(ic, new String[] { ITEM_ANOTHER_NULL, ITEM_DATA_MINUS1, - ITEM_DATA_MINUS1_NULL, ITEM_DATA_MINUS2, ITEM_DATA_MINUS2_NULL, - ITEM_STRING_1, ITEM_STRING_2, ITEM_STRING_NULL2 }); - - ic.sort(new Object[] { PROPERTY_INTEGER_NOT_NULL, - PROPERTY_INTEGER_NULL2, PROPERTY_STRING_ID }, new boolean[] { - true, false, true }); - verifyOrder(ic, new String[] { ITEM_DATA_MINUS2, ITEM_DATA_MINUS2_NULL, - ITEM_DATA_MINUS1, ITEM_DATA_MINUS1_NULL, ITEM_ANOTHER_NULL, - ITEM_STRING_NULL2, ITEM_STRING_1, ITEM_STRING_2 }); - - ic.sort(new Object[] { PROPERTY_INTEGER_NOT_NULL, - PROPERTY_INTEGER_NULL2, PROPERTY_STRING_ID }, new boolean[] { - true, true, true }); - verifyOrder(ic, new String[] { ITEM_DATA_MINUS2_NULL, ITEM_DATA_MINUS2, - ITEM_DATA_MINUS1_NULL, ITEM_DATA_MINUS1, ITEM_ANOTHER_NULL, - ITEM_STRING_NULL2, ITEM_STRING_1, ITEM_STRING_2 }); - - } - - public void testHierarchicalContainer() { - HierarchicalContainer hc = new HierarchicalContainer(); - populateContainer(hc); - hc.sort(new Object[] { "name" }, new boolean[] { true }); - verifyOrder(hc, new String[] { "Audi", "C++", "Call of Duty", "Cars", - "English", "Fallout", "Finnish", "Ford", "Games", "Java", - "Might and Magic", "Natural languages", "PHP", - "Programming languages", "Python", "Red Alert", "Swedish", - "Toyota", "Volvo" }); - TestUtil.assertArrays( - hc.rootItemIds().toArray(), - new Integer[] { nameToId.get("Cars"), nameToId.get("Games"), - nameToId.get("Natural languages"), - nameToId.get("Programming languages") }); - TestUtil.assertArrays( - hc.getChildren(nameToId.get("Games")).toArray(), - new Integer[] { nameToId.get("Call of Duty"), - nameToId.get("Fallout"), - nameToId.get("Might and Magic"), - nameToId.get("Red Alert") }); - } - - private static void populateContainer(HierarchicalContainer container) { - container.addContainerProperty("name", String.class, null); - - addItem(container, "Games", null); - addItem(container, "Call of Duty", "Games"); - addItem(container, "Might and Magic", "Games"); - addItem(container, "Fallout", "Games"); - addItem(container, "Red Alert", "Games"); - - addItem(container, "Cars", null); - addItem(container, "Toyota", "Cars"); - addItem(container, "Volvo", "Cars"); - addItem(container, "Audi", "Cars"); - addItem(container, "Ford", "Cars"); - - addItem(container, "Natural languages", null); - addItem(container, "Swedish", "Natural languages"); - addItem(container, "English", "Natural languages"); - addItem(container, "Finnish", "Natural languages"); - - addItem(container, "Programming languages", null); - addItem(container, "C++", "Programming languages"); - addItem(container, "PHP", "Programming languages"); - addItem(container, "Java", "Programming languages"); - addItem(container, "Python", "Programming languages"); - - } - - private static int index = 0; - private static Map<String, Integer> nameToId = new HashMap<String, Integer>(); - private static Map<Integer, String> idToName = new HashMap<Integer, String>(); - - public static void addItem(IndexedContainer container, String string, - String parent) { - nameToId.put(string, index); - idToName.put(index, string); - - Item item = container.addItem(index); - item.getItemProperty("name").setValue(string); - - if (parent != null && container instanceof HierarchicalContainer) { - ((HierarchicalContainer) container).setParent(index, - nameToId.get(parent)); - } - - index++; - } - - private void verifyOrder(Container.Sortable ic, Object[] idOrder) { - int size = ic.size(); - Object[] actual = new Object[size]; - Iterator<?> i = ic.getItemIds().iterator(); - int index = 0; - while (i.hasNext()) { - Object o = i.next(); - if (o.getClass() == Integer.class - && idOrder[index].getClass() == String.class) { - o = idToName.get(o); - } - actual[index++] = o; - } - - TestUtil.assertArrays(actual, idOrder); - - } - - private void populate(IndexedContainer ic) { - addItem(ic, ITEM_STRING_1, ITEM_STRING_1, 1, 1); - addItem(ic, ITEM_STRING_NULL2, null, 0, null); - addItem(ic, ITEM_STRING_2, ITEM_STRING_2, 2, 2); - addItem(ic, ITEM_ANOTHER_NULL, null, 0, null); - addItem(ic, ITEM_DATA_MINUS1, ITEM_DATA_MINUS1, -1, -1); - addItem(ic, ITEM_DATA_MINUS1_NULL, null, -1, null); - addItem(ic, ITEM_DATA_MINUS2, ITEM_DATA_MINUS2, -2, -2); - addItem(ic, ITEM_DATA_MINUS2_NULL, null, -2, null); - } - - private Item addItem(Container ic, String id, String string_null, - int integer, Integer integer_null) { - Item i = ic.addItem(id); - i.getItemProperty(PROPERTY_STRING_ID).setValue(id); - i.getItemProperty(PROPERTY_STRING_NULL).setValue(string_null); - i.getItemProperty(PROPERTY_INTEGER_NOT_NULL).setValue(integer); - i.getItemProperty(PROPERTY_INTEGER_NULL2).setValue(integer_null); - - return i; - } - - private void addProperties(IndexedContainer ic) { - ic.addContainerProperty("id", String.class, null); - ic.addContainerProperty(PROPERTY_STRING_ID, String.class, ""); - ic.addContainerProperty(PROPERTY_STRING_NULL, String.class, null); - ic.addContainerProperty(PROPERTY_INTEGER_NULL2, Integer.class, null); - ic.addContainerProperty(PROPERTY_INTEGER_NOT_NULL, Integer.class, 0); - ic.addContainerProperty("comparable-null", Integer.class, 0); - } - - public class MyObject implements Comparable<MyObject> { - private String data; - - @Override - public int compareTo(MyObject o) { - if (o == null) { - return 1; - } - - if (o.data == null) { - return data == null ? 0 : 1; - } else if (data == null) { - return -1; - } else { - return data.compareTo(o.data); - } - } - } - -} diff --git a/tests/server-side/com/vaadin/data/util/TestHierarchicalContainer.java b/tests/server-side/com/vaadin/data/util/TestHierarchicalContainer.java deleted file mode 100644 index 10bc71c3a7..0000000000 --- a/tests/server-side/com/vaadin/data/util/TestHierarchicalContainer.java +++ /dev/null @@ -1,263 +0,0 @@ -package com.vaadin.data.util; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.Item; - -public class TestHierarchicalContainer extends - AbstractHierarchicalContainerTest { - - public void testBasicOperations() { - testBasicContainerOperations(new HierarchicalContainer()); - } - - public void testFiltering() { - testContainerFiltering(new HierarchicalContainer()); - } - - public void testSorting() { - testContainerSorting(new HierarchicalContainer()); - } - - public void testOrdered() { - testContainerOrdered(new HierarchicalContainer()); - } - - public void testHierarchicalSorting() { - testHierarchicalSorting(new HierarchicalContainer()); - } - - public void testSortingAndFiltering() { - testContainerSortingAndFiltering(new HierarchicalContainer()); - } - - public void testRemovingItemsFromFilteredContainer() { - HierarchicalContainer container = new HierarchicalContainer(); - initializeContainer(container); - container.setIncludeParentsWhenFiltering(true); - container.addContainerFilter(FULLY_QUALIFIED_NAME, "ab", false, false); - Object p1 = container.getParent("com.vaadin.ui.TabSheet"); - assertEquals("com.vaadin.ui", p1); - - container.removeItem("com.vaadin.ui.TabSheet"); - // Parent for the removed item must be null because the item is no - // longer in the container - p1 = container.getParent("com.vaadin.ui.TabSheet"); - assertNull("Parent should be null, is " + p1, p1); - - container.removeAllItems(); - p1 = container.getParent("com.vaadin.client.Focusable"); - assertNull("Parent should be null, is " + p1, p1); - - } - - public void testParentWhenRemovingFilterFromContainer() { - HierarchicalContainer container = new HierarchicalContainer(); - initializeContainer(container); - container.setIncludeParentsWhenFiltering(true); - container.addContainerFilter(FULLY_QUALIFIED_NAME, "ab", false, false); - Object p1 = container.getParent("com.vaadin.ui.TabSheet"); - assertEquals("com.vaadin.ui", p1); - p1 = container.getParent("com.vaadin.client.ui.VPopupCalendar"); - assertNull(p1); - container.removeAllContainerFilters(); - p1 = container.getParent("com.vaadin.client.ui.VPopupCalendar"); - assertEquals("com.vaadin.client.ui", p1); - - } - - public void testChangeParentInFilteredContainer() { - HierarchicalContainer container = new HierarchicalContainer(); - initializeContainer(container); - container.setIncludeParentsWhenFiltering(true); - container.addContainerFilter(FULLY_QUALIFIED_NAME, "Tab", false, false); - - // Change parent of filtered item - Object p1 = container.getParent("com.vaadin.ui.TabSheet"); - assertEquals("com.vaadin.ui", p1); - container.setParent("com.vaadin.ui.TabSheet", "com.vaadin"); - p1 = container.getParent("com.vaadin.ui.TabSheet"); - assertEquals("com.vaadin", p1); - container.setParent("com.vaadin.ui.TabSheet", "com"); - p1 = container.getParent("com.vaadin.ui.TabSheet"); - assertEquals("com", p1); - container.setParent("com.vaadin.ui.TabSheet", null); - p1 = container.getParent("com.vaadin.ui.TabSheet"); - assertNull(p1); - - // root -> non-root - container.setParent("com.vaadin.ui.TabSheet", "com"); - p1 = container.getParent("com.vaadin.ui.TabSheet"); - assertEquals("com", p1); - - } - - public void testHierarchicalFilteringWithParents() { - HierarchicalContainer container = new HierarchicalContainer(); - initializeContainer(container); - container.setIncludeParentsWhenFiltering(true); - - // Filter by "contains ab" - container.addContainerFilter(FULLY_QUALIFIED_NAME, "ab", false, false); - - // 20 items match the filters and the have 8 parents that should also be - // included - // only one root "com" should exist - // filtered - int expectedSize = 29; - int expectedRoots = 1; - - validateHierarchicalContainer(container, "com", - "com.vaadin.ui.TabSheet", "com.vaadin.client.Focusable", - "blah", true, expectedSize, expectedRoots, true); - - // only include .gwt.client classes - container.removeAllContainerFilters(); - container.addContainerFilter(FULLY_QUALIFIED_NAME, ".gwt.client.", - false, false); - - int packages = 6; - int classes = 112; - - expectedSize = packages + classes; - expectedRoots = 1; - - validateHierarchicalContainer(container, "com", - "com.vaadin.client.WidgetSet", - "com.vaadin.client.ui.VSplitPanelVertical", "blah", true, - expectedSize, expectedRoots, true); - - // Additionally remove all without 'm' in the simple name. - container.addContainerFilter(SIMPLE_NAME, "m", false, false); - - expectedSize = 7 + 18; - expectedRoots = 1; - - validateHierarchicalContainer(container, "com", - "com.vaadin.client.ui.VUriFragmentUtility", - "com.vaadin.client.ui.layout.ChildComponentContainer", "blah", - true, expectedSize, expectedRoots, true); - - } - - public void testRemoveLastChild() { - HierarchicalContainer c = new HierarchicalContainer(); - - c.addItem("root"); - assertEquals(false, c.hasChildren("root")); - - c.addItem("child"); - c.setParent("child", "root"); - assertEquals(true, c.hasChildren("root")); - - c.removeItem("child"); - assertFalse(c.containsId("child")); - assertNull(c.getChildren("root")); - assertNull(c.getChildren("child")); - assertFalse(c.hasChildren("child")); - assertFalse(c.hasChildren("root")); - } - - public void testRemoveLastChildFromFiltered() { - HierarchicalContainer c = new HierarchicalContainer(); - - c.addItem("root"); - assertEquals(false, c.hasChildren("root")); - - c.addItem("child"); - c.setParent("child", "root"); - assertEquals(true, c.hasChildren("root")); - - // Dummy filter that does not remove any items - c.addContainerFilter(new Filter() { - - @Override - public boolean passesFilter(Object itemId, Item item) - throws UnsupportedOperationException { - return true; - } - - @Override - public boolean appliesToProperty(Object propertyId) { - return true; - } - }); - c.removeItem("child"); - - assertFalse(c.containsId("child")); - assertNull(c.getChildren("root")); - assertNull(c.getChildren("child")); - assertFalse(c.hasChildren("child")); - assertFalse(c.hasChildren("root")); - } - - public void testHierarchicalFilteringWithoutParents() { - HierarchicalContainer container = new HierarchicalContainer(); - - initializeContainer(container); - container.setIncludeParentsWhenFiltering(false); - - // Filter by "contains ab" - container.addContainerFilter(SIMPLE_NAME, "ab", false, false); - - // 20 items match the filter. - // com.vaadin.data.BufferedValidatable - // com.vaadin.data.Validatable - // com.vaadin.client.Focusable - // com.vaadin.client.Paintable - // com.vaadin.client.ui.Table - // com.vaadin.client.ui.VLabel - // com.vaadin.client.ui.VScrollTable - // com.vaadin.client.ui.VTablePaging - // com.vaadin.client.ui.VTabsheet - // com.vaadin.client.ui.VTabsheetBase - // com.vaadin.client.ui.VTabsheetPanel - // com.vaadin.server.ChangeVariablesErrorEvent - // com.vaadin.server.Paintable - // com.vaadin.server.Scrollable - // com.vaadin.server.Sizeable - // com.vaadin.server.VariableOwner - // com.vaadin.ui.Label - // com.vaadin.ui.Table - // com.vaadin.ui.TableFieldFactory - // com.vaadin.ui.TabSheet - // all become roots. - int expectedSize = 20; - int expectedRoots = 20; - - validateHierarchicalContainer(container, - "com.vaadin.data.BufferedValidatable", - "com.vaadin.ui.TabSheet", "com.vaadin.client.ui.VTabsheetBase", - "blah", true, expectedSize, expectedRoots, false); - - // only include .gwt.client classes - container.removeAllContainerFilters(); - container.addContainerFilter(FULLY_QUALIFIED_NAME, ".gwt.client.", - false, false); - - int packages = 3; - int classes = 110; - - expectedSize = packages + classes; - expectedRoots = 35 + 1; // com.vaadin.client.ui + - // com.vaadin.client.* - - // Sorting is case insensitive - validateHierarchicalContainer(container, - "com.vaadin.client.ApplicationConfiguration", - "com.vaadin.client.WidgetSet", - "com.vaadin.client.ui.VOptionGroup", "blah", true, - expectedSize, expectedRoots, false); - - // Additionally remove all without 'P' in the simple name. - container.addContainerFilter(SIMPLE_NAME, "P", false, false); - - expectedSize = 13; - expectedRoots = expectedSize; - - validateHierarchicalContainer(container, "com.vaadin.client.Paintable", - "com.vaadin.client.ui.VTabsheetPanel", - "com.vaadin.client.ui.VPopupCalendar", "blah", true, - expectedSize, expectedRoots, false); - - } -} diff --git a/tests/server-side/com/vaadin/data/util/TestIndexedContainer.java b/tests/server-side/com/vaadin/data/util/TestIndexedContainer.java deleted file mode 100644 index 156ff83883..0000000000 --- a/tests/server-side/com/vaadin/data/util/TestIndexedContainer.java +++ /dev/null @@ -1,271 +0,0 @@ -package com.vaadin.data.util; - -import com.vaadin.data.Item; -import com.vaadin.data.util.IndexedContainer; - -public class TestIndexedContainer extends AbstractInMemoryContainerTest { - - public void testBasicOperations() { - testBasicContainerOperations(new IndexedContainer()); - } - - public void testFiltering() { - testContainerFiltering(new IndexedContainer()); - } - - public void testSorting() { - testContainerSorting(new IndexedContainer()); - } - - public void testSortingAndFiltering() { - testContainerSortingAndFiltering(new IndexedContainer()); - } - - public void testContainerOrdered() { - testContainerOrdered(new IndexedContainer()); - } - - public void testContainerIndexed() { - testContainerIndexed(new IndexedContainer(), sampleData[2], 2, true, - "newItemId", true); - } - - public void testItemSetChangeListeners() { - IndexedContainer container = new IndexedContainer(); - ItemSetChangeCounter counter = new ItemSetChangeCounter(); - container.addListener(counter); - - String id1 = "id1"; - String id2 = "id2"; - String id3 = "id3"; - - initializeContainer(container); - counter.reset(); - container.addItem(); - counter.assertOnce(); - container.addItem(id1); - counter.assertOnce(); - - initializeContainer(container); - counter.reset(); - container.addItemAt(0); - counter.assertOnce(); - container.addItemAt(0, id1); - counter.assertOnce(); - container.addItemAt(0, id2); - counter.assertOnce(); - container.addItemAt(container.size(), id3); - counter.assertOnce(); - // no notification if already in container - container.addItemAt(0, id1); - counter.assertNone(); - - initializeContainer(container); - counter.reset(); - container.addItemAfter(null); - counter.assertOnce(); - container.addItemAfter(null, id1); - counter.assertOnce(); - container.addItemAfter(id1); - counter.assertOnce(); - container.addItemAfter(id1, id2); - counter.assertOnce(); - container.addItemAfter(container.firstItemId()); - counter.assertOnce(); - container.addItemAfter(container.lastItemId()); - counter.assertOnce(); - container.addItemAfter(container.lastItemId(), id3); - counter.assertOnce(); - // no notification if already in container - container.addItemAfter(0, id1); - counter.assertNone(); - - initializeContainer(container); - counter.reset(); - container.removeItem(sampleData[0]); - counter.assertOnce(); - - initializeContainer(container); - counter.reset(); - // no notification for removing a non-existing item - container.removeItem(id1); - counter.assertNone(); - - initializeContainer(container); - counter.reset(); - container.removeAllItems(); - counter.assertOnce(); - // already empty - container.removeAllItems(); - counter.assertNone(); - - } - - public void testAddRemoveContainerFilter() { - IndexedContainer container = new IndexedContainer(); - ItemSetChangeCounter counter = new ItemSetChangeCounter(); - container.addListener(counter); - - // simply adding or removing container filters should cause events - // (content changes) - - initializeContainer(container); - counter.reset(); - container.addContainerFilter(SIMPLE_NAME, "a", true, false); - counter.assertOnce(); - container.removeContainerFilters(SIMPLE_NAME); - counter.assertOnce(); - container.addContainerFilter(SIMPLE_NAME, "a", true, false); - counter.assertOnce(); - container.removeAllContainerFilters(); - counter.assertOnce(); - } - - // TODO other tests should check positions after removing filter etc, - // here concentrating on listeners - public void testItemSetChangeListenersFiltering() { - IndexedContainer container = new IndexedContainer(); - ItemSetChangeCounter counter = new ItemSetChangeCounter(); - container.addListener(counter); - - counter.reset(); - container.addContainerFilter(FULLY_QUALIFIED_NAME, "Test", true, false); - // no real change, so no notification required - counter.assertNone(); - - String id1 = "com.example.Test1"; - String id2 = "com.example.Test2"; - String id3 = "com.example.Other"; - - // perform operations while filtering container - - Item item; - - initializeContainer(container); - counter.reset(); - // passes filter - item = container.addItem(id1); - // no event if filtered out - counter.assertNone(); - item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(id1); - counter.assertOnce(); - // passes filter but already in the container - item = container.addItem(id1); - counter.assertNone(); - - initializeContainer(container); - counter.reset(); - // passes filter after change - item = container.addItemAt(0, id1); - counter.assertNone(); - item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(id1); - counter.assertOnce(); - item = container.addItemAt(container.size(), id2); - counter.assertNone(); - item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(id2); - counter.assertOnce(); - // passes filter but already in the container - item = container.addItemAt(0, id1); - counter.assertNone(); - item = container.addItemAt(container.size(), id2); - counter.assertNone(); - - initializeContainer(container); - counter.reset(); - // passes filter - item = container.addItemAfter(null, id1); - counter.assertNone(); - item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(id1); - counter.assertOnce(); - item = container.addItemAfter(container.lastItemId(), id2); - counter.assertNone(); - item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(id2); - counter.assertOnce(); - // passes filter but already in the container - item = container.addItemAfter(null, id1); - counter.assertNone(); - item = container.addItemAfter(container.lastItemId(), id2); - counter.assertNone(); - - // does not pass filter - - // TODO implement rest - - initializeContainer(container); - counter.reset(); - item = container.addItemAfter(null, id3); - counter.assertNone(); - item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(id3); - counter.assertNone(); - - initializeContainer(container); - counter.reset(); - item = container.addItemAfter(container.firstItemId(), id3); - counter.assertNone(); - item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(id3); - counter.assertNone(); - - initializeContainer(container); - counter.reset(); - item = container.addItemAfter(container.lastItemId(), id3); - counter.assertNone(); - item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(id3); - counter.assertNone(); - - initializeContainer(container); - counter.reset(); - item = container.addItemAt(0, id3); - counter.assertNone(); - item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(id3); - counter.assertNone(); - - initializeContainer(container); - counter.reset(); - item = container.addItemAt(1, id3); - counter.assertNone(); - item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(id3); - counter.assertNone(); - - initializeContainer(container); - counter.reset(); - item = container.addItemAt(container.size(), id3); - counter.assertNone(); - item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(id3); - counter.assertNone(); - - // passes filter - - initializeContainer(container); - counter.reset(); - item = container.addItem(id1); - counter.assertNone(); - item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(id1); - counter.assertOnce(); - container.removeItem(id1); - counter.assertOnce(); - // already removed - container.removeItem(id1); - counter.assertNone(); - - item = container.addItem(id3); - counter.assertNone(); - item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(id3); - counter.assertNone(); - // not visible - container.removeItem(id3); - counter.assertNone(); - - // remove all - - initializeContainer(container); - item = container.addItem(id1); - item.getItemProperty(FULLY_QUALIFIED_NAME).setValue(id1); - counter.reset(); - container.removeAllItems(); - counter.assertOnce(); - // no visible items - container.removeAllItems(); - counter.assertNone(); - } - -} diff --git a/tests/server-side/com/vaadin/data/util/filter/AbstractFilterTest.java b/tests/server-side/com/vaadin/data/util/filter/AbstractFilterTest.java deleted file mode 100644 index 6f96c3a51a..0000000000 --- a/tests/server-side/com/vaadin/data/util/filter/AbstractFilterTest.java +++ /dev/null @@ -1,97 +0,0 @@ -package com.vaadin.data.util.filter; - -import junit.framework.TestCase; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.Item; -import com.vaadin.data.Property; -import com.vaadin.data.util.ObjectProperty; -import com.vaadin.data.util.PropertysetItem; - -public abstract class AbstractFilterTest<FILTERTYPE extends Filter> extends - TestCase { - - protected static final String PROPERTY1 = "property1"; - protected static final String PROPERTY2 = "property2"; - - protected static class TestItem<T1, T2> extends PropertysetItem { - - public TestItem(T1 value1, T2 value2) { - addItemProperty(PROPERTY1, new ObjectProperty<T1>(value1)); - addItemProperty(PROPERTY2, new ObjectProperty<T2>(value2)); - } - } - - protected static class NullProperty implements Property<String> { - - @Override - public String getValue() { - return null; - } - - @Override - public void setValue(Object newValue) throws ReadOnlyException { - throw new ReadOnlyException(); - } - - @Override - public Class<String> getType() { - return String.class; - } - - @Override - public boolean isReadOnly() { - return true; - } - - @Override - public void setReadOnly(boolean newStatus) { - // do nothing - } - - } - - public static class SameItemFilter implements Filter { - - private final Item item; - private final Object propertyId; - - public SameItemFilter(Item item) { - this(item, ""); - } - - public SameItemFilter(Item item, Object propertyId) { - this.item = item; - this.propertyId = propertyId; - } - - @Override - public boolean passesFilter(Object itemId, Item item) - throws UnsupportedOperationException { - return this.item == item; - } - - @Override - public boolean appliesToProperty(Object propertyId) { - return this.propertyId != null ? this.propertyId.equals(propertyId) - : true; - } - - @Override - public boolean equals(Object obj) { - if (obj == null || !getClass().equals(obj.getClass())) { - return false; - } - SameItemFilter other = (SameItemFilter) obj; - return item == other.item - && (propertyId == null ? other.propertyId == null - : propertyId.equals(other.propertyId)); - } - - @Override - public int hashCode() { - return item.hashCode(); - } - } - -} diff --git a/tests/server-side/com/vaadin/data/util/filter/AndOrFilterTest.java b/tests/server-side/com/vaadin/data/util/filter/AndOrFilterTest.java deleted file mode 100644 index fdd5b8a645..0000000000 --- a/tests/server-side/com/vaadin/data/util/filter/AndOrFilterTest.java +++ /dev/null @@ -1,232 +0,0 @@ -package com.vaadin.data.util.filter; - -import junit.framework.Assert; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.Item; -import com.vaadin.data.util.BeanItem; - -public class AndOrFilterTest extends AbstractFilterTest<AbstractJunctionFilter> { - - protected Item item1 = new BeanItem<Integer>(1); - protected Item item2 = new BeanItem<Integer>(2); - - public void testNoFilterAnd() { - Filter filter = new And(); - - Assert.assertTrue(filter.passesFilter(null, item1)); - } - - public void testSingleFilterAnd() { - Filter filter = new And(new SameItemFilter(item1)); - - Assert.assertTrue(filter.passesFilter(null, item1)); - Assert.assertFalse(filter.passesFilter(null, item2)); - } - - public void testTwoFilterAnd() { - Filter filter1 = new And(new SameItemFilter(item1), new SameItemFilter( - item1)); - Filter filter2 = new And(new SameItemFilter(item1), new SameItemFilter( - item2)); - - Assert.assertTrue(filter1.passesFilter(null, item1)); - Assert.assertFalse(filter1.passesFilter(null, item2)); - - Assert.assertFalse(filter2.passesFilter(null, item1)); - Assert.assertFalse(filter2.passesFilter(null, item2)); - } - - public void testThreeFilterAnd() { - Filter filter1 = new And(new SameItemFilter(item1), new SameItemFilter( - item1), new SameItemFilter(item1)); - Filter filter2 = new And(new SameItemFilter(item1), new SameItemFilter( - item1), new SameItemFilter(item2)); - - Assert.assertTrue(filter1.passesFilter(null, item1)); - Assert.assertFalse(filter1.passesFilter(null, item2)); - - Assert.assertFalse(filter2.passesFilter(null, item1)); - Assert.assertFalse(filter2.passesFilter(null, item2)); - } - - public void testNoFilterOr() { - Filter filter = new Or(); - - Assert.assertFalse(filter.passesFilter(null, item1)); - } - - public void testSingleFilterOr() { - Filter filter = new Or(new SameItemFilter(item1)); - - Assert.assertTrue(filter.passesFilter(null, item1)); - Assert.assertFalse(filter.passesFilter(null, item2)); - } - - public void testTwoFilterOr() { - Filter filter1 = new Or(new SameItemFilter(item1), new SameItemFilter( - item1)); - Filter filter2 = new Or(new SameItemFilter(item1), new SameItemFilter( - item2)); - - Assert.assertTrue(filter1.passesFilter(null, item1)); - Assert.assertFalse(filter1.passesFilter(null, item2)); - - Assert.assertTrue(filter2.passesFilter(null, item1)); - Assert.assertTrue(filter2.passesFilter(null, item2)); - } - - public void testThreeFilterOr() { - Filter filter1 = new Or(new SameItemFilter(item1), new SameItemFilter( - item1), new SameItemFilter(item1)); - Filter filter2 = new Or(new SameItemFilter(item1), new SameItemFilter( - item1), new SameItemFilter(item2)); - - Assert.assertTrue(filter1.passesFilter(null, item1)); - Assert.assertFalse(filter1.passesFilter(null, item2)); - - Assert.assertTrue(filter2.passesFilter(null, item1)); - Assert.assertTrue(filter2.passesFilter(null, item2)); - } - - public void testAndEqualsHashCode() { - Filter filter0 = new And(); - Filter filter0b = new And(); - Filter filter1a = new And(new SameItemFilter(item1)); - Filter filter1a2 = new And(new SameItemFilter(item1)); - Filter filter1b = new And(new SameItemFilter(item2)); - Filter filter2a = new And(new SameItemFilter(item1), - new SameItemFilter(item1)); - Filter filter2b = new And(new SameItemFilter(item1), - new SameItemFilter(item2)); - Filter filter2b2 = new And(new SameItemFilter(item1), - new SameItemFilter(item2)); - Filter other0 = new Or(); - Filter other1 = new Or(new SameItemFilter(item1)); - - Assert.assertEquals(filter0, filter0); - Assert.assertEquals(filter0, filter0b); - Assert.assertFalse(filter0.equals(filter1a)); - Assert.assertFalse(filter0.equals(other0)); - Assert.assertFalse(filter0.equals(other1)); - - Assert.assertFalse(filter1a.equals(filter1b)); - Assert.assertFalse(filter1a.equals(other1)); - - Assert.assertFalse(filter1a.equals(filter2a)); - Assert.assertFalse(filter2a.equals(filter1a)); - - Assert.assertFalse(filter2a.equals(filter2b)); - Assert.assertEquals(filter2b, filter2b2); - - // hashCode() - Assert.assertEquals(filter0.hashCode(), filter0.hashCode()); - Assert.assertEquals(filter0.hashCode(), filter0b.hashCode()); - Assert.assertEquals(filter1a.hashCode(), filter1a.hashCode()); - Assert.assertEquals(filter1a.hashCode(), filter1a2.hashCode()); - Assert.assertEquals(filter2a.hashCode(), filter2a.hashCode()); - Assert.assertEquals(filter2b.hashCode(), filter2b2.hashCode()); - } - - public void testOrEqualsHashCode() { - Filter filter0 = new Or(); - Filter filter0b = new Or(); - Filter filter1a = new Or(new SameItemFilter(item1)); - Filter filter1a2 = new Or(new SameItemFilter(item1)); - Filter filter1b = new Or(new SameItemFilter(item2)); - Filter filter2a = new Or(new SameItemFilter(item1), new SameItemFilter( - item1)); - Filter filter2b = new Or(new SameItemFilter(item1), new SameItemFilter( - item2)); - Filter filter2b2 = new Or(new SameItemFilter(item1), - new SameItemFilter(item2)); - Filter other0 = new And(); - Filter other1 = new And(new SameItemFilter(item1)); - - Assert.assertEquals(filter0, filter0); - Assert.assertEquals(filter0, filter0b); - Assert.assertFalse(filter0.equals(filter1a)); - Assert.assertFalse(filter0.equals(other0)); - Assert.assertFalse(filter0.equals(other1)); - - Assert.assertFalse(filter1a.equals(filter1b)); - Assert.assertFalse(filter1a.equals(other1)); - - Assert.assertFalse(filter1a.equals(filter2a)); - Assert.assertFalse(filter2a.equals(filter1a)); - - Assert.assertFalse(filter2a.equals(filter2b)); - Assert.assertEquals(filter2b, filter2b2); - - // hashCode() - Assert.assertEquals(filter0.hashCode(), filter0.hashCode()); - Assert.assertEquals(filter0.hashCode(), filter0b.hashCode()); - Assert.assertEquals(filter1a.hashCode(), filter1a.hashCode()); - Assert.assertEquals(filter1a.hashCode(), filter1a2.hashCode()); - Assert.assertEquals(filter2a.hashCode(), filter2a.hashCode()); - Assert.assertEquals(filter2b.hashCode(), filter2b2.hashCode()); - } - - public void testAndAppliesToProperty() { - Filter filter0 = new And(); - Filter filter1a = new And(new SameItemFilter(item1, "a")); - Filter filter1b = new And(new SameItemFilter(item1, "b")); - Filter filter2aa = new And(new SameItemFilter(item1, "a"), - new SameItemFilter(item1, "a")); - Filter filter2ab = new And(new SameItemFilter(item1, "a"), - new SameItemFilter(item1, "b")); - Filter filter3abc = new And(new SameItemFilter(item1, "a"), - new SameItemFilter(item1, "b"), new SameItemFilter(item1, "c")); - - // empty And does not filter out anything - Assert.assertFalse(filter0.appliesToProperty("a")); - Assert.assertFalse(filter0.appliesToProperty("d")); - - Assert.assertTrue(filter1a.appliesToProperty("a")); - Assert.assertFalse(filter1a.appliesToProperty("b")); - Assert.assertFalse(filter1b.appliesToProperty("a")); - Assert.assertTrue(filter1b.appliesToProperty("b")); - - Assert.assertTrue(filter2aa.appliesToProperty("a")); - Assert.assertFalse(filter2aa.appliesToProperty("b")); - Assert.assertTrue(filter2ab.appliesToProperty("a")); - Assert.assertTrue(filter2ab.appliesToProperty("b")); - - Assert.assertTrue(filter3abc.appliesToProperty("a")); - Assert.assertTrue(filter3abc.appliesToProperty("b")); - Assert.assertTrue(filter3abc.appliesToProperty("c")); - Assert.assertFalse(filter3abc.appliesToProperty("d")); - } - - public void testOrAppliesToProperty() { - Filter filter0 = new Or(); - Filter filter1a = new Or(new SameItemFilter(item1, "a")); - Filter filter1b = new Or(new SameItemFilter(item1, "b")); - Filter filter2aa = new Or(new SameItemFilter(item1, "a"), - new SameItemFilter(item1, "a")); - Filter filter2ab = new Or(new SameItemFilter(item1, "a"), - new SameItemFilter(item1, "b")); - Filter filter3abc = new Or(new SameItemFilter(item1, "a"), - new SameItemFilter(item1, "b"), new SameItemFilter(item1, "c")); - - // empty Or filters out everything - Assert.assertTrue(filter0.appliesToProperty("a")); - Assert.assertTrue(filter0.appliesToProperty("d")); - - Assert.assertTrue(filter1a.appliesToProperty("a")); - Assert.assertFalse(filter1a.appliesToProperty("b")); - Assert.assertFalse(filter1b.appliesToProperty("a")); - Assert.assertTrue(filter1b.appliesToProperty("b")); - - Assert.assertTrue(filter2aa.appliesToProperty("a")); - Assert.assertFalse(filter2aa.appliesToProperty("b")); - Assert.assertTrue(filter2ab.appliesToProperty("a")); - Assert.assertTrue(filter2ab.appliesToProperty("b")); - - Assert.assertTrue(filter3abc.appliesToProperty("a")); - Assert.assertTrue(filter3abc.appliesToProperty("b")); - Assert.assertTrue(filter3abc.appliesToProperty("c")); - Assert.assertFalse(filter3abc.appliesToProperty("d")); - } - -} diff --git a/tests/server-side/com/vaadin/data/util/filter/CompareFilterTest.java b/tests/server-side/com/vaadin/data/util/filter/CompareFilterTest.java deleted file mode 100644 index 99e8429a51..0000000000 --- a/tests/server-side/com/vaadin/data/util/filter/CompareFilterTest.java +++ /dev/null @@ -1,260 +0,0 @@ -package com.vaadin.data.util.filter; - -import java.util.Date; - -import junit.framework.Assert; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.Item; -import com.vaadin.data.util.ObjectProperty; -import com.vaadin.data.util.PropertysetItem; -import com.vaadin.data.util.filter.Compare.Equal; -import com.vaadin.data.util.filter.Compare.Greater; -import com.vaadin.data.util.filter.Compare.GreaterOrEqual; -import com.vaadin.data.util.filter.Compare.Less; -import com.vaadin.data.util.filter.Compare.LessOrEqual; - -public class CompareFilterTest extends AbstractFilterTest<Compare> { - - protected Item itemNull; - protected Item itemEmpty; - protected Item itemA; - protected Item itemB; - protected Item itemC; - - protected final Filter equalB = new Equal(PROPERTY1, "b"); - protected final Filter greaterB = new Greater(PROPERTY1, "b"); - protected final Filter lessB = new Less(PROPERTY1, "b"); - protected final Filter greaterEqualB = new GreaterOrEqual(PROPERTY1, "b"); - protected final Filter lessEqualB = new LessOrEqual(PROPERTY1, "b"); - - protected final Filter equalNull = new Equal(PROPERTY1, null); - protected final Filter greaterNull = new Greater(PROPERTY1, null); - protected final Filter lessNull = new Less(PROPERTY1, null); - protected final Filter greaterEqualNull = new GreaterOrEqual(PROPERTY1, - null); - protected final Filter lessEqualNull = new LessOrEqual(PROPERTY1, null); - - @Override - protected void setUp() throws Exception { - super.setUp(); - itemNull = new PropertysetItem(); - itemNull.addItemProperty(PROPERTY1, new ObjectProperty<String>(null, - String.class)); - itemEmpty = new PropertysetItem(); - itemEmpty.addItemProperty(PROPERTY1, new ObjectProperty<String>("", - String.class)); - itemA = new PropertysetItem(); - itemA.addItemProperty(PROPERTY1, new ObjectProperty<String>("a", - String.class)); - itemB = new PropertysetItem(); - itemB.addItemProperty(PROPERTY1, new ObjectProperty<String>("b", - String.class)); - itemC = new PropertysetItem(); - itemC.addItemProperty(PROPERTY1, new ObjectProperty<String>("c", - String.class)); - } - - @Override - protected void tearDown() throws Exception { - super.tearDown(); - itemNull = null; - itemEmpty = null; - itemA = null; - itemB = null; - } - - public void testCompareString() { - Assert.assertFalse(equalB.passesFilter(null, itemEmpty)); - Assert.assertFalse(equalB.passesFilter(null, itemA)); - Assert.assertTrue(equalB.passesFilter(null, itemB)); - Assert.assertFalse(equalB.passesFilter(null, itemC)); - - Assert.assertFalse(greaterB.passesFilter(null, itemEmpty)); - Assert.assertFalse(greaterB.passesFilter(null, itemA)); - Assert.assertFalse(greaterB.passesFilter(null, itemB)); - Assert.assertTrue(greaterB.passesFilter(null, itemC)); - - Assert.assertTrue(lessB.passesFilter(null, itemEmpty)); - Assert.assertTrue(lessB.passesFilter(null, itemA)); - Assert.assertFalse(lessB.passesFilter(null, itemB)); - Assert.assertFalse(lessB.passesFilter(null, itemC)); - - Assert.assertFalse(greaterEqualB.passesFilter(null, itemEmpty)); - Assert.assertFalse(greaterEqualB.passesFilter(null, itemA)); - Assert.assertTrue(greaterEqualB.passesFilter(null, itemB)); - Assert.assertTrue(greaterEqualB.passesFilter(null, itemC)); - - Assert.assertTrue(lessEqualB.passesFilter(null, itemEmpty)); - Assert.assertTrue(lessEqualB.passesFilter(null, itemA)); - Assert.assertTrue(lessEqualB.passesFilter(null, itemB)); - Assert.assertFalse(lessEqualB.passesFilter(null, itemC)); - } - - public void testCompareWithNull() { - // null comparisons: null is less than any other value - Assert.assertFalse(equalB.passesFilter(null, itemNull)); - Assert.assertTrue(greaterB.passesFilter(null, itemNull)); - Assert.assertFalse(lessB.passesFilter(null, itemNull)); - Assert.assertTrue(greaterEqualB.passesFilter(null, itemNull)); - Assert.assertFalse(lessEqualB.passesFilter(null, itemNull)); - - Assert.assertTrue(equalNull.passesFilter(null, itemNull)); - Assert.assertFalse(greaterNull.passesFilter(null, itemNull)); - Assert.assertFalse(lessNull.passesFilter(null, itemNull)); - Assert.assertTrue(greaterEqualNull.passesFilter(null, itemNull)); - Assert.assertTrue(lessEqualNull.passesFilter(null, itemNull)); - - Assert.assertFalse(equalNull.passesFilter(null, itemA)); - Assert.assertFalse(greaterNull.passesFilter(null, itemA)); - Assert.assertTrue(lessNull.passesFilter(null, itemA)); - Assert.assertFalse(greaterEqualNull.passesFilter(null, itemA)); - Assert.assertTrue(lessEqualNull.passesFilter(null, itemA)); - } - - public void testCompareInteger() { - int negative = -1; - int zero = 0; - int positive = 1; - - Item itemNegative = new PropertysetItem(); - itemNegative.addItemProperty(PROPERTY1, new ObjectProperty<Integer>( - negative, Integer.class)); - Item itemZero = new PropertysetItem(); - itemZero.addItemProperty(PROPERTY1, new ObjectProperty<Integer>(zero, - Integer.class)); - Item itemPositive = new PropertysetItem(); - itemPositive.addItemProperty(PROPERTY1, new ObjectProperty<Integer>( - positive, Integer.class)); - - Filter equalZero = new Equal(PROPERTY1, zero); - Assert.assertFalse(equalZero.passesFilter(null, itemNegative)); - Assert.assertTrue(equalZero.passesFilter(null, itemZero)); - Assert.assertFalse(equalZero.passesFilter(null, itemPositive)); - - Filter isPositive = new Greater(PROPERTY1, zero); - Assert.assertFalse(isPositive.passesFilter(null, itemNegative)); - Assert.assertFalse(isPositive.passesFilter(null, itemZero)); - Assert.assertTrue(isPositive.passesFilter(null, itemPositive)); - - Filter isNegative = new Less(PROPERTY1, zero); - Assert.assertTrue(isNegative.passesFilter(null, itemNegative)); - Assert.assertFalse(isNegative.passesFilter(null, itemZero)); - Assert.assertFalse(isNegative.passesFilter(null, itemPositive)); - - Filter isNonNegative = new GreaterOrEqual(PROPERTY1, zero); - Assert.assertFalse(isNonNegative.passesFilter(null, itemNegative)); - Assert.assertTrue(isNonNegative.passesFilter(null, itemZero)); - Assert.assertTrue(isNonNegative.passesFilter(null, itemPositive)); - - Filter isNonPositive = new LessOrEqual(PROPERTY1, zero); - Assert.assertTrue(isNonPositive.passesFilter(null, itemNegative)); - Assert.assertTrue(isNonPositive.passesFilter(null, itemZero)); - Assert.assertFalse(isNonPositive.passesFilter(null, itemPositive)); - } - - public void testCompareDate() { - Date now = new Date(); - // new Date() is only accurate to the millisecond, so repeating it gives - // the same date - Date earlier = new Date(now.getTime() - 1); - Date later = new Date(now.getTime() + 1); - - Item itemEarlier = new PropertysetItem(); - itemEarlier.addItemProperty(PROPERTY1, new ObjectProperty<Date>( - earlier, Date.class)); - Item itemNow = new PropertysetItem(); - itemNow.addItemProperty(PROPERTY1, new ObjectProperty<Date>(now, - Date.class)); - Item itemLater = new PropertysetItem(); - itemLater.addItemProperty(PROPERTY1, new ObjectProperty<Date>(later, - Date.class)); - - Filter equalNow = new Equal(PROPERTY1, now); - Assert.assertFalse(equalNow.passesFilter(null, itemEarlier)); - Assert.assertTrue(equalNow.passesFilter(null, itemNow)); - Assert.assertFalse(equalNow.passesFilter(null, itemLater)); - - Filter after = new Greater(PROPERTY1, now); - Assert.assertFalse(after.passesFilter(null, itemEarlier)); - Assert.assertFalse(after.passesFilter(null, itemNow)); - Assert.assertTrue(after.passesFilter(null, itemLater)); - - Filter before = new Less(PROPERTY1, now); - Assert.assertTrue(before.passesFilter(null, itemEarlier)); - Assert.assertFalse(before.passesFilter(null, itemNow)); - Assert.assertFalse(before.passesFilter(null, itemLater)); - - Filter afterOrNow = new GreaterOrEqual(PROPERTY1, now); - Assert.assertFalse(afterOrNow.passesFilter(null, itemEarlier)); - Assert.assertTrue(afterOrNow.passesFilter(null, itemNow)); - Assert.assertTrue(afterOrNow.passesFilter(null, itemLater)); - - Filter beforeOrNow = new LessOrEqual(PROPERTY1, now); - Assert.assertTrue(beforeOrNow.passesFilter(null, itemEarlier)); - Assert.assertTrue(beforeOrNow.passesFilter(null, itemNow)); - Assert.assertFalse(beforeOrNow.passesFilter(null, itemLater)); - } - - public void testCompareAppliesToProperty() { - Filter filterA = new Equal("a", 1); - Filter filterB = new Equal("b", 1); - - Assert.assertTrue(filterA.appliesToProperty("a")); - Assert.assertFalse(filterA.appliesToProperty("b")); - Assert.assertFalse(filterB.appliesToProperty("a")); - Assert.assertTrue(filterB.appliesToProperty("b")); - } - - public void testCompareEqualsHashCode() { - // most checks with Equal filter, then only some with others - Filter equalNull2 = new Equal(PROPERTY1, null); - Filter equalNullProperty2 = new Equal(PROPERTY2, null); - Filter equalEmpty = new Equal(PROPERTY1, ""); - Filter equalEmpty2 = new Equal(PROPERTY1, ""); - Filter equalEmptyProperty2 = new Equal(PROPERTY2, ""); - Filter equalA = new Equal(PROPERTY1, "a"); - Filter equalB2 = new Equal(PROPERTY1, "b"); - Filter equalBProperty2 = new Equal(PROPERTY2, "b"); - - Filter greaterEmpty = new Greater(PROPERTY1, ""); - - // equals() - Assert.assertEquals(equalNull, equalNull); - Assert.assertEquals(equalNull, equalNull2); - Assert.assertFalse(equalNull.equals(equalNullProperty2)); - Assert.assertFalse(equalNull.equals(equalEmpty)); - Assert.assertFalse(equalNull.equals(equalB)); - - Assert.assertEquals(equalEmpty, equalEmpty); - Assert.assertFalse(equalEmpty.equals(equalNull)); - Assert.assertEquals(equalEmpty, equalEmpty2); - Assert.assertFalse(equalEmpty.equals(equalEmptyProperty2)); - Assert.assertFalse(equalEmpty.equals(equalB)); - - Assert.assertEquals(equalB, equalB); - Assert.assertFalse(equalB.equals(equalNull)); - Assert.assertFalse(equalB.equals(equalEmpty)); - Assert.assertEquals(equalB, equalB2); - Assert.assertFalse(equalB.equals(equalBProperty2)); - Assert.assertFalse(equalB.equals(equalA)); - - Assert.assertEquals(greaterB, greaterB); - Assert.assertFalse(greaterB.equals(lessB)); - Assert.assertFalse(greaterB.equals(greaterEqualB)); - Assert.assertFalse(greaterB.equals(lessEqualB)); - - Assert.assertFalse(greaterNull.equals(greaterEmpty)); - Assert.assertFalse(greaterNull.equals(greaterB)); - Assert.assertFalse(greaterEmpty.equals(greaterNull)); - Assert.assertFalse(greaterEmpty.equals(greaterB)); - Assert.assertFalse(greaterB.equals(greaterNull)); - Assert.assertFalse(greaterB.equals(greaterEmpty)); - - // hashCode() - Assert.assertEquals(equalNull.hashCode(), equalNull2.hashCode()); - Assert.assertEquals(equalEmpty.hashCode(), equalEmpty2.hashCode()); - Assert.assertEquals(equalB.hashCode(), equalB2.hashCode()); - } - -} diff --git a/tests/server-side/com/vaadin/data/util/filter/IsNullFilterTest.java b/tests/server-side/com/vaadin/data/util/filter/IsNullFilterTest.java deleted file mode 100644 index 6f90273de1..0000000000 --- a/tests/server-side/com/vaadin/data/util/filter/IsNullFilterTest.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.vaadin.data.util.filter; - -import junit.framework.Assert; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.Item; -import com.vaadin.data.util.ObjectProperty; -import com.vaadin.data.util.PropertysetItem; - -public class IsNullFilterTest extends AbstractFilterTest<IsNull> { - - public void testIsNull() { - Item item1 = new PropertysetItem(); - item1.addItemProperty("a", new ObjectProperty<String>(null, - String.class)); - item1.addItemProperty("b", - new ObjectProperty<String>("b", String.class)); - Item item2 = new PropertysetItem(); - item2.addItemProperty("a", - new ObjectProperty<String>("a", String.class)); - item2.addItemProperty("b", new ObjectProperty<String>(null, - String.class)); - - Filter filter1 = new IsNull("a"); - Filter filter2 = new IsNull("b"); - - Assert.assertTrue(filter1.passesFilter(null, item1)); - Assert.assertFalse(filter1.passesFilter(null, item2)); - Assert.assertFalse(filter2.passesFilter(null, item1)); - Assert.assertTrue(filter2.passesFilter(null, item2)); - } - - public void testIsNullAppliesToProperty() { - Filter filterA = new IsNull("a"); - Filter filterB = new IsNull("b"); - - Assert.assertTrue(filterA.appliesToProperty("a")); - Assert.assertFalse(filterA.appliesToProperty("b")); - Assert.assertFalse(filterB.appliesToProperty("a")); - Assert.assertTrue(filterB.appliesToProperty("b")); - } - - public void testIsNullEqualsHashCode() { - Filter filter1 = new IsNull("a"); - Filter filter1b = new IsNull("a"); - Filter filter2 = new IsNull("b"); - - // equals() - Assert.assertEquals(filter1, filter1b); - Assert.assertFalse(filter1.equals(filter2)); - Assert.assertFalse(filter1.equals(new And())); - - // hashCode() - Assert.assertEquals(filter1.hashCode(), filter1b.hashCode()); - } - -} diff --git a/tests/server-side/com/vaadin/data/util/filter/NotFilterTest.java b/tests/server-side/com/vaadin/data/util/filter/NotFilterTest.java deleted file mode 100644 index c3b666e6f7..0000000000 --- a/tests/server-side/com/vaadin/data/util/filter/NotFilterTest.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.vaadin.data.util.filter; - -import junit.framework.Assert; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.Item; -import com.vaadin.data.util.BeanItem; - -public class NotFilterTest extends AbstractFilterTest<Not> { - - protected Item item1 = new BeanItem<Integer>(1); - protected Item item2 = new BeanItem<Integer>(2); - - public void testNot() { - Filter origFilter = new SameItemFilter(item1); - Filter filter = new Not(origFilter); - - Assert.assertTrue(origFilter.passesFilter(null, item1)); - Assert.assertFalse(origFilter.passesFilter(null, item2)); - Assert.assertFalse(filter.passesFilter(null, item1)); - Assert.assertTrue(filter.passesFilter(null, item2)); - } - - public void testANotAppliesToProperty() { - Filter filterA = new Not(new SameItemFilter(item1, "a")); - Filter filterB = new Not(new SameItemFilter(item1, "b")); - - Assert.assertTrue(filterA.appliesToProperty("a")); - Assert.assertFalse(filterA.appliesToProperty("b")); - Assert.assertFalse(filterB.appliesToProperty("a")); - Assert.assertTrue(filterB.appliesToProperty("b")); - } - - public void testNotEqualsHashCode() { - Filter origFilter = new SameItemFilter(item1); - Filter filter1 = new Not(origFilter); - Filter filter1b = new Not(new SameItemFilter(item1)); - Filter filter2 = new Not(new SameItemFilter(item2)); - - // equals() - Assert.assertEquals(filter1, filter1b); - Assert.assertFalse(filter1.equals(filter2)); - Assert.assertFalse(filter1.equals(origFilter)); - Assert.assertFalse(filter1.equals(new And())); - - // hashCode() - Assert.assertEquals(filter1.hashCode(), filter1b.hashCode()); - } - -} diff --git a/tests/server-side/com/vaadin/data/util/filter/SimpleStringFilterTest.java b/tests/server-side/com/vaadin/data/util/filter/SimpleStringFilterTest.java deleted file mode 100644 index bc63d57752..0000000000 --- a/tests/server-side/com/vaadin/data/util/filter/SimpleStringFilterTest.java +++ /dev/null @@ -1,130 +0,0 @@ -package com.vaadin.data.util.filter; - -import junit.framework.Assert; - -public class SimpleStringFilterTest extends - AbstractFilterTest<SimpleStringFilter> { - - protected static TestItem<String, String> createTestItem() { - return new TestItem<String, String>("abcde", "TeSt"); - } - - protected TestItem<String, String> getTestItem() { - return createTestItem(); - } - - protected SimpleStringFilter f(Object propertyId, String filterString, - boolean ignoreCase, boolean onlyMatchPrefix) { - return new SimpleStringFilter(propertyId, filterString, ignoreCase, - onlyMatchPrefix); - } - - protected boolean passes(Object propertyId, String filterString, - boolean ignoreCase, boolean onlyMatchPrefix) { - return f(propertyId, filterString, ignoreCase, onlyMatchPrefix) - .passesFilter(null, getTestItem()); - } - - public void testStartsWithCaseSensitive() { - Assert.assertTrue(passes(PROPERTY1, "ab", false, true)); - Assert.assertTrue(passes(PROPERTY1, "", false, true)); - - Assert.assertFalse(passes(PROPERTY2, "ab", false, true)); - Assert.assertFalse(passes(PROPERTY1, "AB", false, true)); - } - - public void testStartsWithCaseInsensitive() { - Assert.assertTrue(passes(PROPERTY1, "AB", true, true)); - Assert.assertTrue(passes(PROPERTY2, "te", true, true)); - Assert.assertFalse(passes(PROPERTY2, "AB", true, true)); - } - - public void testContainsCaseSensitive() { - Assert.assertTrue(passes(PROPERTY1, "ab", false, false)); - Assert.assertTrue(passes(PROPERTY1, "abcde", false, false)); - Assert.assertTrue(passes(PROPERTY1, "cd", false, false)); - Assert.assertTrue(passes(PROPERTY1, "e", false, false)); - Assert.assertTrue(passes(PROPERTY1, "", false, false)); - - Assert.assertFalse(passes(PROPERTY2, "ab", false, false)); - Assert.assertFalse(passes(PROPERTY1, "es", false, false)); - } - - public void testContainsCaseInsensitive() { - Assert.assertTrue(passes(PROPERTY1, "AB", true, false)); - Assert.assertTrue(passes(PROPERTY1, "aBcDe", true, false)); - Assert.assertTrue(passes(PROPERTY1, "CD", true, false)); - Assert.assertTrue(passes(PROPERTY1, "", true, false)); - - Assert.assertTrue(passes(PROPERTY2, "es", true, false)); - - Assert.assertFalse(passes(PROPERTY2, "ab", true, false)); - } - - public void testAppliesToProperty() { - SimpleStringFilter filter = f(PROPERTY1, "ab", false, true); - Assert.assertTrue(filter.appliesToProperty(PROPERTY1)); - Assert.assertFalse(filter.appliesToProperty(PROPERTY2)); - Assert.assertFalse(filter.appliesToProperty("other")); - } - - public void testEqualsHashCode() { - SimpleStringFilter filter = f(PROPERTY1, "ab", false, true); - - SimpleStringFilter f1 = f(PROPERTY2, "ab", false, true); - SimpleStringFilter f1b = f(PROPERTY2, "ab", false, true); - SimpleStringFilter f2 = f(PROPERTY1, "cd", false, true); - SimpleStringFilter f2b = f(PROPERTY1, "cd", false, true); - SimpleStringFilter f3 = f(PROPERTY1, "ab", true, true); - SimpleStringFilter f3b = f(PROPERTY1, "ab", true, true); - SimpleStringFilter f4 = f(PROPERTY1, "ab", false, false); - SimpleStringFilter f4b = f(PROPERTY1, "ab", false, false); - - // equal but not same instance - Assert.assertEquals(f1, f1b); - Assert.assertEquals(f2, f2b); - Assert.assertEquals(f3, f3b); - Assert.assertEquals(f4, f4b); - - // more than one property differ - Assert.assertFalse(f1.equals(f2)); - Assert.assertFalse(f1.equals(f3)); - Assert.assertFalse(f1.equals(f4)); - Assert.assertFalse(f2.equals(f1)); - Assert.assertFalse(f2.equals(f3)); - Assert.assertFalse(f2.equals(f4)); - Assert.assertFalse(f3.equals(f1)); - Assert.assertFalse(f3.equals(f2)); - Assert.assertFalse(f3.equals(f4)); - Assert.assertFalse(f4.equals(f1)); - Assert.assertFalse(f4.equals(f2)); - Assert.assertFalse(f4.equals(f3)); - - // only one property differs - Assert.assertFalse(filter.equals(f1)); - Assert.assertFalse(filter.equals(f2)); - Assert.assertFalse(filter.equals(f3)); - Assert.assertFalse(filter.equals(f4)); - - Assert.assertFalse(f1.equals(null)); - Assert.assertFalse(f1.equals(new Object())); - - Assert.assertEquals(f1.hashCode(), f1b.hashCode()); - Assert.assertEquals(f2.hashCode(), f2b.hashCode()); - Assert.assertEquals(f3.hashCode(), f3b.hashCode()); - Assert.assertEquals(f4.hashCode(), f4b.hashCode()); - } - - public void testNonExistentProperty() { - Assert.assertFalse(passes("other1", "ab", false, true)); - } - - public void testNullValueForProperty() { - TestItem<String, String> item = createTestItem(); - item.addItemProperty("other1", new NullProperty()); - - Assert.assertFalse(f("other1", "ab", false, true).passesFilter(null, - item)); - } - -} diff --git a/tests/server-side/com/vaadin/data/util/sqlcontainer/AllTests.java b/tests/server-side/com/vaadin/data/util/sqlcontainer/AllTests.java deleted file mode 100644 index 057527307f..0000000000 --- a/tests/server-side/com/vaadin/data/util/sqlcontainer/AllTests.java +++ /dev/null @@ -1,145 +0,0 @@ -package com.vaadin.data.util.sqlcontainer; - -import org.junit.runner.RunWith; -import org.junit.runners.Suite; -import org.junit.runners.Suite.SuiteClasses; - -import com.vaadin.data.util.sqlcontainer.connection.J2EEConnectionPoolTest; -import com.vaadin.data.util.sqlcontainer.connection.SimpleJDBCConnectionPoolTest; -import com.vaadin.data.util.sqlcontainer.filters.BetweenTest; -import com.vaadin.data.util.sqlcontainer.filters.LikeTest; -import com.vaadin.data.util.sqlcontainer.generator.SQLGeneratorsTest; -import com.vaadin.data.util.sqlcontainer.query.FreeformQueryTest; -import com.vaadin.data.util.sqlcontainer.query.QueryBuilderTest; -import com.vaadin.data.util.sqlcontainer.query.TableQueryTest; -import com.vaadin.data.util.sqlcontainer.query.generator.DefaultSQLGenerator; -import com.vaadin.data.util.sqlcontainer.query.generator.MSSQLGenerator; -import com.vaadin.data.util.sqlcontainer.query.generator.OracleGenerator; -import com.vaadin.data.util.sqlcontainer.query.generator.SQLGenerator; - -@RunWith(Suite.class) -@SuiteClasses({ SimpleJDBCConnectionPoolTest.class, - J2EEConnectionPoolTest.class, LikeTest.class, QueryBuilderTest.class, - FreeformQueryTest.class, RowIdTest.class, SQLContainerTest.class, - SQLContainerTableQueryTest.class, ColumnPropertyTest.class, - TableQueryTest.class, SQLGeneratorsTest.class, UtilTest.class, - TicketTests.class, BetweenTest.class, ReadOnlyRowIdTest.class }) -public class AllTests { - /* Set the DB used for testing here! */ - public enum DB { - HSQLDB, MYSQL, POSTGRESQL, MSSQL, ORACLE; - } - - /* 0 = HSQLDB, 1 = MYSQL, 2 = POSTGRESQL, 3 = MSSQL, 4 = ORACLE */ - public static final DB db = DB.HSQLDB; - - /* Auto-increment column offset (HSQLDB = 0, MYSQL = 1, POSTGRES = 1) */ - public static int offset; - /* Garbage table creation query (=three queries for oracle) */ - public static String createGarbage; - public static String createGarbageSecond; - public static String createGarbageThird; - /* DB Drivers, urls, usernames and passwords */ - public static String dbDriver; - public static String dbURL; - public static String dbUser; - public static String dbPwd; - /* People -test table creation statement(s) */ - public static String peopleFirst; - public static String peopleSecond; - public static String peopleThird; - /* Versioned -test table createion statement(s) */ - public static String[] versionStatements; - /* SQL Generator used during the testing */ - public static SQLGenerator sqlGen; - - /* Set DB-specific settings based on selected DB */ - static { - sqlGen = new DefaultSQLGenerator(); - switch (db) { - case HSQLDB: - offset = 0; - createGarbage = "create table garbage (id integer generated always as identity, type varchar(32), PRIMARY KEY(id))"; - dbDriver = "org.hsqldb.jdbc.JDBCDriver"; - dbURL = "jdbc:hsqldb:mem:sqlcontainer"; - dbUser = "SA"; - dbPwd = ""; - peopleFirst = "create table people (id integer generated always as identity, name varchar(32), AGE INTEGER)"; - peopleSecond = "alter table people add primary key (id)"; - versionStatements = new String[] { - "create table versioned (id integer generated always as identity, text varchar(255), version tinyint default 0)", - "alter table versioned add primary key (id)" }; - break; - case MYSQL: - offset = 1; - createGarbage = "create table GARBAGE (ID integer auto_increment, type varchar(32), PRIMARY KEY(ID))"; - dbDriver = "com.mysql.jdbc.Driver"; - dbURL = "jdbc:mysql:///sqlcontainer"; - dbUser = "sqlcontainer"; - dbPwd = "sqlcontainer"; - peopleFirst = "create table PEOPLE (ID integer auto_increment not null, NAME varchar(32), AGE INTEGER, primary key(ID))"; - peopleSecond = null; - versionStatements = new String[] { - "create table VERSIONED (ID integer auto_increment not null, TEXT varchar(255), VERSION tinyint default 0, primary key(ID))", - "CREATE TRIGGER upd_version BEFORE UPDATE ON VERSIONED" - + " FOR EACH ROW SET NEW.VERSION = OLD.VERSION+1" }; - break; - case POSTGRESQL: - offset = 1; - createGarbage = "create table GARBAGE (\"ID\" serial PRIMARY KEY, \"TYPE\" varchar(32))"; - dbDriver = "org.postgresql.Driver"; - dbURL = "jdbc:postgresql://localhost:5432/test"; - dbUser = "postgres"; - dbPwd = "postgres"; - peopleFirst = "create table PEOPLE (\"ID\" serial primary key, \"NAME\" VARCHAR(32), \"AGE\" INTEGER)"; - peopleSecond = null; - versionStatements = new String[] { - "create table VERSIONED (\"ID\" serial primary key, \"TEXT\" VARCHAR(255), \"VERSION\" INTEGER DEFAULT 0)", - "CREATE OR REPLACE FUNCTION zz_row_version() RETURNS TRIGGER AS $$" - + "BEGIN" - + " IF TG_OP = 'UPDATE'" - + " AND NEW.\"VERSION\" = old.\"VERSION\"" - + " AND ROW(NEW.*) IS DISTINCT FROM ROW (old.*)" - + " THEN" - + " NEW.\"VERSION\" := NEW.\"VERSION\" + 1;" - + " END IF;" + " RETURN NEW;" + "END;" - + "$$ LANGUAGE plpgsql;", - "CREATE TRIGGER \"mytable_modify_dt_tr\" BEFORE UPDATE" - + " ON VERSIONED FOR EACH ROW" - + " EXECUTE PROCEDURE \"public\".\"zz_row_version\"();" }; - break; - case MSSQL: - offset = 1; - createGarbage = "create table GARBAGE (\"ID\" int identity(1,1) primary key, \"TYPE\" varchar(32))"; - dbDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; - dbURL = "jdbc:sqlserver://localhost:1433;databaseName=tempdb;"; - dbUser = "sa"; - dbPwd = "sa"; - peopleFirst = "create table PEOPLE (\"ID\" int identity(1,1) primary key, \"NAME\" VARCHAR(32), \"AGE\" INTEGER)"; - peopleSecond = null; - versionStatements = new String[] { "create table VERSIONED (\"ID\" int identity(1,1) primary key, \"TEXT\" VARCHAR(255), \"VERSION\" rowversion not null)" }; - sqlGen = new MSSQLGenerator(); - break; - case ORACLE: - offset = 1; - createGarbage = "create table GARBAGE (\"ID\" integer primary key, \"TYPE\" varchar2(32))"; - createGarbageSecond = "create sequence garbage_seq start with 1 increment by 1 nomaxvalue"; - createGarbageThird = "create trigger garbage_trigger before insert on GARBAGE for each row begin select garbage_seq.nextval into :new.ID from dual; end;"; - dbDriver = "oracle.jdbc.OracleDriver"; - dbURL = "jdbc:oracle:thin:test/test@localhost:1521:XE"; - dbUser = "test"; - dbPwd = "test"; - peopleFirst = "create table PEOPLE (\"ID\" integer primary key, \"NAME\" VARCHAR2(32), \"AGE\" INTEGER)"; - peopleSecond = "create sequence people_seq start with 1 increment by 1 nomaxvalue"; - peopleThird = "create trigger people_trigger before insert on PEOPLE for each row begin select people_seq.nextval into :new.ID from dual; end;"; - versionStatements = new String[] { - "create table VERSIONED (\"ID\" integer primary key, \"TEXT\" VARCHAR(255), \"VERSION\" INTEGER DEFAULT 0)", - "create sequence versioned_seq start with 1 increment by 1 nomaxvalue", - "create trigger versioned_trigger before insert on VERSIONED for each row begin select versioned_seq.nextval into :new.ID from dual; end;", - "create sequence versioned_version start with 1 increment by 1 nomaxvalue", - "create trigger versioned_version_trigger before insert or update on VERSIONED for each row begin select versioned_version.nextval into :new.VERSION from dual; end;" }; - sqlGen = new OracleGenerator(); - break; - } - } -} diff --git a/tests/server-side/com/vaadin/data/util/sqlcontainer/ColumnPropertyTest.java b/tests/server-side/com/vaadin/data/util/sqlcontainer/ColumnPropertyTest.java deleted file mode 100644 index 09f620cc2a..0000000000 --- a/tests/server-side/com/vaadin/data/util/sqlcontainer/ColumnPropertyTest.java +++ /dev/null @@ -1,315 +0,0 @@ -package com.vaadin.data.util.sqlcontainer; - -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.Arrays; - -import org.easymock.EasyMock; -import org.junit.Assert; -import org.junit.Test; - -import com.vaadin.data.Property.ReadOnlyException; -import com.vaadin.data.util.sqlcontainer.ColumnProperty.NotNullableException; -import com.vaadin.data.util.sqlcontainer.query.QueryDelegate; - -public class ColumnPropertyTest { - - @Test - public void constructor_legalParameters_shouldSucceed() { - ColumnProperty cp = new ColumnProperty("NAME", false, true, true, - false, "Ville", String.class); - Assert.assertNotNull(cp); - } - - @Test(expected = IllegalArgumentException.class) - public void constructor_missingPropertyId_shouldFail() { - new ColumnProperty(null, false, true, true, false, "Ville", - String.class); - } - - @Test(expected = IllegalArgumentException.class) - public void constructor_missingType_shouldFail() { - new ColumnProperty("NAME", false, true, true, false, "Ville", null); - } - - @Test - public void getValue_defaultValue_returnsVille() { - ColumnProperty cp = new ColumnProperty("NAME", false, true, true, - false, "Ville", String.class); - Assert.assertEquals("Ville", cp.getValue()); - } - - @Test - public void setValue_readWriteNullable_returnsKalle() { - ColumnProperty cp = new ColumnProperty("NAME", false, true, true, - false, "Ville", String.class); - SQLContainer container = EasyMock.createMock(SQLContainer.class); - RowItem owner = new RowItem(container, new RowId(new Object[] { 1 }), - Arrays.asList(cp)); - container.itemChangeNotification(owner); - EasyMock.replay(container); - cp.setValue("Kalle"); - Assert.assertEquals("Kalle", cp.getValue()); - EasyMock.verify(container); - } - - @Test(expected = ReadOnlyException.class) - public void setValue_readOnlyNullable_shouldFail() { - ColumnProperty cp = new ColumnProperty("NAME", true, true, true, - false, "Ville", String.class); - SQLContainer container = EasyMock.createMock(SQLContainer.class); - new RowItem(container, new RowId(new Object[] { 1 }), Arrays.asList(cp)); - EasyMock.replay(container); - cp.setValue("Kalle"); - EasyMock.verify(container); - } - - @Test - public void setValue_readWriteNullable_nullShouldWork() { - ColumnProperty cp = new ColumnProperty("NAME", false, true, true, - false, "Ville", String.class); - SQLContainer container = EasyMock.createMock(SQLContainer.class); - RowItem owner = new RowItem(container, new RowId(new Object[] { 1 }), - Arrays.asList(cp)); - container.itemChangeNotification(owner); - EasyMock.replay(container); - cp.setValue(null); - Assert.assertNull(cp.getValue()); - EasyMock.verify(container); - } - - @Test(expected = NotNullableException.class) - public void setValue_readWriteNotNullable_nullShouldFail() { - ColumnProperty cp = new ColumnProperty("NAME", false, true, false, - false, "Ville", String.class); - SQLContainer container = EasyMock.createMock(SQLContainer.class); - RowItem owner = new RowItem(container, new RowId(new Object[] { 1 }), - Arrays.asList(cp)); - container.itemChangeNotification(owner); - EasyMock.replay(container); - cp.setValue(null); - Assert.assertNotNull(cp.getValue()); - EasyMock.verify(container); - } - - @Test - public void getType_normal_returnsStringClass() { - ColumnProperty cp = new ColumnProperty("NAME", false, true, true, - false, "Ville", String.class); - Assert.assertSame(String.class, cp.getType()); - } - - @Test - public void isReadOnly_readWriteNullable_returnsTrue() { - ColumnProperty cp = new ColumnProperty("NAME", false, true, true, - false, "Ville", String.class); - Assert.assertFalse(cp.isReadOnly()); - } - - @Test - public void isReadOnly_readOnlyNullable_returnsTrue() { - ColumnProperty cp = new ColumnProperty("NAME", true, true, true, - false, "Ville", String.class); - Assert.assertTrue(cp.isReadOnly()); - } - - @Test - public void setReadOnly_readOnlyChangeAllowed_shouldSucceed() { - ColumnProperty cp = new ColumnProperty("NAME", false, true, true, - false, "Ville", String.class); - cp.setReadOnly(true); - Assert.assertTrue(cp.isReadOnly()); - } - - @Test - public void setReadOnly_readOnlyChangeDisallowed_shouldFail() { - ColumnProperty cp = new ColumnProperty("NAME", false, false, true, - false, "Ville", String.class); - cp.setReadOnly(true); - Assert.assertFalse(cp.isReadOnly()); - } - - @Test - public void getPropertyId_normal_returnsNAME() { - ColumnProperty cp = new ColumnProperty("NAME", false, false, true, - false, "Ville", String.class); - Assert.assertEquals("NAME", cp.getPropertyId()); - } - - @Test - public void isModified_valueModified_returnsTrue() { - ColumnProperty cp = new ColumnProperty("NAME", false, true, true, - false, "Ville", String.class); - SQLContainer container = EasyMock.createMock(SQLContainer.class); - RowItem owner = new RowItem(container, new RowId(new Object[] { 1 }), - Arrays.asList(cp)); - container.itemChangeNotification(owner); - EasyMock.replay(container); - cp.setValue("Kalle"); - Assert.assertEquals("Kalle", cp.getValue()); - Assert.assertTrue(cp.isModified()); - EasyMock.verify(container); - } - - @Test - public void isModified_valueNotModified_returnsFalse() { - ColumnProperty cp = new ColumnProperty("NAME", false, false, true, - false, "Ville", String.class); - Assert.assertFalse(cp.isModified()); - } - - @Test - public void setValue_nullOnNullable_shouldWork() { - ColumnProperty cp = new ColumnProperty("NAME", false, true, true, - false, "asdf", String.class); - SQLContainer container = EasyMock.createMock(SQLContainer.class); - new RowItem(container, new RowId(new Object[] { 1 }), Arrays.asList(cp)); - cp.setValue(null); - Assert.assertNull(cp.getValue()); - } - - @Test - public void setValue_resetTonullOnNullable_shouldWork() { - ColumnProperty cp = new ColumnProperty("NAME", false, true, true, false, - null, String.class); - SQLContainer container = EasyMock.createMock(SQLContainer.class); - new RowItem(container, new RowId(new Object[] { 1 }), Arrays.asList(cp)); - cp.setValue("asdf"); - Assert.assertEquals("asdf", cp.getValue()); - cp.setValue(null); - Assert.assertNull(cp.getValue()); - } - - @Test - public void setValue_sendsItemChangeNotification() throws SQLException { - - class TestContainer extends SQLContainer { - Object value = null; - boolean modified = false; - - public TestContainer(QueryDelegate delegate) throws SQLException { - super(delegate); - } - - @Override - public void itemChangeNotification(RowItem changedItem) { - ColumnProperty cp = (ColumnProperty) changedItem - .getItemProperty("NAME"); - value = cp.getValue(); - modified = cp.isModified(); - } - } - - ColumnProperty property = new ColumnProperty("NAME", false, true, true, - false, "Ville", String.class); - - Statement statement = EasyMock.createNiceMock(Statement.class); - EasyMock.replay(statement); - - ResultSetMetaData metadata = EasyMock - .createNiceMock(ResultSetMetaData.class); - EasyMock.replay(metadata); - - ResultSet resultSet = EasyMock.createNiceMock(ResultSet.class); - EasyMock.expect(resultSet.getStatement()).andReturn(statement); - EasyMock.expect(resultSet.getMetaData()).andReturn(metadata); - EasyMock.replay(resultSet); - - QueryDelegate delegate = EasyMock.createNiceMock(QueryDelegate.class); - EasyMock.expect(delegate.getResults(0, 1)).andReturn(resultSet); - EasyMock.replay(delegate); - - TestContainer container = new TestContainer(delegate); - - new RowItem(container, new RowId(new Object[] { 1 }), - Arrays.asList(property)); - - property.setValue("Kalle"); - Assert.assertEquals("Kalle", container.value); - Assert.assertTrue(container.modified); - } - - @Test - public void versionColumnsShouldNotBeInValueMap_shouldReturnFalse() { - ColumnProperty property = new ColumnProperty("NAME", false, true, true, - false, "Ville", String.class); - property.setVersionColumn(true); - - Assert.assertFalse(property.isPersistent()); - } - - @Test - public void neverWritableColumnsShouldNotBeInValueMap_shouldReturnFalse() { - ColumnProperty property = new ColumnProperty("NAME", true, false, true, - false, "Ville", String.class); - - Assert.assertFalse(property.isPersistent()); - } - - @Test - public void writableColumnsShouldBeInValueMap_shouldReturnTrue() { - ColumnProperty property = new ColumnProperty("NAME", false, true, true, - false, "Ville", String.class); - - Assert.assertTrue(property.isPersistent()); - } - - @Test - public void writableButReadOnlyColumnsShouldNotBeInValueMap_shouldReturnFalse() { - ColumnProperty property = new ColumnProperty("NAME", true, true, true, - false, "Ville", String.class); - - Assert.assertFalse(property.isPersistent()); - } - - @Test - public void primKeysShouldBeRowIdentifiers_shouldReturnTrue() { - ColumnProperty property = new ColumnProperty("NAME", false, true, true, - true, "Ville", String.class); - - Assert.assertTrue(property.isRowIdentifier()); - } - - @Test - public void versionColumnsShouldBeRowIdentifiers_shouldReturnTrue() { - ColumnProperty property = new ColumnProperty("NAME", false, true, true, - false, "Ville", String.class); - property.setVersionColumn(true); - - Assert.assertTrue(property.isRowIdentifier()); - } - - @Test - public void nonPrimKeyOrVersionColumnsShouldBeNotRowIdentifiers_shouldReturnFalse() { - ColumnProperty property = new ColumnProperty("NAME", false, true, true, - false, "Ville", String.class); - - Assert.assertFalse(property.isRowIdentifier()); - } - - @Test - public void getOldValueShouldReturnPreviousValue_shouldReturnVille() { - ColumnProperty property = new ColumnProperty("NAME", false, true, true, - false, "Ville", String.class); - - // Here we really don't care about the container management, but in - // order to set the value for a column the owner (RowItem) must be set - // and to create the owner we must have a container... - ArrayList<ColumnProperty> properties = new ArrayList<ColumnProperty>(); - properties.add(property); - - SQLContainer container = EasyMock.createNiceMock(SQLContainer.class); - RowItem rowItem = new RowItem(container, new RowId(new Object[] { 1 }), - Arrays.asList(property)); - - property.setValue("Kalle"); - // Just check that the new value was actually set... - Assert.assertEquals("Kalle", property.getValue()); - // Assert that old value is the original value... - Assert.assertEquals("Ville", property.getOldValue()); - } - -} diff --git a/tests/server-side/com/vaadin/data/util/sqlcontainer/DataGenerator.java b/tests/server-side/com/vaadin/data/util/sqlcontainer/DataGenerator.java deleted file mode 100644 index 489f780d61..0000000000 --- a/tests/server-side/com/vaadin/data/util/sqlcontainer/DataGenerator.java +++ /dev/null @@ -1,139 +0,0 @@ -package com.vaadin.data.util.sqlcontainer; - -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; - -import org.junit.Assert; -import org.junit.Test; - -import com.vaadin.data.util.sqlcontainer.AllTests.DB; -import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool; - -public class DataGenerator { - - @Test - public void testDummy() { - // Added dummy test so JUnit will not complain about - // "No runnable methods". - } - - public static void addPeopleToDatabase(JDBCConnectionPool connectionPool) - throws SQLException { - Connection conn = connectionPool.reserveConnection(); - Statement statement = conn.createStatement(); - try { - statement.execute("drop table PEOPLE"); - if (AllTests.db == DB.ORACLE) { - statement.execute("drop sequence people_seq"); - } - } catch (SQLException e) { - // Will fail if table doesn't exist, which is OK. - conn.rollback(); - } - statement.execute(AllTests.peopleFirst); - if (AllTests.peopleSecond != null) { - statement.execute(AllTests.peopleSecond); - } - if (AllTests.db == DB.ORACLE) { - statement.execute(AllTests.peopleThird); - } - if (AllTests.db == DB.MSSQL) { - statement.executeUpdate("insert into people values('Ville', '23')"); - statement.executeUpdate("insert into people values('Kalle', '7')"); - statement.executeUpdate("insert into people values('Pelle', '18')"); - statement.executeUpdate("insert into people values('Börje', '64')"); - } else { - statement - .executeUpdate("insert into people values(default, 'Ville', '23')"); - statement - .executeUpdate("insert into people values(default, 'Kalle', '7')"); - statement - .executeUpdate("insert into people values(default, 'Pelle', '18')"); - statement - .executeUpdate("insert into people values(default, 'Börje', '64')"); - } - statement.close(); - statement = conn.createStatement(); - ResultSet rs = statement.executeQuery("select * from PEOPLE"); - Assert.assertTrue(rs.next()); - statement.close(); - conn.commit(); - connectionPool.releaseConnection(conn); - } - - public static void addFiveThousandPeople(JDBCConnectionPool connectionPool) - throws SQLException { - Connection conn = connectionPool.reserveConnection(); - Statement statement = conn.createStatement(); - for (int i = 4; i < 5000; i++) { - if (AllTests.db == DB.MSSQL) { - statement.executeUpdate("insert into people values('Person " - + i + "', '" + i % 99 + "')"); - } else { - statement - .executeUpdate("insert into people values(default, 'Person " - + i + "', '" + i % 99 + "')"); - } - } - statement.close(); - conn.commit(); - connectionPool.releaseConnection(conn); - } - - public static void addVersionedData(JDBCConnectionPool connectionPool) - throws SQLException { - Connection conn = connectionPool.reserveConnection(); - Statement statement = conn.createStatement(); - try { - statement.execute("DROP TABLE VERSIONED"); - if (AllTests.db == DB.ORACLE) { - statement.execute("drop sequence versioned_seq"); - statement.execute("drop sequence versioned_version"); - } - } catch (SQLException e) { - // Will fail if table doesn't exist, which is OK. - conn.rollback(); - } - for (String stmtString : AllTests.versionStatements) { - statement.execute(stmtString); - } - if (AllTests.db == DB.MSSQL) { - statement - .executeUpdate("insert into VERSIONED values('Junk', default)"); - } else { - statement - .executeUpdate("insert into VERSIONED values(default, 'Junk', default)"); - } - statement.close(); - statement = conn.createStatement(); - ResultSet rs = statement.executeQuery("select * from VERSIONED"); - Assert.assertTrue(rs.next()); - statement.close(); - conn.commit(); - connectionPool.releaseConnection(conn); - } - - public static void createGarbage(JDBCConnectionPool connectionPool) - throws SQLException { - Connection conn = connectionPool.reserveConnection(); - Statement statement = conn.createStatement(); - try { - statement.execute("drop table GARBAGE"); - if (AllTests.db == DB.ORACLE) { - statement.execute("drop sequence garbage_seq"); - } - } catch (SQLException e) { - // Will fail if table doesn't exist, which is OK. - conn.rollback(); - } - statement.execute(AllTests.createGarbage); - if (AllTests.db == DB.ORACLE) { - statement.execute(AllTests.createGarbageSecond); - statement.execute(AllTests.createGarbageThird); - } - conn.commit(); - connectionPool.releaseConnection(conn); - } -} diff --git a/tests/server-side/com/vaadin/data/util/sqlcontainer/FreeformQueryUtil.java b/tests/server-side/com/vaadin/data/util/sqlcontainer/FreeformQueryUtil.java deleted file mode 100644 index b0e2a232ca..0000000000 --- a/tests/server-side/com/vaadin/data/util/sqlcontainer/FreeformQueryUtil.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.vaadin.data.util.sqlcontainer; - -import java.util.List; - -import org.junit.Test; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.util.sqlcontainer.AllTests.DB; -import com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper; -import com.vaadin.data.util.sqlcontainer.query.generator.filter.QueryBuilder; - -public class FreeformQueryUtil { - - @Test - public void testDummy() { - // Added dummy test so JUnit will not complain about - // "No runnable methods". - } - - public static StatementHelper getQueryWithFilters(List<Filter> filters, - int offset, int limit) { - StatementHelper sh = new StatementHelper(); - if (AllTests.db == DB.MSSQL) { - if (limit > 1) { - offset++; - limit--; - } - StringBuilder query = new StringBuilder(); - query.append("SELECT * FROM (SELECT row_number() OVER ("); - query.append("ORDER BY \"ID\" ASC"); - query.append(") AS rownum, * FROM \"PEOPLE\""); - - if (!filters.isEmpty()) { - query.append(QueryBuilder.getWhereStringForFilters(filters, sh)); - } - query.append(") AS a WHERE a.rownum BETWEEN ").append(offset) - .append(" AND ").append(Integer.toString(offset + limit)); - sh.setQueryString(query.toString()); - return sh; - } else if (AllTests.db == DB.ORACLE) { - if (limit > 1) { - offset++; - limit--; - } - StringBuilder query = new StringBuilder(); - query.append("SELECT * FROM (SELECT x.*, ROWNUM AS " - + "\"rownum\" FROM (SELECT * FROM \"PEOPLE\""); - if (!filters.isEmpty()) { - query.append(QueryBuilder.getWhereStringForFilters(filters, sh)); - } - query.append(") x) WHERE \"rownum\" BETWEEN ? AND ?"); - sh.addParameterValue(offset); - sh.addParameterValue(offset + limit); - sh.setQueryString(query.toString()); - return sh; - } else { - StringBuilder query = new StringBuilder("SELECT * FROM people"); - if (!filters.isEmpty()) { - query.append(QueryBuilder.getWhereStringForFilters(filters, sh)); - } - if (limit != 0 || offset != 0) { - query.append(" LIMIT ? OFFSET ?"); - sh.addParameterValue(limit); - sh.addParameterValue(offset); - } - sh.setQueryString(query.toString()); - return sh; - } - } - -} diff --git a/tests/server-side/com/vaadin/data/util/sqlcontainer/ReadOnlyRowIdTest.java b/tests/server-side/com/vaadin/data/util/sqlcontainer/ReadOnlyRowIdTest.java deleted file mode 100644 index d2b6ee2555..0000000000 --- a/tests/server-side/com/vaadin/data/util/sqlcontainer/ReadOnlyRowIdTest.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.vaadin.data.util.sqlcontainer; - -import junit.framework.Assert; - -import org.junit.Test; - -import com.vaadin.data.util.sqlcontainer.ReadOnlyRowId; - -public class ReadOnlyRowIdTest { - - @Test - public void getRowNum_shouldReturnRowNumGivenInConstructor() { - int rowNum = 1337; - ReadOnlyRowId rid = new ReadOnlyRowId(rowNum); - Assert.assertEquals(rowNum, rid.getRowNum()); - } - - @Test - public void hashCode_shouldBeEqualToHashCodeOfRowNum() { - int rowNum = 1337; - ReadOnlyRowId rid = new ReadOnlyRowId(rowNum); - Assert.assertEquals(Integer.valueOf(rowNum).hashCode(), rid.hashCode()); - } - - @Test - public void equals_compareWithNull_shouldBeFalse() { - ReadOnlyRowId rid = new ReadOnlyRowId(1337); - Assert.assertFalse(rid.equals(null)); - } - - @Test - public void equals_compareWithSameInstance_shouldBeTrue() { - ReadOnlyRowId rid = new ReadOnlyRowId(1337); - ReadOnlyRowId rid2 = rid; - Assert.assertTrue(rid.equals(rid2)); - } - - @Test - public void equals_compareWithOtherType_shouldBeFalse() { - ReadOnlyRowId rid = new ReadOnlyRowId(1337); - Assert.assertFalse(rid.equals(new Object())); - } - - @Test - public void equals_compareWithOtherRowId_shouldBeFalse() { - ReadOnlyRowId rid = new ReadOnlyRowId(1337); - ReadOnlyRowId rid2 = new ReadOnlyRowId(42); - Assert.assertFalse(rid.equals(rid2)); - } -} diff --git a/tests/server-side/com/vaadin/data/util/sqlcontainer/RowIdTest.java b/tests/server-side/com/vaadin/data/util/sqlcontainer/RowIdTest.java deleted file mode 100644 index 399db06869..0000000000 --- a/tests/server-side/com/vaadin/data/util/sqlcontainer/RowIdTest.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.vaadin.data.util.sqlcontainer; - -import org.junit.Assert; -import org.junit.Test; - -import com.vaadin.data.util.sqlcontainer.RowId; - -public class RowIdTest { - - @Test - public void constructor_withArrayOfPrimaryKeyColumns_shouldSucceed() { - RowId id = new RowId(new Object[] { "id", "name" }); - Assert.assertArrayEquals(new Object[] { "id", "name" }, id.getId()); - } - - @Test(expected = IllegalArgumentException.class) - public void constructor_withNullParameter_shouldFail() { - new RowId(null); - } - - @Test - public void hashCode_samePrimaryKeys_sameResult() { - RowId id = new RowId(new Object[] { "id", "name" }); - RowId id2 = new RowId(new Object[] { "id", "name" }); - Assert.assertEquals(id.hashCode(), id2.hashCode()); - } - - @Test - public void hashCode_differentPrimaryKeys_differentResult() { - RowId id = new RowId(new Object[] { "id", "name" }); - RowId id2 = new RowId(new Object[] { "id" }); - Assert.assertFalse(id.hashCode() == id2.hashCode()); - } - - @Test - public void equals_samePrimaryKeys_returnsTrue() { - RowId id = new RowId(new Object[] { "id", "name" }); - RowId id2 = new RowId(new Object[] { "id", "name" }); - Assert.assertEquals(id, id2); - } - - @Test - public void equals_differentPrimaryKeys_returnsFalse() { - RowId id = new RowId(new Object[] { "id", "name" }); - RowId id2 = new RowId(new Object[] { "id" }); - Assert.assertFalse(id.equals(id2.hashCode())); - } - - @Test - public void equals_differentDataType_returnsFalse() { - RowId id = new RowId(new Object[] { "id", "name" }); - Assert.assertFalse(id.equals("Tudiluu")); - Assert.assertFalse(id.equals(new Integer(1337))); - } -} diff --git a/tests/server-side/com/vaadin/data/util/sqlcontainer/SQLContainerTableQueryTest.java b/tests/server-side/com/vaadin/data/util/sqlcontainer/SQLContainerTableQueryTest.java deleted file mode 100644 index 438c40823d..0000000000 --- a/tests/server-side/com/vaadin/data/util/sqlcontainer/SQLContainerTableQueryTest.java +++ /dev/null @@ -1,1519 +0,0 @@ -package com.vaadin.data.util.sqlcontainer; - -import java.math.BigDecimal; -import java.sql.Connection; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import org.easymock.EasyMock; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.vaadin.data.Container.ItemSetChangeEvent; -import com.vaadin.data.Container.ItemSetChangeListener; -import com.vaadin.data.Item; -import com.vaadin.data.util.filter.Like; -import com.vaadin.data.util.sqlcontainer.AllTests.DB; -import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool; -import com.vaadin.data.util.sqlcontainer.connection.SimpleJDBCConnectionPool; -import com.vaadin.data.util.sqlcontainer.query.OrderBy; -import com.vaadin.data.util.sqlcontainer.query.TableQuery; - -public class SQLContainerTableQueryTest { - - private static final int offset = AllTests.offset; - private static final String createGarbage = AllTests.createGarbage; - private JDBCConnectionPool connectionPool; - - @Before - public void setUp() throws SQLException { - - try { - connectionPool = new SimpleJDBCConnectionPool(AllTests.dbDriver, - AllTests.dbURL, AllTests.dbUser, AllTests.dbPwd, 2, 2); - } catch (SQLException e) { - e.printStackTrace(); - Assert.fail(e.getMessage()); - } - - DataGenerator.addPeopleToDatabase(connectionPool); - } - - @After - public void tearDown() { - if (connectionPool != null) { - connectionPool.destroy(); - } - } - - @Test - public void constructor_withTableQuery_shouldSucceed() throws SQLException { - new SQLContainer(new TableQuery("people", connectionPool, - AllTests.sqlGen)); - } - - @Test - public void containsId_withTableQueryAndExistingId_returnsTrue() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Assert.assertTrue(container.containsId(new RowId( - new Object[] { 1 + offset }))); - } - - @Test - public void containsId_withTableQueryAndNonexistingId_returnsFalse() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Assert.assertFalse(container.containsId(new RowId( - new Object[] { 1337 + offset }))); - } - - @Test - public void getContainerProperty_tableExistingItemIdAndPropertyId_returnsProperty() - throws SQLException { - TableQuery t = new TableQuery("people", connectionPool, AllTests.sqlGen); - SQLContainer container = new SQLContainer(t); - if (AllTests.db == DB.ORACLE) { - Assert.assertEquals( - "Ville", - container - .getContainerProperty( - new RowId(new Object[] { new BigDecimal( - 0 + offset) }), "NAME").getValue()); - } else { - Assert.assertEquals( - "Ville", - container.getContainerProperty( - new RowId(new Object[] { 0 + offset }), "NAME") - .getValue()); - } - } - - @Test - public void getContainerProperty_tableExistingItemIdAndNonexistingPropertyId_returnsNull() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Assert.assertNull(container.getContainerProperty(new RowId( - new Object[] { 1 + offset }), "asdf")); - } - - @Test - public void getContainerProperty_tableNonexistingItemId_returnsNull() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Assert.assertNull(container.getContainerProperty(new RowId( - new Object[] { 1337 + offset }), "NAME")); - } - - @Test - public void getContainerPropertyIds_table_returnsIDAndNAME() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Collection<?> propertyIds = container.getContainerPropertyIds(); - Assert.assertEquals(3, propertyIds.size()); - Assert.assertArrayEquals(new String[] { "ID", "NAME", "AGE" }, - propertyIds.toArray()); - } - - @Test - public void getItem_tableExistingItemId_returnsItem() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Item item; - if (AllTests.db == DB.ORACLE) { - item = container.getItem(new RowId(new Object[] { new BigDecimal( - 0 + offset) })); - } else { - item = container.getItem(new RowId(new Object[] { 0 + offset })); - } - Assert.assertNotNull(item); - Assert.assertEquals("Ville", item.getItemProperty("NAME").getValue()); - } - - @Test - public void getItem_commitedModifiedAndRefreshed() throws SQLException { - String OLD_VALUE = "SomeValue"; //$NON-NLS-1$ - String NEW_VALUE = "OtherValue"; //$NON-NLS-1$ - - SQLContainer container = new SQLContainer(new TableQuery("people", //$NON-NLS-1$ - connectionPool, AllTests.sqlGen)); - Object itemID = container.addItem(); - Item item = container.getItem(itemID); - item.getItemProperty("NAME").setValue(OLD_VALUE); //$NON-NLS-1$ - container.commit(); - - itemID = container.getIdByIndex(container.size() - 1); - item = container.getItem(itemID); - Assert.assertEquals(OLD_VALUE, item.getItemProperty("NAME") //$NON-NLS-1$ - .getValue()); - item.getItemProperty("NAME").setValue(NEW_VALUE); //$NON-NLS-1$ - - // refresh the container which free's the caches - // and the modified cache keeps untouched which is a really powerful - // feature - container.refresh(); - - // access the item again will use the item from the modified cache. - item = container.getItem(itemID); - Assert.assertEquals(NEW_VALUE, item.getItemProperty("NAME") //$NON-NLS-1$ - .getValue()); - } - - @Test - public void getItem_table5000RowsWithParameter1337_returnsItemWithId1337() - throws SQLException { - DataGenerator.addFiveThousandPeople(connectionPool); - TableQuery query = new TableQuery("people", connectionPool, - AllTests.sqlGen); - SQLContainer container = new SQLContainer(query); - - Item item; - if (AllTests.db == DB.ORACLE) { - item = container.getItem(new RowId(new Object[] { new BigDecimal( - 1337 + offset) })); - Assert.assertNotNull(item); - Assert.assertEquals(new BigDecimal(1337 + offset), item - .getItemProperty("ID").getValue()); - } else { - item = container.getItem(new RowId(new Object[] { 1337 + offset })); - Assert.assertNotNull(item); - Assert.assertEquals(1337 + offset, item.getItemProperty("ID") - .getValue()); - } - Assert.assertEquals("Person 1337", item.getItemProperty("NAME") - .getValue()); - } - - @Test - public void getItemIds_table_returnsItemIdsWithKeys0through3() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Collection<?> itemIds = container.getItemIds(); - Assert.assertEquals(4, itemIds.size()); - RowId zero = new RowId(new Object[] { 0 + offset }); - RowId one = new RowId(new Object[] { 1 + offset }); - RowId two = new RowId(new Object[] { 2 + offset }); - RowId three = new RowId(new Object[] { 3 + offset }); - if (AllTests.db == DB.ORACLE) { - String[] correct = new String[] { "1", "2", "3", "4" }; - List<String> oracle = new ArrayList<String>(); - for (Object o : itemIds) { - oracle.add(o.toString()); - } - Assert.assertArrayEquals(correct, oracle.toArray()); - } else { - Assert.assertArrayEquals(new Object[] { zero, one, two, three }, - itemIds.toArray()); - } - } - - @Test - public void getType_tableNAMEPropertyId_returnsString() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Assert.assertEquals(String.class, container.getType("NAME")); - } - - @Test - public void getType_tableIDPropertyId_returnsInteger() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - if (AllTests.db == DB.ORACLE) { - Assert.assertEquals(BigDecimal.class, container.getType("ID")); - } else { - Assert.assertEquals(Integer.class, container.getType("ID")); - } - } - - @Test - public void getType_tableNonexistingPropertyId_returnsNull() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Assert.assertNull(container.getType("asdf")); - } - - @Test - public void size_table_returnsFour() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Assert.assertEquals(4, container.size()); - } - - @Test - public void size_tableOneAddedItem_returnsFive() throws SQLException { - Connection conn = connectionPool.reserveConnection(); - Statement statement = conn.createStatement(); - if (AllTests.db == DB.MSSQL) { - statement.executeUpdate("insert into people values('Bengt', 30)"); - } else { - statement - .executeUpdate("insert into people values(default, 'Bengt', 30)"); - } - statement.close(); - conn.commit(); - connectionPool.releaseConnection(conn); - - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Assert.assertEquals(5, container.size()); - } - - @Test - public void indexOfId_tableWithParameterThree_returnsThree() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - if (AllTests.db == DB.ORACLE) { - Assert.assertEquals(3, container.indexOfId(new RowId( - new Object[] { new BigDecimal(3 + offset) }))); - } else { - Assert.assertEquals(3, - container.indexOfId(new RowId(new Object[] { 3 + offset }))); - } - } - - @Test - public void indexOfId_table5000RowsWithParameter1337_returns1337() - throws SQLException { - DataGenerator.addFiveThousandPeople(connectionPool); - TableQuery q = new TableQuery("people", connectionPool, AllTests.sqlGen); - SQLContainer container = new SQLContainer(q); - if (AllTests.db == DB.ORACLE) { - container.getItem(new RowId(new Object[] { new BigDecimal( - 1337 + offset) })); - Assert.assertEquals(1337, container.indexOfId(new RowId( - new Object[] { new BigDecimal(1337 + offset) }))); - } else { - container.getItem(new RowId(new Object[] { 1337 + offset })); - Assert.assertEquals(1337, container.indexOfId(new RowId( - new Object[] { 1337 + offset }))); - } - } - - @Test - public void getIdByIndex_table5000rowsIndex1337_returnsRowId1337() - throws SQLException { - DataGenerator.addFiveThousandPeople(connectionPool); - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object itemId = container.getIdByIndex(1337); - if (AllTests.db == DB.ORACLE) { - Assert.assertEquals( - new RowId(new Object[] { 1337 + offset }).toString(), - itemId.toString()); - } else { - Assert.assertEquals(new RowId(new Object[] { 1337 + offset }), - itemId); - } - } - - @Test - public void getIdByIndex_tableWithPaging5000rowsIndex1337_returnsRowId1337() - throws SQLException { - DataGenerator.addFiveThousandPeople(connectionPool); - TableQuery query = new TableQuery("people", connectionPool, - AllTests.sqlGen); - SQLContainer container = new SQLContainer(query); - Object itemId = container.getIdByIndex(1337); - if (AllTests.db == DB.ORACLE) { - Assert.assertEquals( - new RowId(new Object[] { 1337 + offset }).toString(), - itemId.toString()); - } else { - Assert.assertEquals(new RowId(new Object[] { 1337 + offset }), - itemId); - } - } - - @Test - public void nextItemId_tableCurrentItem1337_returnsItem1338() - throws SQLException { - DataGenerator.addFiveThousandPeople(connectionPool); - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object itemId = container.getIdByIndex(1337); - if (AllTests.db == DB.ORACLE) { - Assert.assertEquals( - new RowId(new Object[] { 1338 + offset }).toString(), - container.nextItemId(itemId).toString()); - } else { - Assert.assertEquals(new RowId(new Object[] { 1338 + offset }), - container.nextItemId(itemId)); - } - } - - @Test - public void prevItemId_tableCurrentItem1337_returns1336() - throws SQLException { - DataGenerator.addFiveThousandPeople(connectionPool); - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object itemId = container.getIdByIndex(1337); - if (AllTests.db == DB.ORACLE) { - Assert.assertEquals( - new RowId(new Object[] { 1336 + offset }).toString(), - container.prevItemId(itemId).toString()); - } else { - Assert.assertEquals(new RowId(new Object[] { 1336 + offset }), - container.prevItemId(itemId)); - } - } - - @Test - public void firstItemId_table_returnsItemId0() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - if (AllTests.db == DB.ORACLE) { - Assert.assertEquals( - new RowId(new Object[] { 0 + offset }).toString(), - container.firstItemId().toString()); - } else { - Assert.assertEquals(new RowId(new Object[] { 0 + offset }), - container.firstItemId()); - } - } - - @Test - public void lastItemId_table5000Rows_returnsItemId4999() - throws SQLException { - DataGenerator.addFiveThousandPeople(connectionPool); - - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - if (AllTests.db == DB.ORACLE) { - Assert.assertEquals( - new RowId(new Object[] { 4999 + offset }).toString(), - container.lastItemId().toString()); - } else { - Assert.assertEquals(new RowId(new Object[] { 4999 + offset }), - container.lastItemId()); - } - } - - @Test - public void isFirstId_tableActualFirstId_returnsTrue() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - if (AllTests.db == DB.ORACLE) { - Assert.assertTrue(container.isFirstId(new RowId( - new Object[] { new BigDecimal(0 + offset) }))); - } else { - Assert.assertTrue(container.isFirstId(new RowId( - new Object[] { 0 + offset }))); - } - } - - @Test - public void isFirstId_tableSecondId_returnsFalse() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - if (AllTests.db == DB.ORACLE) { - Assert.assertFalse(container.isFirstId(new RowId( - new Object[] { new BigDecimal(1 + offset) }))); - } else { - Assert.assertFalse(container.isFirstId(new RowId( - new Object[] { 1 + offset }))); - } - } - - @Test - public void isLastId_tableSecondId_returnsFalse() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - if (AllTests.db == DB.ORACLE) { - Assert.assertFalse(container.isLastId(new RowId( - new Object[] { new BigDecimal(1 + offset) }))); - } else { - Assert.assertFalse(container.isLastId(new RowId( - new Object[] { 1 + offset }))); - } - } - - @Test - public void isLastId_tableLastId_returnsTrue() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - if (AllTests.db == DB.ORACLE) { - Assert.assertTrue(container.isLastId(new RowId( - new Object[] { new BigDecimal(3 + offset) }))); - } else { - Assert.assertTrue(container.isLastId(new RowId( - new Object[] { 3 + offset }))); - } - } - - @Test - public void isLastId_table5000RowsLastId_returnsTrue() throws SQLException { - DataGenerator.addFiveThousandPeople(connectionPool); - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - if (AllTests.db == DB.ORACLE) { - Assert.assertTrue(container.isLastId(new RowId( - new Object[] { new BigDecimal(4999 + offset) }))); - } else { - Assert.assertTrue(container.isLastId(new RowId( - new Object[] { 4999 + offset }))); - } - } - - @Test - public void allIdsFound_table5000RowsLastId_shouldSucceed() - throws SQLException { - DataGenerator.addFiveThousandPeople(connectionPool); - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - for (int i = 0; i < 5000; i++) { - Assert.assertTrue(container.containsId(container.getIdByIndex(i))); - } - } - - @Test - public void allIdsFound_table5000RowsLastId_autoCommit_shouldSucceed() - throws SQLException { - DataGenerator.addFiveThousandPeople(connectionPool); - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - container.setAutoCommit(true); - for (int i = 0; i < 5000; i++) { - Assert.assertTrue(container.containsId(container.getIdByIndex(i))); - } - } - - @Test - public void refresh_table_sizeShouldUpdate() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Assert.assertEquals(4, container.size()); - DataGenerator.addFiveThousandPeople(connectionPool); - container.refresh(); - Assert.assertEquals(5000, container.size()); - } - - @Test - public void refresh_tableWithoutCallingRefresh_sizeShouldNotUpdate() - throws SQLException { - // Yeah, this is a weird one. We're testing that the size doesn't update - // after adding lots of items unless we call refresh inbetween. This to - // make sure that the refresh method actually refreshes stuff and isn't - // a NOP. - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Assert.assertEquals(4, container.size()); - DataGenerator.addFiveThousandPeople(connectionPool); - Assert.assertEquals(4, container.size()); - } - - @Test - public void setAutoCommit_table_shouldSucceed() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - container.setAutoCommit(true); - Assert.assertTrue(container.isAutoCommit()); - container.setAutoCommit(false); - Assert.assertFalse(container.isAutoCommit()); - } - - @Test - public void getPageLength_table_returnsDefault100() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Assert.assertEquals(100, container.getPageLength()); - } - - @Test - public void setPageLength_table_shouldSucceed() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - container.setPageLength(20); - Assert.assertEquals(20, container.getPageLength()); - container.setPageLength(200); - Assert.assertEquals(200, container.getPageLength()); - } - - @Test(expected = UnsupportedOperationException.class) - public void addContainerProperty_normal_isUnsupported() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - container.addContainerProperty("asdf", String.class, ""); - } - - @Test(expected = UnsupportedOperationException.class) - public void removeContainerProperty_normal_isUnsupported() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - container.removeContainerProperty("asdf"); - } - - @Test(expected = UnsupportedOperationException.class) - public void addItemObject_normal_isUnsupported() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - container.addItem("asdf"); - } - - @Test(expected = UnsupportedOperationException.class) - public void addItemAfterObjectObject_normal_isUnsupported() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - container.addItemAfter("asdf", "foo"); - } - - @Test(expected = UnsupportedOperationException.class) - public void addItemAtIntObject_normal_isUnsupported() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - container.addItemAt(2, "asdf"); - } - - @Test(expected = UnsupportedOperationException.class) - public void addItemAtInt_normal_isUnsupported() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - container.addItemAt(2); - } - - @Test(expected = UnsupportedOperationException.class) - public void addItemAfterObject_normal_isUnsupported() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - container.addItemAfter("asdf"); - } - - @Test - public void addItem_tableAddOneNewItem_returnsItemId() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object itemId = container.addItem(); - Assert.assertNotNull(itemId); - } - - @Test - public void addItem_tableAddOneNewItem_autoCommit_returnsFinalItemId() - throws SQLException { - TableQuery query = new TableQuery("people", connectionPool, - AllTests.sqlGen); - SQLContainer container = new SQLContainer(query); - container.setAutoCommit(true); - Object itemId = container.addItem(); - Assert.assertNotNull(itemId); - Assert.assertTrue(itemId instanceof RowId); - Assert.assertFalse(itemId instanceof TemporaryRowId); - } - - @Test - public void addItem_tableAddOneNewItem_autoCommit_sizeIsIncreased() - throws SQLException { - TableQuery query = new TableQuery("people", connectionPool, - AllTests.sqlGen); - SQLContainer container = new SQLContainer(query); - container.setAutoCommit(true); - int originalSize = container.size(); - container.addItem(); - Assert.assertEquals(originalSize + 1, container.size()); - } - - @Test - public void addItem_tableAddOneNewItem_shouldChangeSize() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - int size = container.size(); - container.addItem(); - Assert.assertEquals(size + 1, container.size()); - } - - @Test - public void addItem_tableAddTwoNewItems_shouldChangeSize() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - int size = container.size(); - Object id1 = container.addItem(); - Object id2 = container.addItem(); - Assert.assertEquals(size + 2, container.size()); - Assert.assertNotSame(id1, id2); - Assert.assertFalse(id1.equals(id2)); - } - - @Test - public void nextItemId_tableNewlyAddedItem_returnsNewlyAdded() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object lastId = container.lastItemId(); - Object id = container.addItem(); - Assert.assertEquals(id, container.nextItemId(lastId)); - } - - @Test - public void lastItemId_tableNewlyAddedItem_returnsNewlyAdded() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object lastId = container.lastItemId(); - Object id = container.addItem(); - Assert.assertEquals(id, container.lastItemId()); - Assert.assertNotSame(lastId, container.lastItemId()); - } - - @Test - public void indexOfId_tableNewlyAddedItem_returnsFour() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object id = container.addItem(); - Assert.assertEquals(4, container.indexOfId(id)); - } - - @Test - public void getItem_tableNewlyAddedItem_returnsNewlyAdded() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object id = container.addItem(); - Assert.assertNotNull(container.getItem(id)); - } - - @Test - public void getItemIds_tableNewlyAddedItem_containsNewlyAdded() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object id = container.addItem(); - Assert.assertTrue(container.getItemIds().contains(id)); - } - - @Test - public void getContainerProperty_tableNewlyAddedItem_returnsPropertyOfNewlyAddedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object id = container.addItem(); - Item item = container.getItem(id); - item.getItemProperty("NAME").setValue("asdf"); - Assert.assertEquals("asdf", container.getContainerProperty(id, "NAME") - .getValue()); - } - - @Test - public void containsId_tableNewlyAddedItem_returnsTrue() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object id = container.addItem(); - Assert.assertTrue(container.containsId(id)); - } - - @Test - public void prevItemId_tableTwoNewlyAddedItems_returnsFirstAddedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object id1 = container.addItem(); - Object id2 = container.addItem(); - Assert.assertEquals(id1, container.prevItemId(id2)); - } - - @Test - public void firstItemId_tableEmptyResultSet_returnsFirstAddedItem() - throws SQLException { - DataGenerator.createGarbage(connectionPool); - SQLContainer container = new SQLContainer(new TableQuery("garbage", - connectionPool, AllTests.sqlGen)); - Object id = container.addItem(); - Assert.assertSame(id, container.firstItemId()); - } - - @Test - public void isFirstId_tableEmptyResultSet_returnsFirstAddedItem() - throws SQLException { - DataGenerator.createGarbage(connectionPool); - SQLContainer container = new SQLContainer(new TableQuery("garbage", - connectionPool, AllTests.sqlGen)); - Object id = container.addItem(); - Assert.assertTrue(container.isFirstId(id)); - } - - @Test - public void isLastId_tableOneItemAdded_returnsTrueForAddedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object id = container.addItem(); - Assert.assertTrue(container.isLastId(id)); - } - - @Test - public void isLastId_tableTwoItemsAdded_returnsTrueForLastAddedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - container.addItem(); - Object id2 = container.addItem(); - Assert.assertTrue(container.isLastId(id2)); - } - - @Test - public void getIdByIndex_tableOneItemAddedLastIndexInContainer_returnsAddedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object id = container.addItem(); - Assert.assertEquals(id, container.getIdByIndex(container.size() - 1)); - } - - @Test - public void removeItem_tableNoAddedItems_removesItemFromContainer() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - int size = container.size(); - Object id = container.firstItemId(); - Assert.assertTrue(container.removeItem(id)); - Assert.assertNotSame(id, container.firstItemId()); - Assert.assertEquals(size - 1, container.size()); - } - - @Test - public void containsId_tableRemovedItem_returnsFalse() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object id = container.firstItemId(); - Assert.assertTrue(container.removeItem(id)); - Assert.assertFalse(container.containsId(id)); - } - - @Test - public void removeItem_tableOneAddedItem_removesTheAddedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object id = container.addItem(); - int size = container.size(); - Assert.assertTrue(container.removeItem(id)); - Assert.assertFalse(container.containsId(id)); - Assert.assertEquals(size - 1, container.size()); - } - - @Test - public void getItem_tableItemRemoved_returnsNull() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object id = container.firstItemId(); - Assert.assertTrue(container.removeItem(id)); - Assert.assertNull(container.getItem(id)); - } - - @Test - public void getItem_tableAddedItemRemoved_returnsNull() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object id = container.addItem(); - Assert.assertNotNull(container.getItem(id)); - Assert.assertTrue(container.removeItem(id)); - Assert.assertNull(container.getItem(id)); - } - - @Test - public void getItemIds_tableItemRemoved_shouldNotContainRemovedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object id = container.firstItemId(); - Assert.assertTrue(container.getItemIds().contains(id)); - Assert.assertTrue(container.removeItem(id)); - Assert.assertFalse(container.getItemIds().contains(id)); - } - - @Test - public void getItemIds_tableAddedItemRemoved_shouldNotContainRemovedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object id = container.addItem(); - Assert.assertTrue(container.getItemIds().contains(id)); - Assert.assertTrue(container.removeItem(id)); - Assert.assertFalse(container.getItemIds().contains(id)); - } - - @Test - public void containsId_tableItemRemoved_returnsFalse() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object id = container.firstItemId(); - Assert.assertTrue(container.containsId(id)); - Assert.assertTrue(container.removeItem(id)); - Assert.assertFalse(container.containsId(id)); - } - - @Test - public void containsId_tableAddedItemRemoved_returnsFalse() - throws SQLException { - TableQuery query = new TableQuery("people", connectionPool, - AllTests.sqlGen); - SQLContainer container = new SQLContainer(query); - Object id = container.addItem(); - Assert.assertTrue(container.containsId(id)); - Assert.assertTrue(container.removeItem(id)); - Assert.assertFalse(container.containsId(id)); - } - - @Test - public void nextItemId_tableItemRemoved_skipsRemovedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object first = container.getIdByIndex(0); - Object second = container.getIdByIndex(1); - Object third = container.getIdByIndex(2); - Assert.assertTrue(container.removeItem(second)); - Assert.assertEquals(third, container.nextItemId(first)); - } - - @Test - public void nextItemId_tableAddedItemRemoved_skipsRemovedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object first = container.lastItemId(); - Object second = container.addItem(); - Object third = container.addItem(); - Assert.assertTrue(container.removeItem(second)); - Assert.assertEquals(third, container.nextItemId(first)); - } - - @Test - public void prevItemId_tableItemRemoved_skipsRemovedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object first = container.getIdByIndex(0); - Object second = container.getIdByIndex(1); - Object third = container.getIdByIndex(2); - Assert.assertTrue(container.removeItem(second)); - Assert.assertEquals(first, container.prevItemId(third)); - } - - @Test - public void prevItemId_tableAddedItemRemoved_skipsRemovedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object first = container.lastItemId(); - Object second = container.addItem(); - Object third = container.addItem(); - Assert.assertTrue(container.removeItem(second)); - Assert.assertEquals(first, container.prevItemId(third)); - } - - @Test - public void firstItemId_tableFirstItemRemoved_resultChanges() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object first = container.firstItemId(); - Assert.assertTrue(container.removeItem(first)); - Assert.assertNotSame(first, container.firstItemId()); - } - - @Test - public void firstItemId_tableNewlyAddedFirstItemRemoved_resultChanges() - throws SQLException { - DataGenerator.createGarbage(connectionPool); - SQLContainer container = new SQLContainer(new TableQuery("garbage", - connectionPool, AllTests.sqlGen)); - Object first = container.addItem(); - Object second = container.addItem(); - Assert.assertSame(first, container.firstItemId()); - Assert.assertTrue(container.removeItem(first)); - Assert.assertSame(second, container.firstItemId()); - } - - @Test - public void lastItemId_tableLastItemRemoved_resultChanges() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object last = container.lastItemId(); - Assert.assertTrue(container.removeItem(last)); - Assert.assertNotSame(last, container.lastItemId()); - } - - @Test - public void lastItemId_tableAddedLastItemRemoved_resultChanges() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object last = container.addItem(); - Assert.assertSame(last, container.lastItemId()); - Assert.assertTrue(container.removeItem(last)); - Assert.assertNotSame(last, container.lastItemId()); - } - - @Test - public void isFirstId_tableFirstItemRemoved_returnsFalse() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object first = container.firstItemId(); - Assert.assertTrue(container.removeItem(first)); - Assert.assertFalse(container.isFirstId(first)); - } - - @Test - public void isFirstId_tableAddedFirstItemRemoved_returnsFalse() - throws SQLException { - DataGenerator.createGarbage(connectionPool); - SQLContainer container = new SQLContainer(new TableQuery("garbage", - connectionPool, AllTests.sqlGen)); - Object first = container.addItem(); - container.addItem(); - Assert.assertSame(first, container.firstItemId()); - Assert.assertTrue(container.removeItem(first)); - Assert.assertFalse(container.isFirstId(first)); - } - - @Test - public void isLastId_tableLastItemRemoved_returnsFalse() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object last = container.lastItemId(); - Assert.assertTrue(container.removeItem(last)); - Assert.assertFalse(container.isLastId(last)); - } - - @Test - public void isLastId_tableAddedLastItemRemoved_returnsFalse() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object last = container.addItem(); - Assert.assertSame(last, container.lastItemId()); - Assert.assertTrue(container.removeItem(last)); - Assert.assertFalse(container.isLastId(last)); - } - - @Test - public void indexOfId_tableItemRemoved_returnsNegOne() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object id = container.getIdByIndex(2); - Assert.assertTrue(container.removeItem(id)); - Assert.assertEquals(-1, container.indexOfId(id)); - } - - @Test - public void indexOfId_tableAddedItemRemoved_returnsNegOne() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object id = container.addItem(); - Assert.assertTrue(container.indexOfId(id) != -1); - Assert.assertTrue(container.removeItem(id)); - Assert.assertEquals(-1, container.indexOfId(id)); - } - - @Test - public void getIdByIndex_tableItemRemoved_resultChanges() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object id = container.getIdByIndex(2); - Assert.assertTrue(container.removeItem(id)); - Assert.assertNotSame(id, container.getIdByIndex(2)); - } - - @Test - public void getIdByIndex_tableAddedItemRemoved_resultChanges() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object id = container.addItem(); - container.addItem(); - int index = container.indexOfId(id); - Assert.assertTrue(container.removeItem(id)); - Assert.assertNotSame(id, container.getIdByIndex(index)); - } - - @Test - public void removeAllItems_table_shouldSucceed() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Assert.assertTrue(container.removeAllItems()); - Assert.assertEquals(0, container.size()); - } - - @Test - public void removeAllItems_tableAddedItems_shouldSucceed() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - container.addItem(); - container.addItem(); - Assert.assertTrue(container.removeAllItems()); - Assert.assertEquals(0, container.size()); - } - - @Test - public void commit_tableAddedItem_shouldBeWrittenToDB() throws SQLException { - TableQuery query = new TableQuery("people", connectionPool, - AllTests.sqlGen); - SQLContainer container = new SQLContainer(query); - Object id = container.addItem(); - container.getContainerProperty(id, "NAME").setValue("New Name"); - Assert.assertTrue(id instanceof TemporaryRowId); - Assert.assertSame(id, container.lastItemId()); - container.commit(); - Assert.assertFalse(container.lastItemId() instanceof TemporaryRowId); - Assert.assertEquals("New Name", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - } - - @Test - public void commit_tableTwoAddedItems_shouldBeWrittenToDB() - throws SQLException { - TableQuery query = new TableQuery("people", connectionPool, - AllTests.sqlGen); - SQLContainer container = new SQLContainer(query); - Object id = container.addItem(); - Object id2 = container.addItem(); - container.getContainerProperty(id, "NAME").setValue("Herbert"); - container.getContainerProperty(id2, "NAME").setValue("Larry"); - Assert.assertTrue(id2 instanceof TemporaryRowId); - Assert.assertSame(id2, container.lastItemId()); - container.commit(); - Object nextToLast = container.getIdByIndex(container.size() - 2); - Assert.assertFalse(nextToLast instanceof TemporaryRowId); - Assert.assertEquals("Herbert", - container.getContainerProperty(nextToLast, "NAME").getValue()); - Assert.assertFalse(container.lastItemId() instanceof TemporaryRowId); - Assert.assertEquals("Larry", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - } - - @Test - public void commit_tableRemovedItem_shouldBeRemovedFromDB() - throws SQLException { - TableQuery query = new TableQuery("people", connectionPool, - AllTests.sqlGen); - SQLContainer container = new SQLContainer(query); - Object last = container.lastItemId(); - container.removeItem(last); - container.commit(); - Assert.assertFalse(last.equals(container.lastItemId())); - } - - @Test - public void commit_tableLastItemUpdated_shouldUpdateRowInDB() - throws SQLException { - TableQuery query = new TableQuery("people", connectionPool, - AllTests.sqlGen); - SQLContainer container = new SQLContainer(query); - Object last = container.lastItemId(); - container.getContainerProperty(last, "NAME").setValue("Donald"); - container.commit(); - Assert.assertEquals("Donald", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - } - - @Test - public void rollback_tableItemAdded_discardsAddedItem() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - int size = container.size(); - Object id = container.addItem(); - container.getContainerProperty(id, "NAME").setValue("foo"); - Assert.assertEquals(size + 1, container.size()); - container.rollback(); - Assert.assertEquals(size, container.size()); - Assert.assertFalse("foo".equals(container.getContainerProperty( - container.lastItemId(), "NAME").getValue())); - } - - @Test - public void rollback_tableItemRemoved_restoresRemovedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - int size = container.size(); - Object last = container.lastItemId(); - container.removeItem(last); - Assert.assertEquals(size - 1, container.size()); - container.rollback(); - Assert.assertEquals(size, container.size()); - Assert.assertEquals(last, container.lastItemId()); - } - - @Test - public void rollback_tableItemChanged_discardsChanges() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Object last = container.lastItemId(); - container.getContainerProperty(last, "NAME").setValue("foo"); - container.rollback(); - Assert.assertFalse("foo".equals(container.getContainerProperty( - container.lastItemId(), "NAME").getValue())); - } - - @Test - public void itemChangeNotification_table_isModifiedReturnsTrue() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Assert.assertFalse(container.isModified()); - RowItem last = (RowItem) container.getItem(container.lastItemId()); - container.itemChangeNotification(last); - Assert.assertTrue(container.isModified()); - } - - @Test - public void itemSetChangeListeners_table_shouldFire() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - ItemSetChangeListener listener = EasyMock - .createMock(ItemSetChangeListener.class); - listener.containerItemSetChange(EasyMock.isA(ItemSetChangeEvent.class)); - EasyMock.replay(listener); - - container.addListener(listener); - container.addItem(); - - EasyMock.verify(listener); - } - - @Test - public void itemSetChangeListeners_tableItemRemoved_shouldFire() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - ItemSetChangeListener listener = EasyMock - .createMock(ItemSetChangeListener.class); - listener.containerItemSetChange(EasyMock.isA(ItemSetChangeEvent.class)); - EasyMock.expectLastCall().anyTimes(); - EasyMock.replay(listener); - - container.addListener(listener); - container.removeItem(container.lastItemId()); - - EasyMock.verify(listener); - } - - @Test - public void removeListener_table_shouldNotFire() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - ItemSetChangeListener listener = EasyMock - .createMock(ItemSetChangeListener.class); - EasyMock.replay(listener); - - container.addListener(listener); - container.removeListener(listener); - container.addItem(); - - EasyMock.verify(listener); - } - - @Test - public void isModified_tableRemovedItem_returnsTrue() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Assert.assertFalse(container.isModified()); - container.removeItem(container.lastItemId()); - Assert.assertTrue(container.isModified()); - } - - @Test - public void isModified_tableAddedItem_returnsTrue() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Assert.assertFalse(container.isModified()); - container.addItem(); - Assert.assertTrue(container.isModified()); - } - - @Test - public void isModified_tableChangedItem_returnsTrue() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Assert.assertFalse(container.isModified()); - container.getContainerProperty(container.lastItemId(), "NAME") - .setValue("foo"); - Assert.assertTrue(container.isModified()); - } - - @Test - public void getSortableContainerPropertyIds_table_returnsAllPropertyIds() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - Collection<?> sortableIds = container.getSortableContainerPropertyIds(); - Assert.assertTrue(sortableIds.contains("ID")); - Assert.assertTrue(sortableIds.contains("NAME")); - Assert.assertTrue(sortableIds.contains("AGE")); - Assert.assertEquals(3, sortableIds.size()); - if (AllTests.db == DB.MSSQL || AllTests.db == DB.ORACLE) { - Assert.assertFalse(sortableIds.contains("rownum")); - } - } - - @Test - public void addOrderBy_table_shouldReorderResults() throws SQLException { - TableQuery query = new TableQuery("people", connectionPool, - AllTests.sqlGen); - SQLContainer container = new SQLContainer(query); - // Ville, Kalle, Pelle, Börje - Assert.assertEquals("Ville", - container.getContainerProperty(container.firstItemId(), "NAME") - .getValue()); - Assert.assertEquals("Börje", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - - container.addOrderBy(new OrderBy("NAME", true)); - // Börje, Kalle, Pelle, Ville - Assert.assertEquals("Börje", - container.getContainerProperty(container.firstItemId(), "NAME") - .getValue()); - Assert.assertEquals("Ville", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - } - - @Test(expected = IllegalArgumentException.class) - public void addOrderBy_tableIllegalColumn_shouldFail() throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - container.addOrderBy(new OrderBy("asdf", true)); - } - - @Test - public void sort_table_sortsByName() throws SQLException { - TableQuery query = new TableQuery("people", connectionPool, - AllTests.sqlGen); - SQLContainer container = new SQLContainer(query); - // Ville, Kalle, Pelle, Börje - Assert.assertEquals("Ville", - container.getContainerProperty(container.firstItemId(), "NAME") - .getValue()); - Assert.assertEquals("Börje", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - - container.sort(new Object[] { "NAME" }, new boolean[] { true }); - - // Börje, Kalle, Pelle, Ville - Assert.assertEquals("Börje", - container.getContainerProperty(container.firstItemId(), "NAME") - .getValue()); - Assert.assertEquals("Ville", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - } - - @Test - public void addFilter_table_filtersResults() throws SQLException { - TableQuery query = new TableQuery("people", connectionPool, - AllTests.sqlGen); - SQLContainer container = new SQLContainer(query); - // Ville, Kalle, Pelle, Börje - Assert.assertEquals(4, container.size()); - Assert.assertEquals("Börje", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - - container.addContainerFilter(new Like("NAME", "%lle")); - // Ville, Kalle, Pelle - Assert.assertEquals(3, container.size()); - Assert.assertEquals("Pelle", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - } - - @Test - public void addContainerFilter_filtersResults() throws SQLException { - TableQuery query = new TableQuery("people", connectionPool, - AllTests.sqlGen); - SQLContainer container = new SQLContainer(query); - // Ville, Kalle, Pelle, Börje - Assert.assertEquals(4, container.size()); - - container.addContainerFilter("NAME", "Vi", false, false); - - // Ville - Assert.assertEquals(1, container.size()); - Assert.assertEquals("Ville", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - } - - @Test - public void addContainerFilter_ignoreCase_filtersResults() - throws SQLException { - TableQuery query = new TableQuery("people", connectionPool, - AllTests.sqlGen); - SQLContainer container = new SQLContainer(query); - // Ville, Kalle, Pelle, Börje - Assert.assertEquals(4, container.size()); - - container.addContainerFilter("NAME", "vi", true, false); - - // Ville - Assert.assertEquals(1, container.size()); - Assert.assertEquals("Ville", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - } - - @Test - public void removeAllContainerFilters_table_noFiltering() - throws SQLException { - TableQuery query = new TableQuery("people", connectionPool, - AllTests.sqlGen); - SQLContainer container = new SQLContainer(query); - // Ville, Kalle, Pelle, Börje - Assert.assertEquals(4, container.size()); - - container.addContainerFilter("NAME", "Vi", false, false); - - // Ville - Assert.assertEquals(1, container.size()); - Assert.assertEquals("Ville", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - - container.removeAllContainerFilters(); - - Assert.assertEquals(4, container.size()); - Assert.assertEquals("Börje", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - } - - @Test - public void removeContainerFilters_table_noFiltering() throws SQLException { - TableQuery query = new TableQuery("people", connectionPool, - AllTests.sqlGen); - SQLContainer container = new SQLContainer(query); - // Ville, Kalle, Pelle, Börje - Assert.assertEquals(4, container.size()); - - container.addContainerFilter("NAME", "Vi", false, false); - - // Ville - Assert.assertEquals(1, container.size()); - Assert.assertEquals("Ville", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - - container.removeContainerFilters("NAME"); - - Assert.assertEquals(4, container.size()); - Assert.assertEquals("Börje", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - } - - @Test - public void addFilter_tableBufferedItems_alsoFiltersBufferedItems() - throws SQLException { - TableQuery query = new TableQuery("people", connectionPool, - AllTests.sqlGen); - SQLContainer container = new SQLContainer(query); - // Ville, Kalle, Pelle, Börje - Assert.assertEquals(4, container.size()); - Assert.assertEquals("Börje", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - - Object id1 = container.addItem(); - container.getContainerProperty(id1, "NAME").setValue("Palle"); - Object id2 = container.addItem(); - container.getContainerProperty(id2, "NAME").setValue("Bengt"); - - container.addContainerFilter(new Like("NAME", "%lle")); - - // Ville, Kalle, Pelle, Palle - Assert.assertEquals(4, container.size()); - Assert.assertEquals( - "Ville", - container.getContainerProperty(container.getIdByIndex(0), - "NAME").getValue()); - Assert.assertEquals( - "Kalle", - container.getContainerProperty(container.getIdByIndex(1), - "NAME").getValue()); - Assert.assertEquals( - "Pelle", - container.getContainerProperty(container.getIdByIndex(2), - "NAME").getValue()); - Assert.assertEquals( - "Palle", - container.getContainerProperty(container.getIdByIndex(3), - "NAME").getValue()); - - Assert.assertNull(container.getIdByIndex(4)); - Assert.assertNull(container.nextItemId(container.getIdByIndex(3))); - - Assert.assertFalse(container.containsId(id2)); - Assert.assertFalse(container.getItemIds().contains(id2)); - - Assert.assertNull(container.getItem(id2)); - Assert.assertEquals(-1, container.indexOfId(id2)); - - Assert.assertNotSame(id2, container.lastItemId()); - Assert.assertSame(id1, container.lastItemId()); - } - - @Test - public void sort_tableBufferedItems_sortsBufferedItemsLastInOrderAdded() - throws SQLException { - TableQuery query = new TableQuery("people", connectionPool, - AllTests.sqlGen); - SQLContainer container = new SQLContainer(query); - // Ville, Kalle, Pelle, Börje - Assert.assertEquals("Ville", - container.getContainerProperty(container.firstItemId(), "NAME") - .getValue()); - Assert.assertEquals("Börje", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - - Object id1 = container.addItem(); - container.getContainerProperty(id1, "NAME").setValue("Wilbert"); - Object id2 = container.addItem(); - container.getContainerProperty(id2, "NAME").setValue("Albert"); - - container.sort(new Object[] { "NAME" }, new boolean[] { true }); - - // Börje, Kalle, Pelle, Ville, Wilbert, Albert - Assert.assertEquals("Börje", - container.getContainerProperty(container.firstItemId(), "NAME") - .getValue()); - Assert.assertEquals( - "Wilbert", - container.getContainerProperty( - container.getIdByIndex(container.size() - 2), "NAME") - .getValue()); - Assert.assertEquals("Albert", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - } - -} diff --git a/tests/server-side/com/vaadin/data/util/sqlcontainer/SQLContainerTest.java b/tests/server-side/com/vaadin/data/util/sqlcontainer/SQLContainerTest.java deleted file mode 100644 index 6649bc16e8..0000000000 --- a/tests/server-side/com/vaadin/data/util/sqlcontainer/SQLContainerTest.java +++ /dev/null @@ -1,2458 +0,0 @@ -package com.vaadin.data.util.sqlcontainer; - -import java.math.BigDecimal; -import java.sql.Connection; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.logging.Handler; -import java.util.logging.LogRecord; -import java.util.logging.Logger; - -import org.easymock.EasyMock; -import org.easymock.IAnswer; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.Container.ItemSetChangeEvent; -import com.vaadin.data.Container.ItemSetChangeListener; -import com.vaadin.data.Item; -import com.vaadin.data.util.filter.Compare.Equal; -import com.vaadin.data.util.filter.Like; -import com.vaadin.data.util.sqlcontainer.AllTests.DB; -import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool; -import com.vaadin.data.util.sqlcontainer.connection.SimpleJDBCConnectionPool; -import com.vaadin.data.util.sqlcontainer.query.FreeformQuery; -import com.vaadin.data.util.sqlcontainer.query.FreeformQueryDelegate; -import com.vaadin.data.util.sqlcontainer.query.FreeformStatementDelegate; -import com.vaadin.data.util.sqlcontainer.query.OrderBy; -import com.vaadin.data.util.sqlcontainer.query.generator.MSSQLGenerator; -import com.vaadin.data.util.sqlcontainer.query.generator.OracleGenerator; -import com.vaadin.data.util.sqlcontainer.query.generator.SQLGenerator; -import com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper; -import com.vaadin.data.util.sqlcontainer.query.generator.filter.QueryBuilder; - -public class SQLContainerTest { - private static final int offset = AllTests.offset; - private JDBCConnectionPool connectionPool; - - @Before - public void setUp() throws SQLException { - - try { - connectionPool = new SimpleJDBCConnectionPool(AllTests.dbDriver, - AllTests.dbURL, AllTests.dbUser, AllTests.dbPwd, 2, 2); - } catch (SQLException e) { - e.printStackTrace(); - Assert.fail(e.getMessage()); - } - - DataGenerator.addPeopleToDatabase(connectionPool); - } - - @After - public void tearDown() { - if (connectionPool != null) { - connectionPool.destroy(); - } - } - - @Test - public void constructor_withFreeformQuery_shouldSucceed() - throws SQLException { - new SQLContainer(new FreeformQuery("SELECT * FROM people", - connectionPool, "ID")); - } - - @Test(expected = SQLException.class) - public void constructor_withIllegalFreeformQuery_shouldFail() - throws SQLException { - SQLContainer c = new SQLContainer(new FreeformQuery( - "SELECT * FROM asdf", connectionPool, "ID")); - c.getItem(c.firstItemId()); - } - - @Test - public void containsId_withFreeformQueryAndExistingId_returnsTrue() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Assert.assertTrue(container.containsId(new RowId(new Object[] { 1 }))); - } - - @Test - public void containsId_withFreeformQueryAndNonexistingId_returnsFalse() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Assert.assertFalse(container - .containsId(new RowId(new Object[] { 1337 }))); - } - - @Test - public void getContainerProperty_freeformExistingItemIdAndPropertyId_returnsProperty() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - if (AllTests.db == DB.ORACLE) { - Assert.assertEquals( - "Ville", - container - .getContainerProperty( - new RowId(new Object[] { new BigDecimal( - 0 + offset) }), "NAME").getValue()); - } else { - Assert.assertEquals( - "Ville", - container.getContainerProperty( - new RowId(new Object[] { 0 + offset }), "NAME") - .getValue()); - } - } - - @Test - public void getContainerProperty_freeformExistingItemIdAndNonexistingPropertyId_returnsNull() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Assert.assertNull(container.getContainerProperty(new RowId( - new Object[] { 1 + offset }), "asdf")); - } - - @Test - public void getContainerProperty_freeformNonexistingItemId_returnsNull() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Assert.assertNull(container.getContainerProperty(new RowId( - new Object[] { 1337 + offset }), "NAME")); - } - - @Test - public void getContainerPropertyIds_freeform_returnsIDAndNAME() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Collection<?> propertyIds = container.getContainerPropertyIds(); - Assert.assertEquals(3, propertyIds.size()); - Assert.assertArrayEquals(new String[] { "ID", "NAME", "AGE" }, - propertyIds.toArray()); - } - - @Test - public void getItem_freeformExistingItemId_returnsItem() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Item item; - if (AllTests.db == DB.ORACLE) { - item = container.getItem(new RowId(new Object[] { new BigDecimal( - 0 + offset) })); - } else { - item = container.getItem(new RowId(new Object[] { 0 + offset })); - } - Assert.assertNotNull(item); - Assert.assertEquals("Ville", item.getItemProperty("NAME").getValue()); - } - - @Test - public void getItem_freeform5000RowsWithParameter1337_returnsItemWithId1337() - throws SQLException { - DataGenerator.addFiveThousandPeople(connectionPool); - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Item item; - if (AllTests.db == DB.ORACLE) { - item = container.getItem(new RowId(new Object[] { new BigDecimal( - 1337 + offset) })); - Assert.assertNotNull(item); - Assert.assertEquals(new BigDecimal(1337 + offset), item - .getItemProperty("ID").getValue()); - } else { - item = container.getItem(new RowId(new Object[] { 1337 + offset })); - Assert.assertNotNull(item); - Assert.assertEquals(1337 + offset, item.getItemProperty("ID") - .getValue()); - } - Assert.assertEquals("Person 1337", item.getItemProperty("NAME") - .getValue()); - } - - @Test - public void getItemIds_freeform_returnsItemIdsWithKeys0through3() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Collection<?> itemIds = container.getItemIds(); - Assert.assertEquals(4, itemIds.size()); - RowId zero = new RowId(new Object[] { 0 + offset }); - RowId one = new RowId(new Object[] { 1 + offset }); - RowId two = new RowId(new Object[] { 2 + offset }); - RowId three = new RowId(new Object[] { 3 + offset }); - if (AllTests.db == DB.ORACLE) { - String[] correct = new String[] { "1", "2", "3", "4" }; - List<String> oracle = new ArrayList<String>(); - for (Object o : itemIds) { - oracle.add(o.toString()); - } - Assert.assertArrayEquals(correct, oracle.toArray()); - } else { - Assert.assertArrayEquals(new Object[] { zero, one, two, three }, - itemIds.toArray()); - } - } - - @Test - public void getType_freeformNAMEPropertyId_returnsString() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Assert.assertEquals(String.class, container.getType("NAME")); - } - - @Test - public void getType_freeformIDPropertyId_returnsInteger() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - if (AllTests.db == DB.ORACLE) { - Assert.assertEquals(BigDecimal.class, container.getType("ID")); - } else { - Assert.assertEquals(Integer.class, container.getType("ID")); - } - } - - @Test - public void getType_freeformNonexistingPropertyId_returnsNull() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Assert.assertNull(container.getType("asdf")); - } - - @Test - public void size_freeform_returnsFour() throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Assert.assertEquals(4, container.size()); - } - - @Test - public void size_freeformOneAddedItem_returnsFive() throws SQLException { - Connection conn = connectionPool.reserveConnection(); - Statement statement = conn.createStatement(); - if (AllTests.db == DB.MSSQL) { - statement.executeUpdate("insert into people values('Bengt', '42')"); - } else { - statement - .executeUpdate("insert into people values(default, 'Bengt', '42')"); - } - statement.close(); - conn.commit(); - connectionPool.releaseConnection(conn); - - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Assert.assertEquals(5, container.size()); - } - - @Test - public void indexOfId_freeformWithParameterThree_returnsThree() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - if (AllTests.db == DB.ORACLE) { - Assert.assertEquals(3, container.indexOfId(new RowId( - new Object[] { new BigDecimal(3 + offset) }))); - } else { - Assert.assertEquals(3, - container.indexOfId(new RowId(new Object[] { 3 + offset }))); - } - } - - @Test - public void indexOfId_freeform5000RowsWithParameter1337_returns1337() - throws SQLException { - DataGenerator.addFiveThousandPeople(connectionPool); - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people ORDER BY \"ID\" ASC", connectionPool, - "ID")); - if (AllTests.db == DB.ORACLE) { - container.getItem(new RowId(new Object[] { new BigDecimal( - 1337 + offset) })); - Assert.assertEquals(1337, container.indexOfId(new RowId( - new Object[] { new BigDecimal(1337 + offset) }))); - } else { - container.getItem(new RowId(new Object[] { 1337 + offset })); - Assert.assertEquals(1337, container.indexOfId(new RowId( - new Object[] { 1337 + offset }))); - } - } - - @Test - public void getIdByIndex_freeform5000rowsIndex1337_returnsRowId1337() - throws SQLException { - DataGenerator.addFiveThousandPeople(connectionPool); - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people ORDER BY \"ID\" ASC", connectionPool, - "ID")); - Object itemId = container.getIdByIndex(1337); - if (AllTests.db == DB.ORACLE) { - Assert.assertEquals(new RowId(new Object[] { new BigDecimal( - 1337 + offset) }), itemId); - } else { - Assert.assertEquals(new RowId(new Object[] { 1337 + offset }), - itemId); - } - } - - @SuppressWarnings("unchecked") - @Test - public void getIdByIndex_freeformWithPaging5000rowsIndex1337_returnsRowId1337() - throws SQLException { - DataGenerator.addFiveThousandPeople(connectionPool); - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - connectionPool, "ID"); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - EasyMock.expect( - delegate.getQueryString(EasyMock.anyInt(), EasyMock.anyInt())) - .andAnswer(new IAnswer<String>() { - @Override - public String answer() throws Throwable { - Object[] args = EasyMock.getCurrentArguments(); - int offset = (Integer) (args[0]); - int limit = (Integer) (args[1]); - if (AllTests.db == DB.MSSQL) { - int start = offset + 1; - int end = offset + limit + 1; - String q = "SELECT * FROM (SELECT row_number() OVER" - + " ( ORDER BY \"ID\" ASC) AS rownum, * FROM people)" - + " AS a WHERE a.rownum BETWEEN " - + start - + " AND " + end; - return q; - } else if (AllTests.db == DB.ORACLE) { - int start = offset + 1; - int end = offset + limit + 1; - String q = "SELECT * FROM (SELECT x.*, ROWNUM AS r FROM" - + " (SELECT * FROM people ORDER BY \"ID\" ASC) x) " - + " WHERE r BETWEEN " - + start - + " AND " - + end; - return q; - } else { - return "SELECT * FROM people LIMIT " + limit - + " OFFSET " + offset; - } - } - }).anyTimes(); - delegate.setFilters(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setFilters(EasyMock.isA(List.class)); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(EasyMock.isA(List.class)); - EasyMock.expectLastCall().anyTimes(); - EasyMock.expect(delegate.getCountQuery()) - .andThrow(new UnsupportedOperationException()).anyTimes(); - EasyMock.replay(delegate); - query.setDelegate(delegate); - SQLContainer container = new SQLContainer(query); - Object itemId = container.getIdByIndex(1337); - if (AllTests.db == DB.ORACLE) { - Assert.assertEquals( - new RowId(new Object[] { 1337 + offset }).toString(), - itemId.toString()); - } else { - Assert.assertEquals(new RowId(new Object[] { 1337 + offset }), - itemId); - } - } - - @Test - public void nextItemId_freeformCurrentItem1337_returnsItem1338() - throws SQLException { - DataGenerator.addFiveThousandPeople(connectionPool); - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people ORDER BY \"ID\" ASC", connectionPool, - "ID")); - Object itemId = container.getIdByIndex(1337); - if (AllTests.db == DB.ORACLE) { - Assert.assertEquals( - new RowId(new Object[] { 1338 + offset }).toString(), - container.nextItemId(itemId).toString()); - } else { - Assert.assertEquals(new RowId(new Object[] { 1338 + offset }), - container.nextItemId(itemId)); - } - } - - @Test - public void prevItemId_freeformCurrentItem1337_returns1336() - throws SQLException { - DataGenerator.addFiveThousandPeople(connectionPool); - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people ORDER BY \"ID\" ASC", connectionPool, - "ID")); - Object itemId = container.getIdByIndex(1337); - if (AllTests.db == DB.ORACLE) { - Assert.assertEquals( - new RowId(new Object[] { 1336 + offset }).toString(), - container.prevItemId(itemId).toString()); - } else { - Assert.assertEquals(new RowId(new Object[] { 1336 + offset }), - container.prevItemId(itemId)); - } - } - - @Test - public void firstItemId_freeform_returnsItemId0() throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - if (AllTests.db == DB.ORACLE) { - Assert.assertEquals( - new RowId(new Object[] { 0 + offset }).toString(), - container.firstItemId().toString()); - } else { - Assert.assertEquals(new RowId(new Object[] { 0 + offset }), - container.firstItemId()); - } - } - - @Test - public void lastItemId_freeform5000Rows_returnsItemId4999() - throws SQLException { - DataGenerator.addFiveThousandPeople(connectionPool); - - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people ORDER BY \"ID\" ASC", connectionPool, - "ID")); - if (AllTests.db == DB.ORACLE) { - Assert.assertEquals( - new RowId(new Object[] { 4999 + offset }).toString(), - container.lastItemId().toString()); - } else { - Assert.assertEquals(new RowId(new Object[] { 4999 + offset }), - container.lastItemId()); - } - } - - @Test - public void isFirstId_freeformActualFirstId_returnsTrue() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - if (AllTests.db == DB.ORACLE) { - Assert.assertTrue(container.isFirstId(new RowId( - new Object[] { new BigDecimal(0 + offset) }))); - } else { - Assert.assertTrue(container.isFirstId(new RowId( - new Object[] { 0 + offset }))); - } - } - - @Test - public void isFirstId_freeformSecondId_returnsFalse() throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - if (AllTests.db == DB.ORACLE) { - Assert.assertFalse(container.isFirstId(new RowId( - new Object[] { new BigDecimal(1 + offset) }))); - } else { - Assert.assertFalse(container.isFirstId(new RowId( - new Object[] { 1 + offset }))); - } - } - - @Test - public void isLastId_freeformSecondId_returnsFalse() throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - if (AllTests.db == DB.ORACLE) { - Assert.assertFalse(container.isLastId(new RowId( - new Object[] { new BigDecimal(1 + offset) }))); - } else { - Assert.assertFalse(container.isLastId(new RowId( - new Object[] { 1 + offset }))); - } - } - - @Test - public void isLastId_freeformLastId_returnsTrue() throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - if (AllTests.db == DB.ORACLE) { - Assert.assertTrue(container.isLastId(new RowId( - new Object[] { new BigDecimal(3 + offset) }))); - } else { - Assert.assertTrue(container.isLastId(new RowId( - new Object[] { 3 + offset }))); - } - } - - @Test - public void isLastId_freeform5000RowsLastId_returnsTrue() - throws SQLException { - DataGenerator.addFiveThousandPeople(connectionPool); - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people ORDER BY \"ID\" ASC", connectionPool, - "ID")); - if (AllTests.db == DB.ORACLE) { - Assert.assertTrue(container.isLastId(new RowId( - new Object[] { new BigDecimal(4999 + offset) }))); - } else { - Assert.assertTrue(container.isLastId(new RowId( - new Object[] { 4999 + offset }))); - } - } - - @Test - public void refresh_freeform_sizeShouldUpdate() throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Assert.assertEquals(4, container.size()); - DataGenerator.addFiveThousandPeople(connectionPool); - container.refresh(); - Assert.assertEquals(5000, container.size()); - } - - @Test - public void refresh_freeformWithoutCallingRefresh_sizeShouldNotUpdate() - throws SQLException { - // Yeah, this is a weird one. We're testing that the size doesn't update - // after adding lots of items unless we call refresh inbetween. This to - // make sure that the refresh method actually refreshes stuff and isn't - // a NOP. - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Assert.assertEquals(4, container.size()); - DataGenerator.addFiveThousandPeople(connectionPool); - Assert.assertEquals(4, container.size()); - } - - @Test - public void setAutoCommit_freeform_shouldSucceed() throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - container.setAutoCommit(true); - Assert.assertTrue(container.isAutoCommit()); - container.setAutoCommit(false); - Assert.assertFalse(container.isAutoCommit()); - } - - @Test - public void getPageLength_freeform_returnsDefault100() throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Assert.assertEquals(100, container.getPageLength()); - } - - @Test - public void setPageLength_freeform_shouldSucceed() throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - container.setPageLength(20); - Assert.assertEquals(20, container.getPageLength()); - container.setPageLength(200); - Assert.assertEquals(200, container.getPageLength()); - } - - @Test(expected = UnsupportedOperationException.class) - public void addContainerProperty_normal_isUnsupported() throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - container.addContainerProperty("asdf", String.class, ""); - } - - @Test(expected = UnsupportedOperationException.class) - public void removeContainerProperty_normal_isUnsupported() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - container.removeContainerProperty("asdf"); - } - - @Test(expected = UnsupportedOperationException.class) - public void addItemObject_normal_isUnsupported() throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - container.addItem("asdf"); - } - - @Test(expected = UnsupportedOperationException.class) - public void addItemAfterObjectObject_normal_isUnsupported() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - container.addItemAfter("asdf", "foo"); - } - - @Test(expected = UnsupportedOperationException.class) - public void addItemAtIntObject_normal_isUnsupported() throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - container.addItemAt(2, "asdf"); - } - - @Test(expected = UnsupportedOperationException.class) - public void addItemAtInt_normal_isUnsupported() throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - container.addItemAt(2); - } - - @Test(expected = UnsupportedOperationException.class) - public void addItemAfterObject_normal_isUnsupported() throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - container.addItemAfter("asdf"); - } - - @Test - public void addItem_freeformAddOneNewItem_returnsItemId() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object itemId = container.addItem(); - Assert.assertNotNull(itemId); - } - - @Test - public void addItem_freeformAddOneNewItem_shouldChangeSize() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - int size = container.size(); - container.addItem(); - Assert.assertEquals(size + 1, container.size()); - } - - @Test - public void addItem_freeformAddTwoNewItems_shouldChangeSize() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - int size = container.size(); - Object id1 = container.addItem(); - Object id2 = container.addItem(); - Assert.assertEquals(size + 2, container.size()); - Assert.assertNotSame(id1, id2); - Assert.assertFalse(id1.equals(id2)); - } - - @Test - public void nextItemId_freeformNewlyAddedItem_returnsNewlyAdded() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object lastId = container.lastItemId(); - Object id = container.addItem(); - Assert.assertEquals(id, container.nextItemId(lastId)); - } - - @Test - public void lastItemId_freeformNewlyAddedItem_returnsNewlyAdded() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object lastId = container.lastItemId(); - Object id = container.addItem(); - Assert.assertEquals(id, container.lastItemId()); - Assert.assertNotSame(lastId, container.lastItemId()); - } - - @Test - public void indexOfId_freeformNewlyAddedItem_returnsFour() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object id = container.addItem(); - Assert.assertEquals(4, container.indexOfId(id)); - } - - @Test - public void getItem_freeformNewlyAddedItem_returnsNewlyAdded() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object id = container.addItem(); - Assert.assertNotNull(container.getItem(id)); - } - - @Test - public void getItem_freeformNewlyAddedItemAndFiltered_returnsNull() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - container.addContainerFilter(new Equal("NAME", "asdf")); - Object id = container.addItem(); - Assert.assertNull(container.getItem(id)); - } - - @Test - public void getItemUnfiltered_freeformNewlyAddedItemAndFiltered_returnsNewlyAdded() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - container.addContainerFilter(new Equal("NAME", "asdf")); - Object id = container.addItem(); - Assert.assertNotNull(container.getItemUnfiltered(id)); - } - - @Test - public void getItemIds_freeformNewlyAddedItem_containsNewlyAdded() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object id = container.addItem(); - Assert.assertTrue(container.getItemIds().contains(id)); - } - - @Test - public void getContainerProperty_freeformNewlyAddedItem_returnsPropertyOfNewlyAddedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object id = container.addItem(); - Item item = container.getItem(id); - item.getItemProperty("NAME").setValue("asdf"); - Assert.assertEquals("asdf", container.getContainerProperty(id, "NAME") - .getValue()); - } - - @Test - public void containsId_freeformNewlyAddedItem_returnsTrue() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object id = container.addItem(); - Assert.assertTrue(container.containsId(id)); - } - - @Test - public void prevItemId_freeformTwoNewlyAddedItems_returnsFirstAddedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object id1 = container.addItem(); - Object id2 = container.addItem(); - Assert.assertEquals(id1, container.prevItemId(id2)); - } - - @Test - public void firstItemId_freeformEmptyResultSet_returnsFirstAddedItem() - throws SQLException { - DataGenerator.createGarbage(connectionPool); - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM GARBAGE", connectionPool, "ID")); - Object id = container.addItem(); - Assert.assertSame(id, container.firstItemId()); - } - - @Test - public void isFirstId_freeformEmptyResultSet_returnsFirstAddedItem() - throws SQLException { - DataGenerator.createGarbage(connectionPool); - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM GARBAGE", connectionPool, "ID")); - Object id = container.addItem(); - Assert.assertTrue(container.isFirstId(id)); - } - - @Test - public void isLastId_freeformOneItemAdded_returnsTrueForAddedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object id = container.addItem(); - Assert.assertTrue(container.isLastId(id)); - } - - @Test - public void isLastId_freeformTwoItemsAdded_returnsTrueForLastAddedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - container.addItem(); - Object id2 = container.addItem(); - Assert.assertTrue(container.isLastId(id2)); - } - - @Test - public void getIdByIndex_freeformOneItemAddedLastIndexInContainer_returnsAddedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object id = container.addItem(); - Assert.assertEquals(id, container.getIdByIndex(container.size() - 1)); - } - - @Test - public void removeItem_freeformNoAddedItems_removesItemFromContainer() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - int size = container.size(); - Object id = container.firstItemId(); - Assert.assertTrue(container.removeItem(id)); - Assert.assertNotSame(id, container.firstItemId()); - Assert.assertEquals(size - 1, container.size()); - } - - @Test - public void containsId_freeformRemovedItem_returnsFalse() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object id = container.firstItemId(); - Assert.assertTrue(container.removeItem(id)); - Assert.assertFalse(container.containsId(id)); - } - - @Test - public void containsId_unknownObject() throws SQLException { - - Handler ensureNoLogging = new Handler() { - - @Override - public void publish(LogRecord record) { - Assert.fail("No messages should be logged"); - - } - - @Override - public void flush() { - } - - @Override - public void close() throws SecurityException { - } - }; - - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Logger logger = Logger.getLogger(SQLContainer.class.getName()); - - logger.addHandler(ensureNoLogging); - try { - Assert.assertFalse(container.containsId(new Object())); - } finally { - logger.removeHandler(ensureNoLogging); - } - } - - @Test - public void removeItem_freeformOneAddedItem_removesTheAddedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object id = container.addItem(); - int size = container.size(); - Assert.assertTrue(container.removeItem(id)); - Assert.assertFalse(container.containsId(id)); - Assert.assertEquals(size - 1, container.size()); - } - - @Test - public void getItem_freeformItemRemoved_returnsNull() throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object id = container.firstItemId(); - Assert.assertTrue(container.removeItem(id)); - Assert.assertNull(container.getItem(id)); - } - - @Test - public void getItem_freeformAddedItemRemoved_returnsNull() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object id = container.addItem(); - Assert.assertNotNull(container.getItem(id)); - Assert.assertTrue(container.removeItem(id)); - Assert.assertNull(container.getItem(id)); - } - - @Test - public void getItemIds_freeformItemRemoved_shouldNotContainRemovedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object id = container.firstItemId(); - Assert.assertTrue(container.getItemIds().contains(id)); - Assert.assertTrue(container.removeItem(id)); - Assert.assertFalse(container.getItemIds().contains(id)); - } - - @Test - public void getItemIds_freeformAddedItemRemoved_shouldNotContainRemovedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object id = container.addItem(); - Assert.assertTrue(container.getItemIds().contains(id)); - Assert.assertTrue(container.removeItem(id)); - Assert.assertFalse(container.getItemIds().contains(id)); - } - - @Test - public void containsId_freeformItemRemoved_returnsFalse() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object id = container.firstItemId(); - Assert.assertTrue(container.containsId(id)); - Assert.assertTrue(container.removeItem(id)); - Assert.assertFalse(container.containsId(id)); - } - - @Test - public void containsId_freeformAddedItemRemoved_returnsFalse() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object id = container.addItem(); - Assert.assertTrue(container.containsId(id)); - Assert.assertTrue(container.removeItem(id)); - Assert.assertFalse(container.containsId(id)); - } - - @Test - public void nextItemId_freeformItemRemoved_skipsRemovedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object first = container.getIdByIndex(0); - Object second = container.getIdByIndex(1); - Object third = container.getIdByIndex(2); - Assert.assertTrue(container.removeItem(second)); - Assert.assertEquals(third, container.nextItemId(first)); - } - - @Test - public void nextItemId_freeformAddedItemRemoved_skipsRemovedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object first = container.lastItemId(); - Object second = container.addItem(); - Object third = container.addItem(); - Assert.assertTrue(container.removeItem(second)); - Assert.assertEquals(third, container.nextItemId(first)); - } - - @Test - public void prevItemId_freeformItemRemoved_skipsRemovedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object first = container.getIdByIndex(0); - Object second = container.getIdByIndex(1); - Object third = container.getIdByIndex(2); - Assert.assertTrue(container.removeItem(second)); - Assert.assertEquals(first, container.prevItemId(third)); - } - - @Test - public void prevItemId_freeformAddedItemRemoved_skipsRemovedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object first = container.lastItemId(); - Object second = container.addItem(); - Object third = container.addItem(); - Assert.assertTrue(container.removeItem(second)); - Assert.assertEquals(first, container.prevItemId(third)); - } - - @Test - public void firstItemId_freeformFirstItemRemoved_resultChanges() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object first = container.firstItemId(); - Assert.assertTrue(container.removeItem(first)); - Assert.assertNotSame(first, container.firstItemId()); - } - - @Test - public void firstItemId_freeformNewlyAddedFirstItemRemoved_resultChanges() - throws SQLException { - DataGenerator.createGarbage(connectionPool); - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM GARBAGE", connectionPool, "ID")); - Object first = container.addItem(); - Object second = container.addItem(); - Assert.assertSame(first, container.firstItemId()); - Assert.assertTrue(container.removeItem(first)); - Assert.assertSame(second, container.firstItemId()); - } - - @Test - public void lastItemId_freeformLastItemRemoved_resultChanges() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object last = container.lastItemId(); - Assert.assertTrue(container.removeItem(last)); - Assert.assertNotSame(last, container.lastItemId()); - } - - @Test - public void lastItemId_freeformAddedLastItemRemoved_resultChanges() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object last = container.addItem(); - Assert.assertSame(last, container.lastItemId()); - Assert.assertTrue(container.removeItem(last)); - Assert.assertNotSame(last, container.lastItemId()); - } - - @Test - public void isFirstId_freeformFirstItemRemoved_returnsFalse() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object first = container.firstItemId(); - Assert.assertTrue(container.removeItem(first)); - Assert.assertFalse(container.isFirstId(first)); - } - - @Test - public void isFirstId_freeformAddedFirstItemRemoved_returnsFalse() - throws SQLException { - DataGenerator.createGarbage(connectionPool); - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM GARBAGE", connectionPool, "ID")); - Object first = container.addItem(); - container.addItem(); - Assert.assertSame(first, container.firstItemId()); - Assert.assertTrue(container.removeItem(first)); - Assert.assertFalse(container.isFirstId(first)); - } - - @Test - public void isLastId_freeformLastItemRemoved_returnsFalse() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object last = container.lastItemId(); - Assert.assertTrue(container.removeItem(last)); - Assert.assertFalse(container.isLastId(last)); - } - - @Test - public void isLastId_freeformAddedLastItemRemoved_returnsFalse() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object last = container.addItem(); - Assert.assertSame(last, container.lastItemId()); - Assert.assertTrue(container.removeItem(last)); - Assert.assertFalse(container.isLastId(last)); - } - - @Test - public void indexOfId_freeformItemRemoved_returnsNegOne() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object id = container.getIdByIndex(2); - Assert.assertTrue(container.removeItem(id)); - Assert.assertEquals(-1, container.indexOfId(id)); - } - - @Test - public void indexOfId_freeformAddedItemRemoved_returnsNegOne() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object id = container.addItem(); - Assert.assertTrue(container.indexOfId(id) != -1); - Assert.assertTrue(container.removeItem(id)); - Assert.assertEquals(-1, container.indexOfId(id)); - } - - @Test - public void getIdByIndex_freeformItemRemoved_resultChanges() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object id = container.getIdByIndex(2); - Assert.assertTrue(container.removeItem(id)); - Assert.assertNotSame(id, container.getIdByIndex(2)); - } - - @Test - public void getIdByIndex_freeformAddedItemRemoved_resultChanges() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object id = container.addItem(); - container.addItem(); - int index = container.indexOfId(id); - Assert.assertTrue(container.removeItem(id)); - Assert.assertNotSame(id, container.getIdByIndex(index)); - } - - @Test - public void removeAllItems_freeform_shouldSucceed() throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Assert.assertTrue(container.removeAllItems()); - Assert.assertEquals(0, container.size()); - } - - @Test - public void removeAllItems_freeformAddedItems_shouldSucceed() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - container.addItem(); - container.addItem(); - Assert.assertTrue(container.removeAllItems()); - Assert.assertEquals(0, container.size()); - } - - @SuppressWarnings("unchecked") - @Test - public void commit_freeformAddedItem_shouldBeWrittenToDB() - throws SQLException { - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - EasyMock.expect( - delegate.storeRow(EasyMock.isA(Connection.class), - EasyMock.isA(RowItem.class))) - .andAnswer(new IAnswer<Integer>() { - @Override - public Integer answer() throws Throwable { - Connection conn = (Connection) EasyMock - .getCurrentArguments()[0]; - RowItem item = (RowItem) EasyMock.getCurrentArguments()[1]; - Statement statement = conn.createStatement(); - if (AllTests.db == DB.MSSQL) { - statement - .executeUpdate("insert into people values('" - + item.getItemProperty("NAME") - .getValue() - + "', '" - + item.getItemProperty("AGE") - .getValue() + "')"); - } else { - statement - .executeUpdate("insert into people values(default, '" - + item.getItemProperty("NAME") - .getValue() - + "', '" - + item.getItemProperty("AGE") - .getValue() + "')"); - } - statement.close(); - conn.commit(); - connectionPool.releaseConnection(conn); - return 1; - } - }).anyTimes(); - EasyMock.expect( - delegate.getQueryString(EasyMock.anyInt(), EasyMock.anyInt())) - .andAnswer(new IAnswer<String>() { - @Override - public String answer() throws Throwable { - Object[] args = EasyMock.getCurrentArguments(); - int offset = (Integer) (args[0]); - int limit = (Integer) (args[1]); - if (AllTests.db == DB.MSSQL) { - int start = offset + 1; - int end = offset + limit + 1; - String q = "SELECT * FROM (SELECT row_number() OVER" - + " ( ORDER BY \"ID\" ASC) AS rownum, * FROM people)" - + " AS a WHERE a.rownum BETWEEN " - + start - + " AND " + end; - return q; - } else if (AllTests.db == DB.ORACLE) { - int start = offset + 1; - int end = offset + limit + 1; - String q = "SELECT * FROM (SELECT x.*, ROWNUM AS r FROM" - + " (SELECT * FROM people ORDER BY \"ID\" ASC) x) " - + " WHERE r BETWEEN " - + start - + " AND " - + end; - return q; - } else { - return "SELECT * FROM people LIMIT " + limit - + " OFFSET " + offset; - } - } - }).anyTimes(); - delegate.setFilters(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setFilters(EasyMock.isA(List.class)); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(EasyMock.isA(List.class)); - EasyMock.expectLastCall().anyTimes(); - EasyMock.expect(delegate.getCountQuery()) - .andThrow(new UnsupportedOperationException()).anyTimes(); - - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - connectionPool, "ID"); - query.setDelegate(delegate); - EasyMock.replay(delegate); - SQLContainer container = new SQLContainer(query); - Object id = container.addItem(); - container.getContainerProperty(id, "NAME").setValue("New Name"); - container.getContainerProperty(id, "AGE").setValue(30); - Assert.assertTrue(id instanceof TemporaryRowId); - Assert.assertSame(id, container.lastItemId()); - container.commit(); - Assert.assertFalse(container.lastItemId() instanceof TemporaryRowId); - Assert.assertEquals("New Name", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - EasyMock.verify(delegate); - } - - @SuppressWarnings("unchecked") - @Test - public void commit_freeformTwoAddedItems_shouldBeWrittenToDB() - throws SQLException { - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - EasyMock.expect( - delegate.storeRow(EasyMock.isA(Connection.class), - EasyMock.isA(RowItem.class))) - .andAnswer(new IAnswer<Integer>() { - @Override - public Integer answer() throws Throwable { - Connection conn = (Connection) EasyMock - .getCurrentArguments()[0]; - RowItem item = (RowItem) EasyMock.getCurrentArguments()[1]; - Statement statement = conn.createStatement(); - if (AllTests.db == DB.MSSQL) { - statement - .executeUpdate("insert into people values('" - + item.getItemProperty("NAME") - .getValue() - + "', '" - + item.getItemProperty("AGE") - .getValue() + "')"); - } else { - statement - .executeUpdate("insert into people values(default, '" - + item.getItemProperty("NAME") - .getValue() - + "', '" - + item.getItemProperty("AGE") - .getValue() + "')"); - } - statement.close(); - conn.commit(); - connectionPool.releaseConnection(conn); - return 1; - } - }).anyTimes(); - EasyMock.expect( - delegate.getQueryString(EasyMock.anyInt(), EasyMock.anyInt())) - .andAnswer(new IAnswer<String>() { - @Override - public String answer() throws Throwable { - Object[] args = EasyMock.getCurrentArguments(); - int offset = (Integer) (args[0]); - int limit = (Integer) (args[1]); - if (AllTests.db == DB.MSSQL) { - int start = offset + 1; - int end = offset + limit + 1; - String q = "SELECT * FROM (SELECT row_number() OVER" - + " ( ORDER BY \"ID\" ASC) AS rownum, * FROM people)" - + " AS a WHERE a.rownum BETWEEN " - + start - + " AND " + end; - return q; - } else if (AllTests.db == DB.ORACLE) { - int start = offset + 1; - int end = offset + limit + 1; - String q = "SELECT * FROM (SELECT x.*, ROWNUM AS r FROM" - + " (SELECT * FROM people ORDER BY \"ID\" ASC) x) " - + " WHERE r BETWEEN " - + start - + " AND " - + end; - return q; - } else { - return "SELECT * FROM people LIMIT " + limit - + " OFFSET " + offset; - } - } - }).anyTimes(); - delegate.setFilters(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setFilters(EasyMock.isA(List.class)); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(EasyMock.isA(List.class)); - EasyMock.expectLastCall().anyTimes(); - EasyMock.expect(delegate.getCountQuery()) - .andThrow(new UnsupportedOperationException()).anyTimes(); - - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - connectionPool, "ID"); - query.setDelegate(delegate); - EasyMock.replay(delegate); - SQLContainer container = new SQLContainer(query); - Object id = container.addItem(); - Object id2 = container.addItem(); - container.getContainerProperty(id, "NAME").setValue("Herbert"); - container.getContainerProperty(id, "AGE").setValue(30); - container.getContainerProperty(id2, "NAME").setValue("Larry"); - container.getContainerProperty(id2, "AGE").setValue(50); - Assert.assertTrue(id2 instanceof TemporaryRowId); - Assert.assertSame(id2, container.lastItemId()); - container.commit(); - Object nextToLast = container.getIdByIndex(container.size() - 2); - Assert.assertFalse(nextToLast instanceof TemporaryRowId); - Assert.assertEquals("Herbert", - container.getContainerProperty(nextToLast, "NAME").getValue()); - Assert.assertFalse(container.lastItemId() instanceof TemporaryRowId); - Assert.assertEquals("Larry", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - EasyMock.verify(delegate); - } - - @SuppressWarnings("unchecked") - @Test - public void commit_freeformRemovedItem_shouldBeRemovedFromDB() - throws SQLException { - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - EasyMock.expect( - delegate.removeRow(EasyMock.isA(Connection.class), - EasyMock.isA(RowItem.class))) - .andAnswer(new IAnswer<Boolean>() { - @Override - public Boolean answer() throws Throwable { - Connection conn = (Connection) EasyMock - .getCurrentArguments()[0]; - RowItem item = (RowItem) EasyMock.getCurrentArguments()[1]; - Statement statement = conn.createStatement(); - statement - .executeUpdate("DELETE FROM people WHERE \"ID\"=" - + item.getItemProperty("ID").getValue()); - statement.close(); - return true; - } - }).anyTimes(); - EasyMock.expect( - delegate.getQueryString(EasyMock.anyInt(), EasyMock.anyInt())) - .andAnswer(new IAnswer<String>() { - @Override - public String answer() throws Throwable { - Object[] args = EasyMock.getCurrentArguments(); - int offset = (Integer) (args[0]); - int limit = (Integer) (args[1]); - if (AllTests.db == DB.MSSQL) { - int start = offset + 1; - int end = offset + limit + 1; - String q = "SELECT * FROM (SELECT row_number() OVER" - + " ( ORDER BY \"ID\" ASC) AS rownum, * FROM people)" - + " AS a WHERE a.rownum BETWEEN " - + start - + " AND " + end; - return q; - } else if (AllTests.db == DB.ORACLE) { - int start = offset + 1; - int end = offset + limit + 1; - String q = "SELECT * FROM (SELECT x.*, ROWNUM AS r FROM" - + " (SELECT * FROM people ORDER BY \"ID\" ASC) x) " - + " WHERE r BETWEEN " - + start - + " AND " - + end; - return q; - } else { - return "SELECT * FROM people LIMIT " + limit - + " OFFSET " + offset; - } - } - }).anyTimes(); - delegate.setFilters(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setFilters(EasyMock.isA(List.class)); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(EasyMock.isA(List.class)); - EasyMock.expectLastCall().anyTimes(); - EasyMock.expect(delegate.getCountQuery()) - .andThrow(new UnsupportedOperationException()).anyTimes(); - - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - connectionPool, "ID"); - query.setDelegate(delegate); - EasyMock.replay(delegate); - SQLContainer container = new SQLContainer(query); - Object last = container.lastItemId(); - container.removeItem(last); - container.commit(); - Assert.assertFalse(last.equals(container.lastItemId())); - EasyMock.verify(delegate); - } - - @SuppressWarnings("unchecked") - @Test - public void commit_freeformLastItemUpdated_shouldUpdateRowInDB() - throws SQLException { - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - EasyMock.expect( - delegate.storeRow(EasyMock.isA(Connection.class), - EasyMock.isA(RowItem.class))) - .andAnswer(new IAnswer<Integer>() { - @Override - public Integer answer() throws Throwable { - Connection conn = (Connection) EasyMock - .getCurrentArguments()[0]; - RowItem item = (RowItem) EasyMock.getCurrentArguments()[1]; - Statement statement = conn.createStatement(); - statement.executeUpdate("UPDATE people SET \"NAME\"='" - + item.getItemProperty("NAME").getValue() - + "' WHERE \"ID\"=" - + item.getItemProperty("ID").getValue()); - statement.close(); - conn.commit(); - connectionPool.releaseConnection(conn); - return 1; - } - }).anyTimes(); - EasyMock.expect( - delegate.getQueryString(EasyMock.anyInt(), EasyMock.anyInt())) - .andAnswer(new IAnswer<String>() { - @Override - public String answer() throws Throwable { - Object[] args = EasyMock.getCurrentArguments(); - int offset = (Integer) (args[0]); - int limit = (Integer) (args[1]); - if (AllTests.db == DB.MSSQL) { - int start = offset + 1; - int end = offset + limit + 1; - String q = "SELECT * FROM (SELECT row_number() OVER" - + " ( ORDER BY \"ID\" ASC) AS rownum, * FROM people)" - + " AS a WHERE a.rownum BETWEEN " - + start - + " AND " + end; - return q; - } else if (AllTests.db == DB.ORACLE) { - int start = offset + 1; - int end = offset + limit + 1; - String q = "SELECT * FROM (SELECT x.*, ROWNUM AS r FROM" - + " (SELECT * FROM people ORDER BY \"ID\" ASC) x) " - + " WHERE r BETWEEN " - + start - + " AND " - + end; - return q; - } else { - return "SELECT * FROM people LIMIT " + limit - + " OFFSET " + offset; - } - } - }).anyTimes(); - delegate.setFilters(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setFilters(EasyMock.isA(List.class)); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(EasyMock.isA(List.class)); - EasyMock.expectLastCall().anyTimes(); - EasyMock.expect(delegate.getCountQuery()) - .andThrow(new UnsupportedOperationException()).anyTimes(); - - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - connectionPool, "ID"); - query.setDelegate(delegate); - EasyMock.replay(delegate); - SQLContainer container = new SQLContainer(query); - Object last = container.lastItemId(); - container.getContainerProperty(last, "NAME").setValue("Donald"); - container.commit(); - Assert.assertEquals("Donald", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - EasyMock.verify(delegate); - } - - @Test - public void rollback_freeformItemAdded_discardsAddedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - int size = container.size(); - Object id = container.addItem(); - container.getContainerProperty(id, "NAME").setValue("foo"); - Assert.assertEquals(size + 1, container.size()); - container.rollback(); - Assert.assertEquals(size, container.size()); - Assert.assertFalse("foo".equals(container.getContainerProperty( - container.lastItemId(), "NAME").getValue())); - } - - @Test - public void rollback_freeformItemRemoved_restoresRemovedItem() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - int size = container.size(); - Object last = container.lastItemId(); - container.removeItem(last); - Assert.assertEquals(size - 1, container.size()); - container.rollback(); - Assert.assertEquals(size, container.size()); - Assert.assertEquals(last, container.lastItemId()); - } - - @Test - public void rollback_freeformItemChanged_discardsChanges() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Object last = container.lastItemId(); - container.getContainerProperty(last, "NAME").setValue("foo"); - container.rollback(); - Assert.assertFalse("foo".equals(container.getContainerProperty( - container.lastItemId(), "NAME").getValue())); - } - - @Test - public void itemChangeNotification_freeform_isModifiedReturnsTrue() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Assert.assertFalse(container.isModified()); - RowItem last = (RowItem) container.getItem(container.lastItemId()); - container.itemChangeNotification(last); - Assert.assertTrue(container.isModified()); - } - - @Test - public void itemSetChangeListeners_freeform_shouldFire() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - ItemSetChangeListener listener = EasyMock - .createMock(ItemSetChangeListener.class); - listener.containerItemSetChange(EasyMock.isA(ItemSetChangeEvent.class)); - EasyMock.replay(listener); - - container.addListener(listener); - container.addItem(); - - EasyMock.verify(listener); - } - - @Test - public void itemSetChangeListeners_freeformItemRemoved_shouldFire() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - ItemSetChangeListener listener = EasyMock - .createMock(ItemSetChangeListener.class); - listener.containerItemSetChange(EasyMock.isA(ItemSetChangeEvent.class)); - EasyMock.expectLastCall().anyTimes(); - EasyMock.replay(listener); - - container.addListener(listener); - container.removeItem(container.lastItemId()); - - EasyMock.verify(listener); - } - - @Test - public void removeListener_freeform_shouldNotFire() throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - ItemSetChangeListener listener = EasyMock - .createMock(ItemSetChangeListener.class); - EasyMock.replay(listener); - - container.addListener(listener); - container.removeListener(listener); - container.addItem(); - - EasyMock.verify(listener); - } - - @Test - public void isModified_freeformRemovedItem_returnsTrue() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Assert.assertFalse(container.isModified()); - container.removeItem(container.lastItemId()); - Assert.assertTrue(container.isModified()); - } - - @Test - public void isModified_freeformAddedItem_returnsTrue() throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Assert.assertFalse(container.isModified()); - container.addItem(); - Assert.assertTrue(container.isModified()); - } - - @Test - public void isModified_freeformChangedItem_returnsTrue() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Assert.assertFalse(container.isModified()); - container.getContainerProperty(container.lastItemId(), "NAME") - .setValue("foo"); - Assert.assertTrue(container.isModified()); - } - - @Test - public void getSortableContainerPropertyIds_freeform_returnsAllPropertyIds() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - Collection<?> sortableIds = container.getSortableContainerPropertyIds(); - Assert.assertTrue(sortableIds.contains("ID")); - Assert.assertTrue(sortableIds.contains("NAME")); - Assert.assertTrue(sortableIds.contains("AGE")); - Assert.assertEquals(3, sortableIds.size()); - } - - @SuppressWarnings("unchecked") - @Test - public void addOrderBy_freeform_shouldReorderResults() throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - connectionPool, "ID"); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - final ArrayList<OrderBy> orderBys = new ArrayList<OrderBy>(); - delegate.setFilters(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setFilters(EasyMock.isA(List.class)); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(EasyMock.isA(List.class)); - EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { - @Override - public Object answer() throws Throwable { - List<OrderBy> orders = (List<OrderBy>) EasyMock - .getCurrentArguments()[0]; - orderBys.clear(); - orderBys.addAll(orders); - return null; - } - }).anyTimes(); - EasyMock.expect( - delegate.getQueryString(EasyMock.anyInt(), EasyMock.anyInt())) - .andAnswer(new IAnswer<String>() { - @Override - public String answer() throws Throwable { - Object[] args = EasyMock.getCurrentArguments(); - int offset = (Integer) (args[0]); - int limit = (Integer) (args[1]); - if (AllTests.db == DB.MSSQL) { - SQLGenerator gen = new MSSQLGenerator(); - if (orderBys == null || orderBys.isEmpty()) { - List<OrderBy> ob = new ArrayList<OrderBy>(); - ob.add(new OrderBy("ID", true)); - return gen.generateSelectQuery("people", null, - ob, offset, limit, null) - .getQueryString(); - } else { - return gen.generateSelectQuery("people", null, - orderBys, offset, limit, null) - .getQueryString(); - } - } else if (AllTests.db == DB.ORACLE) { - SQLGenerator gen = new OracleGenerator(); - if (orderBys == null || orderBys.isEmpty()) { - List<OrderBy> ob = new ArrayList<OrderBy>(); - ob.add(new OrderBy("ID", true)); - return gen.generateSelectQuery("people", null, - ob, offset, limit, null) - .getQueryString(); - } else { - return gen.generateSelectQuery("people", null, - orderBys, offset, limit, null) - .getQueryString(); - } - } else { - StringBuffer query = new StringBuffer( - "SELECT * FROM people"); - if (!orderBys.isEmpty()) { - query.append(" ORDER BY "); - for (OrderBy orderBy : orderBys) { - query.append("\"" + orderBy.getColumn() - + "\""); - if (orderBy.isAscending()) { - query.append(" ASC"); - } else { - query.append(" DESC"); - } - } - } - query.append(" LIMIT ").append(limit) - .append(" OFFSET ").append(offset); - return query.toString(); - } - } - }).anyTimes(); - EasyMock.expect(delegate.getCountQuery()) - .andThrow(new UnsupportedOperationException()).anyTimes(); - - EasyMock.replay(delegate); - query.setDelegate(delegate); - SQLContainer container = new SQLContainer(query); - // Ville, Kalle, Pelle, Börje - Assert.assertEquals("Ville", - container.getContainerProperty(container.firstItemId(), "NAME") - .getValue()); - Assert.assertEquals("Börje", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - - container.addOrderBy(new OrderBy("NAME", true)); - // Börje, Kalle, Pelle, Ville - Assert.assertEquals("Börje", - container.getContainerProperty(container.firstItemId(), "NAME") - .getValue()); - Assert.assertEquals("Ville", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - - EasyMock.verify(delegate); - } - - @Test(expected = IllegalArgumentException.class) - public void addOrderBy_freeformIllegalColumn_shouldFail() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", connectionPool, "ID")); - container.addOrderBy(new OrderBy("asdf", true)); - } - - @SuppressWarnings("unchecked") - @Test - public void sort_freeform_sortsByName() throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - connectionPool, "ID"); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - final ArrayList<OrderBy> orderBys = new ArrayList<OrderBy>(); - delegate.setFilters(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setFilters(EasyMock.isA(List.class)); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(EasyMock.isA(List.class)); - EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { - @Override - public Object answer() throws Throwable { - List<OrderBy> orders = (List<OrderBy>) EasyMock - .getCurrentArguments()[0]; - orderBys.clear(); - orderBys.addAll(orders); - return null; - } - }).anyTimes(); - EasyMock.expect( - delegate.getQueryString(EasyMock.anyInt(), EasyMock.anyInt())) - .andAnswer(new IAnswer<String>() { - @Override - public String answer() throws Throwable { - Object[] args = EasyMock.getCurrentArguments(); - int offset = (Integer) (args[0]); - int limit = (Integer) (args[1]); - if (AllTests.db == DB.MSSQL) { - SQLGenerator gen = new MSSQLGenerator(); - if (orderBys == null || orderBys.isEmpty()) { - List<OrderBy> ob = new ArrayList<OrderBy>(); - ob.add(new OrderBy("ID", true)); - return gen.generateSelectQuery("people", null, - ob, offset, limit, null) - .getQueryString(); - } else { - return gen.generateSelectQuery("people", null, - orderBys, offset, limit, null) - .getQueryString(); - } - } else if (AllTests.db == DB.ORACLE) { - SQLGenerator gen = new OracleGenerator(); - if (orderBys == null || orderBys.isEmpty()) { - List<OrderBy> ob = new ArrayList<OrderBy>(); - ob.add(new OrderBy("ID", true)); - return gen.generateSelectQuery("people", null, - ob, offset, limit, null) - .getQueryString(); - } else { - return gen.generateSelectQuery("people", null, - orderBys, offset, limit, null) - .getQueryString(); - } - } else { - StringBuffer query = new StringBuffer( - "SELECT * FROM people"); - if (!orderBys.isEmpty()) { - query.append(" ORDER BY "); - for (OrderBy orderBy : orderBys) { - query.append("\"" + orderBy.getColumn() - + "\""); - if (orderBy.isAscending()) { - query.append(" ASC"); - } else { - query.append(" DESC"); - } - } - } - query.append(" LIMIT ").append(limit) - .append(" OFFSET ").append(offset); - return query.toString(); - } - } - }).anyTimes(); - EasyMock.expect(delegate.getCountQuery()) - .andThrow(new UnsupportedOperationException()).anyTimes(); - EasyMock.replay(delegate); - - query.setDelegate(delegate); - SQLContainer container = new SQLContainer(query); - // Ville, Kalle, Pelle, Börje - Assert.assertEquals("Ville", - container.getContainerProperty(container.firstItemId(), "NAME") - .getValue()); - Assert.assertEquals("Börje", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - - container.sort(new Object[] { "NAME" }, new boolean[] { true }); - - // Börje, Kalle, Pelle, Ville - Assert.assertEquals("Börje", - container.getContainerProperty(container.firstItemId(), "NAME") - .getValue()); - Assert.assertEquals("Ville", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - - EasyMock.verify(delegate); - } - - @SuppressWarnings("unchecked") - @Test - public void addFilter_freeform_filtersResults() throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - connectionPool, "ID"); - FreeformStatementDelegate delegate = EasyMock - .createMock(FreeformStatementDelegate.class); - final ArrayList<Filter> filters = new ArrayList<Filter>(); - delegate.setFilters(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(EasyMock.isA(List.class)); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setFilters(EasyMock.isA(List.class)); - EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { - @Override - public Object answer() throws Throwable { - List<Filter> orders = (List<Filter>) EasyMock - .getCurrentArguments()[0]; - filters.clear(); - filters.addAll(orders); - return null; - } - }).anyTimes(); - EasyMock.expect( - delegate.getQueryStatement(EasyMock.anyInt(), EasyMock.anyInt())) - .andAnswer(new IAnswer<StatementHelper>() { - @Override - public StatementHelper answer() throws Throwable { - Object[] args = EasyMock.getCurrentArguments(); - int offset = (Integer) (args[0]); - int limit = (Integer) (args[1]); - return FreeformQueryUtil.getQueryWithFilters(filters, - offset, limit); - } - }).anyTimes(); - EasyMock.expect(delegate.getCountStatement()) - .andAnswer(new IAnswer<StatementHelper>() { - @Override - @SuppressWarnings("deprecation") - public StatementHelper answer() throws Throwable { - StatementHelper sh = new StatementHelper(); - StringBuffer query = new StringBuffer( - "SELECT COUNT(*) FROM people"); - if (!filters.isEmpty()) { - query.append(QueryBuilder.getWhereStringForFilters( - filters, sh)); - } - sh.setQueryString(query.toString()); - return sh; - } - }).anyTimes(); - - EasyMock.replay(delegate); - query.setDelegate(delegate); - SQLContainer container = new SQLContainer(query); - // Ville, Kalle, Pelle, Börje - Assert.assertEquals(4, container.size()); - Assert.assertEquals("Börje", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - - container.addContainerFilter(new Like("NAME", "%lle")); - // Ville, Kalle, Pelle - Assert.assertEquals(3, container.size()); - Assert.assertEquals("Pelle", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - - EasyMock.verify(delegate); - } - - @SuppressWarnings("unchecked") - @Test - public void addContainerFilter_filtersResults() throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - connectionPool, "ID"); - FreeformStatementDelegate delegate = EasyMock - .createMock(FreeformStatementDelegate.class); - final ArrayList<Filter> filters = new ArrayList<Filter>(); - delegate.setFilters(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(EasyMock.isA(List.class)); - EasyMock.expectLastCall().anyTimes(); - delegate.setFilters(EasyMock.isA(List.class)); - EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { - @Override - public Object answer() throws Throwable { - List<Filter> orders = (List<Filter>) EasyMock - .getCurrentArguments()[0]; - filters.clear(); - filters.addAll(orders); - return null; - } - }).anyTimes(); - EasyMock.expect( - delegate.getQueryStatement(EasyMock.anyInt(), EasyMock.anyInt())) - .andAnswer(new IAnswer<StatementHelper>() { - @Override - public StatementHelper answer() throws Throwable { - Object[] args = EasyMock.getCurrentArguments(); - int offset = (Integer) (args[0]); - int limit = (Integer) (args[1]); - return FreeformQueryUtil.getQueryWithFilters(filters, - offset, limit); - } - }).anyTimes(); - EasyMock.expect(delegate.getCountStatement()) - .andAnswer(new IAnswer<StatementHelper>() { - @Override - @SuppressWarnings("deprecation") - public StatementHelper answer() throws Throwable { - StatementHelper sh = new StatementHelper(); - StringBuffer query = new StringBuffer( - "SELECT COUNT(*) FROM people"); - if (!filters.isEmpty()) { - query.append(QueryBuilder.getWhereStringForFilters( - filters, sh)); - } - sh.setQueryString(query.toString()); - return sh; - } - }).anyTimes(); - - EasyMock.replay(delegate); - query.setDelegate(delegate); - SQLContainer container = new SQLContainer(query); - // Ville, Kalle, Pelle, Börje - Assert.assertEquals(4, container.size()); - - container.addContainerFilter("NAME", "Vi", false, false); - - // Ville - Assert.assertEquals(1, container.size()); - Assert.assertEquals("Ville", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - - EasyMock.verify(delegate); - } - - @SuppressWarnings("unchecked") - @Test - public void addContainerFilter_ignoreCase_filtersResults() - throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - connectionPool, "ID"); - FreeformStatementDelegate delegate = EasyMock - .createMock(FreeformStatementDelegate.class); - final ArrayList<Filter> filters = new ArrayList<Filter>(); - delegate.setFilters(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(EasyMock.isA(List.class)); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setFilters(EasyMock.isA(List.class)); - EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { - @Override - public Object answer() throws Throwable { - List<Filter> orders = (List<Filter>) EasyMock - .getCurrentArguments()[0]; - filters.clear(); - filters.addAll(orders); - return null; - } - }).anyTimes(); - EasyMock.expect( - delegate.getQueryStatement(EasyMock.anyInt(), EasyMock.anyInt())) - .andAnswer(new IAnswer<StatementHelper>() { - @Override - public StatementHelper answer() throws Throwable { - Object[] args = EasyMock.getCurrentArguments(); - int offset = (Integer) (args[0]); - int limit = (Integer) (args[1]); - return FreeformQueryUtil.getQueryWithFilters(filters, - offset, limit); - } - }).anyTimes(); - EasyMock.expect(delegate.getCountStatement()) - .andAnswer(new IAnswer<StatementHelper>() { - @Override - public StatementHelper answer() throws Throwable { - StatementHelper sh = new StatementHelper(); - StringBuffer query = new StringBuffer( - "SELECT COUNT(*) FROM people"); - if (!filters.isEmpty()) { - query.append(QueryBuilder.getWhereStringForFilters( - filters, sh)); - } - sh.setQueryString(query.toString()); - return sh; - } - }).anyTimes(); - - EasyMock.replay(delegate); - query.setDelegate(delegate); - SQLContainer container = new SQLContainer(query); - // Ville, Kalle, Pelle, Börje - Assert.assertEquals(4, container.size()); - - // FIXME LIKE %asdf% doesn't match a string that begins with asdf - container.addContainerFilter("NAME", "vi", true, true); - - // Ville - Assert.assertEquals(1, container.size()); - Assert.assertEquals("Ville", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - - EasyMock.verify(delegate); - } - - @SuppressWarnings("unchecked") - @Test - public void removeAllContainerFilters_freeform_noFiltering() - throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - connectionPool, "ID"); - FreeformStatementDelegate delegate = EasyMock - .createMock(FreeformStatementDelegate.class); - final ArrayList<Filter> filters = new ArrayList<Filter>(); - delegate.setFilters(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(EasyMock.isA(List.class)); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setFilters(EasyMock.isA(List.class)); - EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { - @Override - public Object answer() throws Throwable { - List<Filter> orders = (List<Filter>) EasyMock - .getCurrentArguments()[0]; - filters.clear(); - filters.addAll(orders); - return null; - } - }).anyTimes(); - EasyMock.expect( - delegate.getQueryStatement(EasyMock.anyInt(), EasyMock.anyInt())) - .andAnswer(new IAnswer<StatementHelper>() { - @Override - public StatementHelper answer() throws Throwable { - Object[] args = EasyMock.getCurrentArguments(); - int offset = (Integer) (args[0]); - int limit = (Integer) (args[1]); - return FreeformQueryUtil.getQueryWithFilters(filters, - offset, limit); - } - }).anyTimes(); - EasyMock.expect(delegate.getCountStatement()) - .andAnswer(new IAnswer<StatementHelper>() { - @Override - @SuppressWarnings("deprecation") - public StatementHelper answer() throws Throwable { - StatementHelper sh = new StatementHelper(); - StringBuffer query = new StringBuffer( - "SELECT COUNT(*) FROM people"); - if (!filters.isEmpty()) { - query.append(QueryBuilder.getWhereStringForFilters( - filters, sh)); - } - sh.setQueryString(query.toString()); - return sh; - } - }).anyTimes(); - - EasyMock.replay(delegate); - query.setDelegate(delegate); - SQLContainer container = new SQLContainer(query); - // Ville, Kalle, Pelle, Börje - Assert.assertEquals(4, container.size()); - - container.addContainerFilter("NAME", "Vi", false, false); - - // Ville - Assert.assertEquals(1, container.size()); - Assert.assertEquals("Ville", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - - container.removeAllContainerFilters(); - - Assert.assertEquals(4, container.size()); - Assert.assertEquals("Börje", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - - EasyMock.verify(delegate); - } - - @SuppressWarnings("unchecked") - @Test - public void removeContainerFilters_freeform_noFiltering() - throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - connectionPool, "ID"); - FreeformStatementDelegate delegate = EasyMock - .createMock(FreeformStatementDelegate.class); - final ArrayList<Filter> filters = new ArrayList<Filter>(); - delegate.setFilters(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(EasyMock.isA(List.class)); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setFilters(EasyMock.isA(List.class)); - EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { - @Override - public Object answer() throws Throwable { - List<Filter> orders = (List<Filter>) EasyMock - .getCurrentArguments()[0]; - filters.clear(); - filters.addAll(orders); - return null; - } - }).anyTimes(); - EasyMock.expect( - delegate.getQueryStatement(EasyMock.anyInt(), EasyMock.anyInt())) - .andAnswer(new IAnswer<StatementHelper>() { - @Override - public StatementHelper answer() throws Throwable { - Object[] args = EasyMock.getCurrentArguments(); - int offset = (Integer) (args[0]); - int limit = (Integer) (args[1]); - return FreeformQueryUtil.getQueryWithFilters(filters, - offset, limit); - } - }).anyTimes(); - EasyMock.expect(delegate.getCountStatement()) - .andAnswer(new IAnswer<StatementHelper>() { - @Override - @SuppressWarnings("deprecation") - public StatementHelper answer() throws Throwable { - StatementHelper sh = new StatementHelper(); - StringBuffer query = new StringBuffer( - "SELECT COUNT(*) FROM people"); - if (!filters.isEmpty()) { - query.append(QueryBuilder.getWhereStringForFilters( - filters, sh)); - } - sh.setQueryString(query.toString()); - return sh; - } - }).anyTimes(); - - EasyMock.replay(delegate); - query.setDelegate(delegate); - SQLContainer container = new SQLContainer(query); - // Ville, Kalle, Pelle, Börje - Assert.assertEquals(4, container.size()); - - container.addContainerFilter("NAME", "Vi", false, true); - - // Ville - Assert.assertEquals(1, container.size()); - Assert.assertEquals("Ville", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - - container.removeContainerFilters("NAME"); - - Assert.assertEquals(4, container.size()); - Assert.assertEquals("Börje", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - - EasyMock.verify(delegate); - } - - @SuppressWarnings("unchecked") - @Test - public void addFilter_freeformBufferedItems_alsoFiltersBufferedItems() - throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - connectionPool, "ID"); - FreeformStatementDelegate delegate = EasyMock - .createMock(FreeformStatementDelegate.class); - final ArrayList<Filter> filters = new ArrayList<Filter>(); - delegate.setFilters(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(EasyMock.isA(List.class)); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setFilters(EasyMock.isA(List.class)); - EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { - @Override - public Object answer() throws Throwable { - List<Filter> orders = (List<Filter>) EasyMock - .getCurrentArguments()[0]; - filters.clear(); - filters.addAll(orders); - return null; - } - }).anyTimes(); - EasyMock.expect( - delegate.getQueryStatement(EasyMock.anyInt(), EasyMock.anyInt())) - .andAnswer(new IAnswer<StatementHelper>() { - @Override - public StatementHelper answer() throws Throwable { - Object[] args = EasyMock.getCurrentArguments(); - int offset = (Integer) (args[0]); - int limit = (Integer) (args[1]); - return FreeformQueryUtil.getQueryWithFilters(filters, - offset, limit); - } - }).anyTimes(); - EasyMock.expect(delegate.getCountStatement()) - .andAnswer(new IAnswer<StatementHelper>() { - @Override - @SuppressWarnings("deprecation") - public StatementHelper answer() throws Throwable { - StatementHelper sh = new StatementHelper(); - StringBuffer query = new StringBuffer( - "SELECT COUNT(*) FROM people"); - if (!filters.isEmpty()) { - query.append(QueryBuilder.getWhereStringForFilters( - filters, sh)); - } - sh.setQueryString(query.toString()); - return sh; - } - }).anyTimes(); - - EasyMock.replay(delegate); - query.setDelegate(delegate); - SQLContainer container = new SQLContainer(query); - // Ville, Kalle, Pelle, Börje - Assert.assertEquals(4, container.size()); - Assert.assertEquals("Börje", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - - Object id1 = container.addItem(); - container.getContainerProperty(id1, "NAME").setValue("Palle"); - Object id2 = container.addItem(); - container.getContainerProperty(id2, "NAME").setValue("Bengt"); - - container.addContainerFilter(new Like("NAME", "%lle")); - - // Ville, Kalle, Pelle, Palle - Assert.assertEquals(4, container.size()); - Assert.assertEquals( - "Ville", - container.getContainerProperty(container.getIdByIndex(0), - "NAME").getValue()); - Assert.assertEquals( - "Kalle", - container.getContainerProperty(container.getIdByIndex(1), - "NAME").getValue()); - Assert.assertEquals( - "Pelle", - container.getContainerProperty(container.getIdByIndex(2), - "NAME").getValue()); - Assert.assertEquals( - "Palle", - container.getContainerProperty(container.getIdByIndex(3), - "NAME").getValue()); - - Assert.assertNull(container.getIdByIndex(4)); - Assert.assertNull(container.nextItemId(container.getIdByIndex(3))); - - Assert.assertFalse(container.containsId(id2)); - Assert.assertFalse(container.getItemIds().contains(id2)); - - Assert.assertNull(container.getItem(id2)); - Assert.assertEquals(-1, container.indexOfId(id2)); - - Assert.assertNotSame(id2, container.lastItemId()); - Assert.assertSame(id1, container.lastItemId()); - - EasyMock.verify(delegate); - } - - @SuppressWarnings("unchecked") - @Test - public void sort_freeformBufferedItems_sortsBufferedItemsLastInOrderAdded() - throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - connectionPool, "ID"); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - final ArrayList<OrderBy> orderBys = new ArrayList<OrderBy>(); - delegate.setFilters(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setFilters(EasyMock.isA(List.class)); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(EasyMock.isA(List.class)); - EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { - @Override - public Object answer() throws Throwable { - List<OrderBy> orders = (List<OrderBy>) EasyMock - .getCurrentArguments()[0]; - orderBys.clear(); - orderBys.addAll(orders); - return null; - } - }).anyTimes(); - EasyMock.expect( - delegate.getQueryString(EasyMock.anyInt(), EasyMock.anyInt())) - .andAnswer(new IAnswer<String>() { - @Override - public String answer() throws Throwable { - Object[] args = EasyMock.getCurrentArguments(); - int offset = (Integer) (args[0]); - int limit = (Integer) (args[1]); - if (AllTests.db == DB.MSSQL) { - SQLGenerator gen = new MSSQLGenerator(); - if (orderBys == null || orderBys.isEmpty()) { - List<OrderBy> ob = new ArrayList<OrderBy>(); - ob.add(new OrderBy("ID", true)); - return gen.generateSelectQuery("people", null, - ob, offset, limit, null) - .getQueryString(); - } else { - return gen.generateSelectQuery("people", null, - orderBys, offset, limit, null) - .getQueryString(); - } - } else if (AllTests.db == DB.ORACLE) { - SQLGenerator gen = new OracleGenerator(); - if (orderBys == null || orderBys.isEmpty()) { - List<OrderBy> ob = new ArrayList<OrderBy>(); - ob.add(new OrderBy("ID", true)); - return gen.generateSelectQuery("people", null, - ob, offset, limit, null) - .getQueryString(); - } else { - return gen.generateSelectQuery("people", null, - orderBys, offset, limit, null) - .getQueryString(); - } - } else { - StringBuffer query = new StringBuffer( - "SELECT * FROM people"); - if (!orderBys.isEmpty()) { - query.append(" ORDER BY "); - for (OrderBy orderBy : orderBys) { - query.append("\"" + orderBy.getColumn() - + "\""); - if (orderBy.isAscending()) { - query.append(" ASC"); - } else { - query.append(" DESC"); - } - } - } - query.append(" LIMIT ").append(limit) - .append(" OFFSET ").append(offset); - return query.toString(); - } - } - }).anyTimes(); - EasyMock.expect(delegate.getCountQuery()) - .andThrow(new UnsupportedOperationException()).anyTimes(); - EasyMock.replay(delegate); - - query.setDelegate(delegate); - SQLContainer container = new SQLContainer(query); - // Ville, Kalle, Pelle, Börje - Assert.assertEquals("Ville", - container.getContainerProperty(container.firstItemId(), "NAME") - .getValue()); - Assert.assertEquals("Börje", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - - Object id1 = container.addItem(); - container.getContainerProperty(id1, "NAME").setValue("Wilbert"); - Object id2 = container.addItem(); - container.getContainerProperty(id2, "NAME").setValue("Albert"); - - container.sort(new Object[] { "NAME" }, new boolean[] { true }); - - // Börje, Kalle, Pelle, Ville, Wilbert, Albert - Assert.assertEquals("Börje", - container.getContainerProperty(container.firstItemId(), "NAME") - .getValue()); - Assert.assertEquals( - "Wilbert", - container.getContainerProperty( - container.getIdByIndex(container.size() - 2), "NAME") - .getValue()); - Assert.assertEquals("Albert", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - - EasyMock.verify(delegate); - } - -} diff --git a/tests/server-side/com/vaadin/data/util/sqlcontainer/TicketTests.java b/tests/server-side/com/vaadin/data/util/sqlcontainer/TicketTests.java deleted file mode 100644 index a12786f13a..0000000000 --- a/tests/server-side/com/vaadin/data/util/sqlcontainer/TicketTests.java +++ /dev/null @@ -1,186 +0,0 @@ -package com.vaadin.data.util.sqlcontainer; - -import java.math.BigDecimal; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import org.easymock.EasyMock; -import org.easymock.IAnswer; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.Item; -import com.vaadin.data.util.filter.Compare.Equal; -import com.vaadin.data.util.sqlcontainer.SQLContainer; -import com.vaadin.data.util.sqlcontainer.AllTests.DB; -import com.vaadin.data.util.sqlcontainer.connection.SimpleJDBCConnectionPool; -import com.vaadin.data.util.sqlcontainer.query.FreeformQuery; -import com.vaadin.data.util.sqlcontainer.query.FreeformStatementDelegate; -import com.vaadin.data.util.sqlcontainer.query.TableQuery; -import com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper; -import com.vaadin.data.util.sqlcontainer.query.generator.filter.QueryBuilder; -import com.vaadin.ui.Table; -import com.vaadin.ui.Window; - -public class TicketTests { - - private SimpleJDBCConnectionPool connectionPool; - - @Before - public void setUp() throws SQLException { - connectionPool = new SimpleJDBCConnectionPool(AllTests.dbDriver, - AllTests.dbURL, AllTests.dbUser, AllTests.dbPwd, 2, 2); - DataGenerator.addPeopleToDatabase(connectionPool); - } - - @Test - public void ticket5867_throwsIllegalState_transactionAlreadyActive() - throws SQLException { - SQLContainer container = new SQLContainer(new FreeformQuery( - "SELECT * FROM people", Arrays.asList("ID"), connectionPool)); - Table table = new Table(); - Window w = new Window(); - w.addComponent(table); - table.setContainerDataSource(container); - } - - @SuppressWarnings("unchecked") - @Test - public void ticket6136_freeform_ageIs18() throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - FreeformStatementDelegate delegate = EasyMock - .createMock(FreeformStatementDelegate.class); - final ArrayList<Filter> filters = new ArrayList<Filter>(); - delegate.setFilters(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(EasyMock.isA(List.class)); - EasyMock.expectLastCall().anyTimes(); - delegate.setOrderBy(null); - EasyMock.expectLastCall().anyTimes(); - delegate.setFilters(EasyMock.isA(List.class)); - EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { - @Override - public Object answer() throws Throwable { - List<Filter> orders = (List<Filter>) EasyMock - .getCurrentArguments()[0]; - filters.clear(); - filters.addAll(orders); - return null; - } - }).anyTimes(); - EasyMock.expect( - delegate.getQueryStatement(EasyMock.anyInt(), EasyMock.anyInt())) - .andAnswer(new IAnswer<StatementHelper>() { - @Override - public StatementHelper answer() throws Throwable { - Object[] args = EasyMock.getCurrentArguments(); - int offset = (Integer) (args[0]); - int limit = (Integer) (args[1]); - return FreeformQueryUtil.getQueryWithFilters(filters, - offset, limit); - } - }).anyTimes(); - EasyMock.expect(delegate.getCountStatement()) - .andAnswer(new IAnswer<StatementHelper>() { - @Override - @SuppressWarnings("deprecation") - public StatementHelper answer() throws Throwable { - StatementHelper sh = new StatementHelper(); - StringBuffer query = new StringBuffer( - "SELECT COUNT(*) FROM people"); - if (!filters.isEmpty()) { - query.append(QueryBuilder.getWhereStringForFilters( - filters, sh)); - } - sh.setQueryString(query.toString()); - return sh; - } - }).anyTimes(); - - EasyMock.replay(delegate); - query.setDelegate(delegate); - SQLContainer container = new SQLContainer(query); - // Ville, Kalle, Pelle, Börje - Assert.assertEquals(4, container.size()); - Assert.assertEquals("Börje", - container.getContainerProperty(container.lastItemId(), "NAME") - .getValue()); - - container.addContainerFilter(new Equal("AGE", 18)); - // Pelle - Assert.assertEquals(1, container.size()); - Assert.assertEquals("Pelle", - container.getContainerProperty(container.firstItemId(), "NAME") - .getValue()); - if (AllTests.db == DB.ORACLE) { - Assert.assertEquals(new BigDecimal(18), container - .getContainerProperty(container.firstItemId(), "AGE") - .getValue()); - } else { - Assert.assertEquals( - 18, - container.getContainerProperty(container.firstItemId(), - "AGE").getValue()); - } - - EasyMock.verify(delegate); - } - - @Test - public void ticket6136_table_ageIs18() throws SQLException { - TableQuery query = new TableQuery("people", connectionPool, - AllTests.sqlGen); - SQLContainer container = new SQLContainer(query); - // Ville, Kalle, Pelle, Börje - Assert.assertEquals(4, container.size()); - - container.addContainerFilter(new Equal("AGE", 18)); - - // Pelle - Assert.assertEquals(1, container.size()); - Assert.assertEquals("Pelle", - container.getContainerProperty(container.firstItemId(), "NAME") - .getValue()); - if (AllTests.db == DB.ORACLE) { - Assert.assertEquals(new BigDecimal(18), container - .getContainerProperty(container.firstItemId(), "AGE") - .getValue()); - } else { - Assert.assertEquals( - 18, - container.getContainerProperty(container.firstItemId(), - "AGE").getValue()); - } - } - - @Test - public void ticket7434_getItem_Modified_Changed_Unchanged() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, AllTests.sqlGen)); - - Object id = container.firstItemId(); - Item item = container.getItem(id); - String name = (String) item.getItemProperty("NAME").getValue(); - - // set a different name - item.getItemProperty("NAME").setValue("otherName"); - Assert.assertEquals("otherName", item.getItemProperty("NAME") - .getValue()); - - // access the item and reset the name to its old value - Item item2 = container.getItem(id); - item2.getItemProperty("NAME").setValue(name); - Assert.assertEquals(name, item2.getItemProperty("NAME").getValue()); - - Item item3 = container.getItem(id); - String name3 = (String) item3.getItemProperty("NAME").getValue(); - - Assert.assertEquals(name, name3); - } -} diff --git a/tests/server-side/com/vaadin/data/util/sqlcontainer/UtilTest.java b/tests/server-side/com/vaadin/data/util/sqlcontainer/UtilTest.java deleted file mode 100644 index 31bdf2b81b..0000000000 --- a/tests/server-side/com/vaadin/data/util/sqlcontainer/UtilTest.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.vaadin.data.util.sqlcontainer; - -import junit.framework.Assert; - -import org.junit.Test; - -import com.vaadin.data.util.sqlcontainer.SQLUtil; - -public class UtilTest { - - @Test - public void escapeSQL_noQuotes_returnsSameString() { - Assert.assertEquals("asdf", SQLUtil.escapeSQL("asdf")); - } - - @Test - public void escapeSQL_singleQuotes_returnsEscapedString() { - Assert.assertEquals("O''Brien", SQLUtil.escapeSQL("O'Brien")); - } - - @Test - public void escapeSQL_severalQuotes_returnsEscapedString() { - Assert.assertEquals("asdf''ghjk''qwerty", - SQLUtil.escapeSQL("asdf'ghjk'qwerty")); - } - - @Test - public void escapeSQL_doubleQuotes_returnsEscapedString() { - Assert.assertEquals("asdf\\\"foo", SQLUtil.escapeSQL("asdf\"foo")); - } - - @Test - public void escapeSQL_multipleDoubleQuotes_returnsEscapedString() { - Assert.assertEquals("asdf\\\"foo\\\"bar", - SQLUtil.escapeSQL("asdf\"foo\"bar")); - } - - @Test - public void escapeSQL_backslashes_returnsEscapedString() { - Assert.assertEquals("foo\\\\nbar\\\\r", - SQLUtil.escapeSQL("foo\\nbar\\r")); - } - - @Test - public void escapeSQL_x00_removesX00() { - Assert.assertEquals("foobar", SQLUtil.escapeSQL("foo\\x00bar")); - } - - @Test - public void escapeSQL_x1a_removesX1a() { - Assert.assertEquals("foobar", SQLUtil.escapeSQL("foo\\x1abar")); - } -} diff --git a/tests/server-side/com/vaadin/data/util/sqlcontainer/connection/J2EEConnectionPoolTest.java b/tests/server-side/com/vaadin/data/util/sqlcontainer/connection/J2EEConnectionPoolTest.java deleted file mode 100644 index 65a995120e..0000000000 --- a/tests/server-side/com/vaadin/data/util/sqlcontainer/connection/J2EEConnectionPoolTest.java +++ /dev/null @@ -1,108 +0,0 @@ -package com.vaadin.data.util.sqlcontainer.connection; - -import java.sql.Connection; -import java.sql.SQLException; - -import javax.naming.Context; -import javax.naming.NamingException; -import javax.sql.DataSource; - -import junit.framework.Assert; - -import org.easymock.EasyMock; -import org.junit.Test; - -public class J2EEConnectionPoolTest { - - @Test - public void reserveConnection_dataSourceSpecified_shouldReturnValidConnection() - throws SQLException { - Connection connection = EasyMock.createMock(Connection.class); - connection.setAutoCommit(false); - EasyMock.expectLastCall(); - DataSource ds = EasyMock.createMock(DataSource.class); - ds.getConnection(); - EasyMock.expectLastCall().andReturn(connection); - EasyMock.replay(connection, ds); - - J2EEConnectionPool pool = new J2EEConnectionPool(ds); - Connection c = pool.reserveConnection(); - Assert.assertEquals(connection, c); - EasyMock.verify(connection, ds); - } - - @Test - public void releaseConnection_shouldCloseConnection() throws SQLException { - Connection connection = EasyMock.createMock(Connection.class); - connection.setAutoCommit(false); - EasyMock.expectLastCall(); - connection.close(); - EasyMock.expectLastCall(); - DataSource ds = EasyMock.createMock(DataSource.class); - ds.getConnection(); - EasyMock.expectLastCall().andReturn(connection); - EasyMock.replay(connection, ds); - - J2EEConnectionPool pool = new J2EEConnectionPool(ds); - Connection c = pool.reserveConnection(); - Assert.assertEquals(connection, c); - pool.releaseConnection(c); - EasyMock.verify(connection, ds); - } - - @Test - public void reserveConnection_dataSourceLookedUp_shouldReturnValidConnection() - throws SQLException, NamingException { - Connection connection = EasyMock.createMock(Connection.class); - connection.setAutoCommit(false); - EasyMock.expectLastCall(); - connection.close(); - EasyMock.expectLastCall(); - - DataSource ds = EasyMock.createMock(DataSource.class); - ds.getConnection(); - EasyMock.expectLastCall().andReturn(connection); - - System.setProperty("java.naming.factory.initial", - "com.vaadin.data.util.sqlcontainer.connection.MockInitialContextFactory"); - Context context = EasyMock.createMock(Context.class); - context.lookup("testDataSource"); - EasyMock.expectLastCall().andReturn(ds); - MockInitialContextFactory.setMockContext(context); - - EasyMock.replay(context, connection, ds); - - J2EEConnectionPool pool = new J2EEConnectionPool("testDataSource"); - Connection c = pool.reserveConnection(); - Assert.assertEquals(connection, c); - pool.releaseConnection(c); - EasyMock.verify(context, connection, ds); - } - - @Test(expected = SQLException.class) - public void reserveConnection_nonExistantDataSourceLookedUp_shouldFail() - throws SQLException, NamingException { - System.setProperty("java.naming.factory.initial", - "com.vaadin.addon.sqlcontainer.connection.MockInitialContextFactory"); - Context context = EasyMock.createMock(Context.class); - context.lookup("foo"); - EasyMock.expectLastCall().andThrow(new NamingException("fail")); - MockInitialContextFactory.setMockContext(context); - - EasyMock.replay(context); - - J2EEConnectionPool pool = new J2EEConnectionPool("foo"); - pool.reserveConnection(); - EasyMock.verify(context); - } - - @Test - public void releaseConnection_null_shouldSucceed() throws SQLException { - DataSource ds = EasyMock.createMock(DataSource.class); - EasyMock.replay(ds); - - J2EEConnectionPool pool = new J2EEConnectionPool(ds); - pool.releaseConnection(null); - EasyMock.verify(ds); - } -} diff --git a/tests/server-side/com/vaadin/data/util/sqlcontainer/connection/MockInitialContextFactory.java b/tests/server-side/com/vaadin/data/util/sqlcontainer/connection/MockInitialContextFactory.java deleted file mode 100644 index 324899e373..0000000000 --- a/tests/server-side/com/vaadin/data/util/sqlcontainer/connection/MockInitialContextFactory.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.vaadin.data.util.sqlcontainer.connection; - -import javax.naming.Context; -import javax.naming.NamingException; -import javax.naming.spi.InitialContextFactory; - -import org.junit.Test; - -/** - * Provides a JNDI initial context factory for the MockContext. - */ -public class MockInitialContextFactory implements InitialContextFactory { - private static Context mockCtx = null; - - @Test - public void testDummy() { - // Added dummy test so JUnit will not complain about - // "No runnable methods". - } - - public static void setMockContext(Context ctx) { - mockCtx = ctx; - } - - @Override - public Context getInitialContext(java.util.Hashtable<?, ?> environment) - throws NamingException { - if (mockCtx == null) { - throw new IllegalStateException("mock context was not set."); - } - return mockCtx; - } -} diff --git a/tests/server-side/com/vaadin/data/util/sqlcontainer/connection/SimpleJDBCConnectionPoolTest.java b/tests/server-side/com/vaadin/data/util/sqlcontainer/connection/SimpleJDBCConnectionPoolTest.java deleted file mode 100644 index 147319643d..0000000000 --- a/tests/server-side/com/vaadin/data/util/sqlcontainer/connection/SimpleJDBCConnectionPoolTest.java +++ /dev/null @@ -1,172 +0,0 @@ -package com.vaadin.data.util.sqlcontainer.connection; - -import java.sql.Connection; -import java.sql.SQLException; - -import junit.framework.Assert; - -import org.easymock.EasyMock; -import org.junit.Before; -import org.junit.Test; - -import com.vaadin.data.util.sqlcontainer.AllTests; -import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool; -import com.vaadin.data.util.sqlcontainer.connection.SimpleJDBCConnectionPool; - -public class SimpleJDBCConnectionPoolTest { - private JDBCConnectionPool connectionPool; - - @Before - public void setUp() throws SQLException { - connectionPool = new SimpleJDBCConnectionPool(AllTests.dbDriver, - AllTests.dbURL, AllTests.dbUser, AllTests.dbPwd, 2, 2); - } - - @Test - public void reserveConnection_reserveNewConnection_returnsConnection() - throws SQLException { - Connection conn = connectionPool.reserveConnection(); - Assert.assertNotNull(conn); - } - - @Test - public void releaseConnection_releaseUnused_shouldNotThrowException() - throws SQLException { - Connection conn = connectionPool.reserveConnection(); - connectionPool.releaseConnection(conn); - Assert.assertFalse(conn.isClosed()); - } - - @Test(expected = SQLException.class) - public void reserveConnection_noConnectionsLeft_shouldFail() - throws SQLException { - try { - connectionPool.reserveConnection(); - connectionPool.reserveConnection(); - } catch (SQLException e) { - e.printStackTrace(); - Assert.fail("Exception before all connections used! " - + e.getMessage()); - } - - connectionPool.reserveConnection(); - Assert.fail("Reserving connection didn't fail even though no connections are available!"); - } - - @Test - public void reserveConnection_oneConnectionLeft_returnsConnection() - throws SQLException { - try { - connectionPool.reserveConnection(); - } catch (SQLException e) { - e.printStackTrace(); - Assert.fail("Exception before all connections used! " - + e.getMessage()); - } - - Connection conn = connectionPool.reserveConnection(); - Assert.assertNotNull(conn); - } - - @Test - public void reserveConnection_oneConnectionJustReleased_returnsConnection() - throws SQLException { - Connection conn2 = null; - try { - connectionPool.reserveConnection(); - conn2 = connectionPool.reserveConnection(); - } catch (SQLException e) { - e.printStackTrace(); - Assert.fail("Exception before all connections used! " - + e.getMessage()); - } - - connectionPool.releaseConnection(conn2); - - connectionPool.reserveConnection(); - } - - @Test(expected = IllegalArgumentException.class) - public void construct_allParametersNull_shouldFail() throws SQLException { - SimpleJDBCConnectionPool cp = new SimpleJDBCConnectionPool(null, null, - null, null); - } - - @Test(expected = IllegalArgumentException.class) - public void construct_onlyDriverNameGiven_shouldFail() throws SQLException { - SimpleJDBCConnectionPool cp = new SimpleJDBCConnectionPool( - AllTests.dbDriver, null, null, null); - } - - @Test(expected = IllegalArgumentException.class) - public void construct_onlyDriverNameAndUrlGiven_shouldFail() - throws SQLException { - SimpleJDBCConnectionPool cp = new SimpleJDBCConnectionPool( - AllTests.dbDriver, AllTests.dbURL, null, null); - } - - @Test(expected = IllegalArgumentException.class) - public void construct_onlyDriverNameAndUrlAndUserGiven_shouldFail() - throws SQLException { - SimpleJDBCConnectionPool cp = new SimpleJDBCConnectionPool( - AllTests.dbDriver, AllTests.dbURL, AllTests.dbUser, null); - } - - @Test(expected = RuntimeException.class) - public void construct_nonExistingDriver_shouldFail() throws SQLException { - SimpleJDBCConnectionPool cp = new SimpleJDBCConnectionPool("foo", - AllTests.dbURL, AllTests.dbUser, AllTests.dbPwd); - } - - @Test - public void reserveConnection_newConnectionOpened_shouldSucceed() - throws SQLException { - connectionPool = new SimpleJDBCConnectionPool(AllTests.dbDriver, - AllTests.dbURL, AllTests.dbUser, AllTests.dbPwd, 0, 2); - Connection c = connectionPool.reserveConnection(); - Assert.assertNotNull(c); - } - - @Test - public void releaseConnection_nullConnection_shouldDoNothing() { - connectionPool.releaseConnection(null); - } - - @Test - public void releaseConnection_failingRollback_shouldCallClose() - throws SQLException { - Connection c = EasyMock.createMock(Connection.class); - c.getAutoCommit(); - EasyMock.expectLastCall().andReturn(false); - c.rollback(); - EasyMock.expectLastCall().andThrow(new SQLException("Rollback failed")); - c.close(); - EasyMock.expectLastCall().atLeastOnce(); - EasyMock.replay(c); - // make sure the connection pool is initialized - connectionPool.reserveConnection(); - connectionPool.releaseConnection(c); - EasyMock.verify(c); - } - - @Test - public void destroy_shouldCloseAllConnections() throws SQLException { - Connection c1 = connectionPool.reserveConnection(); - Connection c2 = connectionPool.reserveConnection(); - connectionPool.destroy(); - Assert.assertTrue(c1.isClosed()); - Assert.assertTrue(c2.isClosed()); - } - - @Test - public void destroy_shouldCloseAllConnections2() throws SQLException { - Connection c1 = connectionPool.reserveConnection(); - Connection c2 = connectionPool.reserveConnection(); - connectionPool.releaseConnection(c1); - connectionPool.releaseConnection(c2); - connectionPool.destroy(); - Assert.assertTrue(c1.isClosed()); - Assert.assertTrue(c2.isClosed()); - } - -} diff --git a/tests/server-side/com/vaadin/data/util/sqlcontainer/filters/BetweenTest.java b/tests/server-side/com/vaadin/data/util/sqlcontainer/filters/BetweenTest.java deleted file mode 100644 index da4cfe4522..0000000000 --- a/tests/server-side/com/vaadin/data/util/sqlcontainer/filters/BetweenTest.java +++ /dev/null @@ -1,122 +0,0 @@ -package com.vaadin.data.util.sqlcontainer.filters; - -import junit.framework.Assert; - -import org.easymock.EasyMock; -import org.junit.Test; - -import com.vaadin.data.Item; -import com.vaadin.data.Property; -import com.vaadin.data.util.filter.Between; - -public class BetweenTest { - - private Item itemWithPropertyValue(Object propertyId, Object value) { - Property<?> property = EasyMock.createMock(Property.class); - property.getValue(); - EasyMock.expectLastCall().andReturn(value).anyTimes(); - EasyMock.replay(property); - - Item item = EasyMock.createMock(Item.class); - item.getItemProperty(propertyId); - EasyMock.expectLastCall().andReturn(property).anyTimes(); - EasyMock.replay(item); - return item; - } - - @Test - public void passesFilter_valueIsInRange_shouldBeTrue() { - Item item = itemWithPropertyValue("foo", 15); - Between between = new Between("foo", 1, 30); - Assert.assertTrue(between.passesFilter("foo", item)); - } - - @Test - public void passesFilter_valueIsOutOfRange_shouldBeFalse() { - Item item = itemWithPropertyValue("foo", 15); - Between between = new Between("foo", 0, 2); - Assert.assertFalse(between.passesFilter("foo", item)); - } - - @Test - public void passesFilter_valueNotComparable_shouldBeFalse() { - Item item = itemWithPropertyValue("foo", new Object()); - Between between = new Between("foo", 0, 2); - Assert.assertFalse(between.passesFilter("foo", item)); - } - - @Test - public void appliesToProperty_differentProperties_shoudlBeFalse() { - Between between = new Between("foo", 0, 2); - Assert.assertFalse(between.appliesToProperty("bar")); - } - - @Test - public void appliesToProperty_sameProperties_shouldBeTrue() { - Between between = new Between("foo", 0, 2); - Assert.assertTrue(between.appliesToProperty("foo")); - } - - @Test - public void hashCode_equalInstances_shouldBeEqual() { - Between b1 = new Between("foo", 0, 2); - Between b2 = new Between("foo", 0, 2); - Assert.assertEquals(b1.hashCode(), b2.hashCode()); - } - - @Test - public void equals_differentObjects_shouldBeFalse() { - Between b1 = new Between("foo", 0, 2); - Object obj = new Object(); - Assert.assertFalse(b1.equals(obj)); - } - - @Test - public void equals_sameInstance_shouldBeTrue() { - Between b1 = new Between("foo", 0, 2); - Between b2 = b1; - Assert.assertTrue(b1.equals(b2)); - } - - @Test - public void equals_equalInstances_shouldBeTrue() { - Between b1 = new Between("foo", 0, 2); - Between b2 = new Between("foo", 0, 2); - Assert.assertTrue(b1.equals(b2)); - } - - @Test - public void equals_equalInstances2_shouldBeTrue() { - Between b1 = new Between(null, null, null); - Between b2 = new Between(null, null, null); - Assert.assertTrue(b1.equals(b2)); - } - - @Test - public void equals_secondValueDiffers_shouldBeFalse() { - Between b1 = new Between("foo", 0, 1); - Between b2 = new Between("foo", 0, 2); - Assert.assertFalse(b1.equals(b2)); - } - - @Test - public void equals_firstAndSecondValueDiffers_shouldBeFalse() { - Between b1 = new Between("foo", 0, null); - Between b2 = new Between("foo", 1, 2); - Assert.assertFalse(b1.equals(b2)); - } - - @Test - public void equals_propertyAndFirstAndSecondValueDiffers_shouldBeFalse() { - Between b1 = new Between("foo", null, 1); - Between b2 = new Between("bar", 1, 2); - Assert.assertFalse(b1.equals(b2)); - } - - @Test - public void equals_propertiesDiffer_shouldBeFalse() { - Between b1 = new Between(null, 0, 1); - Between b2 = new Between("bar", 0, 1); - Assert.assertFalse(b1.equals(b2)); - } -} diff --git a/tests/server-side/com/vaadin/data/util/sqlcontainer/filters/LikeTest.java b/tests/server-side/com/vaadin/data/util/sqlcontainer/filters/LikeTest.java deleted file mode 100644 index f1130aad80..0000000000 --- a/tests/server-side/com/vaadin/data/util/sqlcontainer/filters/LikeTest.java +++ /dev/null @@ -1,229 +0,0 @@ -package com.vaadin.data.util.sqlcontainer.filters; - -import org.junit.Assert; -import org.junit.Test; - -import com.vaadin.data.Item; -import com.vaadin.data.util.ObjectProperty; -import com.vaadin.data.util.PropertysetItem; -import com.vaadin.data.util.filter.Like; - -public class LikeTest { - - @Test - public void passesFilter_valueIsNotStringType_shouldFail() { - Like like = new Like("test", "%foo%"); - - Item item = new PropertysetItem(); - item.addItemProperty("test", new ObjectProperty<Integer>(5)); - - Assert.assertFalse(like.passesFilter("id", item)); - } - - @Test - public void passesFilter_containsLikeQueryOnStringContainingValue_shouldSucceed() { - Like like = new Like("test", "%foo%"); - - Item item = new PropertysetItem(); - item.addItemProperty("test", new ObjectProperty<String>("asdfooghij")); - - Assert.assertTrue(like.passesFilter("id", item)); - } - - @Test - public void passesFilter_containsLikeQueryOnStringContainingValueCaseInsensitive_shouldSucceed() { - Like like = new Like("test", "%foo%"); - like.setCaseSensitive(false); - - Item item = new PropertysetItem(); - item.addItemProperty("test", new ObjectProperty<String>("asdfOOghij")); - - Assert.assertTrue(like.passesFilter("id", item)); - } - - @Test - public void passesFilter_containsLikeQueryOnStringContainingValueConstructedCaseInsensitive_shouldSucceed() { - Like like = new Like("test", "%foo%", false); - - Item item = new PropertysetItem(); - item.addItemProperty("test", new ObjectProperty<String>("asdfOOghij")); - - Assert.assertTrue(like.passesFilter("id", item)); - } - - @Test - public void passesFilter_containsLikeQueryOnStringNotContainingValue_shouldFail() { - Like like = new Like("test", "%foo%"); - - Item item = new PropertysetItem(); - item.addItemProperty("test", new ObjectProperty<String>("asdbarghij")); - - Assert.assertFalse(like.passesFilter("id", item)); - } - - @Test - public void passesFilter_containsLikeQueryOnStringExactlyEqualToValue_shouldSucceed() { - Like like = new Like("test", "%foo%"); - - Item item = new PropertysetItem(); - item.addItemProperty("test", new ObjectProperty<String>("foo")); - - Assert.assertTrue(like.passesFilter("id", item)); - } - - @Test - public void passesFilter_containsLikeQueryOnStringEqualToValueMinusOneCharAtTheEnd_shouldFail() { - Like like = new Like("test", "%foo%"); - - Item item = new PropertysetItem(); - item.addItemProperty("test", new ObjectProperty<String>("fo")); - - Assert.assertFalse(like.passesFilter("id", item)); - } - - @Test - public void passesFilter_beginsWithLikeQueryOnStringBeginningWithValue_shouldSucceed() { - Like like = new Like("test", "foo%"); - - Item item = new PropertysetItem(); - item.addItemProperty("test", new ObjectProperty<String>("foobar")); - - Assert.assertTrue(like.passesFilter("id", item)); - } - - @Test - public void passesFilter_beginsWithLikeQueryOnStringNotBeginningWithValue_shouldFail() { - Like like = new Like("test", "foo%"); - - Item item = new PropertysetItem(); - item.addItemProperty("test", new ObjectProperty<String>("barfoo")); - - Assert.assertFalse(like.passesFilter("id", item)); - } - - @Test - public void passesFilter_endsWithLikeQueryOnStringEndingWithValue_shouldSucceed() { - Like like = new Like("test", "%foo"); - - Item item = new PropertysetItem(); - item.addItemProperty("test", new ObjectProperty<String>("barfoo")); - - Assert.assertTrue(like.passesFilter("id", item)); - } - - @Test - public void passesFilter_endsWithLikeQueryOnStringNotEndingWithValue_shouldFail() { - Like like = new Like("test", "%foo"); - - Item item = new PropertysetItem(); - item.addItemProperty("test", new ObjectProperty<String>("foobar")); - - Assert.assertFalse(like.passesFilter("id", item)); - } - - @Test - public void passesFilter_startsWithAndEndsWithOnMatchingValue_shouldSucceed() { - Like like = new Like("test", "foo%bar"); - - Item item = new PropertysetItem(); - item.addItemProperty("test", new ObjectProperty<String>("fooASDFbar")); - - Assert.assertTrue(like.passesFilter("id", item)); - } - - @Test - public void appliesToProperty_valueIsProperty_shouldBeTrue() { - Like like = new Like("test", "%foo"); - Assert.assertTrue(like.appliesToProperty("test")); - } - - @Test - public void appliesToProperty_valueIsNotProperty_shouldBeFalse() { - Like like = new Like("test", "%foo"); - Assert.assertFalse(like.appliesToProperty("bar")); - } - - @Test - public void equals_sameInstances_shouldBeTrue() { - Like like1 = new Like("test", "%foo"); - Like like2 = like1; - Assert.assertTrue(like1.equals(like2)); - } - - @Test - public void equals_twoEqualInstances_shouldBeTrue() { - Like like1 = new Like("test", "foo"); - Like like2 = new Like("test", "foo"); - Assert.assertTrue(like1.equals(like2)); - } - - @Test - public void equals_differentValues_shouldBeFalse() { - Like like1 = new Like("test", "foo"); - Like like2 = new Like("test", "bar"); - Assert.assertFalse(like1.equals(like2)); - } - - @Test - public void equals_differentProperties_shouldBeFalse() { - Like like1 = new Like("foo", "test"); - Like like2 = new Like("bar", "test"); - Assert.assertFalse(like1.equals(like2)); - } - - @Test - public void equals_differentPropertiesAndValues_shouldBeFalse() { - Like like1 = new Like("foo", "bar"); - Like like2 = new Like("baz", "zomg"); - Assert.assertFalse(like1.equals(like2)); - } - - @Test - public void equals_differentClasses_shouldBeFalse() { - Like like1 = new Like("foo", "bar"); - Object obj = new Object(); - Assert.assertFalse(like1.equals(obj)); - } - - @Test - public void equals_bothHaveNullProperties_shouldBeTrue() { - Like like1 = new Like(null, "foo"); - Like like2 = new Like(null, "foo"); - Assert.assertTrue(like1.equals(like2)); - } - - @Test - public void equals_bothHaveNullValues_shouldBeTrue() { - Like like1 = new Like("foo", null); - Like like2 = new Like("foo", null); - Assert.assertTrue(like1.equals(like2)); - } - - @Test - public void equals_onePropertyIsNull_shouldBeFalse() { - Like like1 = new Like(null, "bar"); - Like like2 = new Like("foo", "baz"); - Assert.assertFalse(like1.equals(like2)); - } - - @Test - public void equals_oneValueIsNull_shouldBeFalse() { - Like like1 = new Like("foo", null); - Like like2 = new Like("baz", "bar"); - Assert.assertFalse(like1.equals(like2)); - } - - @Test - public void hashCode_equalInstances_shouldBeEqual() { - Like like1 = new Like("test", "foo"); - Like like2 = new Like("test", "foo"); - Assert.assertEquals(like1.hashCode(), like2.hashCode()); - } - - @Test - public void hashCode_differentPropertiesAndValues_shouldNotEqual() { - Like like1 = new Like("foo", "bar"); - Like like2 = new Like("baz", "zomg"); - Assert.assertTrue(like1.hashCode() != like2.hashCode()); - } -} diff --git a/tests/server-side/com/vaadin/data/util/sqlcontainer/generator/SQLGeneratorsTest.java b/tests/server-side/com/vaadin/data/util/sqlcontainer/generator/SQLGeneratorsTest.java deleted file mode 100644 index e62a06e6e1..0000000000 --- a/tests/server-side/com/vaadin/data/util/sqlcontainer/generator/SQLGeneratorsTest.java +++ /dev/null @@ -1,241 +0,0 @@ -package com.vaadin.data.util.sqlcontainer.generator; - -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.util.filter.Like; -import com.vaadin.data.util.filter.Or; -import com.vaadin.data.util.sqlcontainer.AllTests; -import com.vaadin.data.util.sqlcontainer.DataGenerator; -import com.vaadin.data.util.sqlcontainer.RowItem; -import com.vaadin.data.util.sqlcontainer.SQLContainer; -import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool; -import com.vaadin.data.util.sqlcontainer.connection.SimpleJDBCConnectionPool; -import com.vaadin.data.util.sqlcontainer.query.OrderBy; -import com.vaadin.data.util.sqlcontainer.query.TableQuery; -import com.vaadin.data.util.sqlcontainer.query.generator.DefaultSQLGenerator; -import com.vaadin.data.util.sqlcontainer.query.generator.MSSQLGenerator; -import com.vaadin.data.util.sqlcontainer.query.generator.OracleGenerator; -import com.vaadin.data.util.sqlcontainer.query.generator.SQLGenerator; -import com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper; - -public class SQLGeneratorsTest { - private JDBCConnectionPool connectionPool; - - @Before - public void setUp() throws SQLException { - - try { - connectionPool = new SimpleJDBCConnectionPool(AllTests.dbDriver, - AllTests.dbURL, AllTests.dbUser, AllTests.dbPwd, 2, 2); - } catch (SQLException e) { - e.printStackTrace(); - Assert.fail(e.getMessage()); - } - - DataGenerator.addPeopleToDatabase(connectionPool); - } - - @After - public void tearDown() { - if (connectionPool != null) { - connectionPool.destroy(); - } - } - - @Test - public void generateSelectQuery_basicQuery_shouldSucceed() { - SQLGenerator sg = new DefaultSQLGenerator(); - StatementHelper sh = sg.generateSelectQuery("TABLE", null, null, 0, 0, - null); - Assert.assertEquals(sh.getQueryString(), "SELECT * FROM TABLE"); - } - - @Test - public void generateSelectQuery_pagingAndColumnsSet_shouldSucceed() { - SQLGenerator sg = new DefaultSQLGenerator(); - StatementHelper sh = sg.generateSelectQuery("TABLE", null, null, 4, 8, - "COL1, COL2, COL3"); - Assert.assertEquals(sh.getQueryString(), - "SELECT COL1, COL2, COL3 FROM TABLE LIMIT 8 OFFSET 4"); - } - - /** - * Note: Only tests one kind of filter and ordering. - */ - @Test - public void generateSelectQuery_filtersAndOrderingSet_shouldSucceed() { - SQLGenerator sg = new DefaultSQLGenerator(); - List<com.vaadin.data.Container.Filter> f = new ArrayList<Filter>(); - f.add(new Like("name", "%lle")); - List<OrderBy> ob = Arrays.asList(new OrderBy("name", true)); - StatementHelper sh = sg.generateSelectQuery("TABLE", f, ob, 0, 0, null); - Assert.assertEquals(sh.getQueryString(), - "SELECT * FROM TABLE WHERE \"name\" LIKE ? ORDER BY \"name\" ASC"); - } - - @Test - public void generateSelectQuery_filtersAndOrderingSet_exclusiveFilteringMode_shouldSucceed() { - SQLGenerator sg = new DefaultSQLGenerator(); - List<Filter> f = new ArrayList<Filter>(); - f.add(new Or(new Like("name", "%lle"), new Like("name", "vi%"))); - List<OrderBy> ob = Arrays.asList(new OrderBy("name", true)); - StatementHelper sh = sg.generateSelectQuery("TABLE", f, ob, 0, 0, null); - // TODO - Assert.assertEquals(sh.getQueryString(), - "SELECT * FROM TABLE WHERE (\"name\" LIKE ? " - + "OR \"name\" LIKE ?) ORDER BY \"name\" ASC"); - } - - @Test - public void generateDeleteQuery_basicQuery_shouldSucceed() - throws SQLException { - /* - * No need to run this for Oracle/MSSQL generators since the - * DefaultSQLGenerator method would be called anyway. - */ - if (AllTests.sqlGen instanceof MSSQLGenerator - || AllTests.sqlGen instanceof OracleGenerator) { - return; - } - SQLGenerator sg = AllTests.sqlGen; - TableQuery query = new TableQuery("people", connectionPool, - AllTests.sqlGen); - SQLContainer container = new SQLContainer(query); - - StatementHelper sh = sg.generateDeleteQuery( - "people", - query.getPrimaryKeyColumns(), - null, - (RowItem) container.getItem(container.getItemIds().iterator() - .next())); - Assert.assertEquals("DELETE FROM people WHERE \"ID\" = ?", - sh.getQueryString()); - } - - @Test - public void generateUpdateQuery_basicQuery_shouldSucceed() - throws SQLException { - /* - * No need to run this for Oracle/MSSQL generators since the - * DefaultSQLGenerator method would be called anyway. - */ - if (AllTests.sqlGen instanceof MSSQLGenerator - || AllTests.sqlGen instanceof OracleGenerator) { - return; - } - SQLGenerator sg = new DefaultSQLGenerator(); - TableQuery query = new TableQuery("people", connectionPool); - SQLContainer container = new SQLContainer(query); - - RowItem ri = (RowItem) container.getItem(container.getItemIds() - .iterator().next()); - ri.getItemProperty("NAME").setValue("Viljami"); - - StatementHelper sh = sg.generateUpdateQuery("people", ri); - Assert.assertTrue("UPDATE people SET \"NAME\" = ?, \"AGE\" = ? WHERE \"ID\" = ?" - .equals(sh.getQueryString()) - || "UPDATE people SET \"AGE\" = ?, \"NAME\" = ? WHERE \"ID\" = ?" - .equals(sh.getQueryString())); - } - - @Test - public void generateInsertQuery_basicQuery_shouldSucceed() - throws SQLException { - /* - * No need to run this for Oracle/MSSQL generators since the - * DefaultSQLGenerator method would be called anyway. - */ - if (AllTests.sqlGen instanceof MSSQLGenerator - || AllTests.sqlGen instanceof OracleGenerator) { - return; - } - SQLGenerator sg = new DefaultSQLGenerator(); - TableQuery query = new TableQuery("people", connectionPool); - SQLContainer container = new SQLContainer(query); - - RowItem ri = (RowItem) container.getItem(container.addItem()); - ri.getItemProperty("NAME").setValue("Viljami"); - - StatementHelper sh = sg.generateInsertQuery("people", ri); - - Assert.assertTrue("INSERT INTO people (\"NAME\", \"AGE\") VALUES (?, ?)" - .equals(sh.getQueryString()) - || "INSERT INTO people (\"AGE\", \"NAME\") VALUES (?, ?)" - .equals(sh.getQueryString())); - } - - @Test - public void generateComplexSelectQuery_forOracle_shouldSucceed() - throws SQLException { - SQLGenerator sg = new OracleGenerator(); - List<Filter> f = new ArrayList<Filter>(); - f.add(new Like("name", "%lle")); - List<OrderBy> ob = Arrays.asList(new OrderBy("name", true)); - StatementHelper sh = sg.generateSelectQuery("TABLE", f, ob, 4, 8, - "NAME, ID"); - Assert.assertEquals( - "SELECT * FROM (SELECT x.*, ROWNUM AS \"rownum\" FROM" - + " (SELECT NAME, ID FROM TABLE WHERE \"name\" LIKE ?" - + " ORDER BY \"name\" ASC) x) WHERE \"rownum\" BETWEEN 5 AND 12", - sh.getQueryString()); - } - - @Test - public void generateComplexSelectQuery_forMSSQL_shouldSucceed() - throws SQLException { - SQLGenerator sg = new MSSQLGenerator(); - List<Filter> f = new ArrayList<Filter>(); - f.add(new Like("name", "%lle")); - List<OrderBy> ob = Arrays.asList(new OrderBy("name", true)); - StatementHelper sh = sg.generateSelectQuery("TABLE", f, ob, 4, 8, - "NAME, ID"); - Assert.assertEquals(sh.getQueryString(), - "SELECT * FROM (SELECT row_number() OVER " - + "( ORDER BY \"name\" ASC) AS rownum, NAME, ID " - + "FROM TABLE WHERE \"name\" LIKE ?) " - + "AS a WHERE a.rownum BETWEEN 5 AND 12"); - } - - @Test - public void generateComplexSelectQuery_forOracle_exclusiveFilteringMode_shouldSucceed() - throws SQLException { - SQLGenerator sg = new OracleGenerator(); - List<Filter> f = new ArrayList<Filter>(); - f.add(new Or(new Like("name", "%lle"), new Like("name", "vi%"))); - List<OrderBy> ob = Arrays.asList(new OrderBy("name", true)); - StatementHelper sh = sg.generateSelectQuery("TABLE", f, ob, 4, 8, - "NAME, ID"); - Assert.assertEquals( - sh.getQueryString(), - "SELECT * FROM (SELECT x.*, ROWNUM AS \"rownum\" FROM" - + " (SELECT NAME, ID FROM TABLE WHERE (\"name\" LIKE ?" - + " OR \"name\" LIKE ?) " - + "ORDER BY \"name\" ASC) x) WHERE \"rownum\" BETWEEN 5 AND 12"); - } - - @Test - public void generateComplexSelectQuery_forMSSQL_exclusiveFilteringMode_shouldSucceed() - throws SQLException { - SQLGenerator sg = new MSSQLGenerator(); - List<Filter> f = new ArrayList<Filter>(); - f.add(new Or(new Like("name", "%lle"), new Like("name", "vi%"))); - List<OrderBy> ob = Arrays.asList(new OrderBy("name", true)); - StatementHelper sh = sg.generateSelectQuery("TABLE", f, ob, 4, 8, - "NAME, ID"); - Assert.assertEquals(sh.getQueryString(), - "SELECT * FROM (SELECT row_number() OVER " - + "( ORDER BY \"name\" ASC) AS rownum, NAME, ID " - + "FROM TABLE WHERE (\"name\" LIKE ? " - + "OR \"name\" LIKE ?)) " - + "AS a WHERE a.rownum BETWEEN 5 AND 12"); - } -} diff --git a/tests/server-side/com/vaadin/data/util/sqlcontainer/query/FreeformQueryTest.java b/tests/server-side/com/vaadin/data/util/sqlcontainer/query/FreeformQueryTest.java deleted file mode 100644 index 9b38655186..0000000000 --- a/tests/server-side/com/vaadin/data/util/sqlcontainer/query/FreeformQueryTest.java +++ /dev/null @@ -1,897 +0,0 @@ -package com.vaadin.data.util.sqlcontainer.query; - -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import org.easymock.EasyMock; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.util.filter.Like; -import com.vaadin.data.util.sqlcontainer.AllTests; -import com.vaadin.data.util.sqlcontainer.DataGenerator; -import com.vaadin.data.util.sqlcontainer.RowId; -import com.vaadin.data.util.sqlcontainer.RowItem; -import com.vaadin.data.util.sqlcontainer.SQLContainer; -import com.vaadin.data.util.sqlcontainer.AllTests.DB; -import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool; -import com.vaadin.data.util.sqlcontainer.connection.SimpleJDBCConnectionPool; -import com.vaadin.data.util.sqlcontainer.query.FreeformQuery; -import com.vaadin.data.util.sqlcontainer.query.FreeformQueryDelegate; -import com.vaadin.data.util.sqlcontainer.query.OrderBy; - -public class FreeformQueryTest { - - private static final int offset = AllTests.offset; - private JDBCConnectionPool connectionPool; - - @Before - public void setUp() throws SQLException { - - try { - connectionPool = new SimpleJDBCConnectionPool(AllTests.dbDriver, - AllTests.dbURL, AllTests.dbUser, AllTests.dbPwd, 2, 2); - } catch (SQLException e) { - e.printStackTrace(); - Assert.fail(e.getMessage()); - } - - DataGenerator.addPeopleToDatabase(connectionPool); - } - - @After - public void tearDown() { - if (connectionPool != null) { - connectionPool.destroy(); - } - } - - @Test - public void construction_legalParameters_shouldSucceed() { - FreeformQuery ffQuery = new FreeformQuery("SELECT * FROM foo", - Arrays.asList("ID"), connectionPool); - Assert.assertArrayEquals(new Object[] { "ID" }, ffQuery - .getPrimaryKeyColumns().toArray()); - - Assert.assertEquals("SELECT * FROM foo", ffQuery.getQueryString()); - } - - @Test(expected = IllegalArgumentException.class) - public void construction_emptyQueryString_shouldFail() { - new FreeformQuery("", Arrays.asList("ID"), connectionPool); - } - - @Test - public void construction_nullPrimaryKeys_shouldSucceed() { - new FreeformQuery("SELECT * FROM foo", null, connectionPool); - } - - @Test - public void construction_nullPrimaryKeys2_shouldSucceed() { - new FreeformQuery("SELECT * FROM foo", connectionPool); - } - - @Test - public void construction_emptyPrimaryKeys_shouldSucceed() { - new FreeformQuery("SELECT * FROM foo", connectionPool); - } - - @Test(expected = IllegalArgumentException.class) - public void construction_emptyStringsInPrimaryKeys_shouldFail() { - new FreeformQuery("SELECT * FROM foo", Arrays.asList(""), - connectionPool); - } - - @Test(expected = IllegalArgumentException.class) - public void construction_nullConnectionPool_shouldFail() { - new FreeformQuery("SELECT * FROM foo", Arrays.asList("ID"), null); - } - - @Test - public void getCount_simpleQuery_returnsFour() throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - Assert.assertEquals(4, query.getCount()); - } - - @Test(expected = SQLException.class) - public void getCount_illegalQuery_shouldThrowSQLException() - throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM asdf", - Arrays.asList("ID"), connectionPool); - query.getResults(0, 50); - } - - @Test - public void getCount_simpleQueryTwoMorePeopleAdded_returnsSix() - throws SQLException { - // Add some people - Connection conn = connectionPool.reserveConnection(); - Statement statement = conn.createStatement(); - if (AllTests.db == DB.MSSQL) { - statement.executeUpdate("insert into people values('Bengt', 30)"); - statement.executeUpdate("insert into people values('Ingvar', 50)"); - } else { - statement - .executeUpdate("insert into people values(default, 'Bengt', 30)"); - statement - .executeUpdate("insert into people values(default, 'Ingvar', 50)"); - } - statement.close(); - conn.commit(); - connectionPool.releaseConnection(conn); - - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - - Assert.assertEquals(6, query.getCount()); - } - - @Test - public void getCount_moreComplexQuery_returnsThree() throws SQLException { - FreeformQuery query = new FreeformQuery( - "SELECT * FROM people WHERE \"NAME\" LIKE '%lle'", - connectionPool, new String[] { "ID" }); - Assert.assertEquals(3, query.getCount()); - } - - @Test - public void getCount_normalState_releasesConnection() throws SQLException { - FreeformQuery query = new FreeformQuery( - "SELECT * FROM people WHERE \"NAME\" LIKE '%lle'", - connectionPool, "ID"); - query.getCount(); - query.getCount(); - Assert.assertNotNull(connectionPool.reserveConnection()); - } - - @Test - public void getCount_delegateRegistered_shouldUseDelegate() - throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - EasyMock.expect(delegate.getCountQuery()).andReturn( - "SELECT COUNT(*) FROM people WHERE \"NAME\" LIKE '%lle'"); - EasyMock.replay(delegate); - query.setDelegate(delegate); - Assert.assertEquals(3, query.getCount()); - EasyMock.verify(delegate); - } - - @Test - public void getCount_delegateRegisteredZeroRows_returnsZero() - throws SQLException { - DataGenerator.createGarbage(connectionPool); - FreeformQuery query = new FreeformQuery("SELECT * FROM GARBAGE", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - EasyMock.expect(delegate.getCountQuery()).andReturn( - "SELECT COUNT(*) FROM GARBAGE"); - EasyMock.replay(delegate); - query.setDelegate(delegate); - Assert.assertEquals(0, query.getCount()); - EasyMock.verify(delegate); - } - - @Test - public void getResults_simpleQuery_returnsFourRecords() throws SQLException { - FreeformQuery query = new FreeformQuery( - "SELECT \"ID\",\"NAME\" FROM people", Arrays.asList("ID"), - connectionPool); - query.beginTransaction(); - ResultSet rs = query.getResults(0, 0); - - Assert.assertTrue(rs.next()); - Assert.assertEquals(0 + offset, rs.getInt(1)); - Assert.assertEquals("Ville", rs.getString(2)); - - Assert.assertTrue(rs.next()); - Assert.assertEquals(1 + offset, rs.getInt(1)); - Assert.assertEquals("Kalle", rs.getString(2)); - - Assert.assertTrue(rs.next()); - Assert.assertEquals(2 + offset, rs.getInt(1)); - Assert.assertEquals("Pelle", rs.getString(2)); - - Assert.assertTrue(rs.next()); - Assert.assertEquals(3 + offset, rs.getInt(1)); - Assert.assertEquals("Börje", rs.getString(2)); - - Assert.assertFalse(rs.next()); - query.commit(); - } - - @Test - public void getResults_moreComplexQuery_returnsThreeRecords() - throws SQLException { - FreeformQuery query = new FreeformQuery( - "SELECT * FROM people WHERE \"NAME\" LIKE '%lle'", - Arrays.asList("ID"), connectionPool); - query.beginTransaction(); - ResultSet rs = query.getResults(0, 0); - - Assert.assertTrue(rs.next()); - Assert.assertEquals(0 + offset, rs.getInt(1)); - Assert.assertEquals("Ville", rs.getString(2)); - - Assert.assertTrue(rs.next()); - Assert.assertEquals(1 + offset, rs.getInt(1)); - Assert.assertEquals("Kalle", rs.getString(2)); - - Assert.assertTrue(rs.next()); - Assert.assertEquals(2 + offset, rs.getInt(1)); - Assert.assertEquals("Pelle", rs.getString(2)); - - Assert.assertFalse(rs.next()); - query.commit(); - } - - @Test - public void getResults_noDelegate5000Rows_returns5000rows() - throws SQLException { - DataGenerator.addFiveThousandPeople(connectionPool); - - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - query.beginTransaction(); - ResultSet rs = query.getResults(0, 0); - for (int i = 0; i < 5000; i++) { - Assert.assertTrue(rs.next()); - } - Assert.assertFalse(rs.next()); - query.commit(); - } - - @Test(expected = UnsupportedOperationException.class) - public void setFilters_noDelegate_shouldFail() { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - ArrayList<Filter> filters = new ArrayList<Filter>(); - filters.add(new Like("name", "%lle")); - query.setFilters(filters); - } - - @Test(expected = UnsupportedOperationException.class) - public void setOrderBy_noDelegate_shouldFail() { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - query.setOrderBy(Arrays.asList(new OrderBy("name", true))); - } - - @Test(expected = IllegalStateException.class) - public void storeRow_noDelegateNoTransactionActive_shouldFail() - throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - query.storeRow(new RowItem(new SQLContainer(query), new RowId( - new Object[] { 1 }), null)); - } - - @Test(expected = UnsupportedOperationException.class) - public void storeRow_noDelegate_shouldFail() throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - SQLContainer container = EasyMock.createNiceMock(SQLContainer.class); - EasyMock.replay(container); - query.beginTransaction(); - query.storeRow(new RowItem(container, new RowId(new Object[] { 1 }), - null)); - query.commit(); - EasyMock.verify(container); - } - - @Test(expected = UnsupportedOperationException.class) - public void removeRow_noDelegate_shouldFail() throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - SQLContainer container = EasyMock.createNiceMock(SQLContainer.class); - EasyMock.replay(container); - query.beginTransaction(); - query.removeRow(new RowItem(container, new RowId(new Object[] { 1 }), - null)); - query.commit(); - EasyMock.verify(container); - } - - @Test - public void beginTransaction_readOnly_shouldSucceed() throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - query.beginTransaction(); - } - - @Test - public void commit_readOnly_shouldSucceed() throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - query.beginTransaction(); - query.commit(); - } - - @Test - public void rollback_readOnly_shouldSucceed() throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - query.beginTransaction(); - query.rollback(); - } - - @Test(expected = SQLException.class) - public void commit_noActiveTransaction_shouldFail() throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - query.commit(); - } - - @Test(expected = SQLException.class) - public void rollback_noActiveTransaction_shouldFail() throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - query.rollback(); - } - - @Test - public void containsRowWithKeys_simpleQueryWithExistingKeys_returnsTrue() - throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - Assert.assertTrue(query.containsRowWithKey(1)); - } - - @Test - public void containsRowWithKeys_simpleQueryWithNonexistingKeys_returnsTrue() - throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - Assert.assertFalse(query.containsRowWithKey(1337)); - } - - // (expected = SQLException.class) - @Test - public void containsRowWithKeys_simpleQueryWithInvalidKeys_shouldFail() - throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - Assert.assertFalse(query.containsRowWithKey(38796)); - } - - @Test - public void containsRowWithKeys_queryContainingWhereClauseAndExistingKeys_returnsTrue() - throws SQLException { - FreeformQuery query = new FreeformQuery( - "SELECT * FROM people WHERE \"NAME\" LIKE '%lle'", - Arrays.asList("ID"), connectionPool); - Assert.assertTrue(query.containsRowWithKey(1)); - } - - @Test - public void containsRowWithKeys_queryContainingLowercaseWhereClauseAndExistingKeys_returnsTrue() - throws SQLException { - FreeformQuery query = new FreeformQuery( - "select * from people where \"NAME\" like '%lle'", - Arrays.asList("ID"), connectionPool); - Assert.assertTrue(query.containsRowWithKey(1)); - } - - @Test - public void containsRowWithKeys_nullKeys_shouldFailAndReleaseConnections() - throws SQLException { - FreeformQuery query = new FreeformQuery( - "select * from people where \"NAME\" like '%lle'", - Arrays.asList("ID"), connectionPool); - try { - query.containsRowWithKey(new Object[] { null }); - } catch (SQLException e) { - // We should now be able to reserve two connections - connectionPool.reserveConnection(); - connectionPool.reserveConnection(); - } - } - - /* - * -------- Tests with a delegate --------- - */ - - @Test - public void setDelegate_noExistingDelegate_shouldRegisterNewDelegate() { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - query.setDelegate(delegate); - Assert.assertEquals(delegate, query.getDelegate()); - } - - @Test - public void getResults_hasDelegate_shouldCallDelegate() throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - if (AllTests.db == DB.MSSQL) { - EasyMock.expect(delegate.getQueryString(0, 2)) - .andReturn( - "SELECT * FROM (SELECT row_number()" - + "OVER (ORDER BY id ASC) AS rownum, * FROM people)" - + " AS a WHERE a.rownum BETWEEN 0 AND 2"); - } else if (AllTests.db == DB.ORACLE) { - EasyMock.expect(delegate.getQueryString(0, 2)) - .andReturn( - "SELECT * FROM (SELECT x.*, ROWNUM AS r FROM" - + " (SELECT * FROM people) x) WHERE r BETWEEN 1 AND 2"); - } else { - EasyMock.expect(delegate.getQueryString(0, 2)).andReturn( - "SELECT * FROM people LIMIT 2 OFFSET 0"); - } - EasyMock.replay(delegate); - - query.setDelegate(delegate); - query.beginTransaction(); - query.getResults(0, 2); - EasyMock.verify(delegate); - query.commit(); - } - - @Test - public void getResults_delegateImplementsGetQueryString_shouldHonorOffsetAndPagelength() - throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - if (AllTests.db == DB.MSSQL) { - EasyMock.expect(delegate.getQueryString(0, 2)) - .andReturn( - "SELECT * FROM (SELECT row_number()" - + "OVER (ORDER BY id ASC) AS rownum, * FROM people)" - + " AS a WHERE a.rownum BETWEEN 0 AND 2"); - } else if (AllTests.db == DB.ORACLE) { - EasyMock.expect(delegate.getQueryString(0, 2)) - .andReturn( - "SELECT * FROM (SELECT x.*, ROWNUM AS r FROM" - + " (SELECT * FROM people) x) WHERE r BETWEEN 1 AND 2"); - } else { - EasyMock.expect(delegate.getQueryString(0, 2)).andReturn( - "SELECT * FROM people LIMIT 2 OFFSET 0"); - } - EasyMock.replay(delegate); - query.setDelegate(delegate); - - query.beginTransaction(); - ResultSet rs = query.getResults(0, 2); - int rsoffset = 0; - if (AllTests.db == DB.MSSQL) { - rsoffset++; - } - Assert.assertTrue(rs.next()); - Assert.assertEquals(0 + offset, rs.getInt(1 + rsoffset)); - Assert.assertEquals("Ville", rs.getString(2 + rsoffset)); - - Assert.assertTrue(rs.next()); - Assert.assertEquals(1 + offset, rs.getInt(1 + rsoffset)); - Assert.assertEquals("Kalle", rs.getString(2 + rsoffset)); - - Assert.assertFalse(rs.next()); - - EasyMock.verify(delegate); - query.commit(); - } - - @Test - public void getResults_delegateRegistered5000Rows_returns100rows() - throws SQLException { - DataGenerator.addFiveThousandPeople(connectionPool); - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - if (AllTests.db == DB.MSSQL) { - EasyMock.expect(delegate.getQueryString(200, 100)) - .andReturn( - "SELECT * FROM (SELECT row_number()" - + "OVER (ORDER BY id ASC) AS rownum, * FROM people)" - + " AS a WHERE a.rownum BETWEEN 201 AND 300"); - } else if (AllTests.db == DB.ORACLE) { - EasyMock.expect(delegate.getQueryString(200, 100)) - .andReturn( - "SELECT * FROM (SELECT x.*, ROWNUM AS r FROM" - + " (SELECT * FROM people ORDER BY ID ASC) x) WHERE r BETWEEN 201 AND 300"); - } else { - EasyMock.expect(delegate.getQueryString(200, 100)).andReturn( - "SELECT * FROM people LIMIT 100 OFFSET 200"); - } - EasyMock.replay(delegate); - query.setDelegate(delegate); - - query.beginTransaction(); - ResultSet rs = query.getResults(200, 100); - for (int i = 0; i < 100; i++) { - Assert.assertTrue(rs.next()); - Assert.assertEquals(200 + i + offset, rs.getInt("ID")); - } - Assert.assertFalse(rs.next()); - query.commit(); - } - - @Test - public void setFilters_delegateImplementsSetFilters_shouldPassFiltersToDelegate() { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - List<Filter> filters = new ArrayList<Filter>(); - filters.add(new Like("name", "%lle")); - delegate.setFilters(filters); - - EasyMock.replay(delegate); - query.setDelegate(delegate); - - query.setFilters(filters); - - EasyMock.verify(delegate); - } - - @Test(expected = UnsupportedOperationException.class) - public void setFilters_delegateDoesNotImplementSetFilters_shouldFail() { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - List<Filter> filters = new ArrayList<Filter>(); - filters.add(new Like("name", "%lle")); - delegate.setFilters(filters); - EasyMock.expectLastCall().andThrow(new UnsupportedOperationException()); - EasyMock.replay(delegate); - query.setDelegate(delegate); - - query.setFilters(filters); - - EasyMock.verify(delegate); - } - - @Test - public void setOrderBy_delegateImplementsSetOrderBy_shouldPassArgumentsToDelegate() { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - List<OrderBy> orderBys = Arrays.asList(new OrderBy("name", false)); - delegate.setOrderBy(orderBys); - EasyMock.replay(delegate); - query.setDelegate(delegate); - - query.setOrderBy(orderBys); - - EasyMock.verify(delegate); - } - - @Test(expected = UnsupportedOperationException.class) - public void setOrderBy_delegateDoesNotImplementSetOrderBy_shouldFail() { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - List<OrderBy> orderBys = Arrays.asList(new OrderBy("name", false)); - delegate.setOrderBy(orderBys); - EasyMock.expectLastCall().andThrow(new UnsupportedOperationException()); - EasyMock.replay(delegate); - query.setDelegate(delegate); - - query.setOrderBy(orderBys); - - EasyMock.verify(delegate); - } - - @Test - public void setFilters_noDelegateAndNullParameter_shouldSucceed() { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - query.setFilters(null); - } - - @Test - public void setOrderBy_noDelegateAndNullParameter_shouldSucceed() { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - query.setOrderBy(null); - } - - @Test - public void storeRow_delegateImplementsStoreRow_shouldPassToDelegate() - throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - EasyMock.expect( - delegate.storeRow(EasyMock.isA(Connection.class), - EasyMock.isA(RowItem.class))).andReturn(1); - SQLContainer container = EasyMock.createNiceMock(SQLContainer.class); - EasyMock.replay(delegate, container); - query.setDelegate(delegate); - - query.beginTransaction(); - RowItem row = new RowItem(container, new RowId(new Object[] { 1 }), - null); - query.storeRow(row); - query.commit(); - - EasyMock.verify(delegate, container); - } - - @Test(expected = UnsupportedOperationException.class) - public void storeRow_delegateDoesNotImplementStoreRow_shouldFail() - throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - EasyMock.expect( - delegate.storeRow(EasyMock.isA(Connection.class), - EasyMock.isA(RowItem.class))).andThrow( - new UnsupportedOperationException()); - SQLContainer container = EasyMock.createNiceMock(SQLContainer.class); - EasyMock.replay(delegate, container); - query.setDelegate(delegate); - - query.beginTransaction(); - RowItem row = new RowItem(container, new RowId(new Object[] { 1 }), - null); - query.storeRow(row); - query.commit(); - - EasyMock.verify(delegate, container); - } - - @Test - public void removeRow_delegateImplementsRemoveRow_shouldPassToDelegate() - throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - EasyMock.expect( - delegate.removeRow(EasyMock.isA(Connection.class), - EasyMock.isA(RowItem.class))).andReturn(true); - SQLContainer container = EasyMock.createNiceMock(SQLContainer.class); - EasyMock.replay(delegate, container); - query.setDelegate(delegate); - - query.beginTransaction(); - RowItem row = new RowItem(container, new RowId(new Object[] { 1 }), - null); - query.removeRow(row); - query.commit(); - - EasyMock.verify(delegate, container); - } - - @Test(expected = UnsupportedOperationException.class) - public void removeRow_delegateDoesNotImplementRemoveRow_shouldFail() - throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - EasyMock.expect( - delegate.removeRow(EasyMock.isA(Connection.class), - EasyMock.isA(RowItem.class))).andThrow( - new UnsupportedOperationException()); - SQLContainer container = EasyMock.createNiceMock(SQLContainer.class); - EasyMock.replay(delegate, container); - query.setDelegate(delegate); - - query.beginTransaction(); - RowItem row = new RowItem(container, new RowId(new Object[] { 1 }), - null); - query.removeRow(row); - query.commit(); - - EasyMock.verify(delegate, container); - } - - @Test - public void beginTransaction_delegateRegistered_shouldSucceed() - throws UnsupportedOperationException, SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - EasyMock.replay(delegate); - query.setDelegate(delegate); - - query.beginTransaction(); - } - - @Test(expected = IllegalStateException.class) - public void beginTransaction_transactionAlreadyActive_shouldFail() - throws SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - - query.beginTransaction(); - query.beginTransaction(); - } - - @Test(expected = SQLException.class) - public void commit_delegateRegisteredNoActiveTransaction_shouldFail() - throws UnsupportedOperationException, SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - EasyMock.replay(delegate); - query.setDelegate(delegate); - - query.commit(); - } - - @Test - public void commit_delegateRegisteredActiveTransaction_shouldSucceed() - throws UnsupportedOperationException, SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - EasyMock.replay(delegate); - query.setDelegate(delegate); - - query.beginTransaction(); - query.commit(); - } - - @Test(expected = SQLException.class) - public void commit_delegateRegisteredActiveTransactionDoubleCommit_shouldFail() - throws UnsupportedOperationException, SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - EasyMock.replay(delegate); - query.setDelegate(delegate); - - query.beginTransaction(); - query.commit(); - query.commit(); - } - - @Test(expected = SQLException.class) - public void rollback_delegateRegisteredNoActiveTransaction_shouldFail() - throws UnsupportedOperationException, SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - EasyMock.replay(delegate); - query.setDelegate(delegate); - - query.rollback(); - } - - @Test - public void rollback_delegateRegisteredActiveTransaction_shouldSucceed() - throws UnsupportedOperationException, SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - EasyMock.replay(delegate); - query.setDelegate(delegate); - - query.beginTransaction(); - query.rollback(); - } - - @Test(expected = SQLException.class) - public void rollback_delegateRegisteredActiveTransactionDoubleRollback_shouldFail() - throws UnsupportedOperationException, SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - EasyMock.replay(delegate); - query.setDelegate(delegate); - - query.beginTransaction(); - query.rollback(); - query.rollback(); - } - - @Test(expected = SQLException.class) - public void rollback_delegateRegisteredCommittedTransaction_shouldFail() - throws UnsupportedOperationException, SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - EasyMock.replay(delegate); - query.setDelegate(delegate); - - query.beginTransaction(); - query.commit(); - query.rollback(); - } - - @Test(expected = SQLException.class) - public void commit_delegateRegisteredRollbackedTransaction_shouldFail() - throws UnsupportedOperationException, SQLException { - FreeformQuery query = new FreeformQuery("SELECT * FROM people", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - EasyMock.replay(delegate); - query.setDelegate(delegate); - - query.beginTransaction(); - query.rollback(); - query.commit(); - } - - @Test(expected = SQLException.class) - public void containsRowWithKeys_delegateRegistered_shouldCallGetContainsRowQueryString() - throws SQLException { - FreeformQuery query = new FreeformQuery( - "SELECT * FROM people WHERE name LIKE '%lle'", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - EasyMock.expect(delegate.getContainsRowQueryString(1)).andReturn(""); - EasyMock.replay(delegate); - query.setDelegate(delegate); - - query.containsRowWithKey(1); - - EasyMock.verify(delegate); - } - - @Test - public void containsRowWithKeys_delegateRegistered_shouldUseResultFromGetContainsRowQueryString() - throws SQLException { - FreeformQuery query = new FreeformQuery( - "SELECT * FROM people WHERE \"NAME\" LIKE '%lle'", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - // In order to test that this is the query that is actually used, we use - // a non-existing id in place of the existing one. - EasyMock.expect(delegate.getContainsRowQueryString(1)) - .andReturn( - "SELECT * FROM people WHERE \"NAME\" LIKE '%lle' AND \"ID\" = 1337"); - EasyMock.replay(delegate); - query.setDelegate(delegate); - - // The id (key) used should be 1337 as above, for the call with key = 1 - Assert.assertFalse(query.containsRowWithKey(1)); - - EasyMock.verify(delegate); - } - - @Test - public void containsRowWithKeys_delegateRegisteredGetContainsRowQueryStringNotImplemented_shouldBuildQueryString() - throws SQLException { - FreeformQuery query = new FreeformQuery( - "SELECT * FROM people WHERE \"NAME\" LIKE '%lle'", - Arrays.asList("ID"), connectionPool); - FreeformQueryDelegate delegate = EasyMock - .createMock(FreeformQueryDelegate.class); - EasyMock.expect(delegate.getContainsRowQueryString(1)).andThrow( - new UnsupportedOperationException()); - EasyMock.replay(delegate); - query.setDelegate(delegate); - - Assert.assertTrue(query.containsRowWithKey(1)); - - EasyMock.verify(delegate); - } -} diff --git a/tests/server-side/com/vaadin/data/util/sqlcontainer/query/QueryBuilderTest.java b/tests/server-side/com/vaadin/data/util/sqlcontainer/query/QueryBuilderTest.java deleted file mode 100644 index f635b23ebd..0000000000 --- a/tests/server-side/com/vaadin/data/util/sqlcontainer/query/QueryBuilderTest.java +++ /dev/null @@ -1,311 +0,0 @@ -package com.vaadin.data.util.sqlcontainer.query; - -import java.util.ArrayList; - -import junit.framework.Assert; - -import org.easymock.EasyMock; -import org.junit.Test; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.util.filter.And; -import com.vaadin.data.util.filter.Between; -import com.vaadin.data.util.filter.Compare.Equal; -import com.vaadin.data.util.filter.Compare.Greater; -import com.vaadin.data.util.filter.Compare.GreaterOrEqual; -import com.vaadin.data.util.filter.Compare.Less; -import com.vaadin.data.util.filter.Compare.LessOrEqual; -import com.vaadin.data.util.filter.IsNull; -import com.vaadin.data.util.filter.Like; -import com.vaadin.data.util.filter.Not; -import com.vaadin.data.util.filter.Or; -import com.vaadin.data.util.filter.SimpleStringFilter; -import com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper; -import com.vaadin.data.util.sqlcontainer.query.generator.filter.QueryBuilder; -import com.vaadin.data.util.sqlcontainer.query.generator.filter.StringDecorator; - -public class QueryBuilderTest { - - private StatementHelper mockedStatementHelper(Object... values) { - StatementHelper sh = EasyMock.createMock(StatementHelper.class); - for (Object val : values) { - sh.addParameterValue(val); - EasyMock.expectLastCall(); - } - EasyMock.replay(sh); - return sh; - } - - // escape bad characters and wildcards - - @Test - public void getWhereStringForFilter_equals() { - StatementHelper sh = mockedStatementHelper("Fido"); - Equal f = new Equal("NAME", "Fido"); - Assert.assertEquals("\"NAME\" = ?", - QueryBuilder.getWhereStringForFilter(f, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilter_greater() { - StatementHelper sh = mockedStatementHelper(18); - Greater f = new Greater("AGE", 18); - Assert.assertEquals("\"AGE\" > ?", - QueryBuilder.getWhereStringForFilter(f, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilter_less() { - StatementHelper sh = mockedStatementHelper(65); - Less f = new Less("AGE", 65); - Assert.assertEquals("\"AGE\" < ?", - QueryBuilder.getWhereStringForFilter(f, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilter_greaterOrEqual() { - StatementHelper sh = mockedStatementHelper(18); - GreaterOrEqual f = new GreaterOrEqual("AGE", 18); - Assert.assertEquals("\"AGE\" >= ?", - QueryBuilder.getWhereStringForFilter(f, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilter_lessOrEqual() { - StatementHelper sh = mockedStatementHelper(65); - LessOrEqual f = new LessOrEqual("AGE", 65); - Assert.assertEquals("\"AGE\" <= ?", - QueryBuilder.getWhereStringForFilter(f, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilter_simpleStringFilter() { - StatementHelper sh = mockedStatementHelper("Vi%"); - SimpleStringFilter f = new SimpleStringFilter("NAME", "Vi", false, true); - Assert.assertEquals("\"NAME\" LIKE ?", - QueryBuilder.getWhereStringForFilter(f, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilter_simpleStringFilterMatchAnywhere() { - StatementHelper sh = mockedStatementHelper("%Vi%"); - SimpleStringFilter f = new SimpleStringFilter("NAME", "Vi", false, - false); - Assert.assertEquals("\"NAME\" LIKE ?", - QueryBuilder.getWhereStringForFilter(f, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilter_simpleStringFilterMatchAnywhereIgnoreCase() { - StatementHelper sh = mockedStatementHelper("%VI%"); - SimpleStringFilter f = new SimpleStringFilter("NAME", "Vi", true, false); - Assert.assertEquals("UPPER(\"NAME\") LIKE ?", - QueryBuilder.getWhereStringForFilter(f, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilter_startsWith() { - StatementHelper sh = mockedStatementHelper("Vi%"); - Like f = new Like("NAME", "Vi%"); - Assert.assertEquals("\"NAME\" LIKE ?", - QueryBuilder.getWhereStringForFilter(f, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilter_startsWithNumber() { - StatementHelper sh = mockedStatementHelper("1%"); - Like f = new Like("AGE", "1%"); - Assert.assertEquals("\"AGE\" LIKE ?", - QueryBuilder.getWhereStringForFilter(f, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilter_endsWith() { - StatementHelper sh = mockedStatementHelper("%lle"); - Like f = new Like("NAME", "%lle"); - Assert.assertEquals("\"NAME\" LIKE ?", - QueryBuilder.getWhereStringForFilter(f, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilter_contains() { - StatementHelper sh = mockedStatementHelper("%ill%"); - Like f = new Like("NAME", "%ill%"); - Assert.assertEquals("\"NAME\" LIKE ?", - QueryBuilder.getWhereStringForFilter(f, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilter_between() { - StatementHelper sh = mockedStatementHelper(18, 65); - Between f = new Between("AGE", 18, 65); - Assert.assertEquals("\"AGE\" BETWEEN ? AND ?", - QueryBuilder.getWhereStringForFilter(f, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilter_caseInsensitive_equals() { - StatementHelper sh = mockedStatementHelper("FIDO"); - Like f = new Like("NAME", "Fido"); - f.setCaseSensitive(false); - Assert.assertEquals("UPPER(\"NAME\") LIKE ?", - QueryBuilder.getWhereStringForFilter(f, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilter_caseInsensitive_startsWith() { - StatementHelper sh = mockedStatementHelper("VI%"); - Like f = new Like("NAME", "Vi%"); - f.setCaseSensitive(false); - Assert.assertEquals("UPPER(\"NAME\") LIKE ?", - QueryBuilder.getWhereStringForFilter(f, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilter_caseInsensitive_endsWith() { - StatementHelper sh = mockedStatementHelper("%LLE"); - Like f = new Like("NAME", "%lle"); - f.setCaseSensitive(false); - Assert.assertEquals("UPPER(\"NAME\") LIKE ?", - QueryBuilder.getWhereStringForFilter(f, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilter_caseInsensitive_contains() { - StatementHelper sh = mockedStatementHelper("%ILL%"); - Like f = new Like("NAME", "%ill%"); - f.setCaseSensitive(false); - Assert.assertEquals("UPPER(\"NAME\") LIKE ?", - QueryBuilder.getWhereStringForFilter(f, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilters_listOfFilters() { - StatementHelper sh = mockedStatementHelper("%lle", 18); - ArrayList<Filter> filters = new ArrayList<Filter>(); - filters.add(new Like("NAME", "%lle")); - filters.add(new Greater("AGE", 18)); - Assert.assertEquals(" WHERE \"NAME\" LIKE ? AND \"AGE\" > ?", - QueryBuilder.getWhereStringForFilters(filters, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilters_oneAndFilter() { - StatementHelper sh = mockedStatementHelper("%lle", 18); - ArrayList<Filter> filters = new ArrayList<Filter>(); - filters.add(new And(new Like("NAME", "%lle"), new Greater("AGE", 18))); - Assert.assertEquals(" WHERE (\"NAME\" LIKE ? AND \"AGE\" > ?)", - QueryBuilder.getWhereStringForFilters(filters, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilters_oneOrFilter() { - StatementHelper sh = mockedStatementHelper("%lle", 18); - ArrayList<Filter> filters = new ArrayList<Filter>(); - filters.add(new Or(new Like("NAME", "%lle"), new Greater("AGE", 18))); - Assert.assertEquals(" WHERE (\"NAME\" LIKE ? OR \"AGE\" > ?)", - QueryBuilder.getWhereStringForFilters(filters, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilters_complexCompoundFilters() { - StatementHelper sh = mockedStatementHelper("%lle", 18, 65, "Pelle"); - ArrayList<Filter> filters = new ArrayList<Filter>(); - filters.add(new Or(new And(new Like("NAME", "%lle"), new Or(new Less( - "AGE", 18), new Greater("AGE", 65))), - new Equal("NAME", "Pelle"))); - Assert.assertEquals( - " WHERE ((\"NAME\" LIKE ? AND (\"AGE\" < ? OR \"AGE\" > ?)) OR \"NAME\" = ?)", - QueryBuilder.getWhereStringForFilters(filters, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilters_complexCompoundFiltersAndSingleFilter() { - StatementHelper sh = mockedStatementHelper("%lle", 18, 65, "Pelle", - "Virtanen"); - ArrayList<Filter> filters = new ArrayList<Filter>(); - filters.add(new Or(new And(new Like("NAME", "%lle"), new Or(new Less( - "AGE", 18), new Greater("AGE", 65))), - new Equal("NAME", "Pelle"))); - filters.add(new Equal("LASTNAME", "Virtanen")); - Assert.assertEquals( - " WHERE ((\"NAME\" LIKE ? AND (\"AGE\" < ? OR \"AGE\" > ?)) OR \"NAME\" = ?) AND \"LASTNAME\" = ?", - QueryBuilder.getWhereStringForFilters(filters, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilters_emptyList_shouldReturnEmptyString() { - ArrayList<Filter> filters = new ArrayList<Filter>(); - Assert.assertEquals("", QueryBuilder.getWhereStringForFilters(filters, - new StatementHelper())); - } - - @Test - public void getWhereStringForFilters_NotFilter() { - StatementHelper sh = mockedStatementHelper(18); - ArrayList<Filter> filters = new ArrayList<Filter>(); - filters.add(new Not(new Equal("AGE", 18))); - Assert.assertEquals(" WHERE NOT \"AGE\" = ?", - QueryBuilder.getWhereStringForFilters(filters, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilters_complexNegatedFilter() { - StatementHelper sh = mockedStatementHelper(65, 18); - ArrayList<Filter> filters = new ArrayList<Filter>(); - filters.add(new Not(new Or(new Equal("AGE", 65), new Equal("AGE", 18)))); - Assert.assertEquals(" WHERE NOT (\"AGE\" = ? OR \"AGE\" = ?)", - QueryBuilder.getWhereStringForFilters(filters, sh)); - EasyMock.verify(sh); - } - - @Test - public void getWhereStringForFilters_isNull() { - ArrayList<Filter> filters = new ArrayList<Filter>(); - filters.add(new IsNull("NAME")); - Assert.assertEquals(" WHERE \"NAME\" IS NULL", QueryBuilder - .getWhereStringForFilters(filters, new StatementHelper())); - } - - @Test - public void getWhereStringForFilters_isNotNull() { - ArrayList<Filter> filters = new ArrayList<Filter>(); - filters.add(new Not(new IsNull("NAME"))); - Assert.assertEquals(" WHERE \"NAME\" IS NOT NULL", QueryBuilder - .getWhereStringForFilters(filters, new StatementHelper())); - } - - @Test - public void getWhereStringForFilters_customStringDecorator() { - QueryBuilder.setStringDecorator(new StringDecorator("[", "]")); - ArrayList<Filter> filters = new ArrayList<Filter>(); - filters.add(new Not(new IsNull("NAME"))); - Assert.assertEquals(" WHERE [NAME] IS NOT NULL", QueryBuilder - .getWhereStringForFilters(filters, new StatementHelper())); - // Reset the default string decorator - QueryBuilder.setStringDecorator(new StringDecorator("\"", "\"")); - } -} diff --git a/tests/server-side/com/vaadin/data/util/sqlcontainer/query/TableQueryTest.java b/tests/server-side/com/vaadin/data/util/sqlcontainer/query/TableQueryTest.java deleted file mode 100644 index e135894013..0000000000 --- a/tests/server-side/com/vaadin/data/util/sqlcontainer/query/TableQueryTest.java +++ /dev/null @@ -1,663 +0,0 @@ -package com.vaadin.data.util.sqlcontainer.query; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.util.filter.Compare.Equal; -import com.vaadin.data.util.filter.Like; -import com.vaadin.data.util.sqlcontainer.AllTests; -import com.vaadin.data.util.sqlcontainer.AllTests.DB; -import com.vaadin.data.util.sqlcontainer.DataGenerator; -import com.vaadin.data.util.sqlcontainer.OptimisticLockException; -import com.vaadin.data.util.sqlcontainer.RowItem; -import com.vaadin.data.util.sqlcontainer.SQLContainer; -import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool; -import com.vaadin.data.util.sqlcontainer.connection.SimpleJDBCConnectionPool; -import com.vaadin.data.util.sqlcontainer.query.generator.DefaultSQLGenerator; - -public class TableQueryTest { - private static final int offset = AllTests.offset; - private JDBCConnectionPool connectionPool; - - @Before - public void setUp() throws SQLException { - - try { - connectionPool = new SimpleJDBCConnectionPool(AllTests.dbDriver, - AllTests.dbURL, AllTests.dbUser, AllTests.dbPwd, 2, 2); - } catch (SQLException e) { - e.printStackTrace(); - Assert.fail(e.getMessage()); - } - - DataGenerator.addPeopleToDatabase(connectionPool); - } - - @After - public void tearDown() { - if (connectionPool != null) { - connectionPool.destroy(); - } - } - - /********************************************************************** - * TableQuery construction tests - **********************************************************************/ - @Test - public void construction_legalParameters_shouldSucceed() { - TableQuery tQuery = new TableQuery("people", connectionPool, - new DefaultSQLGenerator()); - Assert.assertArrayEquals(new Object[] { "ID" }, tQuery - .getPrimaryKeyColumns().toArray()); - boolean correctTableName = "people".equalsIgnoreCase(tQuery - .getTableName()); - Assert.assertTrue(correctTableName); - } - - @Test - public void construction_legalParameters_defaultGenerator_shouldSucceed() { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - Assert.assertArrayEquals(new Object[] { "ID" }, tQuery - .getPrimaryKeyColumns().toArray()); - boolean correctTableName = "people".equalsIgnoreCase(tQuery - .getTableName()); - Assert.assertTrue(correctTableName); - } - - @Test(expected = IllegalArgumentException.class) - public void construction_nonExistingTableName_shouldFail() { - new TableQuery("skgwaguhsd", connectionPool, new DefaultSQLGenerator()); - } - - @Test(expected = IllegalArgumentException.class) - public void construction_emptyTableName_shouldFail() { - new TableQuery("", connectionPool, new DefaultSQLGenerator()); - } - - @Test(expected = IllegalArgumentException.class) - public void construction_nullSqlGenerator_shouldFail() { - new TableQuery("people", connectionPool, null); - } - - @Test(expected = IllegalArgumentException.class) - public void construction_nullConnectionPool_shouldFail() { - new TableQuery("people", null, new DefaultSQLGenerator()); - } - - /********************************************************************** - * TableQuery row count tests - **********************************************************************/ - @Test - public void getCount_simpleQuery_returnsFour() throws SQLException { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - Assert.assertEquals(4, tQuery.getCount()); - } - - @Test - public void getCount_simpleQueryTwoMorePeopleAdded_returnsSix() - throws SQLException { - // Add some people - Connection conn = connectionPool.reserveConnection(); - Statement statement = conn.createStatement(); - if (AllTests.db == DB.MSSQL) { - statement.executeUpdate("insert into people values('Bengt', 30)"); - statement.executeUpdate("insert into people values('Ingvar', 50)"); - } else { - statement - .executeUpdate("insert into people values(default, 'Bengt', 30)"); - statement - .executeUpdate("insert into people values(default, 'Ingvar', 50)"); - } - statement.close(); - conn.commit(); - connectionPool.releaseConnection(conn); - - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - - Assert.assertEquals(6, tQuery.getCount()); - } - - @Test - public void getCount_normalState_releasesConnection() throws SQLException { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - tQuery.getCount(); - tQuery.getCount(); - Assert.assertNotNull(connectionPool.reserveConnection()); - } - - /********************************************************************** - * TableQuery get results tests - **********************************************************************/ - @Test - public void getResults_simpleQuery_returnsFourRecords() throws SQLException { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - tQuery.beginTransaction(); - ResultSet rs = tQuery.getResults(0, 0); - - Assert.assertTrue(rs.next()); - Assert.assertEquals(0 + offset, rs.getInt(1)); - Assert.assertEquals("Ville", rs.getString(2)); - - Assert.assertTrue(rs.next()); - Assert.assertEquals(1 + offset, rs.getInt(1)); - Assert.assertEquals("Kalle", rs.getString(2)); - - Assert.assertTrue(rs.next()); - Assert.assertEquals(2 + offset, rs.getInt(1)); - Assert.assertEquals("Pelle", rs.getString(2)); - - Assert.assertTrue(rs.next()); - Assert.assertEquals(3 + offset, rs.getInt(1)); - Assert.assertEquals("Börje", rs.getString(2)); - - Assert.assertFalse(rs.next()); - tQuery.commit(); - } - - @Test - public void getResults_noDelegate5000Rows_returns5000rows() - throws SQLException { - DataGenerator.addFiveThousandPeople(connectionPool); - - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - - tQuery.beginTransaction(); - ResultSet rs = tQuery.getResults(0, 0); - for (int i = 0; i < 5000; i++) { - Assert.assertTrue(rs.next()); - } - Assert.assertFalse(rs.next()); - tQuery.commit(); - } - - /********************************************************************** - * TableQuery transaction management tests - **********************************************************************/ - @Test - public void beginTransaction_readOnly_shouldSucceed() throws SQLException { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - tQuery.beginTransaction(); - } - - @Test(expected = IllegalStateException.class) - public void beginTransaction_transactionAlreadyActive_shouldFail() - throws SQLException { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - - tQuery.beginTransaction(); - tQuery.beginTransaction(); - } - - @Test - public void commit_readOnly_shouldSucceed() throws SQLException { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - tQuery.beginTransaction(); - tQuery.commit(); - } - - @Test - public void rollback_readOnly_shouldSucceed() throws SQLException { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - tQuery.beginTransaction(); - tQuery.rollback(); - } - - @Test(expected = SQLException.class) - public void commit_noActiveTransaction_shouldFail() throws SQLException { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - tQuery.commit(); - } - - @Test(expected = SQLException.class) - public void rollback_noActiveTransaction_shouldFail() throws SQLException { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - tQuery.rollback(); - } - - /********************************************************************** - * TableQuery row query with given keys tests - **********************************************************************/ - @Test - public void containsRowWithKeys_existingKeys_returnsTrue() - throws SQLException { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - Assert.assertTrue(tQuery.containsRowWithKey(1)); - } - - @Test - public void containsRowWithKeys_nonexistingKeys_returnsTrue() - throws SQLException { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - - Assert.assertFalse(tQuery.containsRowWithKey(1337)); - } - - @Test - public void containsRowWithKeys_invalidKeys_shouldFail() - throws SQLException { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - boolean b = true; - try { - b = tQuery.containsRowWithKey("foo"); - } catch (SQLException se) { - return; - } - Assert.assertFalse(b); - } - - @Test - public void containsRowWithKeys_nullKeys_shouldFailAndReleaseConnections() - throws SQLException { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - try { - tQuery.containsRowWithKey(new Object[] { null }); - org.junit.Assert - .fail("null should throw an IllegalArgumentException from StatementHelper"); - } catch (IllegalArgumentException e) { - // We should now be able to reserve two connections - connectionPool.reserveConnection(); - connectionPool.reserveConnection(); - } - } - - /********************************************************************** - * TableQuery filtering and ordering tests - **********************************************************************/ - @Test - public void setFilters_shouldReturnCorrectCount() throws SQLException { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - List<Filter> filters = new ArrayList<Filter>(); - filters.add(new Like("NAME", "%lle")); - tQuery.setFilters(filters); - Assert.assertEquals(3, tQuery.getCount()); - } - - @Test - public void setOrderByNameAscending_shouldReturnCorrectOrder() - throws SQLException { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - - List<OrderBy> orderBys = Arrays.asList(new OrderBy("NAME", true)); - tQuery.setOrderBy(orderBys); - - tQuery.beginTransaction(); - ResultSet rs; - rs = tQuery.getResults(0, 0); - - Assert.assertTrue(rs.next()); - Assert.assertEquals(3 + offset, rs.getInt(1)); - Assert.assertEquals("Börje", rs.getString(2)); - - Assert.assertTrue(rs.next()); - Assert.assertEquals(1 + offset, rs.getInt(1)); - Assert.assertEquals("Kalle", rs.getString(2)); - - Assert.assertTrue(rs.next()); - Assert.assertEquals(2 + offset, rs.getInt(1)); - Assert.assertEquals("Pelle", rs.getString(2)); - - Assert.assertTrue(rs.next()); - Assert.assertEquals(0 + offset, rs.getInt(1)); - Assert.assertEquals("Ville", rs.getString(2)); - - Assert.assertFalse(rs.next()); - tQuery.commit(); - } - - @Test - public void setOrderByNameDescending_shouldReturnCorrectOrder() - throws SQLException { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - - List<OrderBy> orderBys = Arrays.asList(new OrderBy("NAME", false)); - tQuery.setOrderBy(orderBys); - - tQuery.beginTransaction(); - ResultSet rs; - rs = tQuery.getResults(0, 0); - - Assert.assertTrue(rs.next()); - Assert.assertEquals(0 + offset, rs.getInt(1)); - Assert.assertEquals("Ville", rs.getString(2)); - - Assert.assertTrue(rs.next()); - Assert.assertEquals(2 + offset, rs.getInt(1)); - Assert.assertEquals("Pelle", rs.getString(2)); - - Assert.assertTrue(rs.next()); - Assert.assertEquals(1 + offset, rs.getInt(1)); - Assert.assertEquals("Kalle", rs.getString(2)); - - Assert.assertTrue(rs.next()); - Assert.assertEquals(3 + offset, rs.getInt(1)); - Assert.assertEquals("Börje", rs.getString(2)); - - Assert.assertFalse(rs.next()); - tQuery.commit(); - } - - @Test - public void setFilters_nullParameter_shouldSucceed() { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - tQuery.setFilters(null); - } - - @Test - public void setOrderBy_nullParameter_shouldSucceed() { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - tQuery.setOrderBy(null); - } - - /********************************************************************** - * TableQuery row removal tests - **********************************************************************/ - @Test - public void removeRowThroughContainer_legalRowItem_shouldSucceed() - throws SQLException { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - SQLContainer container = new SQLContainer(tQuery); - container.setAutoCommit(false); - Assert.assertTrue(container.removeItem(container.getItemIds() - .iterator().next())); - - Assert.assertEquals(4, tQuery.getCount()); - Assert.assertEquals(3, container.size()); - container.commit(); - - Assert.assertEquals(3, tQuery.getCount()); - Assert.assertEquals(3, container.size()); - } - - @Test - public void removeRowThroughContainer_nonexistingRowId_shouldFail() - throws SQLException { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - - SQLContainer container = new SQLContainer(tQuery); - container.setAutoCommit(true); - Assert.assertFalse(container.removeItem("foo")); - } - - /********************************************************************** - * TableQuery row adding / modification tests - **********************************************************************/ - @Test - public void insertRowThroughContainer_shouldSucceed() throws SQLException { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - tQuery.setVersionColumn("ID"); - - SQLContainer container = new SQLContainer(tQuery); - container.setAutoCommit(false); - - Object item = container.addItem(); - Assert.assertNotNull(item); - - Assert.assertEquals(4, tQuery.getCount()); - Assert.assertEquals(5, container.size()); - container.commit(); - - Assert.assertEquals(5, tQuery.getCount()); - Assert.assertEquals(5, container.size()); - } - - @Test - public void modifyRowThroughContainer_shouldSucceed() throws SQLException { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - - // In this test the primary key is used as a version column - tQuery.setVersionColumn("ID"); - SQLContainer container = new SQLContainer(tQuery); - container.setAutoCommit(false); - - /* Check that the container size is correct and there is no 'Viljami' */ - Assert.assertEquals(4, container.size()); - List<Filter> filters = new ArrayList<Filter>(); - filters.add(new Equal("NAME", "Viljami")); - tQuery.setFilters(filters); - Assert.assertEquals(0, tQuery.getCount()); - tQuery.setFilters(null); - - /* Fetch first item, modify and commit */ - Object item = container.getItem(container.getItemIds().iterator() - .next()); - Assert.assertNotNull(item); - - RowItem ri = (RowItem) item; - Assert.assertNotNull(ri.getItemProperty("NAME")); - ri.getItemProperty("NAME").setValue("Viljami"); - - container.commit(); - - // Check that the size is still correct and only 1 'Viljami' is found - Assert.assertEquals(4, tQuery.getCount()); - Assert.assertEquals(4, container.size()); - tQuery.setFilters(filters); - Assert.assertEquals(1, tQuery.getCount()); - } - - @Test - public void storeRow_noVersionColumn_shouldSucceed() - throws UnsupportedOperationException, SQLException { - TableQuery tQuery = new TableQuery("people", connectionPool, - AllTests.sqlGen); - SQLContainer container = new SQLContainer(tQuery); - Object id = container.addItem(); - RowItem row = (RowItem) container.getItem(id); - row.getItemProperty("NAME").setValue("R2D2"); - row.getItemProperty("AGE").setValue(123); - tQuery.beginTransaction(); - tQuery.storeRow(row); - tQuery.commit(); - - Connection conn = connectionPool.reserveConnection(); - PreparedStatement stmt = conn - .prepareStatement("SELECT * FROM PEOPLE WHERE \"NAME\" = ?"); - stmt.setString(1, "R2D2"); - ResultSet rs = stmt.executeQuery(); - Assert.assertTrue(rs.next()); - rs.close(); - stmt.close(); - connectionPool.releaseConnection(conn); - } - - @Test - public void storeRow_versionSetAndEqualToDBValue_shouldSucceed() - throws SQLException { - DataGenerator.addVersionedData(connectionPool); - - TableQuery tQuery = new TableQuery("versioned", connectionPool, - AllTests.sqlGen); - tQuery.setVersionColumn("VERSION"); - SQLContainer container = new SQLContainer(tQuery); - RowItem row = (RowItem) container.getItem(container.firstItemId()); - Assert.assertEquals("Junk", row.getItemProperty("TEXT").getValue()); - - row.getItemProperty("TEXT").setValue("asdf"); - container.commit(); - - Connection conn = connectionPool.reserveConnection(); - PreparedStatement stmt = conn - .prepareStatement("SELECT * FROM VERSIONED WHERE \"TEXT\" = ?"); - stmt.setString(1, "asdf"); - ResultSet rs = stmt.executeQuery(); - Assert.assertTrue(rs.next()); - rs.close(); - stmt.close(); - conn.commit(); - connectionPool.releaseConnection(conn); - } - - @Test(expected = OptimisticLockException.class) - public void storeRow_versionSetAndLessThanDBValue_shouldThrowException() - throws SQLException { - if (AllTests.db == DB.HSQLDB) { - throw new OptimisticLockException( - "HSQLDB doesn't support row versioning for optimistic locking - don't run this test.", - null); - } - DataGenerator.addVersionedData(connectionPool); - - TableQuery tQuery = new TableQuery("versioned", connectionPool, - AllTests.sqlGen); - tQuery.setVersionColumn("VERSION"); - SQLContainer container = new SQLContainer(tQuery); - RowItem row = (RowItem) container.getItem(container.firstItemId()); - Assert.assertEquals("Junk", row.getItemProperty("TEXT").getValue()); - - row.getItemProperty("TEXT").setValue("asdf"); - - // Update the version using another connection. - Connection conn = connectionPool.reserveConnection(); - PreparedStatement stmt = conn - .prepareStatement("UPDATE VERSIONED SET \"TEXT\" = ? WHERE \"ID\" = ?"); - stmt.setString(1, "foo"); - stmt.setObject(2, row.getItemProperty("ID").getValue()); - stmt.executeUpdate(); - stmt.close(); - conn.commit(); - connectionPool.releaseConnection(conn); - - container.commit(); - } - - @Test - public void removeRow_versionSetAndEqualToDBValue_shouldSucceed() - throws SQLException { - DataGenerator.addVersionedData(connectionPool); - - TableQuery tQuery = new TableQuery("versioned", connectionPool, - AllTests.sqlGen); - tQuery.setVersionColumn("VERSION"); - SQLContainer container = new SQLContainer(tQuery); - RowItem row = (RowItem) container.getItem(container.firstItemId()); - Assert.assertEquals("Junk", row.getItemProperty("TEXT").getValue()); - - container.removeItem(container.firstItemId()); - container.commit(); - - Connection conn = connectionPool.reserveConnection(); - PreparedStatement stmt = conn - .prepareStatement("SELECT * FROM VERSIONED WHERE \"TEXT\" = ?"); - stmt.setString(1, "Junk"); - ResultSet rs = stmt.executeQuery(); - Assert.assertFalse(rs.next()); - rs.close(); - stmt.close(); - conn.commit(); - connectionPool.releaseConnection(conn); - } - - @Test(expected = OptimisticLockException.class) - public void removeRow_versionSetAndLessThanDBValue_shouldThrowException() - throws SQLException { - if (AllTests.db == AllTests.DB.HSQLDB) { - // HSQLDB doesn't support versioning, so this is to make the test - // green. - throw new OptimisticLockException(null); - } - DataGenerator.addVersionedData(connectionPool); - - TableQuery tQuery = new TableQuery("versioned", connectionPool, - AllTests.sqlGen); - tQuery.setVersionColumn("VERSION"); - SQLContainer container = new SQLContainer(tQuery); - RowItem row = (RowItem) container.getItem(container.firstItemId()); - Assert.assertEquals("Junk", row.getItemProperty("TEXT").getValue()); - - // Update the version using another connection. - Connection conn = connectionPool.reserveConnection(); - PreparedStatement stmt = conn - .prepareStatement("UPDATE VERSIONED SET \"TEXT\" = ? WHERE \"ID\" = ?"); - stmt.setString(1, "asdf"); - stmt.setObject(2, row.getItemProperty("ID").getValue()); - stmt.executeUpdate(); - stmt.close(); - conn.commit(); - connectionPool.releaseConnection(conn); - - container.removeItem(container.firstItemId()); - container.commit(); - } - - @Test - public void removeRow_throwsOptimisticLockException_shouldStillWork() - throws SQLException { - if (AllTests.db == AllTests.DB.HSQLDB) { - // HSQLDB doesn't support versioning, so this is to make the test - // green. - return; - } - DataGenerator.addVersionedData(connectionPool); - - TableQuery tQuery = new TableQuery("versioned", connectionPool, - AllTests.sqlGen); - tQuery.setVersionColumn("VERSION"); - SQLContainer container = new SQLContainer(tQuery); - RowItem row = (RowItem) container.getItem(container.firstItemId()); - Assert.assertEquals("Junk", row.getItemProperty("TEXT").getValue()); - - // Update the version using another connection. - Connection conn = connectionPool.reserveConnection(); - PreparedStatement stmt = conn - .prepareStatement("UPDATE VERSIONED SET \"TEXT\" = ? WHERE \"ID\" = ?"); - stmt.setString(1, "asdf"); - stmt.setObject(2, row.getItemProperty("ID").getValue()); - stmt.executeUpdate(); - stmt.close(); - conn.commit(); - connectionPool.releaseConnection(conn); - - Object itemToRemove = container.firstItemId(); - try { - container.removeItem(itemToRemove); - container.commit(); - } catch (OptimisticLockException e) { - // This is expected, refresh and try again. - container.rollback(); - container.removeItem(itemToRemove); - container.commit(); - } - Object id = container.addItem(); - RowItem item = (RowItem) container.getItem(id); - item.getItemProperty("TEXT").setValue("foo"); - container.commit(); - } - -}
\ No newline at end of file |