create new key generator for shorter repository ids

This commit is contained in:
Sebastian Sdorra
2012-08-30 17:35:56 +02:00
parent 4ca0d820e7
commit 6fee63203f
5 changed files with 286 additions and 11 deletions

View File

@@ -40,8 +40,9 @@ import com.google.inject.Provider;
import org.junit.Test;
import sonia.scm.Type;
import sonia.scm.repository.xml.XmlRepositoryDAO;
import sonia.scm.config.ScmConfiguration;
import sonia.scm.repository.xml.XmlRepositoryDAO;
import sonia.scm.security.DefaultKeyGenerator;
import sonia.scm.store.JAXBStoreFactory;
import sonia.scm.store.StoreFactory;
import sonia.scm.util.MockUtil;
@@ -73,7 +74,7 @@ public class DefaultRepositoryManagerTest extends RepositoryManagerTestBase
*/
@Test
public void getRepositoryFromRequestUriTest()
throws RepositoryException, IOException
throws RepositoryException, IOException
{
RepositoryManager m = createManager();
@@ -86,9 +87,9 @@ public class DefaultRepositoryManagerTest extends RepositoryManagerTestBase
assertEquals("scm-test", m.getFromUri("hg/scm-test").getName());
assertEquals("scm-test", m.getFromUri("/hg/scm-test").getName());
assertEquals("project1/test-1",
m.getFromUri("/git/project1/test-1").getName());
m.getFromUri("/git/project1/test-1").getName());
assertEquals("project1/test-1",
m.getFromUri("/git/project1/test-1/ka/some/path").getName());
m.getFromUri("/git/project1/test-1/ka/some/path").getName());
assertNull(m.getFromUri("/git/project1/test-3/ka/some/path"));
}
@@ -138,8 +139,8 @@ public class DefaultRepositoryManagerTest extends RepositoryManagerTestBase
ScmConfiguration configuration = new ScmConfiguration();
return new DefaultRepositoryManager(configuration, contextProvider,
MockUtil.getAdminSecurityContextProvider(), repositoryDAO,
handlerSet, listenerProvider, hookProvider);
new DefaultKeyGenerator(), MockUtil.getAdminSecurityContextProvider(),
repositoryDAO, handlerSet, listenerProvider, hookProvider);
}
/**
@@ -153,7 +154,7 @@ public class DefaultRepositoryManagerTest extends RepositoryManagerTestBase
* @throws RepositoryException
*/
private void createRepository(RepositoryManager m, Repository repository)
throws RepositoryException, IOException
throws RepositoryException, IOException
{
m.create(repository);
}

View File

@@ -0,0 +1,172 @@
/**
* 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.security;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.collect.Sets;
import org.junit.Test;
import static org.junit.Assert.*;
//~--- JDK imports ------------------------------------------------------------
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
*
* @author Sebastian Sdorra
*/
public class DefaultKeyGeneratorTest
{
/**
* Method description
*
*
* @throws ExecutionException
* @throws InterruptedException
* @throws TimeoutException
*/
@Test
public void testMultiThreaded()
throws InterruptedException, ExecutionException, TimeoutException
{
final DefaultKeyGenerator generator = new DefaultKeyGenerator();
ExecutorService executor = Executors.newFixedThreadPool(30);
Set<Future<Set<String>>> futureSet = Sets.newHashSet();
for (int i = 0; i < 10; i++)
{
Future<Set<String>> future = executor.submit(new Callable<Set<String>>()
{
@Override
public Set<String> call()
{
Set<String> keys = Sets.newHashSet();
for (int i = 0; i < 1000; i++)
{
String key = generator.createKey();
if (keys.contains(key))
{
fail("dublicate key");
}
keys.add(key);
}
return keys;
}
});
futureSet.add(future);
}
executor.shutdown();
Set<String> keys = Sets.newHashSet();
for (Future<Set<String>> future : futureSet)
{
Set<String> futureKeys = future.get(5, TimeUnit.SECONDS);
assertNotNull(futureKeys);
assertEquals(1000, futureKeys.size());
for (String key : futureKeys)
{
if (keys.contains(key))
{
fail("dublicate key");
}
keys.add(key);
}
}
assertEquals(10000, keys.size());
}
/**
* Method description
*
*/
@Test
public void testSimple()
{
DefaultKeyGenerator generator = new DefaultKeyGenerator();
String key = generator.createKey();
assertNotNull(key);
assertTrue(key.length() > 0);
}
/**
* Method description
*
*/
@Test
public void testUniqueness()
{
DefaultKeyGenerator generator = new DefaultKeyGenerator();
Set<String> keys = Sets.newHashSet();
for (int i = 0; i < 10000; i++)
{
String key = generator.createKey();
if (keys.contains(key))
{
fail("dublicate key");
}
keys.add(key);
}
assertEquals(10000, keys.size());
}
}