You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by su...@apache.org on 2012/08/02 14:53:25 UTC
svn commit: r1368437 - in
/incubator/stanbol/branches/contenthub-two-layered-structure/contenthub:
store/file/src/main/java/org/apache/stanbol/contenthub/store/file/
test/src/main/java/org/apache/stanbol/contenthub/test/index/ldpath/
test/src/main/java...
Author: suat
Date: Thu Aug 2 12:53:24 2012
New Revision: 1368437
URL: http://svn.apache.org/viewvc?rev=1368437&view=rev
Log:
STANBOL-498:
-Updated changes according to the possibility of the multiple store instances
Modified:
incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/store/file/src/main/java/org/apache/stanbol/contenthub/store/file/FileStore.java
incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/index/ldpath/LDPathSemanticIndexManagerTest.java
incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/index/ldpath/LDPathSemanticIndexTest.java
incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/revisionmanager/RevisionManagerTest.java
incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/revisionmanager/StoreDBManagerTest.java
Modified: incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/store/file/src/main/java/org/apache/stanbol/contenthub/store/file/FileStore.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/store/file/src/main/java/org/apache/stanbol/contenthub/store/file/FileStore.java?rev=1368437&r1=1368436&r2=1368437&view=diff
==============================================================================
--- incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/store/file/src/main/java/org/apache/stanbol/contenthub/store/file/FileStore.java (original)
+++ incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/store/file/src/main/java/org/apache/stanbol/contenthub/store/file/FileStore.java Thu Aug 2 12:53:24 2012
@@ -138,6 +138,8 @@ public class FileStore implements Store<
public static final String FILE_STORE_NAME = "filestore";
+ private String name = "contenthubFileStore";
+
private static int MAX_ID_LENGTH = 1024;
private static final String CREATE_RECENTLY_ENHANCED_TABLE = "CREATE TABLE "
@@ -177,9 +179,6 @@ public class FileStore implements Store<
private static final String REMOVE_RECENTLY_ENHANCED_ITEM = "DELETE FROM " + RECENTLY_ENHANCED_TABLE_NAME
+ " WHERE " + FIELD_ID + "=?";
- private static final String SELECT_EPOCH = "SELECT epoch FROM " + StoreDBManager.EPOCH_TABLE_NAME
- + " WHERE tableName = ?";
-
private final Logger log = LoggerFactory.getLogger(FileStore.class);
private File storeFolder;
@@ -204,6 +203,20 @@ public class FileStore implements Store<
Map<String,Object> storeProperties;
+ public FileStore() {
+
+ }
+
+ /**
+ * Public constructor which is intended to be used from the tests. This component is an OSGi based
+ * component, so this constructor MUST NOT be used to obtain a {@link FileStore} instance.
+ *
+ * @param name
+ */
+ public FileStore(String name) {
+ this.name = name;
+ }
+
@Activate
protected void activate(ComponentContext componentContext) throws StoreException {
// check store folder
@@ -243,7 +256,7 @@ public class FileStore implements Store<
*/
@Override
public String getName() {
- return "contenthubFileStore";
+ return name;
}
/*
@@ -257,34 +270,7 @@ public class FileStore implements Store<
@Override
public long getEpoch() throws StoreException {
- // get connection
- Connection con = dbManager.getConnection();
-
- PreparedStatement ps = null;
- ResultSet rs = null;
- long epoch;
- try {
- ps = con.prepareStatement(SELECT_EPOCH);
- ps.setString(1, revisionManager.getStoreID(this));
- rs = ps.executeQuery();
-
- if (rs.next()) {
- epoch = rs.getLong(1);
- } else {
- log.error(String.format("There is not an epoch record for the Store: %s", getName()));
- throw new StoreException(String.format("There is not an epoch record for the Store: %s",
- getName()));
- }
-
- } catch (SQLException e) {
- log.error("Failed to execute query", e);
- throw new StoreException("Failed to execute query", e);
- } finally {
- dbManager.closeResultSet(rs);
- dbManager.closeStatement(ps);
- dbManager.closeConnection(con);
- }
- return epoch;
+ return revisionManager.getEpoch(this);
}
@Override
@@ -327,7 +313,7 @@ public class FileStore implements Store<
@Override
public void removeAll() throws StoreException {
// get changes to obtain identifier of the all changed ContentItems
- ChangeSet<ContentItem> changes = changes(Long.MIN_VALUE, Long.MIN_VALUE, Integer.MAX_VALUE);
+ ChangeSet<ContentItem> changes = changes(getEpoch(), Long.MIN_VALUE, Integer.MAX_VALUE);
List<ContentItem> removed = new ArrayList<ContentItem>();
Iterator<String> idIterator = changes.iterator();
while (idIterator.hasNext()) {
Modified: incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/index/ldpath/LDPathSemanticIndexManagerTest.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/index/ldpath/LDPathSemanticIndexManagerTest.java?rev=1368437&r1=1368436&r2=1368437&view=diff
==============================================================================
--- incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/index/ldpath/LDPathSemanticIndexManagerTest.java (original)
+++ incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/index/ldpath/LDPathSemanticIndexManagerTest.java Thu Aug 2 12:53:24 2012
@@ -109,33 +109,48 @@ public class LDPathSemanticIndexManagerT
indexMetadata.put(LDPathSemanticIndex.PROP_LD_PATH_PROGRAM, program);
String pid = ldPathSemanticIndexManager.createIndex(indexMetadata);
- // create and retrieve LDPathSemanticIndex
- LDPathSemanticIndex semanticIndex = (LDPathSemanticIndex) semanticIndexManager.getIndex(name);
+ // retrieve LDPathSemanticIndex
+ LDPathSemanticIndex semanticIndex;
+ String indexMetadataFilePath;
+ File indexMetadataDirectory;
+ File file;
+ try {
+ semanticIndex = (LDPathSemanticIndex) semanticIndexManager.getIndex(name);
+ int timeoutCount = 0;
+ while (semanticIndex == null) {
+ if (timeoutCount == 8) break;
+ Thread.sleep(500);
+ semanticIndex = (LDPathSemanticIndex) semanticIndexManager.getIndex(name);
+ timeoutCount++;
+ }
+ assertNotNull("Failed to create LDPathSemanticIndex with name " + name, semanticIndex);
+
+ // check IndexMetadata folder exists
+ indexMetadataDirectory = bundleContext
+ .getServiceReference(LDPathSemanticIndexManager.class.getName()).getBundle()
+ .getBundleContext().getDataFile(LDPathSemanticIndexManager.class.getName());
+ assertTrue("IndexMetadata Directory does not exist", indexMetadataDirectory.exists());
+
+ // check IndexMetadata files of indexes before remove index
+ indexMetadataFilePath = indexMetadataDirectory.getAbsolutePath() + File.separator + pid
+ + ".props";
+ file = new File(indexMetadataFilePath);
+ assertTrue("IndexMetadata File cannot be found for pid: " + pid, file.exists());
+
+ } finally {
+ // remove LDPathSemanticIndex
+ ldPathSemanticIndexManager.removeIndex(pid);
+ }
+
+ // wait some time to let OSGi remove the configuration
int timeoutCount = 0;
- while (semanticIndex == null) {
+ semanticIndex = (LDPathSemanticIndex) semanticIndexManager.getIndex(name);
+ while (semanticIndex != null) {
if (timeoutCount == 8) break;
Thread.sleep(500);
semanticIndex = (LDPathSemanticIndex) semanticIndexManager.getIndex(name);
timeoutCount++;
}
- assertNotNull("Failed to create LDPathSemanticIndex with name " + name, semanticIndex);
-
- // check IndexMetadata folder exists
- File indexMetadataDirectory = bundleContext
- .getServiceReference(LDPathSemanticIndexManager.class.getName()).getBundle()
- .getBundleContext().getDataFile(LDPathSemanticIndexManager.class.getName());
- assertTrue("IndexMetadata Directory does not exist", indexMetadataDirectory.exists());
-
- // check IndexMetadata files of indexes before remove index
- String indexMetadataFilePath = indexMetadataDirectory.getAbsolutePath() + File.separator + pid
- + ".props";
- File file = new File(indexMetadataFilePath);
- assertTrue("IndexMetadata File cannot be found for pid: " + pid, file.exists());
-
- // remove LDPathSemanticIndex
- ldPathSemanticIndexManager.removeIndex(pid);
-
- semanticIndex = (LDPathSemanticIndex) semanticIndexManager.getIndex(name);
assertNull(String.format("LDPathSemanticIndex with name %s cannot be removed", name), semanticIndex);
if (semanticIndex == null) {
@@ -147,7 +162,6 @@ public class LDPathSemanticIndexManagerT
indexMetadataFilePath = indexMetadataDirectory.getAbsolutePath() + File.separator + pid + ".props";
file = new File(indexMetadataFilePath);
assertFalse("IndexMetadata File cannot be removed for pid: " + pid, file.exists());
-
}
@Test
Modified: incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/index/ldpath/LDPathSemanticIndexTest.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/index/ldpath/LDPathSemanticIndexTest.java?rev=1368437&r1=1368436&r2=1368437&view=diff
==============================================================================
--- incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/index/ldpath/LDPathSemanticIndexTest.java (original)
+++ incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/index/ldpath/LDPathSemanticIndexTest.java Thu Aug 2 12:53:24 2012
@@ -124,7 +124,7 @@ public class LDPathSemanticIndexTest {
}
if (fileStore == null) {
if (bundleContext != null) {
- fileStore = getContenthubStore(bundleContext);
+ fileStore = getContenthubStore();
if (fileStore == null) {
throw new IllegalStateException("Null Store");
}
@@ -406,8 +406,55 @@ public class LDPathSemanticIndexTest {
}
@Test
- public void epochChangeTest() {
+ public void epochChangeTest() throws IndexManagementException,
+ InterruptedException,
+ IOException,
+ IndexException,
+ StoreException,
+ SearchException {
+ String name = "test_index_name_for_epoch";
+ String program = "@prefix dbp-ont: <http://dbpedia.org/ontology/>; person_entities = .[rdf:type is dbp-ont:Person]:: xsd:anyURI (termVectors=\"true\");";
+ Properties props = new Properties();
+ props.put(LDPathSemanticIndex.PROP_NAME, name);
+ props.put(LDPathSemanticIndex.PROP_LD_PATH_PROGRAM, program);
+ props.put(LDPathSemanticIndex.PROP_DESCRIPTION, "epoch program");
+ props.put(LDPathSemanticIndex.PROP_INDEX_CONTENT, false);
+ props.put(LDPathSemanticIndex.PROP_BATCH_SIZE, 10);
+ props.put(LDPathSemanticIndex.PROP_STORE_CHECK_PERIOD, 1);
+ props.put(LDPathSemanticIndex.PROP_SOLR_CHECK_TIME, 5);
+ String pid = ldPathSemanticIndexManager.createIndex(props);
+
+ try {
+ LDPathSemanticIndex semanticIndex = (LDPathSemanticIndex) semanticIndexManager.getIndex(name);
+ int timeoutCount = 0;
+ while (semanticIndex == null) {
+ if (timeoutCount == 8) break;
+ Thread.sleep(500);
+ semanticIndex = (LDPathSemanticIndex) semanticIndexManager.getIndex(name);
+ timeoutCount++;
+ }
+
+ ContentItem ci = contentItemFactory.createContentItem(new StringSource(
+ "Michael Jackson is a very famous person, and he was born in Indiana."));
+ fileStore.put(ci);
+ fileStore.removeAll();
+
+ // make sure that the index will perform reindexing
+ Thread.sleep(1500);
+
+ // index ci to new semantic index
+ while (semanticIndex.getState() != IndexState.ACTIVE) {
+ Thread.sleep(500);
+ }
+ String query = "*:*";
+ SolrDocumentList sdl = solrSearch.search(query, name).getResults();
+ assertNotNull("Result must not be null for query " + query, sdl);
+ assertTrue("There should be no indexed item", sdl.size() == 0);
+
+ } finally {
+ ldPathSemanticIndexManager.removeIndex(pid);
+ }
}
@After
@@ -420,7 +467,7 @@ public class LDPathSemanticIndexTest {
}
@SuppressWarnings("unchecked")
- private Store<ContentItem> getContenthubStore(BundleContext bundleContext) {
+ private Store<ContentItem> getContenthubStore() {
Store<ContentItem> contentHubStore = null;
try {
ServiceReference[] stores = bundleContext.getServiceReferences(Store.class.getName(), null);
Modified: incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/revisionmanager/RevisionManagerTest.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/revisionmanager/RevisionManagerTest.java?rev=1368437&r1=1368436&r2=1368437&view=diff
==============================================================================
--- incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/revisionmanager/RevisionManagerTest.java (original)
+++ incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/revisionmanager/RevisionManagerTest.java Thu Aug 2 12:53:24 2012
@@ -19,21 +19,33 @@ package org.apache.stanbol.contenthub.te
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
+import java.io.IOException;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
+import java.sql.Statement;
import java.util.Iterator;
import org.apache.sling.junit.annotations.SlingAnnotationsTestRunner;
import org.apache.sling.junit.annotations.TestReference;
+import org.apache.stanbol.commons.semanticindex.index.IndexException;
+import org.apache.stanbol.commons.semanticindex.index.IndexManagementException;
import org.apache.stanbol.commons.semanticindex.store.ChangeSet;
import org.apache.stanbol.commons.semanticindex.store.Store;
import org.apache.stanbol.commons.semanticindex.store.StoreException;
import org.apache.stanbol.contenthub.revisionmanager.RevisionManager;
import org.apache.stanbol.contenthub.revisionmanager.StoreDBManager;
+import org.apache.stanbol.contenthub.store.file.FileStore;
+import org.apache.stanbol.enhancer.servicesapi.ContentItem;
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -48,11 +60,27 @@ public class RevisionManagerTest {
private StoreDBManager dbManager;
@TestReference
+ private BundleContext bundleContext;
+
private Store<?> store;
+ @Before
+ public void before() throws IndexManagementException, IndexException, InterruptedException, IOException {
+ if (store == null) {
+ if (bundleContext != null) {
+ store = getContenthubStore();
+ if (store == null) {
+ throw new IllegalStateException("Null Store");
+ }
+ } else {
+ throw new IllegalStateException("Null bundle context");
+ }
+ }
+ }
+
@Test
public void revisionManagerTest() {
- assertNotNull("Expecting revisionManager to be injected by Sling test runner", revisionManager);
+ assertNotNull("Expecting RevisionManager to be injected by Sling test runner", revisionManager);
}
@Test
@@ -61,20 +89,27 @@ public class RevisionManagerTest {
}
@Test
- public void updateChangeTest() throws StoreException, SQLException {
- // do the update
- String contentItemID = "contenthub_test_content_item_id";
- revisionManager.updateRevision(store, contentItemID);
+ public void bundleContextTest() {
+ assertNotNull("Expecting BundleContext to be injected by Sling test runner", bundleContext);
+ }
- // check the update
- String query = String.format("SELECT id, revision FROM %s WHERE id = ?",
- revisionManager.getStoreID(store));
- Connection con = dbManager.getConnection();
+ @Test
+ public void updateRevisionTest() throws StoreException, SQLException {
+ Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
+ // do the update
+ String contentItemID = "contenthub_test_content_item_id";
+ long newRevision = revisionManager.updateRevision(store, contentItemID);
boolean recordExist = false;
long revisionNumber = Long.MAX_VALUE;
+ String query;
try {
+ query = String.format("SELECT id, revision FROM %s WHERE id = ?",
+ revisionManager.getStoreID(store));
+
+ // check the update
+ con = dbManager.getConnection();
ps = con.prepareStatement(query);
ps.setString(1, contentItemID);
rs = ps.executeQuery();
@@ -82,31 +117,30 @@ public class RevisionManagerTest {
recordExist = true;
revisionNumber = rs.getLong(2);
}
- } finally {
- dbManager.closeResultSet(rs);
dbManager.closeStatement(ps);
- }
- assertTrue("failed to obtain content item revision", recordExist);
- assertTrue("wrong revision number", (revisionNumber <= System.currentTimeMillis()));
+ assertTrue("failed to obtain content item revision", recordExist);
+ assertTrue("wrong revision number", (revisionNumber == newRevision));
- // clear the update
- query = String.format("DELETE FROM %s WHERE id = ?", revisionManager.getStoreID(store));
- ps = null;
- try {
- ps = con.prepareStatement(query);
- ps.setString(1, contentItemID);
- int result = ps.executeUpdate();
- if (result != 1) {
- log.warn(
- "Wrong number of updated records while removing the test record. Updated record number: {}",
- result);
- }
} catch (SQLException e) {
log.warn("Failed to remove the test record", e);
} finally {
- dbManager.closeConnection(con);
- dbManager.closeStatement(ps);
+ // clear the update
+ try {
+ query = String.format("DELETE FROM %s WHERE id = ?", revisionManager.getStoreID(store));
+ ps = con.prepareStatement(query);
+ ps.setString(1, contentItemID);
+ int result = ps.executeUpdate();
+ if (result != 1) {
+ log.warn(
+ "Wrong number of updated records while removing the test record. Updated record number: {}",
+ result);
+ }
+ } finally {
+ dbManager.closeResultSet(rs);
+ dbManager.closeStatement(ps);
+ dbManager.closeConnection(con);
+ }
}
}
@@ -119,7 +153,7 @@ public class RevisionManagerTest {
*/
@Test
public void iterativeChangesTest() throws StoreException, InterruptedException, SQLException {
- log.warn("DO NOT UPDATE THE STORE: {}DURING EXECUTION OF THIS TEST", store.getName());
+ log.warn("DO NOT UPDATE THE STORE: {} DURING EXECUTION OF THIS TEST", store.getName());
Connection con = dbManager.getConnection();
String contentItemID = "contenthub_test_content_item_id";
PreparedStatement ps = null;
@@ -282,4 +316,94 @@ public class RevisionManagerTest {
ChangeSet<?> changeSet = revisionManager.getChanges(store, revision, 1);
assertTrue("There must be no changes", !changeSet.iterator().hasNext());
}
+
+ @Test
+ public void getStoreIDTest() {
+ assertTrue("Store ID must be same with the name of the Store", revisionManager.getStoreID(store)
+ .equals(store.getName()));
+ }
+
+ @Test
+ public void initializeRevisionTablesTest() throws StoreException, SQLException {
+ FileStore fileStore = new FileStore("revisionManagerTestStore");
+ Connection con = null;
+ PreparedStatement ps = null;
+ String tableName = revisionManager.getStoreID(fileStore);
+ try {
+ revisionManager.initializeRevisionTables(fileStore);
+ // check table
+ assertTrue(String.format("There is no table having name: %s", tableName),
+ dbManager.existsTable(tableName));
+
+ // check epoch table entry
+ con = dbManager.getConnection();
+ ps = null;
+ ResultSet rs = null;
+ boolean recordExists = false;
+ try {
+ ps = con.prepareStatement("SELECT epoch FROM " + StoreDBManager.EPOCH_TABLE_NAME
+ + " WHERE tableName = ?");
+ ps.setString(1, tableName);
+ rs = ps.executeQuery();
+ if (rs.next()) {
+ recordExists = true;
+ }
+
+ assertTrue(String.format("There is no entry for tableName: %s in epochTable", tableName),
+ recordExists);
+ } finally {
+ dbManager.closeResultSet(rs);
+ dbManager.closeStatement(ps);
+ }
+ } finally {
+ // clear test data
+ Statement stmt = null;
+ try {
+ // first remove the the table
+ stmt = con.createStatement();
+ stmt.executeUpdate("DROP TABLE " + tableName);
+
+ } finally {
+ dbManager.closeStatement(stmt);
+ }
+ try {
+ // delete the entry from epoch table
+ ps = con.prepareStatement("DELETE FROM " + StoreDBManager.EPOCH_TABLE_NAME
+ + " WHERE tableName = ?");
+ ps.setString(1, tableName);
+ ps.executeUpdate();
+
+ } finally {
+ dbManager.closeStatement(ps);
+ dbManager.closeConnection(con);
+ }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private Store<ContentItem> getContenthubStore() {
+ Store<ContentItem> contentHubStore = null;
+ try {
+ ServiceReference[] stores = bundleContext.getServiceReferences(Store.class.getName(), null);
+ for (ServiceReference serviceReference : stores) {
+ Object store = bundleContext.getService(serviceReference);
+ Type[] genericInterfaces = store.getClass().getGenericInterfaces();
+ if (genericInterfaces.length == 1 && genericInterfaces[0] instanceof ParameterizedType) {
+ Type[] types = ((ParameterizedType) genericInterfaces[0]).getActualTypeArguments();
+ try {
+ @SuppressWarnings("unused")
+ Class<ContentItem> contentItemClass = (Class<ContentItem>) types[0];
+ if (((Store<ContentItem>) store).getName().equals("contenthubFileStore")) {
+ contentHubStore = (Store<ContentItem>) store;
+ }
+ } catch (ClassCastException e) {
+ // ignore
+ }
+ }
+ }
+ } catch (InvalidSyntaxException e) {
+ // ignore as there is no filter
+ }
+ return contentHubStore;
+ }
}
Modified: incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/revisionmanager/StoreDBManagerTest.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/revisionmanager/StoreDBManagerTest.java?rev=1368437&r1=1368436&r2=1368437&view=diff
==============================================================================
--- incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/revisionmanager/StoreDBManagerTest.java (original)
+++ incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/revisionmanager/StoreDBManagerTest.java Thu Aug 2 12:53:24 2012
@@ -20,24 +20,24 @@ import static org.junit.Assert.assertNot
import static org.junit.Assert.assertTrue;
import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
import java.sql.SQLException;
+import java.sql.Statement;
import org.apache.sling.junit.annotations.SlingAnnotationsTestRunner;
import org.apache.sling.junit.annotations.TestReference;
-import org.apache.stanbol.commons.semanticindex.store.Store;
import org.apache.stanbol.commons.semanticindex.store.StoreException;
import org.apache.stanbol.contenthub.revisionmanager.RevisionManager;
import org.apache.stanbol.contenthub.revisionmanager.StoreDBManager;
-import org.apache.stanbol.enhancer.servicesapi.ContentItem;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@RunWith(SlingAnnotationsTestRunner.class)
public class StoreDBManagerTest {
- private static final Logger log = LoggerFactory.getLogger(StoreDBManagerTest.class);
-
@TestReference
private StoreDBManager dbManager;
@@ -45,31 +45,105 @@ public class StoreDBManagerTest {
private RevisionManager revisionManager;
@TestReference
- private Store<ContentItem> store;
+ private BundleContext bundleContext;
@Test
public void dbManagerTest() {
- assertNotNull("Expecting FileStoreDBManager to be injected by Sling test runner", dbManager);
+ assertNotNull("Expecting StoreDBManager to be injected by Sling test runner", dbManager);
+ }
+
+ @Test
+ public void dbRevisionManagerTest() {
+ assertNotNull("Expecting RevisionManager to be injected by Sling test runner", revisionManager);
+ }
+
+ @Test
+ public void bundleContextTest() {
+ assertNotNull("Expecting BundleContext to be injected by Sling test runner", bundleContext);
}
@Test
public void testConnection() throws StoreException {
- Connection connection = dbManager.getConnection();
- assertTrue("Null connection", connection != null);
- if (connection != null) {
- try {
- connection.close();
- } catch (SQLException e) {
- log.warn("Failed to close test connection");
- }
+ Connection con = dbManager.getConnection();
+ assertTrue("Null connection", con != null);
+ dbManager.closeConnection(con);
+ }
+
+ @Test
+ public void testEpochTable() throws StoreException {
+ assertTrue(StoreDBManager.EPOCH_TABLE_NAME + " has not been created",
+ dbManager.existsTable(StoreDBManager.EPOCH_TABLE_NAME));
+ }
+
+ @Test
+ public void testCreateRevisionTable() throws StoreException, SQLException {
+ String tableName = "StoreDBManagerRevisionTable";
+ dbManager.createRevisionTable(tableName);
+ assertTrue("Failed to create " + tableName, dbManager.existsTable(tableName));
+ // clear test data
+ Connection con = dbManager.getConnection();
+ Statement stmt = null;
+ try {
+ // first remove the the table
+ stmt = con.createStatement();
+ stmt.executeUpdate("DROP TABLE " + tableName);
+
+ } finally {
+ dbManager.closeStatement(stmt);
+ dbManager.closeConnection(con);
}
}
@Test
- public void testTables() throws StoreException {
- assertTrue("recently_enhanced_content_items does not exist",
- dbManager.existsTable("recently_enhanced_content_items"));
- assertTrue(String.format("%s does not exist", revisionManager.getStoreID(store)),
- dbManager.existsTable(revisionManager.getStoreID(store)));
+ public void testTruncateTable() throws StoreException, SQLException {
+ String tableName = "truncatetable";
+ // create dummy table
+ dbManager.createRevisionTable(tableName);
+
+ Connection con = null;
+ PreparedStatement ps = null;
+ ResultSet rs = null;
+ try {
+ // put some value into it
+ String insertQuery = "INSERT INTO " + tableName + "(id, revision) VALUES(?,?)";
+ long initialRevision = System.currentTimeMillis();
+ con = dbManager.getConnection();
+ for (int i = 0; i < 5; i++) {
+ ps = con.prepareStatement(insertQuery);
+ ps.setString(1, "id" + i);
+ ps.setLong(2, initialRevision + i);
+ ps.executeUpdate();
+ ps.clearParameters();
+ }
+
+ // truncate table
+ ps.close();
+ dbManager.truncateTable(tableName);
+
+ // check the values
+ ps = con.prepareStatement("SELECT * FROM " + tableName);
+ rs = ps.executeQuery();
+
+ boolean recordExists = false;
+ if (rs.next()) {
+ recordExists = true;
+ }
+ assertTrue("There are still records after truncate", recordExists == false);
+ } finally {
+ dbManager.closeResultSet(rs);
+ dbManager.closeStatement(ps);
+
+ // clear test data
+ Statement stmt = null;
+ try {
+ // first remove the the table
+ stmt = con.createStatement();
+ stmt.executeUpdate("DROP TABLE " + tableName);
+
+ } finally {
+ dbManager.closeStatement(stmt);
+ dbManager.closeConnection(con);
+ }
+ }
}
}