001 // Copyright 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.components; 016 017 import static org.easymock.EasyMock.expect; 018 019 import org.apache.tapestry.BaseComponentTestCase; 020 import org.apache.tapestry.IBinding; 021 import org.apache.tapestry.IMarkupWriter; 022 import org.apache.tapestry.IRender; 023 import org.apache.tapestry.IRequestCycle; 024 import org.apache.tapestry.services.ResponseBuilder; 025 import org.apache.tapestry.spec.IComponentSpecification; 026 import org.testng.annotations.Test; 027 028 /** 029 * Tests for {@link org.apache.tapestry.components.Conditional} component. 030 * 031 * @author Howard M. Lewis Ship 032 * @since 4.0 033 */ 034 @Test 035 public class TestConditional extends BaseComponentTestCase 036 { 037 private IRender newRender(IMarkupWriter writer, IRequestCycle cycle) 038 { 039 IRender render = newMock(IRender.class); 040 041 render.render(writer, cycle); 042 043 return render; 044 } 045 046 public void testFalseAndFalse() 047 { 048 Conditional conditional = newInstance(Conditional.class, new Object[] 049 { "condition", Boolean.FALSE, "invert", Boolean.FALSE }); 050 051 conditional.renderComponent(null, null); 052 } 053 054 public void testTrueAndTrue() 055 { 056 Conditional conditional = newInstance(Conditional.class, new Object[] 057 { "condition", Boolean.TRUE, "invert", Boolean.TRUE }); 058 059 conditional.renderComponent(null, null); 060 } 061 062 public void testRenderSimple() 063 { 064 IMarkupWriter writer = newWriter(); 065 IRequestCycle cycle = newCycle(false, writer); 066 IRender body = newRender(writer, cycle); 067 068 Conditional conditional = newInstance(Conditional.class, 069 new Object[] { "condition", Boolean.TRUE }); 070 071 expect(cycle.renderStackPush(conditional)).andReturn(conditional); 072 073 conditional.addBody(body); 074 075 expect(cycle.renderStackPop()).andReturn(conditional); 076 077 replay(); 078 079 conditional.render(writer, cycle); 080 081 verify(); 082 } 083 084 public void testIgnoreElementWhenRewinding() 085 { 086 IMarkupWriter writer = newWriter(); 087 IRequestCycle cycle = newCycle(); 088 IRender body = newMock(IRender.class); 089 090 ResponseBuilder builder = newMock(ResponseBuilder.class); 091 092 Conditional conditional = newInstance(Conditional.class, 093 new Object[] { "condition", Boolean.TRUE, "element", "div" }); 094 095 expect(cycle.renderStackPush(conditional)).andReturn(conditional); 096 097 expect(cycle.isRewinding()).andReturn(true); 098 099 expect(cycle.getResponseBuilder()).andReturn(builder); 100 101 builder.render(writer, body, cycle); 102 103 expect(cycle.renderStackPop()).andReturn(conditional); 104 105 replay(); 106 107 conditional.addBody(body); 108 109 conditional.render(writer, cycle); 110 111 verify(); 112 } 113 114 public void testElement() 115 { 116 IMarkupWriter writer = newWriter(); 117 IRequestCycle cycle = newCycle(false, null); 118 119 IComponentSpecification spec = newSpec("informal", null); 120 IBinding informal = newBinding("informal-value"); 121 122 IRender body = newRender(writer, cycle); 123 124 Conditional conditional = newInstance(Conditional.class, 125 new Object[] { 126 "condition", Boolean.TRUE, 127 "element", "div", 128 "specification", spec 129 }); 130 131 conditional.addBody(body); 132 conditional.setBinding("informal", informal); 133 134 expect(cycle.renderStackPush(conditional)).andReturn(conditional); 135 136 writer.begin("div"); 137 138 writer.attribute("informal", "informal-value"); 139 140 // We've trained body, but there's no way to ensure, 141 // using EasyMock, that methods are invoked in the correct 142 // order. But sometimes you have to trust the code ( 143 // and trust that future developers won't break something 144 // that obvious!). 145 146 trainResponseBuilder(cycle, writer); 147 148 writer.end("div"); 149 150 expect(cycle.renderStackPop()).andReturn(conditional); 151 152 replay(); 153 154 conditional.render(writer, cycle); 155 156 verify(); 157 } 158 }