001: /*
002: * Copyright (C) 2007 Rob Manning
003: * manningr@users.sourceforge.net
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package net.sourceforge.squirrel_sql.client.session.schemainfo;
020:
021: import static org.junit.Assert.fail;
022:
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: import net.sourceforge.squirrel_sql.BaseSQuirreLJUnit4TestCase;
027: import net.sourceforge.squirrel_sql.fw.sql.ISQLDatabaseMetaData;
028: import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
029: import net.sourceforge.squirrel_sql.fw.sql.TableInfo;
030: import net.sourceforge.squirrel_sql.test.TestUtil;
031:
032: import org.junit.After;
033: import org.junit.Before;
034: import org.junit.Test;
035:
036: public class SchemaInfoCacheTest extends BaseSQuirreLJUnit4TestCase {
037:
038: SchemaInfoCache schemaInfoCacheUnderTest = new SchemaInfoCache();
039: ISQLDatabaseMetaData mockMetaData = null;
040: Exception exceptionEncountered = null;
041:
042: @Before
043: public void setUp() throws Exception {
044: mockMetaData = TestUtil.getEasyMockH2SQLMetaData();
045: for (int i = 0; i < 20; i++) {
046: String tableName = "table" + i;
047: ITableInfo info = new TableInfo("cat", "schema", tableName,
048: "TABLE", null, mockMetaData);
049: schemaInfoCacheUnderTest.writeToTableCache(info);
050: }
051: }
052:
053: @After
054: public void tearDown() throws Exception {
055: mockMetaData = null;
056: }
057:
058: // This test disabled for now until we figure out a better way to do
059: // concurrent modifications to the schemaInfoCache.
060: //@Test
061: public final void testGetITableInfosForReadOnly() {
062:
063: @SuppressWarnings("unchecked")
064: Map map = schemaInfoCacheUnderTest.getTableNamesForReadOnly();
065: @SuppressWarnings("unchecked")
066: IteratorThread thread = new IteratorThread(map.values()
067: .iterator());
068:
069: Thread t = new Thread(thread);
070: t.start();
071: sleep(500);
072: schemaInfoCacheUnderTest.clearTables(null, null, null, null);
073: sleep(500);
074: if (exceptionEncountered != null) {
075: exceptionEncountered.printStackTrace();
076: fail("Unexpected exception: "
077: + exceptionEncountered.toString());
078:
079: }
080: }
081:
082: private class IteratorThread implements Runnable {
083:
084: private Iterator<ITableInfo> iterator = null;
085:
086: public IteratorThread(Iterator<ITableInfo> i) {
087: iterator = i;
088: }
089:
090: /**
091: * @see java.lang.Runnable#run()
092: */
093: public void run() {
094: try {
095: while (iterator.hasNext()) {
096: iterator.next();
097: sleep(500);
098: }
099: } catch (Exception e) {
100: exceptionEncountered = e;
101: }
102: }
103:
104: }
105:
106: private void sleep(long millis) {
107: try {
108: Thread.sleep(millis);
109: } catch (Exception e) {
110: }
111: }
112: }
|