/* Copyright (c) 2011 James Ahlborn Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package com.healthmarketscience.jackcess; import java.nio.ByteBuffer; import java.util.Arrays; import java.util.Date; import java.util.List; import com.healthmarketscience.jackcess.complex.Attachment; import com.healthmarketscience.jackcess.complex.ComplexDataType; import com.healthmarketscience.jackcess.complex.ComplexValueForeignKey; import com.healthmarketscience.jackcess.complex.SingleValue; import com.healthmarketscience.jackcess.complex.UnsupportedValue; import com.healthmarketscience.jackcess.complex.Version; import com.healthmarketscience.jackcess.impl.ByteUtil; import com.healthmarketscience.jackcess.impl.ColumnImpl; import com.healthmarketscience.jackcess.impl.PageChannel; import junit.framework.TestCase; import static com.healthmarketscience.jackcess.TestUtil.*; import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; /** * * @author James Ahlborn */ public class ComplexColumnTest extends TestCase { public ComplexColumnTest(String name) { super(name); } public void testVersions() throws Exception { for(final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMPLEX)) { Database db = openCopy(testDB); db.setTimeZone(TEST_TZ); Table t1 = db.getTable("Table1"); Column col = t1.getColumn("append-memo-data"); assertTrue(col.isAppendOnly()); Column verCol = col.getVersionHistoryColumn(); assertNotNull(verCol); assertEquals(ComplexDataType.VERSION_HISTORY, verCol.getComplexInfo().getType()); for(Row row : t1) { String rowId = row.getString("id"); ComplexValueForeignKey complexValueFk = (ComplexValueForeignKey)verCol.getRowValue(row); String curValue = (String)col.getRowValue(row); if(rowId.equals("row1")) { checkVersions(1, complexValueFk, curValue); } else if(rowId.equals("row2")) { checkVersions(2, complexValueFk, curValue, "row2-memo", new Date(1315876862334L)); } else if(rowId.equals("row3")) { checkVersions(3, complexValueFk, curValue, "row3-memo-again", new Date(1315876965382L), "row3-memo-revised", new Date(1315876953077L), "row3-memo", new Date(1315876879126L)); } else if(rowId.equals("row4")) { checkVersions(4, complexValueFk, curValue, "row4-memo", new Date(1315876945758L)); } else { assertTrue(false); } } Object[] row8 = {"row8", Column.AUTO_NUMBER, "some-data", "row8-memo", Column.AUTO_NUMBER, Column.AUTO_NUMBER}; t1.addRow(row8); ComplexValueForeignKey row8ValFk = (ComplexValueForeignKey) verCol.getRowValue(row8); Date upTime = new Date(); row8ValFk.addVersion("row8-memo", upTime); checkVersions(row8ValFk.get(), row8ValFk, "row8-memo", "row8-memo", upTime); Cursor cursor = CursorBuilder.createCursor(t1); assertTrue(cursor.findFirstRow(t1.getColumn("id"), "row3")); ComplexValueForeignKey row3ValFk = (ComplexValueForeignKey) cursor.getCurrentRowValue(verCol); cursor.setCurrentRowValue(col, "new-value"); Version v = row3ValFk.addVersion("new-value", upTime); checkVersions(3, row3ValFk, "new-value", "new-value", upTime, "row3-memo-again", new Date(1315876965382L), "row3-memo-revised", new Date(1315876953077L), "row3-memo", new Date(1315876879126L)); try { v.update(); fail("UnsupportedOperationException should have been thrown"); } catch(UnsupportedOperationException expected) { // success } checkVersions(3, row3ValFk, "new-value", "new-value", upTime, "row3-memo-again", new Date(1315876965382L), "row3-memo-revised", new Date(1315876953077L), "row3-memo", new Date(1315876879126L)); try { v.delete(); fail("UnsupportedOperationException should have been thrown"); } catch(UnsupportedOperationException expected) { // success } checkVersions(3, row3ValFk, "new-value", "new-value", upTime, "row3-memo-again", new Date(1315876965382L), "row3-memo-revised", new Date(1315876953077L), "row3-memo", new Date(1315876879126L)); try { v.getComplexValueForeignKey().deleteAllValues(); fail("UnsupportedOperationException should have been thrown"); } catch(UnsupportedOperationException expected) { // success } checkVersions(3, row3ValFk, "new-value", "new-value", upTime, "row3-memo-again", new Date(1315876965382L), "row3-memo-revised", new Date(1315876953077L), "row3-memo", new Date(1315876879126L)); db.close(); } } public void testAttachments() throws Exception { for(final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMPLEX)) { Database db = openCopy(testDB); Table t1 = db.getTable("Table1"); Column col = t1.getColumn("attach-data"); assertEquals(ComplexDataType.ATTACHMENT, col.getComplexInfo().getType()); for(Row row : t1) { String rowId = row.getString("id"); ComplexValueForeignKey complexValueFk = (ComplexValueForeignKey)col.getRowValue(row); if(rowId.equals("row1")) { checkAttachments(1, complexValueFk); } else if(rowId.equals("row2")) { checkAttachments(2, complexValueFk, "test_data.txt", "test_data2.txt"); } else if(rowId.equals("row3")) { checkAttachments(3, complexValueFk); } else if(rowId.equals("row4")) { checkAttachments(4, complexValueFk, "test_data2.txt"); } else { assertTrue(false); } } Object[] row8 = {"row8", Column.AUTO_NUMBER, "some-data", "row8-memo", Column.AUTO_NUMBER, Column.AUTO_NUMBER}; t1.addRow(row8); ComplexValueForeignKey row8ValFk = (ComplexValueForeignKey) col.getRowValue(row8); row8ValFk.addAttachment(null, "test_data.txt", "txt", getFileBytes("test_data.txt"), null, null); checkAttachments(row8ValFk.get(), row8ValFk, "test_data.txt"); row8ValFk.addEncodedAttachment(null, "test_data2.txt", "txt", getEncodedFileBytes("test_data2.txt"), null, null); checkAttachments(row8ValFk.get(), row8ValFk, "test_data.txt", "test_data2.txt"); Cursor cursor = CursorBuilder.createCursor(t1); assertTrue(cursor.findFirstRow(t1.getColumn("id"), "row4")); ComplexValueForeignKey row4ValFk = (ComplexValueForeignKey) cursor.getCurrentRowValue(col); Attachment a = row4ValFk.addAttachment(null, "test_data.txt", "txt", getFileBytes("test_data.txt"), null, null); checkAttachments(4, row4ValFk, "test_data2.txt", "test_data.txt"); a.setFileType("zip"); a.setFileName("some_data.zip"); byte[] newBytes = "this is not a zip file".getBytes("US-ASCII"); a.setFileData(newBytes); a.update(); Attachment updated = row4ValFk.getAttachments().g
(function( QUnit ) {
var subsuiteFrame;
QUnit.extend( QUnit, {
testSuites: function( suites ) {
for ( var i = 0; i < suites.length; i++ ) {
(function( suite ) {
asyncTest( suite, function() {
QUnit.runSuite( suite );
});
}( suites[i] ) );
}
QUnit.done = function() {
subsuiteFrame.style.display = "none";
};
},
testStart: function( data ) {
// update the test status to show which test suite is running
QUnit.id( "qunit-testresult" ).innerHTML = "Running " + data.name + "...<br> ";
},
testDone: function() {
var current = QUnit.id( this.config.current.id ),
children = current.children;
// undo the auto-expansion of failed tests
for ( var i = 0; i < children.length; i++ ) {
if ( children[i].nodeName === "OL" ) {
children[i].style.display = "none";
}
}
},
runSuite: function( suite ) {
var body = document.getElementsByTagName( "body" )[0],
iframe = subsuiteFrame = document.createElement( "iframe" ),
iframeWin;
iframe.className = "qunit-subsuite";
body.appendChild( iframe );
function onIframeLoad() {
var module, test,
count = 0;
QUnit.extend( iframeWin.QUnit, {
moduleStart: function( data ) {
// capture module name for messages
module = data.name;
},
testStart: function( data ) {
// capture test name for messages
test = data.name;
},
log: function( data ) {
// pass all test details through to the main page
var message = module + ": " + test + ": " + data.message;
expect( ++count );
QUnit.push( data.result, data.actual, data.expected, message );
},
done: function() {
// start the wrapper test from the main page
start();
}
});
}
QUnit.addEvent( iframe, "load", onIframeLoad );
iframeWin = iframe.contentWindow;
iframe.setAttribute( "src", suite );
this.runSuite = function( suite ) {
iframe.setAttribute( "src", suite );
};
}
});
}( QUnit ) );