KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > tagutil > ScopedIdGeneratorTest


1 /**
2  * Jun 27, 2005
3  *
4  * Copyright 2004 - 2005 uitags
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package net.sf.uitags.tagutil;
19
20 import javax.servlet.jsp.PageContext JavaDoc;
21
22 import junit.framework.TestCase;
23
24 import org.springframework.mock.web.MockPageContext;
25
26 /**
27  * Tests {@link net.sf.uitags.tagutil.ScopedIdGenerator}.
28  *
29  * @author jonni
30  * @version $Id: ScopedIdGeneratorTest.java,v 1.2 2006/08/16 12:06:22 jonni Exp $
31  */

32 public final class ScopedIdGeneratorTest extends TestCase {
33   /**
34    * Main method for the tests.
35    *
36    * @param args main arguments
37    */

38   public static void main(String JavaDoc[] args) {
39     junit.textui.TestRunner.run(ScopedIdGeneratorTest.class);
40   }
41
42   /** {@inheritDoc} */
43   protected void setUp() throws Exception JavaDoc {
44     super.setUp();
45   }
46
47   /** {@inheritDoc} */
48   protected void tearDown() throws Exception JavaDoc {
49     super.tearDown();
50   }
51
52   /**
53    * Ensures that {@link ScopedIdGenerator#nextId(int, String, PageContext)}
54    * generates unique IDs.
55    */

56   public void testUngroupedNextId() {
57     long id0, id1, id2;
58     PageContext JavaDoc pageContext = new MockPageContext();
59
60     id0 = ScopedIdGenerator.nextId(
61         PageContext.PAGE_SCOPE, "id", pageContext);
62     id1 = ScopedIdGenerator.nextId(
63         PageContext.PAGE_SCOPE, "id", pageContext);
64     id2 = ScopedIdGenerator.nextId(
65         PageContext.PAGE_SCOPE, "id", pageContext);
66     assertTrue(id0 != id1);
67     assertTrue(id1 != id2);
68     assertTrue(id2 != id0);
69
70     id0 = ScopedIdGenerator.nextId(
71         PageContext.REQUEST_SCOPE, "id", pageContext);
72     id1 = ScopedIdGenerator.nextId(
73         PageContext.REQUEST_SCOPE, "id", pageContext);
74     id2 = ScopedIdGenerator.nextId(
75         PageContext.REQUEST_SCOPE, "id", pageContext);
76     assertTrue(id0 != id1);
77     assertTrue(id1 != id2);
78     assertTrue(id2 != id0);
79
80     // Session scope can't be tested as we haven't got a HTTP session
81

82     id0 = ScopedIdGenerator.nextId(
83         PageContext.APPLICATION_SCOPE, "id", pageContext);
84     id1 = ScopedIdGenerator.nextId(
85         PageContext.APPLICATION_SCOPE, "id", pageContext);
86     id2 = ScopedIdGenerator.nextId(
87         PageContext.APPLICATION_SCOPE, "id", pageContext);
88     assertTrue(id0 != id1);
89     assertTrue(id1 != id2);
90     assertTrue(id2 != id0);
91   }
92
93   /**
94    * Ensures that {@link ScopedIdGenerator#nextId(int, String, PageContext)}
95    * generates unique IDs.
96    */

97   public void testGroupedNextId() {
98     long id0, id1, id2;
99     PageContext JavaDoc pageContext = new MockPageContext();
100
101     id0 = ScopedIdGenerator.nextId(
102         PageContext.PAGE_SCOPE, "id", pageContext, "group1");
103     id1 = ScopedIdGenerator.nextId(
104         PageContext.PAGE_SCOPE, "id", pageContext, "group1");
105     id2 = ScopedIdGenerator.nextId(
106         PageContext.PAGE_SCOPE, "id", pageContext, "group2");
107     assertTrue(id0 == id1);
108     assertTrue(id1 != id2);
109     assertTrue(id2 != id0);
110
111     id0 = ScopedIdGenerator.nextId(
112         PageContext.REQUEST_SCOPE, "id", pageContext, null);
113     id1 = ScopedIdGenerator.nextId(
114         PageContext.REQUEST_SCOPE, "id", pageContext, "group2");
115     id2 = ScopedIdGenerator.nextId(
116         PageContext.REQUEST_SCOPE, "id", pageContext, "group2");
117     assertTrue(id0 != id1);
118     assertTrue(id1 == id2);
119     assertTrue(id2 != id0);
120
121     // Session scope can't be tested as we haven't got a HTTP session
122

123     id0 = ScopedIdGenerator.nextId(
124         PageContext.APPLICATION_SCOPE, "id", pageContext, "group1");
125     id1 = ScopedIdGenerator.nextId(
126         PageContext.APPLICATION_SCOPE, "id", pageContext, "group2");
127     id2 = ScopedIdGenerator.nextId(
128         PageContext.APPLICATION_SCOPE, "id", pageContext, "group3");
129     assertTrue(id0 != id1);
130     assertTrue(id1 != id2);
131     assertTrue(id2 != id0);
132   }
133 }
134
Popular Tags