View Javadoc
1   /**
2    * Copyright 2005-2016 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.field;
17  
18  import static org.mockito.Mockito.mock;
19  import static org.mockito.Mockito.when;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.junit.Before;
25  import org.junit.Test;
26  import org.kuali.rice.krad.keyvalues.KeyValuesFinder;
27  import org.kuali.rice.krad.keyvalues.KeyValuesFinderFactory;
28  import org.kuali.rice.krad.uif.UifConstants;
29  import org.kuali.rice.krad.uif.component.BindingInfo;
30  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
31  import org.kuali.rice.krad.uif.lifecycle.ViewPostMetadata;
32  import org.kuali.rice.krad.uif.service.ViewHelperService;
33  import org.kuali.rice.krad.uif.view.View;
34  import org.mockito.Mockito;
35  
36  /**
37   * tests InputField object and methods
38   *
39  **/
40  public class InputFieldTest {
41  
42      View view = null;
43      TestModel model = null;
44      KeyValuesFinder optionsFinder = null;
45      BindingInfo bindingInfo = null;
46  
47  
48      @Before
49      public void setUp() {
50          view = Mockito.mock(View.class);
51          ViewHelperService mockViewHelperService = mock(ViewHelperService.class);
52          when(view.getViewHelperService()).thenReturn(mockViewHelperService);
53  
54          optionsFinder = Mockito.mock(KeyValuesFinder.class);
55          bindingInfo = Mockito.mock(BindingInfo.class);
56          model = new TestModel();
57      }
58  
59      @Test
60      public void testPerformFinalizeWithNonStringFieldOptions() throws Exception {
61          // setup options finder
62          Map<String, String> map = new HashMap<String, String>();
63          map.put("testInteger", "1");
64          optionsFinder = KeyValuesFinderFactory.fromMap(map);
65  
66          // setup preconditions (view status is final; bindinginfo return testInteger)
67          when(view.getViewStatus()).thenReturn(UifConstants.ViewStatus.FINAL);
68          when(bindingInfo.getBindingPath()).thenReturn("testInteger");
69          when(bindingInfo.clone()).thenReturn(bindingInfo);
70  
71          // setup input field with binding info and readonly
72          final InputField testObj = new InputFieldBase();
73          testObj.setBindingInfo(bindingInfo);
74          testObj.setReadOnly(true);
75          testObj.setOptionsFinder(optionsFinder);
76          
77          ViewLifecycle.encapsulateLifecycle(view, model, new ViewPostMetadata(), null, null, new Runnable(){
78              @Override
79              public void run() {
80                  testObj.performFinalize(model, testObj);
81              }});
82  
83      }
84  
85      // Simple model object to return testInteger integer
86      private class TestModel {
87          public int getTestInteger() {
88              return 1;
89          }
90      }
91  }