/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.jasper.tagplugins.jstl.core; import static org.junit.Assert.assertEquals; import org.junit.Test; /** * Test case for & * * @author Sheldon Shao */ public class TestWhen { @Test public void testWhen() throws Exception { //Test no <c:choose> outside SimpleTagPluginContext context1 = new SimpleTagPluginContext(); When when = new When(); SimpleTagPluginContext context = new SimpleTagPluginContext(); context.setParentContext(context1); context.addAttribute("test", "${true}"); when.doTag(context); assertEquals("", context.getCode()); // "if(${true}){" before fix //Test with <c:choose> outside Choose choose = new Choose(); SimpleTagPluginContext chooseContext = new SimpleTagPluginContext(); context = new SimpleTagPluginContext(); context.setParentContext(chooseContext); context.addAttribute("test", "${true}"); chooseContext.addBody(when, context); choose.doTag(chooseContext); assertEquals("if(${true}){}", chooseContext.getCode()); } @Test public void testOtherwise() throws Exception { //Test no <c:choose> outside When when = new When(); SimpleTagPluginContext context1 = new SimpleTagPluginContext(); context1.addAttribute("test", "${true}"); when.doTag(context1); Otherwise otherwise = new Otherwise(); SimpleTagPluginContext context2 = new SimpleTagPluginContext(); otherwise.doTag(context2); assertEquals("", context1.getCode()); assertEquals("", context2.getCode()); // "} else {" before fix //Test with <c:choose> outside Choose choose = new Choose(); SimpleTagPluginContext chooseContext = new SimpleTagPluginContext(); SimpleTagPluginContext whenContext = new SimpleTagPluginContext(); whenContext.setParentContext(chooseContext); whenContext.addAttribute("test", "${true}"); chooseContext.addBody(when, whenContext); otherwise = new Otherwise(); SimpleTagPluginContext otherwiseContext = new SimpleTagPluginContext(); otherwiseContext.setParentContext(chooseContext); chooseContext.addBody(otherwise, otherwiseContext); choose.doTag(chooseContext); assertEquals("if(${true}){} else {}", chooseContext.getCode()); } }