improved test harness - removed arbitrary waits
git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2513 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
parent
4892e81ed4
commit
52ac3598f8
|
@ -16,150 +16,153 @@ import org.junit.Test;
|
|||
|
||||
public class PropertyUserStoreTest
|
||||
{
|
||||
String testFileDir="target" + File.separator + "property-user-store-test";
|
||||
String testFile = testFileDir + File.separator + "users.txt";
|
||||
|
||||
@Before
|
||||
public void before() throws Exception
|
||||
{
|
||||
File file = new File(testFileDir);
|
||||
file.mkdirs();
|
||||
|
||||
writeInitialUsers(testFile);
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() throws Exception
|
||||
{
|
||||
File file = new File(testFile);
|
||||
String testFileDir = "target" + File.separator + "property-user-store-test";
|
||||
String testFile = testFileDir + File.separator + "users.txt";
|
||||
|
||||
file.delete();
|
||||
}
|
||||
@Before
|
||||
public void before() throws Exception
|
||||
{
|
||||
File file = new File(testFileDir);
|
||||
file.mkdirs();
|
||||
|
||||
private void writeInitialUsers(String testFile) throws Exception
|
||||
{
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(testFile));
|
||||
writer.append("tom: tom, roleA\n");
|
||||
writer.append("dick: dick, roleB\n");
|
||||
writer.append("harry: harry, roleA, roleB\n");
|
||||
writer.close();
|
||||
}
|
||||
|
||||
private void writeAdditionalUser(String testFile) throws Exception
|
||||
{
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(testFile, true));
|
||||
writer.append("skip: skip, roleA\n");
|
||||
writer.close();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testPropertyUserStoreLoad() throws Exception
|
||||
{
|
||||
final AtomicInteger userCount = new AtomicInteger();
|
||||
|
||||
PropertyUserStore store = new PropertyUserStore();
|
||||
|
||||
store.setConfig(testFile);
|
||||
|
||||
store.registerUserListener(new PropertyUserStore.UserListener() {
|
||||
|
||||
public void update(String username, Credential credential, String[] roleArray)
|
||||
{
|
||||
userCount.getAndIncrement();
|
||||
}
|
||||
|
||||
public void remove(String username)
|
||||
{
|
||||
writeInitialUsers(testFile);
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
store.start();
|
||||
|
||||
Assert.assertEquals(3, userCount.get());
|
||||
}
|
||||
@After
|
||||
public void after() throws Exception
|
||||
{
|
||||
File file = new File(testFile);
|
||||
|
||||
@Test
|
||||
public void testPropertyUserStoreLoadUpdateUser() throws Exception
|
||||
{
|
||||
|
||||
final AtomicInteger userCount = new AtomicInteger();
|
||||
|
||||
final List<String> users = new ArrayList<String>();
|
||||
|
||||
PropertyUserStore store = new PropertyUserStore();
|
||||
store.setRefreshInterval(1);
|
||||
store.setConfig(testFile);
|
||||
|
||||
store.registerUserListener(new PropertyUserStore.UserListener()
|
||||
{
|
||||
public void update(String username, Credential credential, String[] roleArray)
|
||||
{
|
||||
if ( !users.contains(username))
|
||||
{
|
||||
users.add(username);
|
||||
userCount.getAndIncrement();
|
||||
}
|
||||
}
|
||||
file.delete();
|
||||
}
|
||||
|
||||
public void remove(String username)
|
||||
{
|
||||
private void writeInitialUsers(String testFile) throws Exception
|
||||
{
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(testFile));
|
||||
writer.append("tom: tom, roleA\n");
|
||||
writer.append("dick: dick, roleB\n");
|
||||
writer.append("harry: harry, roleA, roleB\n");
|
||||
writer.close();
|
||||
}
|
||||
|
||||
private void writeAdditionalUser(String testFile) throws Exception
|
||||
{
|
||||
Thread.sleep(1001);
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(testFile,true));
|
||||
writer.append("skip: skip, roleA\n");
|
||||
writer.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertyUserStoreLoad() throws Exception
|
||||
{
|
||||
final AtomicInteger userCount = new AtomicInteger();
|
||||
|
||||
PropertyUserStore store = new PropertyUserStore();
|
||||
|
||||
store.setConfig(testFile);
|
||||
|
||||
store.registerUserListener(new PropertyUserStore.UserListener()
|
||||
{
|
||||
|
||||
public void update(String username, Credential credential, String[] roleArray)
|
||||
{
|
||||
userCount.getAndIncrement();
|
||||
}
|
||||
|
||||
public void remove(String username)
|
||||
{
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
store.start();
|
||||
|
||||
Assert.assertEquals(3,userCount.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertyUserStoreLoadUpdateUser() throws Exception
|
||||
{
|
||||
|
||||
final AtomicInteger userCount = new AtomicInteger();
|
||||
|
||||
final List<String> users = new ArrayList<String>();
|
||||
|
||||
PropertyUserStore store = new PropertyUserStore();
|
||||
store.setRefreshInterval(1);
|
||||
store.setConfig(testFile);
|
||||
|
||||
store.registerUserListener(new PropertyUserStore.UserListener()
|
||||
{
|
||||
public void update(String username, Credential credential, String[] roleArray)
|
||||
{
|
||||
if (!users.contains(username))
|
||||
{
|
||||
users.add(username);
|
||||
userCount.getAndIncrement();
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(String username)
|
||||
{
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
store.start();
|
||||
Assert.assertEquals(3,userCount.get());
|
||||
|
||||
writeAdditionalUser(testFile);
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
while (userCount.get() < 4 && (System.currentTimeMillis() - start) < 10000)
|
||||
Thread.sleep(10);
|
||||
Assert.assertEquals(4,userCount.get());
|
||||
|
||||
Assert.assertTrue(users.contains("skip"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertyUserStoreLoadRemoveUser() throws Exception
|
||||
{
|
||||
writeAdditionalUser(testFile);
|
||||
|
||||
final AtomicInteger userCount = new AtomicInteger();
|
||||
|
||||
final List<String> users = new ArrayList<String>();
|
||||
|
||||
PropertyUserStore store = new PropertyUserStore();
|
||||
store.setRefreshInterval(2);
|
||||
store.setConfig(testFile);
|
||||
|
||||
store.registerUserListener(new PropertyUserStore.UserListener()
|
||||
{
|
||||
|
||||
public void update(String username, Credential credential, String[] roleArray)
|
||||
{
|
||||
if (!users.contains(username))
|
||||
{
|
||||
users.add(username);
|
||||
userCount.getAndIncrement();
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(String username)
|
||||
{
|
||||
users.remove(username);
|
||||
userCount.getAndDecrement();
|
||||
}
|
||||
});
|
||||
|
||||
store.start();
|
||||
|
||||
Assert.assertEquals(4,userCount.get());
|
||||
|
||||
Thread.sleep(2000);
|
||||
writeInitialUsers(testFile);
|
||||
Thread.sleep(3000);
|
||||
Assert.assertEquals(3,userCount.get());
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
store.start();
|
||||
Assert.assertEquals(3, userCount.get());
|
||||
|
||||
Thread.sleep(1100);
|
||||
Assert.assertEquals(3, userCount.get());
|
||||
|
||||
writeAdditionalUser(testFile);
|
||||
|
||||
Thread.sleep(2200);
|
||||
|
||||
Assert.assertEquals(4, userCount.get());
|
||||
Assert.assertTrue(users.contains("skip"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertyUserStoreLoadRemoveUser() throws Exception
|
||||
{
|
||||
writeAdditionalUser(testFile);
|
||||
|
||||
final AtomicInteger userCount = new AtomicInteger();
|
||||
|
||||
final List<String> users = new ArrayList<String>();
|
||||
|
||||
PropertyUserStore store = new PropertyUserStore();
|
||||
store.setRefreshInterval(2);
|
||||
store.setConfig(testFile);
|
||||
|
||||
store.registerUserListener(new PropertyUserStore.UserListener() {
|
||||
|
||||
public void update(String username, Credential credential, String[] roleArray) {
|
||||
if ( !users.contains(username))
|
||||
{
|
||||
users.add(username);
|
||||
userCount.getAndIncrement();
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(String username) {
|
||||
users.remove(username);
|
||||
userCount.getAndDecrement();
|
||||
}
|
||||
});
|
||||
|
||||
store.start();
|
||||
|
||||
Assert.assertEquals(4, userCount.get());
|
||||
|
||||
Thread.sleep(2000);
|
||||
writeInitialUsers(testFile);
|
||||
Thread.sleep(3000);
|
||||
Assert.assertEquals(3, userCount.get());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue