View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.tests.services.persistence.generic;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertTrue;
21  
22  import java.util.HashSet;
23  import java.util.Set;
24  
25  import org.junit.Test;
26  
27  import ch.elca.el4j.services.persistence.generic.primarykey.PrimaryKeyGenerator;
28  import ch.elca.el4j.services.persistence.generic.primarykey.UuidPrimaryKeyGenerator;
29  
30  // Checkstyle: MagicNumber off
31  
32  /**
33   * This is the unit test for <code>UuidPrimaryKeyGenerator</code>.
34   *
35   * @svnLink $Revision: 3884 $;$Date: 2009-08-04 15:48:31 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/test/java/ch/elca/el4j/tests/services/persistence/generic/UuidPrimaryKeyGeneratorTest.java $
36   *
37   * @author Jacques-Olivier Haenni (JOH)
38   */
39  public class UuidPrimaryKeyGeneratorTest {
40  	/**
41  	 * Tests the key size.
42  	 */
43  	@Test
44  	public void testKeySize() {
45  		String key = getPrimaryKeyGenerator().getPrimaryKey();
46  		assertEquals("The size of the PK is not correct.", 32, key.length());
47  	}
48  
49  	/**
50  	 * Tests whether generated keys are unique.
51  	 */
52  	@Test
53  	public void testKeyUnicity() {
54  		PrimaryKeyGenerator pkg = getPrimaryKeyGenerator();
55  		Set<String> set = new HashSet<String>();
56  		int count = 2000;
57  		for (int i = 0; i < count; i++) {
58  			set.add(pkg.getPrimaryKey());
59  		}
60  		assertEquals("Some generated keys were equals.", count, set.size());
61  	}
62  
63  	/**
64  	 * Checks the keys' format.
65  	 *
66  	 */
67  	@Test
68  	public void testKeyFormat() {
69  		PrimaryKeyGenerator pkg = getPrimaryKeyGenerator();
70  		int count = 2000;
71  		for (int i = 0; i < count; i++) {
72  			String key = pkg.getPrimaryKey();
73  			assertEquals("The size of the PK is not correct.",
74  					32, key.length());
75  			assertTrue("The key contains spaces.", key.indexOf(' ') == -1);
76  		}
77  	}
78  
79  	/**
80  	 * @return Returns a new key generator instance.
81  	 */
82  	private PrimaryKeyGenerator getPrimaryKeyGenerator() {
83  		return new UuidPrimaryKeyGenerator();
84  	}
85  }
86  //Checkstyle: MagicNumber on