001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.util;
016    
017    import java.util.List;
018    
019    import org.apache.hivemind.ApplicationRuntimeException;
020    import org.apache.tapestry.BaseComponentTestCase;
021    import org.testng.annotations.Test;
022    
023    /**
024     * Tests for {@link org.apache.tapestry.util.DefaultPrimaryKeyConverter}.
025     * 
026     * @author Howard M. Lewis Ship
027     * @since 4.0
028     */
029    @Test
030    public class DefaultPrimaryKeyConverterTest extends BaseComponentTestCase
031    {
032        /**
033         * Test the starting values of a number of properties.
034         */
035        public void testInitialValues()
036        {
037            DefaultPrimaryKeyConverter cv = new DefaultPrimaryKeyConverter();
038    
039            assertTrue(cv.getAllValues().isEmpty());
040            assertTrue(cv.getValues().isEmpty());
041            assertTrue(cv.getDeletedValues().isEmpty());
042            assertNull(cv.getLastValue());
043        }
044    
045        public void testAdd()
046        {
047            DefaultPrimaryKeyConverter cv = new DefaultPrimaryKeyConverter();
048    
049            cv.add("fred", "flintstone");
050    
051            assertEquals("flintstone", cv.getLastValue());
052    
053            cv.add("barney", "rubble");
054    
055            assertEquals("rubble", cv.getLastValue());
056    
057            List l = cv.getValues();
058    
059            assertEquals("flintstone", l.get(0));
060            assertEquals("rubble", l.get(1));
061        }
062    
063        public void testAddDuplicate()
064        {
065            DefaultPrimaryKeyConverter cv = new DefaultPrimaryKeyConverter();
066    
067            cv.add("fred", "flintstone");
068            cv.add("barney", "rubble");
069    
070            try
071            {
072                cv.add("fred", "macmurray");
073                unreachable();
074            }
075            catch (ApplicationRuntimeException ex)
076            {
077                assertEquals("Key 'fred' already exists in this primary key converter.", ex
078                        .getMessage());
079            }
080        }
081    
082        public void testClear()
083        {
084            DefaultPrimaryKeyConverter cv = new DefaultPrimaryKeyConverter();
085    
086            cv.add("fred", "flintstone");
087            cv.add("barney", "rubble");
088            cv.setDeleted(true);
089    
090            cv.clear();
091    
092            assertTrue(cv.getAllValues().isEmpty());
093            assertTrue(cv.getValues().isEmpty());
094            assertTrue(cv.getDeletedValues().isEmpty());
095            assertNull(cv.getLastValue());
096        }
097    
098        public void testDelete()
099        {
100            DefaultPrimaryKeyConverter cv = new DefaultPrimaryKeyConverter();
101    
102            cv.add("fred", "flintstone");
103            cv.add("barney", "rubble");
104    
105            assertEquals("flintstone", cv.getValue("fred"));
106    
107            assertEquals(false, cv.isDeleted());
108    
109            assertTrue(cv.getDeletedValues().isEmpty());
110    
111            assertEquals("flintstone", cv.getLastValue());
112    
113            cv.setDeleted(true);
114    
115            assertEquals(true, cv.isDeleted());
116    
117            assertTrue(cv.getDeletedValues().contains("flintstone"));
118            assertFalse(cv.getValues().contains("flintstone"));
119            assertTrue(cv.getAllValues().contains("flintstone"));
120    
121            cv.setDeleted(false);
122    
123            assertFalse(cv.isDeleted());
124    
125            assertFalse(cv.getDeletedValues().contains("flintstone"));
126            assertTrue(cv.getValues().contains("flintstone"));
127        }
128    
129        public void testGetPrimaryKey()
130        {
131            DefaultPrimaryKeyConverter cv = new DefaultPrimaryKeyConverter();
132    
133            cv.add("fred", "flintstone");
134            cv.add("barney", "rubble");
135    
136            assertEquals("fred", cv.getPrimaryKey("flintstone"));
137            assertEquals("flintstone", cv.getLastValue());
138    
139            assertEquals("barney", cv.getPrimaryKey("rubble"));
140            assertEquals("rubble", cv.getLastValue());
141        }
142    
143        public void testGetPrimaryKeyNotFound()
144        {
145            DefaultPrimaryKeyConverter cv = new DefaultPrimaryKeyConverter();
146    
147            try
148            {
149                cv.getPrimaryKey("flintstone");
150                unreachable();
151            }
152            catch (ApplicationRuntimeException ex)
153            {
154                assertEquals("Value flintstone not found.", ex.getMessage());
155            }
156    
157        }
158    
159        public void testGetValue()
160        {
161            DefaultPrimaryKeyConverter cv = new DefaultPrimaryKeyConverter();
162    
163            cv.add("fred", "flintstone");
164            cv.add("barney", "rubble");
165    
166            assertEquals("flintstone", cv.getValue("fred"));
167            assertEquals("flintstone", cv.getLastValue());
168    
169            assertEquals("rubble", cv.getValue("barney"));
170            assertEquals("rubble", cv.getLastValue());
171        }
172    
173        public void testGetValueNotFound()
174        {
175            DefaultPrimaryKeyConverter cv = new DefaultPrimaryKeyConverter();
176    
177            assertEquals(null, cv.getValue("unknown"));
178        }
179    
180        public void testGetValueSubclassOverride()
181        {
182            DefaultPrimaryKeyConverter cv = new DefaultPrimaryKeyConverter()
183            {
184                public Object provideMissingValue(Object primaryKey)
185                {
186                    assertEquals("fred", primaryKey);
187    
188                    return "flintstone";
189                }
190            };
191    
192            assertEquals("flintstone", cv.getValue("fred"));
193        }
194    
195        public void testGetValueSubclassThrowsException()
196        {
197            final RuntimeException re = new ApplicationRuntimeException("flintstone");
198    
199            DefaultPrimaryKeyConverter cv = new DefaultPrimaryKeyConverter()
200            {
201                public Object provideMissingValue(Object primaryKey)
202                {
203                    assertEquals("fred", primaryKey);
204    
205                    throw re;
206                }
207            };
208    
209            try
210            {
211                cv.getValue("fred");
212                unreachable();
213    
214            }
215            catch (ApplicationRuntimeException ex)
216            {
217                assertSame(re, ex);
218            }
219        }
220    
221        public void testGetDeletedValues()
222        {
223            DefaultPrimaryKeyConverter cv = new DefaultPrimaryKeyConverter();
224    
225            cv.add("fred", "flintstone");
226            cv.add("barney", "rubble");
227    
228            assertEquals("fred", cv.getPrimaryKey("flintstone"));
229    
230            cv.setDeleted(true);
231    
232            assertTrue(cv.getDeletedValues().contains("flintstone"));
233        }
234    
235        public void testRemoveDeletedValues()
236        {
237            DefaultPrimaryKeyConverter cv = new DefaultPrimaryKeyConverter();
238    
239            cv.add("fred", "flintstone");
240            cv.add("barney", "rubble");
241    
242            assertEquals("flintstone", cv.getValue("fred"));
243    
244            cv.setDeleted(true);
245    
246            cv.removeDeletedValues();
247    
248            Object[] values = cv.getValues().toArray();
249            assertEquals(1, values.length);
250            assertEquals("rubble", values[0]);
251    
252            assertEquals(cv.getAllValues(), cv.getValues());
253        }
254    }