001/* $Id: TestConfigurablePluginAttributes.java 1102402 2011-05-12 18:03:26Z simonetripodi $ 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.apache.commons.digester3.plugins; 020 021import static org.junit.Assert.*; 022 023import java.util.LinkedList; 024import java.util.List; 025 026import org.apache.commons.digester3.Digester; 027import org.apache.commons.digester3.plugins.PluginCreateRule; 028import org.apache.commons.digester3.plugins.PluginDeclarationRule; 029import org.apache.commons.digester3.plugins.PluginRules; 030import org.junit.Test; 031 032/** 033 * Test cases for functionality which sets what xml attributes specify the plugin class or plugin declaration id. 034 */ 035 036public class TestConfigurablePluginAttributes 037{ 038 039 // --------------------------------------------------------------- Test cases 040 @Test 041 public void testDefaultBehaviour() 042 throws Exception 043 { 044 // tests that by default the attributes used are 045 // named "plugin-class" and "plugin-id" 046 047 Digester digester = new Digester(); 048 digester.setNamespaceAware( true ); 049 PluginRules rc = new PluginRules(); 050 digester.setRules( rc ); 051 052 PluginDeclarationRule pdr = new PluginDeclarationRule(); 053 digester.addRule( "root/plugin", pdr ); 054 055 PluginCreateRule widgetPluginRule = new PluginCreateRule( Widget.class ); 056 digester.addRule( "root/widget", widgetPluginRule ); 057 digester.addSetNext( "root/widget", "addWidget" ); 058 059 PluginCreateRule gadgetPluginRule = new PluginCreateRule( Widget.class ); 060 digester.addRule( "root/gadget", gadgetPluginRule ); 061 digester.addSetNext( "root/gadget", "addGadget" ); 062 063 MultiContainer root = new MultiContainer(); 064 digester.push( root ); 065 066 try 067 { 068 digester.parse( Utils.getInputStream( this, "test7.xml" ) ); 069 070 } 071 catch ( Exception e ) 072 { 073 throw e; 074 } 075 076 List<Widget> widgets = root.getWidgets(); 077 assertNotNull( widgets ); 078 assertEquals( 4, widgets.size() ); 079 080 assertEquals( TextLabel.class, widgets.get( 0 ).getClass() ); 081 assertEquals( TextLabel.class, widgets.get( 1 ).getClass() ); 082 assertEquals( TextLabel.class, widgets.get( 2 ).getClass() ); 083 assertEquals( TextLabel.class, widgets.get( 3 ).getClass() ); 084 085 List<Widget> gadgets = root.getGadgets(); 086 assertNotNull( gadgets ); 087 assertEquals( 4, gadgets.size() ); 088 089 assertEquals( TextLabel.class, gadgets.get( 0 ).getClass() ); 090 assertEquals( TextLabel.class, gadgets.get( 1 ).getClass() ); 091 assertEquals( TextLabel.class, gadgets.get( 2 ).getClass() ); 092 assertEquals( TextLabel.class, gadgets.get( 3 ).getClass() ); 093 } 094 095 @Test 096 public void testGlobalOverride() 097 throws Exception 098 { 099 // Tests that using setDefaultPluginXXXX overrides behaviour for all 100 // PluginCreateRule instances. Also tests specifying attributes 101 // with "null" for namespace (ie attributes not in any namespace). 102 // 103 // note that in order not to screw up all other tests, we need 104 // to reset the global names after we finish here! 105 106 Digester digester = new Digester(); 107 digester.setNamespaceAware( true ); 108 PluginRules rc = new PluginRules(); 109 digester.setRules( rc ); 110 111 rc.setPluginIdAttribute( null, "id" ); 112 rc.setPluginClassAttribute( null, "class" ); 113 114 PluginDeclarationRule pdr = new PluginDeclarationRule(); 115 digester.addRule( "root/plugin", pdr ); 116 117 PluginCreateRule widgetPluginRule = new PluginCreateRule( Widget.class ); 118 digester.addRule( "root/widget", widgetPluginRule ); 119 digester.addSetNext( "root/widget", "addWidget" ); 120 121 PluginCreateRule gadgetPluginRule = new PluginCreateRule( Widget.class ); 122 digester.addRule( "root/gadget", gadgetPluginRule ); 123 digester.addSetNext( "root/gadget", "addGadget" ); 124 125 MultiContainer root = new MultiContainer(); 126 digester.push( root ); 127 128 try 129 { 130 digester.parse( Utils.getInputStream( this, "test7.xml" ) ); 131 132 } 133 catch ( Exception e ) 134 { 135 throw e; 136 } 137 138 List<Widget> widgets = root.getWidgets(); 139 assertNotNull( widgets ); 140 assertEquals( 4, widgets.size() ); 141 142 assertEquals( Slider.class, widgets.get( 0 ).getClass() ); 143 assertEquals( Slider.class, widgets.get( 1 ).getClass() ); 144 assertEquals( Slider.class, widgets.get( 2 ).getClass() ); 145 assertEquals( Slider.class, widgets.get( 3 ).getClass() ); 146 147 List<Widget> gadgets = root.getGadgets(); 148 assertNotNull( gadgets ); 149 assertEquals( 4, gadgets.size() ); 150 151 assertEquals( Slider.class, gadgets.get( 0 ).getClass() ); 152 assertEquals( Slider.class, gadgets.get( 1 ).getClass() ); 153 assertEquals( Slider.class, gadgets.get( 2 ).getClass() ); 154 assertEquals( Slider.class, gadgets.get( 3 ).getClass() ); 155 } 156 157 @Test 158 public void testInstanceOverride() 159 throws Exception 160 { 161 // Tests that using setPluginXXXX overrides behaviour for only 162 // that particular PluginCreateRule instance. Also tests that 163 // attributes can be in namespaces. 164 165 Digester digester = new Digester(); 166 digester.setNamespaceAware( true ); 167 PluginRules rc = new PluginRules(); 168 digester.setRules( rc ); 169 170 PluginDeclarationRule pdr = new PluginDeclarationRule(); 171 digester.addRule( "root/plugin", pdr ); 172 173 // for plugins at pattern "root/widget", use xml attributes "id" and 174 // "class" in the custom namespace as the values for plugin id and 175 // class, not the default (and non-namespaced) values of 176 // "plugin-id" and "plugin-class". 177 PluginCreateRule widgetPluginRule = new PluginCreateRule( Widget.class ); 178 widgetPluginRule.setPluginIdAttribute( "http://commons.apache.org/digester/plugins", "id" ); 179 widgetPluginRule.setPluginClassAttribute( "http://commons.apache.org/digester/plugins", "class" ); 180 digester.addRule( "root/widget", widgetPluginRule ); 181 digester.addSetNext( "root/widget", "addWidget" ); 182 183 PluginCreateRule gadgetPluginRule = new PluginCreateRule( Widget.class ); 184 digester.addRule( "root/gadget", gadgetPluginRule ); 185 digester.addSetNext( "root/gadget", "addGadget" ); 186 187 MultiContainer root = new MultiContainer(); 188 digester.push( root ); 189 190 try 191 { 192 digester.parse( Utils.getInputStream( this, "test7.xml" ) ); 193 } 194 catch ( Exception e ) 195 { 196 throw e; 197 } 198 199 List<Widget> widgets = root.getWidgets(); 200 assertNotNull( widgets ); 201 assertEquals( 4, widgets.size() ); 202 203 assertEquals( TextLabel2.class, widgets.get( 0 ).getClass() ); 204 assertEquals( TextLabel2.class, widgets.get( 1 ).getClass() ); 205 assertEquals( TextLabel2.class, widgets.get( 2 ).getClass() ); 206 assertEquals( TextLabel2.class, widgets.get( 3 ).getClass() ); 207 208 List<Widget> gadgets = root.getGadgets(); 209 assertNotNull( gadgets ); 210 assertEquals( 4, gadgets.size() ); 211 212 assertEquals( TextLabel.class, gadgets.get( 0 ).getClass() ); 213 assertEquals( TextLabel.class, gadgets.get( 1 ).getClass() ); 214 assertEquals( TextLabel.class, gadgets.get( 2 ).getClass() ); 215 assertEquals( TextLabel.class, gadgets.get( 3 ).getClass() ); 216 } 217 218 // inner classes used for testing 219 220 public static class MultiContainer 221 { 222 private LinkedList<Widget> widgets = new LinkedList<Widget>(); 223 224 private LinkedList<Widget> gadgets = new LinkedList<Widget>(); 225 226 public MultiContainer() 227 { 228 } 229 230 public void addWidget( Widget child ) 231 { 232 widgets.add( child ); 233 } 234 235 public List<Widget> getWidgets() 236 { 237 return widgets; 238 } 239 240 public void addGadget( Widget child ) 241 { 242 gadgets.add( child ); 243 } 244 245 public List<Widget> getGadgets() 246 { 247 return gadgets; 248 } 249 } 250}