View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.uif.container;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.concurrent.Callable;
21  
22  import org.junit.Before;
23  import org.junit.Test;
24  import org.kuali.rice.krad.uif.UifConstants;
25  import org.kuali.rice.krad.uif.component.Component;
26  import org.kuali.rice.krad.uif.control.Control;
27  import org.kuali.rice.krad.uif.control.SelectControl;
28  import org.kuali.rice.krad.uif.control.TextAreaControl;
29  import org.kuali.rice.krad.uif.field.InputField;
30  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
31  import org.kuali.rice.krad.uif.service.ViewHelperService;
32  import org.kuali.rice.krad.uif.view.View;
33  
34  import static org.junit.Assert.*;
35  import static org.mockito.Mockito.*;
36  
37  /**
38   * various tests for CollectionGroup
39   *
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   */
42  public class CollectionGroupTest {
43      private CollectionGroup group;
44  
45      @Before
46      public void setup() {
47          group = ViewLifecycle.encapsulateInitialization(new Callable<CollectionGroup>(){
48              @Override
49              public CollectionGroup call() throws Exception {
50                  CollectionGroup group = new CollectionGroup();
51                  List<Component> items = new ArrayList<Component>();
52                  InputField field = new InputField();
53                  field.setControl(new SelectControl());
54                  items.add(field);
55                  items.add(new TextAreaControl());
56                  group.setItems(items);
57                  return group;
58              }});
59      }
60  
61      /**
62       * test that the collection group is set in all nested components' contexts
63       */
64      @Test
65      public void testPushCollectionGroupToReference() {
66          View view = mock(View.class);
67          ViewHelperService helper = mock(ViewHelperService.class);
68          when(view.getViewHelperService()).thenReturn(helper);
69          ViewLifecycle.encapsulateLifecycle(view, null, null, null, new Runnable(){
70              @Override
71              public void run() {
72                  CollectionGroup mutableGroup = group.copy();
73                  mutableGroup.pushCollectionGroupToReference();
74                  for (Component component: mutableGroup.getItems()) {
75                      testForCollectionGroupInContext(component, mutableGroup);
76                  }
77                  Control innerControl = ((InputField) mutableGroup.getItems().get(0)).getControl();
78                  testForCollectionGroupInContext(innerControl, mutableGroup);
79              }});
80      }
81  
82      /**
83       * test that the collection group is available in the component's contexts
84       *
85       * @param component
86       */
87      private void testForCollectionGroupInContext(Component component, CollectionGroup group) {
88          assertTrue("The component does not have the collection group key in the context",
89                  component.getContext().containsKey(UifConstants.ContextVariableNames.COLLECTION_GROUP));
90          assertTrue("The collection group found is not the parent group",
91                  component.getContext().get(UifConstants.ContextVariableNames.COLLECTION_GROUP) == group);
92      }
93      
94  }