From efca0b593bc32e274b0923b87ecba989ca0406b4 Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Tue, 17 Nov 2009 01:33:30 +0000 Subject: [PATCH] add some more tests/utility code for row updates git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@410 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../healthmarketscience/jackcess/Cursor.java | 19 +++++- .../healthmarketscience/jackcess/Table.java | 18 ++++++ .../jackcess/DatabaseTest.java | 64 +++++++++++++------ 3 files changed, 81 insertions(+), 20 deletions(-) diff --git a/src/java/com/healthmarketscience/jackcess/Cursor.java b/src/java/com/healthmarketscience/jackcess/Cursor.java index 8e7f61f..f05c689 100644 --- a/src/java/com/healthmarketscience/jackcess/Cursor.java +++ b/src/java/com/healthmarketscience/jackcess/Cursor.java @@ -28,6 +28,7 @@ King of Prussia, PA 19406 package com.healthmarketscience.jackcess; import java.io.IOException; +import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.LinkedHashMap; @@ -884,6 +885,20 @@ public abstract class Cursor implements Iterable> return _table.getRowValue(_rowState, _curPos.getRowId(), column); } + /** + * Updates a single value in the current row. + * @throws IllegalStateException if the current row is not valid (at + * beginning or end of table), or deleted. + */ + public void setCurrentRowValue(Column column, Object value) + throws IOException + { + Object[] row = new Object[_table.getColumnCount()]; + Arrays.fill(row, Column.KEEP_VALUE); + row[column.getColumnIndex()] = value; + _table.updateRow(_rowState, _curPos.getRowId(), row); + } + /** * Returns {@code true} if this cursor is up-to-date with respect to the * relevant table and related table objects, {@code false} otherwise. @@ -902,8 +917,8 @@ public abstract class Cursor implements Iterable> * Finds the next non-deleted row after the given row (as defined by this * cursor) and returns the id of the row, where "next" may be backwards if * moveForward is {@code false}. If there are no more rows, the returned - * rowId should equal the value returned by {@link #getLastPosition} if moving - * forward and {@link #getFirstPosition} if moving backward. + * rowId should equal the value returned by {@link #getLastPosition} if + * moving forward and {@link #getFirstPosition} if moving backward. */ protected abstract Position findAnotherPosition(RowState rowState, Position curPos, diff --git a/src/java/com/healthmarketscience/jackcess/Table.java b/src/java/com/healthmarketscience/jackcess/Table.java index efc9a1a..a358406 100644 --- a/src/java/com/healthmarketscience/jackcess/Table.java +++ b/src/java/com/healthmarketscience/jackcess/Table.java @@ -1151,6 +1151,24 @@ public class Table return row; } + /** + * Converts a map of columnName -> columnValue to an array of row values + * appropriate for a call to {@link #updateCurrentRow(Object...)}. + */ + public Object[] asUpdateRow(Map rowMap) { + Object[] row = new Object[_columns.size()]; + Arrays.fill(row, Column.KEEP_VALUE); + if(rowMap == null) { + return row; + } + for(Column col : _columns) { + if(rowMap.containsKey(col.getName())) { + row[col.getColumnIndex()] = rowMap.get(col.getName()); + } + } + return row; + } + /** * Add a single row to this table and write it to disk *

diff --git a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java index fac88f3..cb42a90 100644 --- a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java @@ -160,7 +160,7 @@ public class DatabaseTest extends TestCase { } public void testGetColumns() throws Exception { - List columns = open().getTable("Table1").getColumns(); + List columns = open().getTable("Table1").getColumns(); assertEquals(9, columns.size()); checkColumn(columns, 0, "A", DataType.TEXT); checkColumn(columns, 1, "B", DataType.TEXT); @@ -173,11 +173,11 @@ public class DatabaseTest extends TestCase { checkColumn(columns, 8, "I", DataType.BOOLEAN); } - static void checkColumn(List columns, int columnNumber, String name, + static void checkColumn(List columns, int columnNumber, String name, DataType dataType) throws Exception { - Column column = (Column) columns.get(columnNumber); + Column column = columns.get(columnNumber); assertEquals(name, column.getName()); assertEquals(dataType, column.getType()); } @@ -461,7 +461,7 @@ public class DatabaseTest extends TestCase { File bogusFile = new File("fooby-dooby.mdb"); assertTrue(!bogusFile.exists()); try { - Database db = open(bogusFile); + open(bogusFile); fail("FileNotFoundException should have been thrown"); } catch(FileNotFoundException e) { } @@ -930,46 +930,74 @@ public class DatabaseTest extends TestCase { t.addRow("row" + i, Column.AUTO_NUMBER, "initial data"); } - t.reset(); - t.getNextRow(); - Map row = t.getNextRow(); + Cursor c = Cursor.createCursor(t); + c.reset(); + c.moveNextRows(2); + Map row = c.getCurrentRow(); assertEquals(createExpectedRow("name", "row1", "id", 2, "data", "initial data"), row); - t.updateCurrentRow(Column.KEEP_VALUE, Column.AUTO_NUMBER, "new data"); + c.updateCurrentRow(Column.KEEP_VALUE, Column.AUTO_NUMBER, "new data"); - t.getNextRow(); - t.getNextRow(); - row = t.getNextRow(); + c.moveNextRows(3); + row = c.getCurrentRow(); assertEquals(createExpectedRow("name", "row4", "id", 5, "data", "initial data"), row); - t.updateCurrentRow(Column.KEEP_VALUE, Column.AUTO_NUMBER, "a larger amount of new data"); + c.updateCurrentRow(Column.KEEP_VALUE, Column.AUTO_NUMBER, "a larger amount of new data"); - t.reset(); - t.getNextRow(); - row = t.getNextRow(); + c.reset(); + c.moveNextRows(2); + row = c.getCurrentRow(); assertEquals(createExpectedRow("name", "row1", "id", 2, "data", "new data"), row); - t.getNextRow(); - t.getNextRow(); - row = t.getNextRow(); + c.moveNextRows(3); + row = c.getCurrentRow(); assertEquals(createExpectedRow("name", "row4", "id", 5, "data", "a larger amount of new data"), row); + t.reset(); + + String str = createString(100); + for(int i = 10; i < 50; ++i) { + t.addRow("row" + i, Column.AUTO_NUMBER, "big data_" + str); + } + + c.reset(); + c.moveNextRows(9); + row = c.getCurrentRow(); + + assertEquals(createExpectedRow("name", "row8", + "id", 9, + "data", "initial data"), + row); + + String newText = "updated big data_" + createString(200); + + c.setCurrentRowValue(t.getColumn("data"), newText); + + c.reset(); + c.moveNextRows(9); + row = c.getCurrentRow(); + + assertEquals(createExpectedRow("name", "row8", + "id", 9, + "data", newText), + row); + db.close(); } -- 2.39.5