Clover coverage report - Drools - 2.0-rc2
Coverage timestamp: Wed May 11 2005 07:12:26 BST
file stats: LOC: 138   Methods: 2
NCLOC: 83   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ClassFieldObjectTypeFactory.java 90% 74.1% 50% 76.9%
coverage coverage
 1    package org.drools.semantics.base;
 2   
 3    /*
 4    * $Id: ClassFieldObjectTypeFactory.java,v 1.7.2.5 2005/04/30 13:49:43 mproctor Exp $
 5    *
 6    * Copyright 2004 (C) The Werken Company. All Rights Reserved.
 7    *
 8    * Redistribution and use of this software and associated documentation
 9    * ("Software"), with or without modification, are permitted provided that the
 10    * following conditions are met:
 11    *
 12    * 1. Redistributions of source code must retain copyright statements and
 13    * notices. Redistributions must also contain a copy of this document.
 14    *
 15    * 2. Redistributions in binary form must reproduce the above copyright notice,
 16    * this list of conditions and the following disclaimer in the documentation
 17    * and/or other materials provided with the distribution.
 18    *
 19    * 3. The name "drools" must not be used to endorse or promote products derived
 20    * from this Software without prior written permission of The Werken Company.
 21    * For written permission, please contact bob@werken.com.
 22    *
 23    * 4. Products derived from this Software may not be called "drools" nor may
 24    * "drools" appear in their names without prior written permission of The Werken
 25    * Company. "drools" is a registered trademark of The Werken Company.
 26    *
 27    * 5. Due credit should be given to The Werken Company.
 28    * (http://drools.werken.com/).
 29    *
 30    * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS''
 31    * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 32    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 33    * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE
 34    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 35    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 36    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 37    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 38    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 39    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 40    * POSSIBILITY OF SUCH DAMAGE.
 41    *
 42    */
 43   
 44    import java.util.HashSet;
 45    import java.util.Iterator;
 46    import java.util.Set;
 47   
 48    import org.drools.rule.Rule;
 49    import org.drools.smf.Configuration;
 50    import org.drools.smf.FactoryException;
 51    import org.drools.smf.ObjectTypeFactory;
 52    import org.drools.spi.ImportEntry;
 53    import org.drools.spi.Importer;
 54    import org.drools.spi.ObjectType;
 55    import org.drools.spi.RuleBaseContext;
 56   
 57    public class ClassFieldObjectTypeFactory
 58    implements
 59    ObjectTypeFactory
 60    {
 61    private static final ClassFieldObjectTypeFactory INSTANCE = new ClassFieldObjectTypeFactory( );
 62   
 63  0 public static ClassFieldObjectTypeFactory getInstance()
 64    {
 65  0 return INSTANCE;
 66    }
 67   
 68  21 public ObjectType newObjectType(Rule rule,
 69    RuleBaseContext context,
 70    Configuration config) throws FactoryException
 71    {
 72  21 String className = config.getText( );
 73  21 String fieldName = config.getAttribute( "field" );
 74  21 String fieldValue = config.getAttribute( "value" );
 75   
 76  21 if ( className == null || className.trim( ).equals( "" ) )
 77    {
 78  1 throw new FactoryException( "no class name specified" );
 79    }
 80   
 81  20 if ( fieldName == null || fieldName.trim( ).equals( "" ) )
 82    {
 83  1 throw new FactoryException( "no field name specified" );
 84    }
 85   
 86  19 if ( fieldValue == null || fieldValue.trim( ).equals( "" ) )
 87    {
 88  1 throw new FactoryException( "no field value specified" );
 89    }
 90  18 Class clazz = null;
 91  18 try
 92    {
 93  18 ClassLoader cl = (ClassLoader) context.get( "smf-classLoader" );
 94  18 if ( cl == null )
 95    {
 96  3 cl = Thread.currentThread( ).getContextClassLoader( );
 97  3 context.put( "smf-classLoader",
 98    cl );
 99    }
 100   
 101  18 if ( cl == null )
 102    {
 103  0 cl = getClass( ).getClassLoader( );
 104  0 context.put( "smf-classLoader",
 105    cl );
 106    }
 107   
 108  18 Importer importer = rule.getImporter( );
 109  18 clazz = importer.importClass( cl,
 110    className );
 111   
 112    // make sure field getter exists
 113  18 clazz.getMethod( "get" + fieldName.toUpperCase( ).charAt( 0 ) + fieldName.substring( 1 ),
 114    (Class[]) null );
 115   
 116    }
 117    catch ( SecurityException e )
 118    {
 119  0 throw new FactoryException( "Field '" + fieldName + "' is not accessible for Class '" + className + "'" );
 120    }
 121    catch ( NoSuchMethodException e )
 122    {
 123  0 throw new FactoryException( "Field '" + fieldName + "' does not exist for Class '" + className + "'" );
 124    }
 125    catch ( ClassNotFoundException e )
 126    {
 127  0 throw new FactoryException( e.getMessage( ) );
 128    }
 129    catch ( Error e )
 130    {
 131  0 throw new FactoryException( e.getMessage( ) );
 132    }
 133   
 134  18 return new ClassFieldObjectType( clazz,
 135    fieldName,
 136    fieldValue );
 137    }
 138    }