rdonkin 02/01/31 11:20:27 Modified: betwixt/src/java/org/apache/commons/betwixt/expression Context.java betwixt/src/test/org/apache/commons/betwixt/expression TestEvaluation.java Added: betwixt/src/java/org/apache/commons/betwixt/expression CyclicReferenceException.java Log: newContext now throws an exception when the given bean is already in a parent context Revision Changes Path 1.5 +17 -7 jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/Context.java Index: Context.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/Context.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- Context.java 30 Jan 2002 19:35:36 -0000 1.4 +++ Context.java 31 Jan 2002 19:20:27 -0000 1.5 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/Context.java,v 1.4 2002/01/30 19:35:36 rdonkin Exp $ - * $Revision: 1.4 $ - * $Date: 2002/01/30 19:35:36 $ + * $Header: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/Context.java,v 1.5 2002/01/31 19:20:27 rdonkin Exp $ + * $Revision: 1.5 $ + * $Date: 2002/01/31 19:20:27 $ * * ==================================================================== * @@ -56,7 +56,7 @@ * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. - * $Id: Context.java,v 1.4 2002/01/30 19:35:36 rdonkin Exp $ + * $Id: Context.java,v 1.5 2002/01/31 19:20:27 rdonkin Exp $ */ package org.apache.commons.betwixt.expression; @@ -86,7 +86,7 @@ * If the child is a parent then that operation fails. </p> * * @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a> - * @version $Revision: 1.4 $ + * @version $Revision: 1.5 $ */ public class Context { @@ -135,8 +135,18 @@ this.parent = parent; } - /** Returns a new child context with the given bean but the same log and variables. */ - public Context newContext(Object newBean) { + /** Returns a new child context with the given bean but the same log and variables. + * + * @param newBean create a child context for this bean + * @throws CyclicReferenceException if the given bean is already in a parent context + */ + public Context newContext(Object newBean) throws CyclicReferenceException { + // first check that we aren't introducing a cycle + if (isAncester(newBean)) { + log.info("Found cyclic reference!"); + log.debug(newBean); + throw new CyclicReferenceException(); + } return new Context(newBean, variables, log, this); } 1.1 jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/CyclicReferenceException.java Index: CyclicReferenceException.java =================================================================== /* * $Header: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/CyclicReferenceException.java,v 1.1 2002/01/31 19:20:27 rdonkin Exp $ * $Revision: 1.1 $ * $Date: 2002/01/31 19:20:27 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999-2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * $Id: CyclicReferenceException.java,v 1.1 2002/01/31 19:20:27 rdonkin Exp $ */ package org.apache.commons.betwixt.expression; /** * <p>Thrown when bean evaluation finds a cycle reference.</p> * * @author <a href="mailto:[EMAIL PROTECTED]">Robert Burrell Donkin</a> * @version $Revision: 1.1 $ */ public class CyclicReferenceException extends Exception { /** Message used with empty constructor */ private static final String DEFAULT_MESSAGE = "Bean graph contains a cyclic reference"; /** Construct exception with default message. */ public CyclicReferenceException() { super(DEFAULT_MESSAGE); } /** Construct exception with given message */ public CyclicReferenceException(String message) { super(message); } } 1.2 +39 -6 jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/expression/TestEvaluation.java Index: TestEvaluation.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/expression/TestEvaluation.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- TestEvaluation.java 30 Jan 2002 19:35:37 -0000 1.1 +++ TestEvaluation.java 31 Jan 2002 19:20:27 -0000 1.2 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/expression/TestEvaluation.java,v 1.1 2002/01/30 19:35:37 rdonkin Exp $ - * $Revision: 1.1 $ - * $Date: 2002/01/30 19:35:37 $ + * $Header: /home/cvs/jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/expression/TestEvaluation.java,v 1.2 2002/01/31 19:20:27 rdonkin Exp $ + * $Revision: 1.2 $ + * $Date: 2002/01/31 19:20:27 $ * * ==================================================================== * @@ -57,7 +57,7 @@ * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * - * $Id: TestEvaluation.java,v 1.1 2002/01/30 19:35:37 rdonkin Exp $ + * $Id: TestEvaluation.java,v 1.2 2002/01/31 19:20:27 rdonkin Exp $ */ package org.apache.commons.betwixt.expression; @@ -71,7 +71,7 @@ /** Test harness for the evaluation of beans using contexts. * * @author Robert Burrell Donkin - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ */ public class TestEvaluation extends TestCase { @@ -86,7 +86,7 @@ } public void testAncesterSearch() throws Exception { - + // this tests ancester searchs CustomerBean alpha = new CustomerBean(); CustomerBean beta = new CustomerBean(); CustomerBean gamma = new CustomerBean(); @@ -105,6 +105,39 @@ assertTrue("(4) isAncester fails when maybe parent is actually child.", !ctxBeta.isAncester(gamma)); assertTrue("(3) isAncester fails to recognize an indirect parent.", ctxGamma.isAncester(alpha)); + } + + public void testChildContext() throws Exception { + // this tests raising exceptions properly when children are created + CustomerBean alpha = new CustomerBean(); + CustomerBean beta = new CustomerBean(); + CustomerBean gamma = new CustomerBean(); + + Context ctxAlpha = new Context(alpha, log); + + // check that you can't make a child context with the parent bean + try { + // let's try creating a new context with bean alpha + Context temp = ctxAlpha.newContext(alpha); + // we shouldn't get to here! + fail("(1) You can make a child context with the parent bean"); + + } catch (CyclicReferenceException e) { + // this is what we're expecting! + } + + Context ctxBeta = ctxAlpha.newContext(beta); + + // check that you can't make a child context with the direct grandparent bean + try { + // let's try creating a new context with bean alpha + Context temp = ctxBeta.newContext(alpha); + // we shouldn't get to here! + fail("(2) You can make a child context with the direct grandparent bean"); + + } catch (CyclicReferenceException e) { + // this is what we're expecting! + } } }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>