View Javadoc

1   /**
2    * Copyright 2005-2015 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 org.kuali.rice.krad.uif.component.Component;
19  import org.kuali.rice.krad.uif.element.Label;
20  import org.kuali.rice.krad.uif.element.Message;
21  import org.kuali.rice.krad.uif.service.ViewHelperService;
22  import org.kuali.rice.krad.uif.view.View;
23  
24  import org.junit.Test;
25  
26  import static junit.framework.Assert.*;
27  import static org.mockito.Mockito.*;
28  
29  /**
30   * test various FieldBase methods
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  public class FieldBaseTest {
34  
35      @Test
36      /**
37       * Tests rendering on required messages
38       *
39       * @see KULRICE-7130
40       */
41      public void testRequiredMessageDisplay() {
42  
43          // create mock objects for view, view helper service, model, and component
44          View mockView =  mock(View.class);
45          ViewHelperService mockViewHelperService = mock(ViewHelperService.class);
46          when(mockView.getViewHelperService()).thenReturn(mockViewHelperService);
47          Object nullModel = null;
48          Component mockComponent = mock(Component.class);
49  
50          // build asterisk required message and mock label to test rendering
51          Label mockLabel = mock(Label.class);
52  
53          Message requiredMessage = new Message();
54          requiredMessage.setMessageText("*");
55          requiredMessage.setRender(true);
56          when(mockLabel.getRequiredMessage()).thenReturn(requiredMessage);
57  
58          try {
59              FieldBase fieldBase = new FieldBase();
60              fieldBase.setFieldLabel(mockLabel);
61              fieldBase.setRequired(true);
62  
63              // required and not readonly - render
64              fieldBase.setReadOnly(false);
65              fieldBase.performFinalize(mockView, nullModel, mockComponent);
66              assertTrue(fieldBase.getFieldLabel().getRequiredMessage().isRender());
67  
68              // required and readonly -  do not render
69              fieldBase.setReadOnly(true);
70              fieldBase.performFinalize(mockView, nullModel, mockComponent);
71              assertFalse(fieldBase.getFieldLabel().getRequiredMessage().isRender());
72          } catch(Exception ex) {
73              fail("Unit Test Exception - " + ex.getMessage());
74          }
75      }
76  }