Skip to content
Browse files

Create a proper test suite.

Create tests folder.
Create a number of unit tests. Work in progress.
Move various test bean folder trees into tests folder.
Add an ant build file to automate testing.
  • Loading branch information...
1 parent 771a2dd commit 2ae085c07d5d121f5b424447e0b9eecff2085117 @seancorfield committed Dec 27, 2011
View
3 .gitignore
@@ -1,4 +1,7 @@
.project
.settings
+mxunit
+tests/report
+tests/results
WEB-INF
settings.xml
View
25 Application.cfc
@@ -1,5 +1,10 @@
-// caution: requires FW/1 2.0 Alpha 5 or later!!
-component extends="org.corfield.framework" {
+component {
+ this.name = "InjectOneTests";
+ // used to test search via mapping:
+ this.mappings[ "/goldfish/trumpets" ] = expandPath( "/tests/extrabeans" );
+}
+
+/*
this.mappings[ '/goldfish/trumpets' ] = expandPath( '/extrabeans' );
function setupApplication() {
var xbf = new ioc( '/goldfish/trumpets',
@@ -23,9 +28,15 @@ component extends="org.corfield.framework" {
}
function loader( any ioc ) {
- var bf = new ioc( '' );
- bf.addBean( 'config', '/some/xml/file.xml' );
- bf.declareBean( 'configObject', 'declared.things.myconfig' );
- ioc.addBean( 'myconfig', bf.injectProperties( 'configObject', { name = "MyConfig" } ) );
+ ioc.addBean( 'myconfig1',
+ new ioc( '' )
+ .addBean( 'config', '/some/xml/file.xml' )
+ .declareBean( 'configObject', 'declared.things.myconfig' )
+ .injectProperties( 'configObject',
+ { name = "MyConfig" } ) );
+ ioc.addBean( 'myconfig2',
+ ioc.injectProperties(
+ new declared.things.myconfig( '/some/other/file.xml' ),
+ { name = "MyConfigBean" } ) );
}
-}
+*/
View
44 build.xml
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<project name="InjectOne" basedir="." default="help">
+
+ <property name="test.server" value="localhost"/>
+ <property name="test.port" value="80"/>
+ <property name="mxunit.home" value="${basedir}/mxunit"/>
+
+ <target name="help">
+ <echo message="test - run the unit tests"/>
+ </target>
+
+ <target name="test" depends="clean">
+ <taskdef name="mxunittask" classname="org.mxunit.ant.MXUnitAntTask" classpath="${mxunit.home}/ant/lib/mxunit-ant.jar"/>
+ <mkdir dir="${basedir}/tests/report"/>
+ <mkdir dir="${basedir}/tests/results"/>
+ <mxunittask server="${test.server}" port="${test.port}"
+ outputdir="${basedir}/tests/results"
+ haltonerror="false" haltonfailure="false"
+ verbose="false"
+ errorproperty="testfailed" failureproperty="testfailed">
+ <testcase name="tests.Empty" packageName="tests.Empty"/>
+ <testcase name="tests.BeanInfo" packageName="tests.BeanInfo"/>
+ <testcase name="tests.Constant" packageName="tests.Constant"/>
+ <testcase name="tests.InjectProperties" packageName="tests.InjectProperties"/>
+ <testcase name="tests.Parent" packageName="tests.Parent"/>
+ <testcase name="tests.AddBean" packageName="tests.AddBean"/>
+ <testcase name="tests.Mapping" packageName="tests.Mapping"/>
+ <testcase name="tests.ExtraBeans" packageName="tests.ExtraBeans"/>
+ </mxunittask>
+ <junitreport todir="${basedir}/tests/report">
+ <fileset dir="${basedir}/tests/results">
+ <include name="**/*.xml"/>
+ </fileset>
+ <report format="frames" todir="${basedir}/tests/report" styledir="${mxunit.home}/ant/xsl"/>
+ </junitreport>
+ <echo message="Test results: http://${test.server}:${test.port}/tests/report/index.html"/>
+ </target>
+
+ <target name="clean">
+ <delete dir="${basedir}/tests/report" includeemptydirs="true"/>
+ <delete dir="${basedir}/tests/results" includeemptydirs="true"/>
+ </target>
+
+</project>
View
10 declared/things/myconfig.cfc
@@ -1,10 +0,0 @@
-component accessors=true {
- property string name;
- function init( any config ) {
- variables.config = config;
- }
- function explain() {
- writeDump( var = variables.config, label = "MyConfig.config" );
- writeDump( var = variables.name, label = "MyConfig.name" );
- }
-}
View
22 tests/AddBean.cfc
@@ -0,0 +1,22 @@
+component extends="mxunit.framework.TestCase" {
+
+ function setup() {
+ variables.added =
+ new ioc( "" )
+ .addBean( "known", 42 );
+ }
+
+ function shouldHaveKnownValue() {
+ assertEquals( 42, variables.added.getBean( "known" ) );
+ }
+
+ function shouldBeSingleton() {
+ assertTrue( variables.added.isSingleton( "known" ) );
+ }
+
+ function shouldHaveKnownMetadata() {
+ var info = variables.added.getBeanInfo( "known" );
+ assertEquals( 42, info.value );
+ assertTrue( info.isSingleton );
+ }
+}
View
22 tests/BeanInfo.cfc
@@ -0,0 +1,22 @@
+component extends="mxunit.framework.TestCase" {
+
+ function setup() {
+ variables.factory = new ioc( "" );
+ }
+
+ function shouldBeAStruct() {
+ var info = variables.factory.getBeanInfo();
+ assertTrue( isStruct( info ) );
+ assertEquals( 1, structCount( info ) );
+ assertEquals( "beaninfo", structKeyList( info ) );
+ }
+
+ function shouldContainMetadata() {
+ var info = variables.factory.getBeanInfo( "beanfactory" );
+ assertTrue( isStruct( info ) );
+ assertEquals( 2, structCount( info ) );
+ assertTrue( structKeyExists( info, "value" ) );
+ assertTrue( info.isSingleTon );
+ }
+
+}
View
20 tests/Constant.cfc
@@ -0,0 +1,20 @@
+component extends="mxunit.framework.TestCase" {
+
+ function setup() {
+ variables.constants = new ioc( "", { constants = { known = 42 } } );
+ }
+
+ function shouldHaveKnownValue() {
+ assertEquals( 42, variables.constants.getBean( "known" ) );
+ }
+
+ function shouldBeSingleton() {
+ assertTrue( variables.constants.isSingleton( "known" ) );
+ }
+
+ function shouldHaveKnownMetadata() {
+ var info = variables.constants.getBeanInfo( "known" );
+ assertEquals( 42, info.value );
+ assertTrue( info.isSingleton );
+ }
+}
View
17 tests/Empty.cfc
@@ -0,0 +1,17 @@
+component extends="mxunit.framework.TestCase" {
+
+ function setup() {
+ variables.emptyFactory = new ioc( "" );
+ }
+
+ function shouldContainBeanFactory() {
+ assertTrue( variables.emptyFactory.containsBean( "beanFactory" ) );
+ }
+
+ function shouldContainJustOneBean() {
+ var info = variables.emptyFactory.getBeanInfo();
+ assertEquals( 1, structCount( info.beaninfo ) );
+ assertEquals( "beanfactory", structKeyList( info.beaninfo ) );
+ }
+
+}
View
30 tests/ExtraBeans.cfc
@@ -0,0 +1,30 @@
+component extends="mxunit.framework.TestCase" {
+
+ function shouldResolveMapping() {
+ var factory = new ioc( "/tests/extrabeans" );
+ application.itemCount = 0;
+ assertTrue( factory.containsBean( "item" ) );
+ assertTrue( factory.containsBean( "itemSheep" ) );
+ var item1 = factory.getBean( "item" );
+ var item2 = factory.getBean( "itemSheep" );
+ // since sheep was not specified as singular, item should
+ // be a singleton and itemSheep should be an alias to it
+ assertSame( item1, item2 );
+ assertEquals( 1, application.itemCount );
+ }
+
+ function shouldResolveMappingWithSingular() {
+ var factory = new ioc( "/tests/extrabeans",
+ { singulars = { sheep = "bean" } } );
+ application.itemCount = 0;
+ assertTrue( factory.containsBean( "item" ) );
+ assertTrue( factory.containsBean( "itemBean" ) );
+ var item1 = factory.getBean( "item" );
+ var item2 = factory.getBean( "itemBean" );
+ // since sheep was mapped to bean as a singular it should
+ // be a transient and those items should be unique
+ assertNotSame( item1, item2 );
+ assertEquals( 2, application.itemCount );
+ }
+
+}
View
35 tests/InjectProperties.cfc
@@ -0,0 +1,35 @@
+component extends="mxunit.framework.TestCase" {
+
+ function setup() {
+ variables.ioc = new ioc( "" );
+ }
+
+ function shouldInjectWithType() {
+ var bean = ioc.injectProperties( "tests.declared.things.myconfig", { name = "ByType" } );
+ assertEquals( "ByType", bean.getName() );
+ try {
+ var data = bean.getConfig();
+ fail( "constructor should not have been called" );
+ } catch ( any e ) {
+ assertEquals( "expression", e.type );
+ }
+ }
+
+ function shouldInjectWithObject() {
+ var bean = ioc.injectProperties(
+ new declared.things.myconfig( "object" ),
+ { name = "ByObject" } );
+ assertEquals( "ByObject", bean.getName() );
+ assertEquals( "object", bean.getConfig() );
+ }
+
+ function shouldInjectWithName() {
+ variables.ioc
+ .addBean( "data", "data" )
+ .declareBean( "configObject", "tests.declared.things.myconfig" );
+ var bean = variables.ioc.injectProperties( "configObject", { name = "ByName" } );
+ assertEquals( "ByName", bean.getName() );
+ assertEquals( "data", bean.getConfig() );
+ }
+
+}
View
30 tests/Mapping.cfc
@@ -0,0 +1,30 @@
+component extends="mxunit.framework.TestCase" {
+
+ function shouldResolveMapping() {
+ var factory = new ioc( "/goldfish/trumpets" );
+ application.itemCount = 0;
+ assertTrue( factory.containsBean( "item" ) );
+ assertTrue( factory.containsBean( "itemSheep" ) );
+ var item1 = factory.getBean( "item" );
+ var item2 = factory.getBean( "itemSheep" );
+ // since sheep was not specified as singular, item should
+ // be a singleton and itemSheep should be an alias to it
+ assertSame( item1, item2 );
+ assertEquals( 1, application.itemCount );
+ }
+
+ function shouldResolveMappingWithSingular() {
+ var factory = new ioc( "/goldfish/trumpets",
+ { singulars = { sheep = "bean" } } );
+ application.itemCount = 0;
+ assertTrue( factory.containsBean( "item" ) );
+ assertTrue( factory.containsBean( "itemBean" ) );
+ var item1 = factory.getBean( "item" );
+ var item2 = factory.getBean( "itemBean" );
+ // since sheep was mapped to bean as a singular it should
+ // be a transient and those items should be unique
+ assertNotSame( item1, item2 );
+ assertEquals( 2, application.itemCount );
+ }
+
+}
View
38 tests/Parent.cfc
@@ -0,0 +1,38 @@
+component extends="mxunit.framework.TestCase" {
+
+ function setup() {
+ variables.parent = new ioc( "", { constants = { one = 1, two = 2 } } );
+ variables.factory = new ioc( "", { constants = { one = "I", three = "III" } } );
+ variables.factory.setParent( variables.parent );
+ }
+
+ function shouldFindInParent() {
+ assertEquals( 2, variables.factory.getBean( "two" ) );
+ }
+
+ function shouldFindInChild() {
+ assertEquals( "I", variables.factory.getBean( "one" ) );
+ assertEquals( "III", variables.factory.getBean( "three" ) );
+ }
+
+ function shouldContainViaParent() {
+ assertTrue( variables.factory.containsBean( "two" ) );
+ }
+
+ function shouldGetMetadataViaParent() {
+ var info = variables.factory.getBeanInfo( "two" );
+ assertEquals( 2, info.value );
+ assertTrue( info.isSingleton );
+ }
+
+ function shouldBeSingletonViaParent() {
+ assertTrue( variables.factory.isSingleton( "two" ) );
+ }
+
+ function shouldHaveParentInMetadata() {
+ var info = variables.factory.getBeanInfo();
+ assertTrue( structKeyExists( info, "parent" ) );
+ assertEquals( variables.parent.getBeanInfo(), info.parent );
+ }
+
+}
View
0 declared/things/example.cfc → tests/declared/things/example.cfc
File renamed without changes.
View
17 tests/declared/things/myconfig.cfc
@@ -0,0 +1,17 @@
+component accessors=true {
+
+ property string name;
+
+ function init( string data = "none" ) {
+ setConfig( data );
+ }
+
+ function setConfig( string config ) {
+ variables.config = config;
+ }
+
+ function getConfig() {
+ return variables.config;
+ }
+
+}
View
13 extrabeans/sheep/item.cfc → tests/extrabeans/sheep/item.cfc
@@ -1,12 +1,13 @@
component accessors="true" {
property beanfactory;
param name="application.itemCount" default="0";
- this.itemNumber = ++application.itemCount;
- function init( any magicValue ) {
- this.magicValue = magicValue;
- return this;
- }
+
+ function init() {
+ this.itemNumber = ++application.itemCount;
+ }
+
function getNewItem() {
return variables.beanfactory.getBean( "item" );
}
-}
+
+}
View
0 model/services/fava.cfc → tests/model/beans/fava.cfc
File renamed without changes.
View
0 model/beans/pinto.cfc → tests/model/beans/pinto.cfc
File renamed without changes.
View
0 model/fish/user.cfc → tests/model/fish/user.cfc
File renamed without changes.
View
0 model/beans/fava.cfc → tests/model/services/fava.cfc
File renamed without changes.
View
0 model/services/product.cfc → tests/model/services/product.cfc
File renamed without changes.
View
0 services/user.cfc → tests/services/user.cfc
File renamed without changes.
View
3 views/main/default.cfm
@@ -14,5 +14,6 @@
<cfdump var="#bf.getBean('userfish').product.getUserService()#" label="bf.getBean('userfish').product.getUserService()"/>
<cfdump var="#bf.getBean('magicvalue')#" label="bf.getBean('magicvalue')"/>
<cfdump var="#bf.getBean('item')#" label="bf.getBean('item') - transient"/>
-<cfset bf.getBean('myconfig').explain() />
+<cfset bf.getBean('myconfig1').explain() />
+<cfset bf.getBean('myconfig2').explain() />
<cfdump var="#bf.getBeanInfo()#"/>

0 comments on commit 2ae085c

Please sign in to comment.
Something went wrong with that request. Please try again.