org.jopendocument.util.cache.ICacheTest Maven / Gradle / Ivy
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2008 jOpenDocument, by ILM Informatique. All rights reserved.
*
* The contents of this file are subject to the terms of the GNU
* General Public License Version 3 only ("GPL").
* You may not use this file except in compliance with the License.
* You can obtain a copy of the License at http://www.gnu.org/licenses/gpl-3.0.html
* See the License for the specific language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each file.
*
*/
package org.jopendocument.util.cache;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.lang.Thread.State;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import java.util.concurrent.CountDownLatch;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class ICacheTest {
private final ThreadGroup thg = new ThreadGroup("testjunit") {
public void uncaughtException(Thread t, Throwable e) {
ICacheTest.this.threadException = e;
}
};
private Thread launch(String name, Runnable runnable) {
final Thread res = new Thread(this.thg, runnable, name);
res.start();
// really let res start...
Thread.yield();
return res;
}
private void join(final Thread t) throws Throwable {
t.join();
if (this.threadException != null) {
final Throwable thw = this.threadException;
this.threadException = null;
throw thw;
}
}
private static final int delay = 1;
private ICache cache;
protected Throwable threadException;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
this.cache = new ICache(delay);
}
@After
public void tearDown() throws Exception {
this.cache = null;
}
@Test
public void testWatcher() throws NoSuchMethodException {
class BogusCacheWatcher extends CacheWatcher {
public BogusCacheWatcher(ICache c, D d) {
super(c, d);
}
public void clear() {
this.clearCache();
}
}
final ICache cache = new ICache(delay);
cache.setWatcherFactory(new CacheWatcherFactory() {
public CacheWatcher createWatcher(ICache cache, String obj) throws Exception {
return new BogusCacheWatcher(cache, obj);
}
});
final String sel = "SELECT * FROM CONTACT";
final String sel2 = "SELECT NOM FROM CONTACT";
final List r = new ArrayList();
final String data = "CONTACT_TABLE";
assertFalse(cache.dependsOn(data));
// put both in cache
final Set extends CacheWatcher> put = cache.put(sel, r, Collections.singleton(data));
final BogusCacheWatcher cw = (BogusCacheWatcher) put.iterator().next();
cache.put(sel2, r, Collections.singleton(data));
// now cache depends on data
assertTrue(cache.dependsOn(data));
cache.clear(sel2);
// still depends on data because of sel
assertTrue(cache.dependsOn(data));
assertSame(r, cache.get(sel).getRes());
// apres une modif, le cache est invalidé
cw.clear();
assertSame(CacheResult.State.NOT_IN_CACHE, cache.get(sel).getState());
assertFalse(cache.dependsOn(data));
}
@Test
public void testInterrupt() throws Throwable {
// test that we can interrupt a waiting thread
this.getCache().addRunning("SELECT *");
final Thread tInt = launch("test interrupt", new Runnable() {
public void run() {
final CacheResult © 2015 - 2025 Weber Informatics LLC | Privacy Policy