Changes

Jump to: navigation, search

Template Method

736 bytes added, 18:21, 19 March 2007
Apache
== Real-life Application ==
=== Apache Excalibur===  
<pre>
/* * Copyright 1997-2004 The 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. * Licensed 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.avalon.framework.componenttest; import org.testapache.avalon.framework.CascadingError;import org.apache.avalon.framework.CascadingThrowable;
import junit.framework.TestCase;
import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.component.DefaultComponentManager;
/**
* Test the basic public methods of DefaultComponentManagerTestCase for {@link CascadingError}.
*
* @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
* @version $Id$
*/
public final class DefaultComponentManagerTestCase CascadingErrorTestCase extends TestCase
{
public void testConstructor()
{
assertNotNull( new CascadingError( null, null ) );
assertNotNull( new CascadingError( "msg", null ) );
assertNotNull( new CascadingError( "msg", new RuntimeException() ) );
assertNotNull( new CascadingError( null, new RuntimeException() ) );
 
//assertNotNull( new CascadingError( "msg" ) );
//ambiguous assertNotNull( new CascadingError( null ) );
//assertNotNull( new CascadingError() );
}
class DefaultRoleA implements Component,RoleApublic void testGetCause()
{
public DefaultRoleARuntimeException re = new RuntimeException(); CascadingError e = new CascadingError( "msg", re );  assertEquals( re, e.getCause() );  e = new CascadingError( "msg", null ); assertNull( e.getCause() );  // default to jdk 1.3 cause (not that it seems to help, // but it still makes sense) /*Exception exc = new Exception("blah"); try
{
try
{
throw exc;
}
catch( Exception ex )
{
throw new CascadingError();
}
}
catch( CascadingError ex )
{
ex.getCause();
}*/
}
class DefaultRoleB implements Component,RoleBpublic void testCasts()
{
public DefaultRoleBCascadingError e = new CascadingError("msg", null ); {assertTrue( e instanceof Error ); }assertTrue( e instanceof CascadingThrowable );
}
}
</pre>
<pre>
/*
* 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.
*/
private DefaultComponentManager m_componentManagerpackage org.apache.avalon.framework.test;
protected boolean m_exceptionThrownimport org.apache.avalon.framework.CascadingException;import org.apache.avalon.framework.CascadingThrowable;
import junit.framework.TestCase;
public DefaultComponentManagerTestCase()/** * TestCase for {@link CascadingException}. this( * * @author <a href="DefaultComponentManager Test Casemailto:dev@avalon.apache.org");>Avalon Development Team</a> } * @version $Id$ */public class CascadingExceptionTestCase extends TestCase{ public DefaultComponentManagerTestCasevoid testConstructor( final String name )
{
superassertNotNull( name new CascadingException( null, null ) ); assertNotNull( new CascadingException( "msg", null ) ); assertNotNull( new CascadingException( "msg", new RuntimeException() ) ); } assertNotNull( new CascadingException( null, new RuntimeException() ) );
protected void setUp assertNotNull( new CascadingException("msg" ) ); throws Exception { m_componentManager = // ambiguous assertNotNull( new DefaultComponentManagerCascadingException(null ) ); m_exceptionThrown = false//assertNotNull( new CascadingException() );
}
protected public void tearDowntestGetCause() throws Exception
{
m_componentManager RuntimeException re = nullnew RuntimeException(); }  /** * lookup contract: * return first component found for role * search in hirarchy from current componentManager up. * if no compnent exist for role a in hierarchy * throw ComponentException */ CascadingException e = new CascadingException( "msg", re );
assertEquals( re, e.getCause() );
public void testlookup1() throws Exception { DefaultRoleB roleBinBase e = new DefaultRoleB(); DefaultRoleB roleBinParent = new DefaultRoleB(); DefaultRoleA roleAinParent = new DefaultRoleA();  m_componentManager.putCascadingException(RoleA.ROLE"msg",roleAinParentnull ); m_componentManager.putassertNull(RoleBe.ROLE,roleBinParent); DefaultComponentManager baseComponentManager = new DefaultComponentManagergetCause(m_componentManager); baseComponentManager.put(RoleB.ROLE,roleBinBase); Object lookupAinBase = baseComponentManager.lookup(RoleA.ROLE); Object lookupBinBase = baseComponentManager.lookup(RoleB.ROLE); Object lookupBinParent = m_componentManager.lookup(RoleB.ROLE); assertTrue( lookupAinBase instanceof RoleA); assertEquals( lookupBinBase, roleBinBase ); assertEquals( lookupBinParent, roleBinParent ); assertEquals( lookupAinBase,roleAinParent); }
public void testlookup2 // default to jdk 1.3 cause (not that it seems to help, // but it still makes sense) throws /*Exception { m_componentManager.put(RoleA.ROLE,exc = new DefaultRoleAException("blah")); Object o = null;
try
{
o = m_componentManager.lookuptry { throw exc; } catch( Exception ex ) { throw new CascadingException(RoleB.ROLE); }
}
catch (ComponentException ceCascadingException ex )
{
m_exceptionThrown = true; } if ex.getCause(o == null) assertTrue("ComponentException was not thrown when component was not found by lookup." ,m_exceptionThrown ); else assertTrue("component was found by lookup ,when there was no component.",false);  public void testhasComponent() throws Exception { m_componentManager.put(RoleA.ROLE,new DefaultRoleA()); assertTrue(m_componentManager.hasComponent(RoleA.ROLE)); assertTrue(!m_componentManager.hasComponent(RoleB.ROLE));*/
}
public void testmakeReadOnlytestCasts() throws Exception
{
//before read only m_componentManager.putCascadingException e = new CascadingException(RoleA.ROLE"msg",new DefaultRoleA()); Object a = m_componentManager.lookup(RoleA.ROLEnull ); assertTrue( a e instanceof RoleAException ); m_componentManager.makeReadOnly(); //after read only try { m_componentManager.put(RoleB.ROLE,new DefaultRoleB()); } catch (IllegalStateException se) { m_exceptionThrown = true; } assertTrue("IllegalStateException was not thrown in put after makeReadOnly." , m_exceptionThrown e instanceof CascadingThrowable );
}
}
1
edit

Navigation menu