db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb/broker AllTests.java
Date Wed, 27 Oct 2004 20:47:25 GMT
tomdz       2004/10/27 13:47:25

  Modified:    src/test/org/apache/ojb/broker AllTests.java
  Added:       src/test/org/apache/ojb/broker/core/configuration
                        PicoComponentContainerTest.java
                        ComponentContainerTestBase.java
                        SpringComponentContainerTest.java
  Log:
  Added tests for the component container implementations.
  
  Revision  Changes    Path
  1.1                  db-ojb/src/test/org/apache/ojb/broker/core/configuration/PicoComponentContainerTest.java
  
  Index: PicoComponentContainerTest.java
  ===================================================================
  package org.apache.ojb.broker.core.configuration;
  
  /* Copyright 2002-2004 The Apache Software Foundation
   *
   * Licensed 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.
   */
  
  /**
   * Tests the {@link org.apache.ojb.broker.core.configuration.PicoComponentContainer}.
   *
   * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
   */
  public class PicoComponentContainerTest extends ComponentContainerTestBase
  {
      public static void main(String[] args)
      {
          String[] arr = { PicoComponentContainerTest.class.getName() };
  
          junit.textui.TestRunner.main(arr);
      }
  
      protected void setUp() throws Exception
      {
          super.setUp();
          _container = new PicoComponentContainer();
      }
  }
  
  
  
  1.1                  db-ojb/src/test/org/apache/ojb/broker/core/configuration/ComponentContainerTestBase.java
  
  Index: ComponentContainerTestBase.java
  ===================================================================
  package org.apache.ojb.broker.core.configuration;
  
  /* Copyright 2002-2004 The Apache Software Foundation
   *
   * Licensed 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.
   */
  
  import junit.framework.TestCase;
  
  /**
   * Base class for {@link org.apache.ojb.broker.core.configuration.ComponentContainer} tests.
   *
   * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
   */
  public abstract class ComponentContainerTestBase extends TestCase
  {
      protected ComponentContainer _container;
  
      protected void tearDown() throws Exception
      {
          _container = null;
          super.tearDown();
      }
  
      public void testCreation1()
      {
          _container.setImplementationClass(A.class, AImpl.class);
  
          assertEquals(AImpl.class, _container.getImplementationClass(A.class));
  
          Object obj = _container.getInstance(A.class);
  
          assertTrue(obj instanceof AImpl);
      }
  
      public void testCreation2()
      {
          _container.setImplementationClass(BBase.class, BImpl.class);
  
          assertEquals(BImpl.class, _container.getImplementationClass(BBase.class));
  
          Object obj = _container.getInstance(BBase.class);
  
          assertTrue(obj instanceof BImpl);
      }
  
      public void testCreation3()
      {
          _container.setImplementationClass(A.class, AImpl.class);
          _container.setImplementationClass(C.class, CImpl.class);
  
          assertEquals(AImpl.class, _container.getImplementationClass(A.class));
          assertEquals(CImpl.class, _container.getImplementationClass(C.class));
  
          Object obj1 = _container.getInstance(C.class);
  
          assertTrue(obj1 instanceof CImpl);
          assertTrue(((CImpl)obj1).getSub() instanceof AImpl);
  
          try
          {
              _container.getInstance(CRefined.class);
              fail();
          }
          catch (ObjectCreationException ex)
          {
              // is expected
          }
      }
  
      public void testCreation4()
      {
          // we have to register AImpl at the factory
          _container.setImplementationClass(A.class, AImpl.class);
          _container.setImplementationClass(BBase.class, DImpl.class);
  
          assertEquals(AImpl.class, _container.getImplementationClass(A.class));
          assertEquals(DImpl.class, _container.getImplementationClass(BBase.class));
  
          Object obj = _container.getInstance(BBase.class);
  
          assertTrue(obj instanceof DImpl);
          assertTrue(((DImpl)obj).getSub() instanceof AImpl);
      }
  
      public void testCreation5()
      {
          // no type is registered for A
          _container.setImplementationClass(E1.class, EImpl.class);
  
          assertNull(_container.getImplementationClass(A.class));
          assertEquals(EImpl.class, _container.getImplementationClass(E1.class));
  
          Object obj = _container.getInstance(E1.class);
  
          assertTrue(obj instanceof EImpl);
          assertNull(((EImpl)obj).getSub());
      }
  
      public void testCreation6()
      {
          _container.setImplementationClass(A.class, FImpl.class);
          _container.setImplementationClass(E3.class, BImpl.class);
  
          assertEquals(FImpl.class, _container.getImplementationClass(A.class));
          assertEquals(BImpl.class, _container.getImplementationClass(E3.class));
  
          Object obj = _container.getInstance(A.class);
  
          assertTrue(obj instanceof FImpl);
          assertTrue(((FImpl)obj).getSub() instanceof BImpl);
      }
  
      public void testCreation7()
      {
          _container.setImplementationClass(A.class, AImpl.class);
          _container.setImplementationClass(BBase.class, GImpl.class);
  
          assertEquals(AImpl.class, _container.getImplementationClass(A.class));
          assertEquals(GImpl.class, _container.getImplementationClass(BBase.class));
  
          Object obj = _container.getInstance(BBase.class);
  
          assertTrue(obj instanceof GImpl);
          assertTrue(((GImpl)obj).getSub() instanceof AImpl);
      }
  
      public void testCreation8()
      {
          A singleton = new HImpl(new CImpl(null));
  
          _container.setImplementationClass(A.class, AImpl.class);
          _container.setSingletonInstance(A.class, singleton);
  
          assertEquals(HImpl.class, _container.getImplementationClass(A.class));
  
          Object obj = _container.getInstance(A.class);
  
          assertTrue(singleton == obj);
      }
  
      public void testCreation9()
      {
          ComponentContainer subContainer = _container.createChildContainer();
  
          _container.setImplementationClass(A.class, AImpl.class);
          _container.setImplementationClass(C.class, CImpl.class);
          subContainer.setImplementationClass(A.class, HImpl.class);
  
          Object obj = subContainer.getInstance(A.class);
  
          assertTrue(obj instanceof HImpl);
          assertTrue(((HImpl)obj).getSub() instanceof CImpl);
      }
  
      public void testCreation10()
      {
          _container.setImplementationClass(A.class, AImpl.class);
  
          Object obj1 = _container.getInstance(A.class);
          Object obj2 = _container.getInstance(A.class);
  
          assertTrue(obj1 instanceof AImpl);
          assertTrue(obj2 instanceof AImpl);
          assertFalse(obj1 == obj2);
      }
  
      public void testCreation11()
      {
          _container.setImplementationClass(A.class, AImpl.class);
  
          A obj1 = (A)_container.getInstance(A.class);
  
          _container.setSingletonInstance(A.class, obj1);
  
          A obj2 = (A)_container.getInstance(A.class);
  
          assertTrue(obj1 == obj2);
      }
  
      public void testCreation12()
      {
          ComponentContainer subContainer = _container.createChildContainer();
  
          _container.setImplementationClass(A.class, AImpl.class);
  
          A obj1 = (A)_container.getInstance(A.class);
  
          _container.setSingletonInstance(A.class, obj1);
  
          A obj2 = (A)subContainer.getInstance(A.class);
  
          assertTrue(obj1 == obj2);
      }
  
      public void testCreation13()
      {
          ComponentContainer subContainer = _container.createChildContainer();
  
          _container.setImplementationClass(A.class, AImpl.class);
          subContainer.setImplementationClass(A.class, AImpl.class);
  
          A obj1 = (A)_container.getInstance(A.class);
  
          _container.setSingletonInstance(A.class, obj1);
  
          A obj2 = (A)subContainer.getInstance(A.class);
  
          // singleton only registered at parent factory
          assertFalse(obj1 == obj2);
      }
  
      public void testCreation14()
      {
          _container.setImplementationClass(A.class, FImpl.class);
          _container.setImplementationClass(AImpl.class, AImpl.class);
          _container.setImplementationClass(E3.class, BImpl.class);
  
          A obj1 = (A)_container.getInstance(A.class);
  
          assertTrue(obj1 instanceof FImpl);
  
          AImpl obj2 = (AImpl)_container.getInstance(AImpl.class);
  
          assertNotNull(obj2);
      }
  
      public void testConfiguration1()
      {
          _container.setImplementationClass(A.class, AImpl.class);
          _container.setProperty(A.class, "text", "test", false);
  
          A obj = (A)_container.getInstance(A.class);
  
          assertEquals("test", obj.getText());
      }
  
      public void testConfiguration2()
      {
          _container.setImplementationClass(A.class, AImpl.class);
          _container.setImplementationClass(C.class, CImpl.class);
          _container.setProperty(CImpl.class, "num", "1", false);
  
          CImpl obj = (CImpl)_container.getInstance(C.class);
  
          assertEquals(new Integer(1), obj.getNum());
      }
  
      public void testConfiguration3()
      {
          _container.setImplementationClass(A.class, AImpl.class);
          _container.setProperty(A.class,     "text", "test1", false);
          _container.setProperty(AImpl.class, "text", "test2", false);
  
          A obj = (A)_container.getInstance(A.class);
  
          assertEquals("test2", obj.getText());
      }
  
      public void testConfiguration4()
      {
          ComponentContainer subContainer = _container.createChildContainer();
  
          _container.setImplementationClass(A.class, AImpl.class);
          _container.setProperty(A.class, "text", "test1", false);
          subContainer.setProperty(AImpl.class, "text", "test2", false);
  
          A obj = (A)subContainer.getInstance(A.class);
  
          assertEquals("test2", obj.getText());
      }
  
      public void testConfiguration5()
      {
          ComponentContainer subContainer = _container.createChildContainer();
  
          _container.setImplementationClass(A.class, AImpl.class);
          _container.setProperty(AImpl.class, "text", "test1", false);
          subContainer.setProperty(A.class, "text", "test2", false);
  
          A obj = (A)subContainer.getInstance(A.class);
  
          assertEquals("test1", obj.getText());
      }
  
      public void testConfiguration6()
      {
          _container.setImplementationClass(A.class, AImpl.class);
          _container.setProperty(A.class, "text", "test1", false);
  
          A obj1 = (A)_container.getInstance(A.class);
  
          _container.setProperty(AImpl.class, "text", "test2", false);
          
          A obj2 = (A)_container.getInstance(A.class);
  
          assertEquals("test1", obj1.getText());
          assertEquals("test2", obj2.getText());
      }
  
      public void testConfiguration7()
      {
          _container.setImplementationClass(A.class, AImpl.class);
          _container.setProperty(A.class, "text", "test1", false);
  
          A obj1 = (A)_container.getInstance(A.class);
  
          _container.setSingletonInstance(A.class, obj1);
          _container.setProperty(AImpl.class, "text", "test2", false);
  
          A obj2 = (A)_container.getInstance(A.class);
  
          assertTrue(obj1 == obj2);
          assertEquals("test1", obj1.getText());
          assertEquals("test1", obj2.getText());
      }
  
      public void testConfiguration8()
      {
          _container.setProperty(A.class, "class", AImpl.class.getName(), false);
  
          A obj = (A)_container.getInstance(A.class);
  
          assertTrue(obj instanceof AImpl);
      }
  
      public void testConfiguration9()
      {
          _container.setImplementationClass(A.class, AImpl.class);
          _container.setProperty(A.class, "text", "test1", false);
          _container.setProperty(A.class, "text", "test2", false);
  
          A obj = (A)_container.getInstance(A.class);
  
          assertEquals("test1", obj.getText());
      }
  
      public void testConfiguration10()
      {
          _container.setImplementationClass(A.class, AImpl.class);
          _container.setProperty(A.class, "text", "test1", false);
          _container.setProperty(A.class, "text", "test2", true);
  
          A obj = (A)_container.getInstance(A.class);
  
          assertEquals("test2", obj.getText());
      }
  
      //
      // The types used by the tests
      //
      
      public interface A
      {
          public String getText();
          public void setText(String text);
      }
  
      public static class AImpl implements A
      {
          private String _text;
  
          public String getText()
          {
              return _text;
          }
  
          public void setText(String text)
          {
              _text = text;
          }
      }
  
      public abstract static class BBase
      {}
  
      public static class BImpl extends BBase implements E3
      {
          public BImpl()
          {}
      }
  
      public interface C
      {}
  
      public interface CRefined extends C
      {}
  
      public static class CImpl implements CRefined
      {
          private A _a;
          private Integer _num;
  
          public CImpl(A a)
          {
              _a = a;
          }
  
          public A getSub()
          {
              return _a;
          }
  
          public Integer getNum()
          {
              return _num;
          }
  
          public void setNum(Integer num)
          {
              _num = num;
          }
      }
  
      public static class DImpl extends BBase
      {
          private A _a;
  
          public DImpl(AImpl a)
          {
              _a = a;
          }
  
          public A getSub()
          {
              return _a;
          }
      }
  
      public interface E1
      {}
  
      public interface E2
      {}
  
      public interface E3
      {}
  
      public static class EImpl implements E1, E2
      {
          private A _a;
  
          public EImpl()
          {
              _a = null;
          }
  
          public EImpl(A a)
          {
              _a = a;
          }
  
          public A getSub()
          {
              return _a;
          }
      }
  
      public static class FImpl implements A
      {
          private Object _e;
          private String _text;
  
          public FImpl(E3 e)
          {
              _e = e;
          }
          public FImpl(EImpl e)
          {
              _e = e;
          }
  
          public Object getSub()
          {
              return _e;
          }
  
          public String getText()
          {
              return _text;
          }
  
          public void setText(String text)
          {
              _text = text;
          }
      }
  
      public abstract static class FSub extends FImpl
      {
          public FSub(E3 e)
          {
              super(e);
          }
      }
  
      public static class GImpl extends BBase
      {
          private Object _sub;
  
          public GImpl(A a)
          {
              _sub = a;
          }
  
          public GImpl(C c)
          {
              _sub = c;
          }
  
          public Object getSub()
          {
              return _sub;
          }
      }
  
      public static class HImpl extends AImpl
      {
          private C _c;
  
          public HImpl(C c)
          {
              _c = c;
          }
  
          public HImpl(C c, String text)
          {
              _c = c;
          }
  
          public C getSub()
          {
              return _c;
          }
      }
  }
  
  
  
  1.1                  db-ojb/src/test/org/apache/ojb/broker/core/configuration/SpringComponentContainerTest.java
  
  Index: SpringComponentContainerTest.java
  ===================================================================
  package org.apache.ojb.broker.core.configuration;
  
  /* Copyright 2002-2004 The Apache Software Foundation
   *
   * Licensed 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.
   */
  
  /**
   * Tests the {@link org.apache.ojb.broker.core.configuration.SpringComponentContainer}.
   *
   * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
   */
  public class SpringComponentContainerTest extends ComponentContainerTestBase
  {
      public static void main(String[] args)
      {
          String[] arr = { SpringComponentContainerTest.class.getName() };
  
          junit.textui.TestRunner.main(arr);
      }
  
      protected void setUp() throws Exception
      {
          super.setUp();
          _container = new SpringComponentContainer();
      }
  }
  
  
  
  1.56      +4 -0      db-ojb/src/test/org/apache/ojb/broker/AllTests.java
  
  Index: AllTests.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/AllTests.java,v
  retrieving revision 1.55
  retrieving revision 1.56
  diff -u -r1.55 -r1.56
  --- AllTests.java	26 Oct 2004 15:27:41 -0000	1.55
  +++ AllTests.java	27 Oct 2004 20:47:25 -0000	1.56
  @@ -6,6 +6,8 @@
   import org.apache.ojb.broker.accesslayer.sql.TableAliasHandlerTest;
   import org.apache.ojb.broker.cache.LocalCacheTest;
   import org.apache.ojb.broker.cache.ObjectCacheTest;
  +import org.apache.ojb.broker.core.configuration.PicoComponentContainerTest;
  +import org.apache.ojb.broker.core.configuration.SpringComponentContainerTest;
   import org.apache.ojb.broker.metadata.CustomAttributesTest;
   import org.apache.ojb.broker.metadata.MetadataMultithreadedTest;
   import org.apache.ojb.broker.metadata.MetadataTest;
  @@ -104,6 +106,8 @@
           suite.addTestSuite(MultithreadedReadTest.class);
           suite.addTestSuite(MetadataMultithreadedTest.class);
           suite.addTestSuite(FactoryTest.class);
  +        suite.addTestSuite(PicoComponentContainerTest.class);
  +        suite.addTestSuite(SpringComponentContainerTest.class);
   
           return suite;
       }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message