001 /** 002 * Copyright 2005-2011 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.kew.rule; 017 018 import org.junit.Test; 019 import org.kuali.rice.core.api.util.ClassLoaderUtils; 020 import org.kuali.rice.kew.rule.bo.RuleTemplateBo; 021 import org.kuali.rice.kew.rule.bo.RuleTemplateAttributeBo; 022 import org.kuali.rice.kew.service.KEWServiceLocator; 023 import org.kuali.rice.kew.test.KEWTestCase; 024 025 import java.util.List; 026 027 import static org.junit.Assert.*; 028 029 /** 030 * Tests the RuleTemplateAttribute class. 031 * 032 * @author Kuali Rice Team (rice.collab@kuali.org) 033 */ 034 public class RuleTemplateAttributeTest extends KEWTestCase { 035 036 protected void loadTestData() throws Exception { 037 loadXmlFile("RuleTemplateAttributeTestConfig.xml"); 038 } 039 040 @Test 041 public void testGetWorkflowAttribute() throws Exception { 042 RuleTemplateBo template = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName("TemplateWithRuleValidationAttribute"); 043 List<RuleTemplateAttributeBo> ruleTemplateAttributes = (List<RuleTemplateAttributeBo>) template.getRuleTemplateAttributes(); 044 int index = 0; 045 for (RuleTemplateAttributeBo ruleTemplateAttribute : ruleTemplateAttributes) { 046 boolean runtimeThrown = false; 047 WorkflowRuleAttribute attribute = null; 048 try { 049 attribute = ruleTemplateAttribute.getWorkflowAttribute(); 050 } catch (RuntimeException e) { 051 runtimeThrown = true; 052 } 053 if (index == 0) { 054 // should be the TestRuleAttribute 055 assertFalse("RuntimeException should not have been thrown.", runtimeThrown); 056 assertNotNull("Attribute should exist.", attribute); 057 attribute = (WorkflowRuleAttribute) ClassLoaderUtils.unwrapFromProxy(attribute); 058 assertEquals("Should be TestRuleAttribute", TestRuleAttribute.class, attribute.getClass()); 059 } else if (index == 1) { 060 // should be the TestRuleDelegationAttribute so should be null 061 assertTrue("RuntimeException should have been thrown.", runtimeThrown); 062 assertNull("This should be the rule delegation attribute, not a WorkflowAttribute.", attribute); 063 } 064 index++; 065 } 066 } 067 068 @Test 069 public void testIsWorkflowAttribute() throws Exception { 070 RuleTemplateBo template = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName("TemplateWithRuleValidationAttribute"); 071 List<RuleTemplateAttributeBo> ruleTemplateAttributes = (List<RuleTemplateAttributeBo>) template.getRuleTemplateAttributes(); 072 int index = 0; 073 for (RuleTemplateAttributeBo ruleTemplateAttribute : ruleTemplateAttributes) { 074 boolean isWorkflowAttribute = ruleTemplateAttribute.isWorkflowAttribute(); 075 Object attribute = ruleTemplateAttribute.getAttribute(); 076 attribute = ClassLoaderUtils.unwrapFromProxy(attribute); 077 if (index == 0) { 078 // should be the TestRuleAttribute 079 assertNotNull(attribute); 080 assertEquals("Should be TestRuleAttribute", TestRuleAttribute.class, attribute.getClass()); 081 assertTrue("TestRuleAttribute is a workflow attribute.", isWorkflowAttribute); 082 } else if (index == 1) { 083 // should be the TestRuleValidationAttribute so should be null 084 assertEquals("Should be TestRuleValidationAttribute", TestRuleValidationAttribute.class, attribute.getClass()); 085 assertFalse("TestRuleValidationAttribute is not a workflow attribute", isWorkflowAttribute); 086 } 087 index++; 088 } 089 } 090 091 }