001/* $Id: TestDefaultPlugin.java 1212519 2011-12-09 17:01:07Z sebb $ 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.List; 024 025import org.apache.commons.digester3.Digester; 026import org.apache.commons.digester3.plugins.PluginConfigurationException; 027import org.apache.commons.digester3.plugins.PluginCreateRule; 028import org.apache.commons.digester3.plugins.PluginInvalidInputException; 029import org.apache.commons.digester3.plugins.PluginRules; 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.impl.NoOpLog; 032import org.junit.Test; 033import org.xml.sax.SAXParseException; 034 035public class TestDefaultPlugin 036{ 037 038 // --------------------------------------------------------------- Test cases 039 @Test 040 public void testDefaultPlugins1() 041 throws Exception 042 { 043 // * tests that when a PluginCreateRule is defined with a default 044 // class, that the default class is instantiated when no class 045 // or id is specified in the xml file. 046 Digester digester = new Digester(); 047 PluginRules rc = new PluginRules(); 048 digester.setRules( rc ); 049 050 PluginCreateRule pcr = new PluginCreateRule( Widget.class, TextLabel.class ); 051 digester.addRule( "root/widget", pcr ); 052 digester.addSetNext( "root/widget", "addChild" ); 053 054 Container root = new Container(); 055 digester.push( root ); 056 057 digester.parse( Utils.getInputStream( this, "test2.xml" ) ); 058 059 Object child; 060 List<Widget> children = root.getChildren(); 061 assertNotNull( children ); 062 assertEquals( 3, children.size() ); 063 064 child = children.get( 0 ); 065 assertNotNull( child ); 066 assertEquals( TextLabel.class, child.getClass() ); 067 TextLabel label1 = (TextLabel) child; 068 assertEquals( "label1", label1.getLabel() ); 069 070 child = children.get( 1 ); 071 assertNotNull( child ); 072 assertEquals( TextLabel.class, child.getClass() ); 073 TextLabel label2 = (TextLabel) child; 074 assertEquals( "label2", label2.getLabel() ); 075 076 child = children.get( 2 ); 077 assertNotNull( child ); 078 assertEquals( Slider.class, child.getClass() ); 079 Slider slider1 = (Slider) child; 080 assertEquals( "slider1", slider1.getLabel() ); 081 } 082 083 public void testDefaultPlugins2() 084 throws Exception 085 { 086 // * tests that when there is no default plugin, it is an error 087 // not to have one of plugin-class or plugin-id specified 088 Digester digester = new Digester(); 089 PluginRules rc = new PluginRules(); 090 digester.setRules( rc ); 091 092 PluginCreateRule pcr = new PluginCreateRule( Widget.class ); 093 digester.addRule( "root/widget", pcr ); 094 digester.addSetNext( "root/widget", "addChild" ); 095 096 Container root = new Container(); 097 digester.push( root ); 098 099 Log oldLog = digester.getLogger(); 100 try 101 { 102 digester.setLogger( new NoOpLog() ); 103 digester.parse( Utils.getInputStream( this, "test2.xml" ) ); 104 fail("Expected SAXParseException"); 105 } 106 catch ( SAXParseException e ) 107 { 108 assertEquals( PluginInvalidInputException.class, e.getException().getClass() ); 109 } 110 finally 111 { 112 digester.setLogger( oldLog ); 113 } 114 115 } 116 117 public void testDefaultPlugins3() 118 throws Exception 119 { 120 // * tests that the default plugin must implement or extend the 121 // plugin base class. 122 Digester digester = new Digester(); 123 PluginRules rc = new PluginRules(); 124 digester.setRules( rc ); 125 126 PluginCreateRule pcr = new PluginCreateRule( Widget.class, Object.class ); 127 digester.addRule( "root/widget", pcr ); 128 digester.addSetNext( "root/widget", "addChild" ); 129 130 Container root = new Container(); 131 digester.push( root ); 132 133 Log oldLog = digester.getLogger(); 134 try 135 { 136 digester.setLogger( new NoOpLog() ); 137 digester.parse( Utils.getInputStream( this, "test2.xml" ) ); 138 fail("Expected SAXParseException"); 139 } 140 catch ( SAXParseException e ) 141 { 142 assertEquals( PluginConfigurationException.class, e.getException().getClass() ); 143 } 144 finally 145 { 146 digester.setLogger( oldLog ); 147 } 148 } 149}