2010-11-26 15:49:53 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) 2010, Sebastian Sdorra
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
|
* 3. Neither the name of SCM-Manager; nor the names of its
|
|
|
|
|
* contributors may be used to endorse or promote products derived from this
|
|
|
|
|
* software without specific prior written permission.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
|
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*
|
|
|
|
|
* http://bitbucket.org/sdorra/scm-manager
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package sonia.scm.user;
|
|
|
|
|
|
|
|
|
|
//~--- non-JDK imports --------------------------------------------------------
|
|
|
|
|
|
2012-08-30 13:20:26 +02:00
|
|
|
import org.apache.shiro.SecurityUtils;
|
|
|
|
|
import org.apache.shiro.subject.Subject;
|
|
|
|
|
|
2010-11-26 15:49:53 +01:00
|
|
|
import org.junit.Test;
|
|
|
|
|
|
2010-12-04 17:34:47 +01:00
|
|
|
import sonia.scm.Manager;
|
|
|
|
|
import sonia.scm.ManagerTestBase;
|
2010-11-26 15:49:53 +01:00
|
|
|
|
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
|
|
|
|
|
//~--- JDK imports ------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @author Sebastian Sdorra
|
|
|
|
|
*/
|
|
|
|
|
public abstract class UserManagerTestBase
|
2012-08-30 13:20:26 +02:00
|
|
|
extends ManagerTestBase<User, UserException>
|
2010-11-26 15:49:53 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
public static final int THREAD_COUNT = 10;
|
|
|
|
|
|
|
|
|
|
//~--- methods --------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws UserException
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
public void testCreate() throws UserException, IOException
|
|
|
|
|
{
|
2010-12-06 20:05:04 +01:00
|
|
|
User zaphod = UserTestData.createZaphod();
|
2010-11-26 15:49:53 +01:00
|
|
|
|
2010-12-04 17:34:47 +01:00
|
|
|
manager.create(zaphod);
|
2010-11-26 15:49:53 +01:00
|
|
|
|
2010-12-04 17:34:47 +01:00
|
|
|
User otherUser = manager.get("zaphod");
|
2010-11-26 15:49:53 +01:00
|
|
|
|
|
|
|
|
assertNotNull(otherUser);
|
|
|
|
|
assertUserEquals(zaphod, otherUser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws UserException
|
|
|
|
|
*/
|
|
|
|
|
@Test(expected = UserAllreadyExistException.class)
|
|
|
|
|
public void testCreateExisting() throws UserException, IOException
|
|
|
|
|
{
|
2010-12-06 20:05:04 +01:00
|
|
|
User zaphod = UserTestData.createZaphod();
|
2010-11-26 15:49:53 +01:00
|
|
|
|
2010-12-04 17:34:47 +01:00
|
|
|
manager.create(zaphod);
|
|
|
|
|
assertNotNull(manager.get("zaphod"));
|
2010-11-26 15:49:53 +01:00
|
|
|
|
2010-12-06 20:05:04 +01:00
|
|
|
User sameUser = UserTestData.createZaphod();
|
2010-11-26 15:49:53 +01:00
|
|
|
|
2010-12-04 17:34:47 +01:00
|
|
|
manager.create(sameUser);
|
2010-11-26 15:49:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws UserException
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
public void testDelete() throws UserException, IOException
|
|
|
|
|
{
|
2010-12-06 20:05:04 +01:00
|
|
|
User zaphod = UserTestData.createZaphod();
|
2010-11-26 15:49:53 +01:00
|
|
|
|
2010-12-04 17:34:47 +01:00
|
|
|
manager.create(zaphod);
|
|
|
|
|
assertNotNull(manager.get("zaphod"));
|
|
|
|
|
manager.delete(zaphod);
|
|
|
|
|
assertNull(manager.get("zaphod"));
|
2010-11-26 15:49:53 +01:00
|
|
|
}
|
|
|
|
|
|
2013-01-29 20:39:08 +01:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws UserException
|
|
|
|
|
*/
|
|
|
|
|
@Test(expected = UserNotFoundException.class)
|
|
|
|
|
public void testDeleteNotFound() throws UserException, IOException
|
|
|
|
|
{
|
|
|
|
|
manager.delete(UserTestData.createDent());
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-26 15:49:53 +01:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws UserException
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
public void testGet() throws UserException, IOException
|
|
|
|
|
{
|
2010-12-06 20:05:04 +01:00
|
|
|
User zaphod = UserTestData.createZaphod();
|
2010-11-26 15:49:53 +01:00
|
|
|
|
2010-12-04 17:34:47 +01:00
|
|
|
manager.create(zaphod);
|
|
|
|
|
assertNotNull(manager.get("zaphod"));
|
2010-11-26 15:49:53 +01:00
|
|
|
|
|
|
|
|
// test for reference
|
|
|
|
|
zaphod.setDisplayName("Tricia McMillan");
|
2010-12-04 17:34:47 +01:00
|
|
|
zaphod = manager.get("zaphod");
|
2010-11-26 15:49:53 +01:00
|
|
|
assertNotNull(zaphod);
|
|
|
|
|
assertEquals("Zaphod Beeblebrox", zaphod.getDisplayName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws UserException
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
public void testGetAll() throws UserException, IOException
|
|
|
|
|
{
|
2010-12-06 20:05:04 +01:00
|
|
|
User zaphod = UserTestData.createZaphod();
|
2010-11-26 15:49:53 +01:00
|
|
|
|
2010-12-04 17:34:47 +01:00
|
|
|
manager.create(zaphod);
|
|
|
|
|
assertNotNull(manager.get("zaphod"));
|
2010-11-26 15:49:53 +01:00
|
|
|
|
2010-12-06 20:05:04 +01:00
|
|
|
User trillian = UserTestData.createTrillian();
|
2010-11-26 15:49:53 +01:00
|
|
|
|
2010-12-04 17:34:47 +01:00
|
|
|
manager.create(trillian);
|
|
|
|
|
assertNotNull(manager.get("trillian"));
|
2010-11-26 15:49:53 +01:00
|
|
|
|
|
|
|
|
boolean foundZaphod = false;
|
|
|
|
|
boolean foundTrillian = false;
|
2010-12-04 17:34:47 +01:00
|
|
|
Collection<User> users = manager.getAll();
|
2010-11-26 15:49:53 +01:00
|
|
|
|
|
|
|
|
assertNotNull(users);
|
|
|
|
|
assertFalse(users.isEmpty());
|
|
|
|
|
assertTrue(users.size() >= 2);
|
|
|
|
|
|
|
|
|
|
for (User u : users)
|
|
|
|
|
{
|
|
|
|
|
if (u.getName().equals("zaphod"))
|
|
|
|
|
{
|
|
|
|
|
foundZaphod = true;
|
|
|
|
|
assertUserEquals(zaphod, u);
|
|
|
|
|
}
|
|
|
|
|
else if (u.getName().equals("trillian"))
|
|
|
|
|
{
|
|
|
|
|
foundTrillian = true;
|
|
|
|
|
assertUserEquals(trillian, u);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assertTrue(foundZaphod);
|
|
|
|
|
assertTrue(foundTrillian);
|
|
|
|
|
|
|
|
|
|
// test for reference
|
|
|
|
|
trillian = null;
|
|
|
|
|
|
|
|
|
|
for (User u : users)
|
|
|
|
|
{
|
|
|
|
|
if (u.getName().equals("trillian"))
|
|
|
|
|
{
|
|
|
|
|
trillian = u;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assertNotNull(trillian);
|
|
|
|
|
trillian.setDisplayName("Zaphod Beeblebrox");
|
|
|
|
|
|
|
|
|
|
User reference = null;
|
|
|
|
|
|
2010-12-04 17:34:47 +01:00
|
|
|
for (User u : manager.getAll())
|
2010-11-26 15:49:53 +01:00
|
|
|
{
|
|
|
|
|
if (u.getName().equals("trillian"))
|
|
|
|
|
{
|
|
|
|
|
reference = u;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assertNotNull(reference);
|
|
|
|
|
assertEquals(reference.getDisplayName(), "Tricia McMillan");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws UserException
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
public void testModify() throws UserException, IOException
|
|
|
|
|
{
|
2010-12-06 20:05:04 +01:00
|
|
|
User zaphod = UserTestData.createZaphod();
|
2010-11-26 15:49:53 +01:00
|
|
|
|
2010-12-04 17:34:47 +01:00
|
|
|
manager.create(zaphod);
|
|
|
|
|
assertNotNull(manager.get("zaphod"));
|
2010-11-26 15:49:53 +01:00
|
|
|
zaphod.setDisplayName("Tricia McMillan");
|
2010-12-04 17:34:47 +01:00
|
|
|
manager.modify(zaphod);
|
2010-11-26 15:49:53 +01:00
|
|
|
|
2010-12-04 17:34:47 +01:00
|
|
|
User otherUser = manager.get("zaphod");
|
2010-11-26 15:49:53 +01:00
|
|
|
|
|
|
|
|
assertNotNull(otherUser);
|
|
|
|
|
assertEquals(otherUser.getDisplayName(), "Tricia McMillan");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws UserException
|
|
|
|
|
*/
|
|
|
|
|
@Test(expected = UserException.class)
|
|
|
|
|
public void testModifyNotExisting() throws UserException, IOException
|
|
|
|
|
{
|
2013-01-29 20:39:08 +01:00
|
|
|
manager.modify(UserTestData.createZaphod());
|
2010-11-26 15:49:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws InterruptedException
|
|
|
|
|
* @throws UserException
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
public void testMultiThreaded()
|
2012-08-30 13:20:26 +02:00
|
|
|
throws UserException, IOException, InterruptedException
|
2010-11-26 15:49:53 +01:00
|
|
|
{
|
2010-12-04 17:34:47 +01:00
|
|
|
int initialSize = manager.getAll().size();
|
2010-11-26 15:49:53 +01:00
|
|
|
List<MultiThreadTester> testers = new ArrayList<MultiThreadTester>();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < THREAD_COUNT; i++)
|
|
|
|
|
{
|
2010-12-04 17:34:47 +01:00
|
|
|
testers.add(new MultiThreadTester(manager));
|
2010-11-26 15:49:53 +01:00
|
|
|
}
|
|
|
|
|
|
2012-08-30 13:20:26 +02:00
|
|
|
Subject subject = SecurityUtils.getSubject();
|
|
|
|
|
|
2010-11-26 15:49:53 +01:00
|
|
|
for (MultiThreadTester tester : testers)
|
|
|
|
|
{
|
2012-08-30 13:20:26 +02:00
|
|
|
subject.associateWith(tester);
|
2010-11-26 15:49:53 +01:00
|
|
|
new Thread(tester).start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boolean fin = false;
|
|
|
|
|
|
|
|
|
|
while (!fin)
|
|
|
|
|
{
|
|
|
|
|
Thread.sleep(100l);
|
|
|
|
|
fin = true;
|
|
|
|
|
|
|
|
|
|
for (MultiThreadTester tester : testers)
|
|
|
|
|
{
|
|
|
|
|
if (!tester.finished)
|
|
|
|
|
{
|
|
|
|
|
fin = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-04 17:34:47 +01:00
|
|
|
assertTrue((initialSize + THREAD_COUNT) == manager.getAll().size());
|
2010-11-26 15:49:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws UserException
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
public void testRefresh() throws UserException, IOException
|
|
|
|
|
{
|
2010-12-06 20:05:04 +01:00
|
|
|
User zaphod = UserTestData.createZaphod();
|
2010-11-26 15:49:53 +01:00
|
|
|
|
2010-12-04 17:34:47 +01:00
|
|
|
manager.create(zaphod);
|
|
|
|
|
assertNotNull(manager.get("zaphod"));
|
2010-11-26 15:49:53 +01:00
|
|
|
zaphod.setDisplayName("Tricia McMillan");
|
2010-12-04 17:34:47 +01:00
|
|
|
manager.refresh(zaphod);
|
2010-11-26 15:49:53 +01:00
|
|
|
assertEquals(zaphod.getDisplayName(), "Zaphod Beeblebrox");
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-29 20:39:08 +01:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws UserException
|
|
|
|
|
*/
|
|
|
|
|
@Test(expected = UserNotFoundException.class)
|
|
|
|
|
public void testRefreshNotFound() throws UserException, IOException
|
|
|
|
|
{
|
|
|
|
|
manager.refresh(UserTestData.createDent());
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-26 15:49:53 +01:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param user
|
|
|
|
|
* @param otherUser
|
|
|
|
|
*/
|
|
|
|
|
private void assertUserEquals(User user, User otherUser)
|
|
|
|
|
{
|
|
|
|
|
assertEquals(user.getName(), otherUser.getName());
|
|
|
|
|
assertEquals(user.getDisplayName(), otherUser.getDisplayName());
|
|
|
|
|
assertEquals(user.getMail(), otherUser.getMail());
|
|
|
|
|
assertEquals(user.getPassword(), otherUser.getPassword());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//~--- inner classes --------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @version Enter version here..., 2010-11-23
|
|
|
|
|
* @author Sebastian Sdorra
|
|
|
|
|
*/
|
|
|
|
|
private static class MultiThreadTester implements Runnable
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs ...
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param userManager
|
|
|
|
|
*/
|
2010-12-04 17:34:47 +01:00
|
|
|
public MultiThreadTester(Manager<User, UserException> userManager)
|
2010-11-26 15:49:53 +01:00
|
|
|
{
|
2010-12-04 17:34:47 +01:00
|
|
|
this.manager = userManager;
|
2010-11-26 15:49:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//~--- methods ------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void run()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
User user = createUser();
|
|
|
|
|
|
|
|
|
|
modifyAndDeleteUser(user);
|
|
|
|
|
createUser();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw new RuntimeException(ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
finished = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws UserException
|
|
|
|
|
*/
|
|
|
|
|
private User createUser() throws UserException, IOException
|
|
|
|
|
{
|
|
|
|
|
String id = UUID.randomUUID().toString();
|
|
|
|
|
User user = new User(id, id.concat(" displayName"),
|
2012-08-30 13:20:26 +02:00
|
|
|
id.concat("@mail.com"));
|
2010-11-26 15:49:53 +01:00
|
|
|
|
2010-12-04 17:34:47 +01:00
|
|
|
manager.create(user);
|
2010-11-26 15:49:53 +01:00
|
|
|
|
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param user
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws UserException
|
|
|
|
|
*/
|
|
|
|
|
private void modifyAndDeleteUser(User user)
|
2012-08-30 13:20:26 +02:00
|
|
|
throws UserException, IOException
|
2010-11-26 15:49:53 +01:00
|
|
|
{
|
|
|
|
|
String name = user.getName();
|
|
|
|
|
String nd = name.concat(" new displayname");
|
|
|
|
|
|
|
|
|
|
user.setDisplayName(nd);
|
2010-12-04 17:34:47 +01:00
|
|
|
manager.modify(user);
|
2010-11-26 15:49:53 +01:00
|
|
|
|
2010-12-04 17:34:47 +01:00
|
|
|
User otherUser = manager.get(name);
|
2010-11-26 15:49:53 +01:00
|
|
|
|
|
|
|
|
assertNotNull(otherUser);
|
|
|
|
|
assertEquals(nd, otherUser.getDisplayName());
|
2010-12-04 17:34:47 +01:00
|
|
|
manager.delete(user);
|
|
|
|
|
otherUser = manager.get(name);
|
2010-11-26 15:49:53 +01:00
|
|
|
assertNull(otherUser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//~--- fields -------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
private boolean finished = false;
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
2010-12-04 17:34:47 +01:00
|
|
|
private Manager<User, UserException> manager;
|
2010-11-26 15:49:53 +01:00
|
|
|
}
|
|
|
|
|
}
|