BasicStoreTest.java
/***************************************************************************
Copyright 2013 Emily Estes
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 net.metanotion.contentstore;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.sql.DataSource;
import net.metanotion.io.File;
import net.metanotion.io.JavaFileSystem;
import net.metanotion.sql.DbUtil;
public final class BasicStoreTest {
private static void assertTrue(final boolean result) {
if(!result) { throw new AssertionError("Expected true"); }
}
public static final int ARGS_URL = 0;
public static final int ARGS_DBNAME = 1;
public static final int ARGS_USER = 2;
public static final int ARGS_PASS = 3;
public static final int ARGS_UPLOADS = 4;
public static final int ARGS_TESTFILES = 5;
public static void main(final String[] args) throws Exception {
final String url = args[ARGS_URL];
final String dbName = args[ARGS_DBNAME];
final String user = args[ARGS_USER];
final String pass = args[ARGS_PASS];
try (final Connection conn = DriverManager.getConnection(url, user, pass)) {
DbUtil.createDatabase(conn, dbName, user, true);
}
final DataSource ds = DbUtil.startDBConnectionPool(url + dbName, user, pass);
DbUtil.runSchema(ds, BasicStore.schemaFactory());
final java.io.File rootFolder = new java.io.File(args[ARGS_UPLOADS]);
final java.io.File test1 = new java.io.File(rootFolder.toString() + "test1/");
test1.mkdirs();
final ScheduledExecutorService es = Executors.newScheduledThreadPool(BasicStore.DEFAULT_POOL_SIZE);
try {
final JavaFileSystem uploadFolder = new JavaFileSystem(test1.toString());
final JavaFileSystem testFiles = new JavaFileSystem(args[ARGS_TESTFILES]);
final ContentStore store = new BasicStore(ds, uploadFolder, es, 1000L, TimeUnit.MILLISECONDS);
final Collection c = store.makeCollection("test");
try {
store.getCollection(100);
throw new AssertionError("Requested a collection that doesn't exist.");
} catch (final RuntimeException ex) {
// Expected.
}
assertTrue("test".equals(c.getTitle()));
final Entry e = c.append("Title 1");
e.update("a test entry", "text/plain");
StoreUtils.get(e);
final Entry e2 = c.append("Title 2");
final File f = testFiles.get("/file1.txt");
final InputStream i = f.openInput();
final Entry e3 = e2.update(i, f.toString(), "text/plain");
i.close();
final Entry e4 = e3.setTitle("Title 2 v2");
assertTrue("Title 2 v2".equals(e4.getTitle()));
StoreUtils.get(e4);
store.getEntry(e.oid());
final Collection c2 = store.makeCollection("test 2");
final Collection c3 = store.getCollection("test");
final Collection c4 = store.getCollection(1);
c4.setTitle("test 1 v2");
c.header(20, 0);
c.elements(1,0);
System.out.println("Here");
for(final Entry e0: c.elements(20, 0)) {
System.out.println(e0.getTitle() + " " + e0.oid());
}
System.out.println("Here2");
c.elements(1, 20);
c2.header(20, 0);
c2.elements(1, 0);
c2.elements(20, 0);
c2.elements(1, 20);
e.append("another collection");
e.delete();
final Entry e5 = e4.move(c2);
e5.delete();
c3.delete();
Thread.sleep(2000L);
testPermissions(store, testFiles);
} finally {
es.shutdownNow();
}
}
public static final class Perm implements PermissionModel {
@Override public void canCollectionRead(final long cid) throws SecurityException {
}
@Override public void canEntryRead(final long eid) throws SecurityException {
}
@Override public void canCollectionCreate(final String name) throws SecurityException {
}
@Override public void canEntryModify(final long eid) throws SecurityException {
}
@Override public void canEntryDelete(final long eid) throws SecurityException {
}
@Override public void canEntryCreate(final long cid) throws SecurityException {
}
@Override public void canCollectionModify(final long cid) throws SecurityException {
}
@Override public void canCollectionDelete(final long cid) throws SecurityException {
}
@Override public Entry setOwner(final Entry e) throws SecurityException {
return e;
}
@Override public Collection setOwner(final Collection c) throws SecurityException {
return c;
}
}
public static void testPermissions(final ContentStore store, final JavaFileSystem testFiles) throws Exception {
final ContentStore secure = new SecureSession(store, new Perm());
final Collection c = secure.makeCollection("secure_test");
final Entry e = c.append("Title 1");
e.update("a test entry", "text/plain");
StoreUtils.get(e);
final Entry e2 = c.append("Title 2");
final File f = testFiles.get("/file1.txt");
final InputStream i = f.openInput();
final Entry e3 = e2.update(i, f.toString(), "text/plain");
i.close();
final Entry e4 = e3.setTitle("Title 2 v2");
StoreUtils.get(e4);
secure.getEntry(e4.oid());
final Collection c2 = secure.makeCollection("secure_test 2");
final Collection c3 = secure.getCollection("secure_test");
final Collection c4 = secure.getCollection(c.oid());
c.header(20, 0);
c.elements(1,0);
System.out.println("Here");
for(final Entry e0: c.elements(20, 0)) {
System.out.println(e0.getTitle() + " " + e0.oid());
}
System.out.println("Here2");
c.elements(1, 20);
c2.header(20, 0);
c2.elements(1, 0);
c2.elements(20, 0);
c2.elements(1, 20);
e.append("secure_another collection");
e.delete();
final Entry e5 = e4.move(c2);
e5.delete();
c3.delete();
}
}