1 package javax.faces.component;
2
3 import javax.faces.FacesException;
4
5 import org.easymock.EasyMock;
6 import static org.easymock.EasyMock.*;
7 import static org.testng.Assert.*;
8 import org.testng.annotations.Test;
9
10
11
12
13
14
15
16
17 public class UIComponentBaseGetClientIdTest extends AbstractUIComponentBaseTest
18 {
19 @Test(expectedExceptions = {NullPointerException.class})
20 public void testNullFacesContext() throws Exception
21 {
22 _testImpl.getClientId(null);
23 }
24
25 @Test
26 public void testWithoutParentAndNoRenderer() throws Exception
27 {
28 String expectedClientId = "testId";
29 _testImpl.setId(expectedClientId);
30 expect(_testImpl.getParent()).andReturn(null);
31 expect(_testImpl.getRenderer(EasyMock.same(_facesContext))).andReturn(null);
32 _mocksControl.replay();
33 assertEquals(expectedClientId, _testImpl.getClientId(_facesContext));
34 _mocksControl.verify();
35 assertEquals(expectedClientId, _testImpl.getClientId(_facesContext));
36 }
37
38 @Test
39 public void testWithRenderer() throws Exception
40 {
41 String id = "testId";
42 String expectedClientId = "convertedClientId";
43 _testImpl.setId(id);
44 expect(_testImpl.getParent()).andReturn(null);
45 expect(_testImpl.getRenderer(EasyMock.same(_facesContext))).andReturn(_renderer);
46 expect(_renderer.convertClientId(EasyMock.same(_facesContext), EasyMock.eq(id))).andReturn(expectedClientId);
47 _mocksControl.replay();
48 assertEquals(expectedClientId, _testImpl.getClientId(_facesContext));
49 _mocksControl.verify();
50 assertEquals(expectedClientId, _testImpl.getClientId(_facesContext));
51 }
52
53 @Test
54 public void testWithParentNamingContainer() throws Exception
55 {
56 String id = "testId";
57 String containerClientId = "containerClientId";
58 String expectedClientId = containerClientId + NamingContainer.SEPARATOR_CHAR + id;
59 UIComponent parent = _mocksControl.createMock(UIComponent.class);
60 UIComponent namingContainer = _mocksControl.createMock(TestNamingContainerComponent.class);
61 _testImpl.setId(id);
62 expect(_testImpl.getParent()).andReturn(parent);
63 expect(parent.getParent()).andReturn(namingContainer);
64 expect(namingContainer.getContainerClientId(EasyMock.same(_facesContext))).andReturn(containerClientId);
65
66 expect(_testImpl.getRenderer(EasyMock.same(_facesContext))).andReturn(_renderer);
67 expect(_renderer.convertClientId(EasyMock.same(_facesContext), EasyMock.eq(expectedClientId))).andReturn(expectedClientId);
68 _mocksControl.replay();
69 assertEquals(expectedClientId, _testImpl.getClientId(_facesContext));
70 _mocksControl.verify();
71 assertEquals(expectedClientId, _testImpl.getClientId(_facesContext));
72 }
73
74 @Test
75 public void testWithParentNamingContainerChanging() throws Exception
76 {
77 String id = "testId";
78 String containerClientId = "containerClientId";
79 UIComponent parent = _mocksControl.createMock(UIComponent.class);
80 UIComponent namingContainer = _mocksControl.createMock(TestNamingContainerComponent.class);
81 for (int i = 0; i < 10; i++)
82 {
83 _testImpl.setId(id);
84 String expectedClientId = containerClientId + i + NamingContainer.SEPARATOR_CHAR + id;
85 expect(_testImpl.getParent()).andReturn(parent);
86 expect(parent.getParent()).andReturn(namingContainer);
87 expect(namingContainer.getContainerClientId(EasyMock.same(_facesContext))).andReturn(containerClientId + i);
88
89 expect(_testImpl.getRenderer(EasyMock.same(_facesContext))).andReturn(_renderer);
90 expect(_renderer.convertClientId(EasyMock.same(_facesContext), EasyMock.eq(expectedClientId)))
91 .andReturn(expectedClientId);
92 _mocksControl.replay();
93 assertEquals(expectedClientId, _testImpl.getClientId(_facesContext));
94 _mocksControl.verify();
95 assertEquals(expectedClientId, _testImpl.getClientId(_facesContext));
96 _mocksControl.reset();
97 }
98 }
99
100 @Test
101 public void testWithoutId() throws Exception
102 {
103 UIViewRoot viewRoot = _mocksControl.createMock(UIViewRoot.class);
104 expect(_facesContext.getViewRoot()).andReturn(viewRoot);
105 String expectedId = "uniqueId";
106 expect(viewRoot.createUniqueId()).andReturn(expectedId);
107 expect(_testImpl.getParent()).andReturn(null).anyTimes();
108 expect(_testImpl.getRenderer(EasyMock.same(_facesContext))).andReturn(null);
109 _mocksControl.replay();
110 assertEquals(expectedId, _testImpl.getClientId(_facesContext));
111 assertEquals(expectedId, _testImpl.getId());
112 _mocksControl.verify();
113 assertEquals(expectedId, _testImpl.getClientId(_facesContext));
114 }
115
116 @Test(expectedExceptions = {FacesException.class})
117 public void testWithoutIdAndNoUIViewRoot() throws Exception
118 {
119 expect(_testImpl.getParent()).andReturn(null).anyTimes();
120 expect(_facesContext.getViewRoot()).andReturn(null);
121 _mocksControl.replay();
122 _testImpl.getClientId(_facesContext);
123 }
124
125 public abstract static class TestNamingContainerComponent extends UIComponent implements NamingContainer
126 {
127 }
128
129 }