[atunit commit] r18 - in trunk: . example/atunit/example example/atunit/example/subjects lib/core/java2html

0 views
Skip to first unread message

codesite...@google.com

unread,
Nov 11, 2007, 5:20:05 PM11/11/07
to atunit...@googlegroups.com
Author: logan.johnson
Date: Sun Nov 11 14:19:10 2007
New Revision: 18

Added:
trunk/example/atunit/example/subjects/
trunk/example/atunit/example/subjects/GuiceUser.java
trunk/example/atunit/example/subjects/GuiceUserManager.java
trunk/example/atunit/example/subjects/Logger.java
trunk/example/atunit/example/subjects/User.java
trunk/example/atunit/example/subjects/UserDao.java
trunk/example/atunit/example/subjects/UserManager.java
trunk/example/atunit/example/subjects/UserManagerImpl.java
trunk/lib/core/java2html/
trunk/lib/core/java2html/java2html.jar (contents, props changed)
Modified:
trunk/build.xml
trunk/example/atunit/example/ExampleAtUnitTest.java
trunk/example/atunit/example/ExampleEasyMockTest.java
trunk/example/atunit/example/ExampleGuiceAndJMockTest.java
trunk/example/atunit/example/ExampleGuiceTest.java
trunk/example/atunit/example/ExampleJMockTest.java
trunk/example/atunit/example/ExampleSpringEasyMockTest.java
trunk/example/atunit/example/ExampleSpringEasyMockTest.xml
trunk/example/atunit/example/ExampleSpringTest.java
trunk/example/atunit/example/ExampleSpringTest.xml
trunk/example/atunit/example/ExampleSpringWithContextLocationTest.java
trunk/example/atunit/example/ExampleSpringWithoutXmlTest.java

Log:
- Cleaned up examples so they're easier to read and compare.
- added a build task to generate javadoc from everything and
syntax-highlighted html from the examples.

Modified: trunk/build.xml
==============================================================================
--- trunk/build.xml (original)
+++ trunk/build.xml Sun Nov 11 14:19:10 2007
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
-<project name="atunit" default="jar">
+<project name="atunit" default="dist">

<property name="version" value="0.6"/>

@@ -37,10 +37,44 @@
<fileset dir="${test.dir}" includes="**/*.xml"/>
</copy>
</target>
+
+ <target name="docs" depends="compile">
+ <javadoc
+ access="public"
+ author="true"
+ classpathref="compile.classpath"
+ destdir="${build.dir}/doc"
+ nodeprecated="false"
+ nodeprecatedlist="false"
+ noindex="false"
+ nonavbar="false"
+ notree="false"
+ source="1.5"
+ sourcepath="${src.dir}"
+ splitindex="true"
+ use="true"
+ version="true">
+ <link href="http://junit.sourceforge.net/javadoc/"/>
+ <link href="http://java.sun.com/j2se/1.5.0/docs/api/"/>
+ </javadoc>
+
+ <taskdef name="java2html"
+ classname="de.java2html.anttasks.Java2HtmlTask"
+ classpathref="compile.classpath"
+ />
+ <java2html
+ srcdir="${example.dir}"
+ destdir="${build.dir}/doc"
+ outputFormat="xhtml11"
+ tabs="4"
+ addLineAnchors="true"/>
+ </target>

<target name="jar" depends="compile,test" description="package into jars">
<jar destfile="${build.dir}/${ant.project.name}-${version}.jar" basedir="${build.dir}/classes"/>
</target>
+
+ <target name="dist" depends="clean,jar,docs"/>

<target name="test" depends="compile" description="execute unit tests">
<delete dir="${build.dir}/testoutput"/>

Modified: trunk/example/atunit/example/ExampleAtUnitTest.java
==============================================================================
--- trunk/example/atunit/example/ExampleAtUnitTest.java (original)
+++ trunk/example/atunit/example/ExampleAtUnitTest.java Sun Nov 11
14:19:10 2007
@@ -1,19 +1,3 @@
-/**
- * Copyright (C) 2007 Logan Johnson
- *
- * 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.
- */
-
package atunit.example;

import static org.junit.Assert.*;
@@ -22,34 +6,35 @@
import org.junit.Test;
import org.junit.runner.RunWith;

-import atunit.AtUnit;
-import atunit.Unit;
-
-
+import atunit.*;
+import atunit.example.subjects.User;

/**
* This example shows AtUnit at its most basic: No mocks, no container.
- * @author logan
- *
*/
-@RunWith(AtUnit.class)
+@RunWith(AtUnit.class) // tell JUnit to use AtUnit
public class ExampleAtUnitTest {

- /**
- * You must have exactly one field annotated with {@link Unit}. This
is the
- * unit under test. The annotation really only serves as encouragement
- * of good testing practice.
- */
- @Unit String unit;
+ /*
+ * An AtUnit test must have exactly one field annotated with @Unit to
+ * indicate the unit under test. (The @Unit annotation doesn't do anything,
+ * it just serves to encourage good testing practice.)
+ */
+ @Unit User user;

@Before
public void setUp() {
- unit = "set";
+ user = new User(123, "testusername");
+ }
+
+ @Test
+ public void testGetId() {
+ assertEquals(123, user.getId().intValue());
}

@Test
- public void testUnit() {
- assertEquals("set", unit);
+ public void testGetUsername() {
+ assertEquals("testusername", user.getUsername());
}

-}
+}
\ No newline at end of file

Modified: trunk/example/atunit/example/ExampleEasyMockTest.java
==============================================================================
--- trunk/example/atunit/example/ExampleEasyMockTest.java (original)
+++ trunk/example/atunit/example/ExampleEasyMockTest.java Sun Nov 11
14:19:10 2007
@@ -1,19 +1,3 @@
-/**
- * Copyright (C) 2007 Logan Johnson
- *
- * 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.
- */
-
package atunit.example;

import static org.easymock.EasyMock.*;
@@ -23,56 +7,39 @@
import org.junit.Test;
import org.junit.runner.RunWith;

-import atunit.AtUnit;
-import atunit.Mock;
-import atunit.MockFramework;
-import atunit.Unit;
+import atunit.*;
+import atunit.example.subjects.*;

/**
* This example demonstrates AtUnit's EasyMock integration.
*
- * Note the MockFramework annotation which tells AtUnit to use EasyMock.
- *
- * @author Logan Johnson <logan....@gmail.com>
*/
@RunWith(AtUnit.class)
-@MockFramework(MockFramework.Option.EASYMOCK)
+@MockFramework(MockFramework.Option.EASYMOCK) // tells AtUnit to use EasyMock
public class ExampleEasyMockTest {

- @Unit Manager manager;
- @Mock DAO dao;
+ @Unit UserManagerImpl manager;
+ User fred;
+
+ /*
+ * Any field annotated with @Mock will be populated automatically by AtUnit
+ * with a mock object provided by EasyMock.
+ */
+ @Mock UserDao dao;
+ @Stub Logger log;

@Before
public void setUp() {
- manager = new Manager(dao);
+ manager = new UserManagerImpl(dao, log);
+ fred = new User(1, "Fred");
}

@Test
public void testGetStringForSomeReason() {
- expect(dao.getString()).andReturn("test string");
+ expect(dao.load(1)).andReturn(fred);
replay(dao);
-
- assertEquals("test string", manager.getAStringForSomeReason());
-
+ assertSame(fred, manager.getUser(1));
verify(dao);
- }
-
-
- protected static class Manager {
-
- final DAO dao;
-
- public Manager(DAO dao) {
- this.dao = dao;
- }
-
- public String getAStringForSomeReason() {
- return dao.getString();
- }
- }
-
- protected static interface DAO {
- public String getString();
}

}

Modified: trunk/example/atunit/example/ExampleGuiceAndJMockTest.java
==============================================================================
--- trunk/example/atunit/example/ExampleGuiceAndJMockTest.java (original)
+++ trunk/example/atunit/example/ExampleGuiceAndJMockTest.java Sun Nov
11 14:19:10 2007
@@ -1,18 +1,3 @@
-/**
- * Copyright (C) 2007 Logan Johnson
- *
- * 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.
- */

package atunit.example;

@@ -23,100 +8,60 @@
import org.junit.Test;
import org.junit.runner.RunWith;

+import atunit.*;
+import atunit.example.subjects.*;

-
-import atunit.AtUnit;
-import atunit.Container;
-import atunit.Mock;
-import atunit.MockFramework;
-import atunit.Stub;
-import atunit.Unit;
-
-import com.google.inject.Binder;
import com.google.inject.Inject;
-import com.google.inject.Module;
-import com.google.inject.name.Named;
-import com.google.inject.name.Names;


/**
- * This example shows the full power of AtUnit. Mock objects are
supplied by
- * JMock, just as in {@link ExampleJMockTest}. Dependency injection is provided
- * by Guice, just as in ExampleGuiceTest.
+ * This example demonstrates the combined integration of Guice and
JMock. The
+ * combined integration of a mock framework and a dependency injection container
+ * is where AtUnit really shines, because it allows you to simply
declare some
+ * fields and start testing.
+ *
+ * See ExampleGuiceTest and ExampleJMockTest for introductions to
AtUnit's Guice
+ * and JMock support.
*
- * Any fields created by the MockFramework and not injected by the Container
- * will be injected by AtUnit itself, so there's usually no need to
use Guice
- * annotations on your Mockery, mocks, or stubs.
+ * Please note also that any Container and MockFramework can be used together.
*
- * @author Logan Johnson <logan....@gmail.com>
*/
@RunWith(AtUnit.class)
@Container(Container.Option.GUICE)
@MockFramework(MockFramework.Option.JMOCK)
-public class ExampleGuiceAndJMockTest implements Module {
-
- @Inject @Unit ExampleClass myUnit;
+public class ExampleGuiceAndJMockTest {
+
+ @Inject @Unit GuiceUserManager manager;
+ @Inject User emptyUser;

+ /*
+ * Just as it does when no Container is used, AtUnit creates the
Mockery and
+ * uses JMock to create any @Mock or @Stub fields. However, once they're
+ * created, AtUnit binds them into the Guice injector so that Guice can
+ * inject them anywhere they're needed.
+ *
+ * Since AtUnit creates these fields, you have two choices: You can mark
+ * them with @Inject, in which case they will be directly injected by Guice
+ * like any other field; or you can forego @Inject and AtUnit will
set them
+ * after Guice is finished. Since you very rarely need the full power of
+ * Guice when actually setting these kinds of fields, you can usually do
+ * without @Inject on them.
+ *
+ */
Mockery mockery;
- @Mock ExampleInterface myMock;
- @Stub IgnoredInterface ignored;
-
- @Inject @Named("field") String setting;
-
+ @Mock UserDao dao;
+ @Stub Logger ignoredLogger;

- public void configure(Binder b) {
- b.bind(String.class).annotatedWith(Names.named("field")).toInstance("hooray");
- b.bind(String.class).annotatedWith(Names.named("non-field")).toInstance("yippee");
- }
-

@Test
- public void tInjection() {
+ public void testGetUser() {

- // verify that the injected Mockery works
mockery.checking(new Expectations() {{
- one (myMock).isAwesome();
- will(returnValue(true));
+ one (dao).load(with(equal(500)));
+ will(returnValue(emptyUser));
}});
- assertTrue(myMock.isAwesome());

- // verify that bindings from test class module are set
- assertEquals("hooray", setting);
-
- // verify that injection happens between fields
- assertSame(myMock, myUnit.getFace());
-
- // verify that injection happens between fields and non-field bindings
- assertEquals("yippee", myUnit.getString());
-
- // verify that annotation-driven expectations (currently
just 'ignored=true') are respected
- ignored.failIfNotIgnored();
- }
-
-
- public static interface IgnoredInterface {
- public void failIfNotIgnored();
+ assertSame(emptyUser, manager.getUser(500));
}

- public static class ExampleClass {
-
- final ExampleInterface face;
- final String string;
-
- @Inject
- public ExampleClass(ExampleInterface face, @Named("non-field")
String string) {
- this.face = face;
- this.string = string;
- }
- public ExampleInterface getFace() {
- return face;
- }
- public String getString() {
- return string;
- }
- }
-
- public static interface ExampleInterface {
- boolean isAwesome();
- }
}

Modified: trunk/example/atunit/example/ExampleGuiceTest.java
==============================================================================
--- trunk/example/atunit/example/ExampleGuiceTest.java (original)
+++ trunk/example/atunit/example/ExampleGuiceTest.java Sun Nov 11
14:19:10 2007
@@ -1,19 +1,3 @@
-/**
- * Copyright (C) 2007 Logan Johnson
- *
- * 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.
- */
-
package atunit.example;

import static org.junit.Assert.*;
@@ -21,54 +5,53 @@
import org.junit.Test;
import org.junit.runner.RunWith;

-import atunit.AtUnit;
-import atunit.Container;
-import atunit.Unit;
+import atunit.*;
+import atunit.example.subjects.*;

import com.google.inject.Binder;
import com.google.inject.Inject;
import com.google.inject.Module;
-import com.google.inject.name.Named;
import com.google.inject.name.Names;

/**
* This example demonstrates AtUnit's Guice integration.
*
- * Note the Container annotation which tells AtUnit to use Guice.
- *
* Fields are fully injected by Guice, and are themselves injected
into your
- * test. Your test does not have to implement Module, but if it does the
- * bindings it configures will also be used.
+ * test.
*
+ * An AtUnit test does *not* have to implement Module, but if it does the
+ * bindings it configures will also be used.
*/
@RunWith(AtUnit.class)
-@Container(Container.Option.GUICE)
+@Container(Container.Option.GUICE) // use Guice for dependency injection
public class ExampleGuiceTest implements Module {

- @Inject @Unit InjectedStringHolder holder;
- @Inject @Named("my string") String boundString;
-
+ @Inject @Unit GuiceUser user;
+
+ /*
+ * If your test does implement Module, the module configuration will be
+ * merged with any bindings created by AtUnit.
+ *
+ * This example is fairly contrived. Typically you'll just want to inject
+ * mock objects into your @Unit object, and AtUnit can do that without
+ * making you explicitly configure the bindings yourself. See
+ * ExampleGuiceAndJMockTest for an example.
+ *
+ */
public void configure(Binder b) {
- b.bind(String.class).annotatedWith(Names.named("my string")).toInstance("configured");
+ b.bind(String.class).annotatedWith(Names.named("user.name")).toInstance("fred");
+ b.bind(Integer.class).annotatedWith(Names.named("user.id")).toInstance(500);
}

+
@Test
- public void tGetString() {
- assertEquals("configured", boundString);
- assertEquals(boundString, holder.getString());
+ public void testGetId() {
+ assertEquals(500, user.getId().intValue());
}
-

- protected static class InjectedStringHolder {
- final String string;
-
- @Inject
- public InjectedStringHolder(@Named("my string") String stringToHold) {
- this.string = stringToHold;
- }
-
- public String getString() {
- return string;
- }
+ @Test
+ public void testGetUsername() {
+ assertEquals("fred", user.getUsername());
}
+
}

Modified: trunk/example/atunit/example/ExampleJMockTest.java
==============================================================================
--- trunk/example/atunit/example/ExampleJMockTest.java (original)
+++ trunk/example/atunit/example/ExampleJMockTest.java Sun Nov 11
14:19:10 2007
@@ -1,22 +1,6 @@
-/**
- * Copyright (C) 2007 Logan Johnson
- *
- * 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.
- */
-
package atunit.example;

-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;

import org.jmock.Expectations;
import org.jmock.Mockery;
@@ -24,71 +8,47 @@
import org.junit.Test;
import org.junit.runner.RunWith;

-import atunit.AtUnit;
-import atunit.Mock;
-import atunit.MockFramework;
-import atunit.Unit;
+import atunit.*;
+import atunit.example.subjects.*;

/**
- * This example shows AtUnit's JMock integration, with no container.
- *
- * Note the MockFramework annotation which tells AtUnit to use JMock.
- *
- * @author Logan Johnson <logan....@gmail.com>
+ * This example demonstrates AtUnit's JMock integration.
*/
@RunWith(AtUnit.class)
-@MockFramework(MockFramework.Option.JMOCK)
+@MockFramework(MockFramework.Option.JMOCK) // use JMock for mock objects
public class ExampleJMockTest {
-
- /**
- * You must declare exactly one field of type (or assignable from
type) {@link Mockery}.
+
+ @Unit UserManagerImpl manager;
+ User emptyUser;
+
+ /*
+ * You must declare exactly one field of type (or assignable from
type) Mockery.
*/
Mockery mockery;

- /**
- * Fields annotated with {@link Mock} are dynamically mocked and injected.
- * Just declare and you're done. An exception will be thrown if the field
- * type is not an interface.
- */
- @Mock ExampleInterface myMock;
-
- /**
- * As always, exactly one field must be declared the unit under test.
This field cannot be a mock.
+ /*
+ * Fields annotated with @Mock are mocked and injected. An exception
will be thrown
+ * if the field type is not an interface.
+ *
+ * A @Stub is just a @Mock which is ignored by JMock.
*/
- @Unit ExampleClass unit;
+ @Mock UserDao dao;
+ @Stub Logger log;
+

@Before
public void setUp() {
- unit = new ExampleClass(myMock);
+ manager = new UserManagerImpl(dao, log);
+ emptyUser = new User();
}

@Test
- public void exampleTest() {
-
- mockery.checking(new Expectations() {{
- one(myMock).isAwesome();
- will(returnValue(true));
+ public void testGetUser() {
+ mockery.checking(new Expectations() {{
+ one (dao).load(with(equal(500)));
+ will(returnValue(emptyUser));
}});
-
- assertTrue(unit.isAtUnitAwesome());
- }
-
-
- public static interface ExampleInterface {
- boolean isAwesome();
- }
-
- public static class ExampleClass {
-
- private ExampleInterface ei;
-
- public ExampleClass(ExampleInterface ei) {
- this.ei = ei;
- }
-
- public boolean isAtUnitAwesome() {
- return ei.isAwesome();
- }
+ assertSame(emptyUser, manager.getUser(500));
}


Modified: trunk/example/atunit/example/ExampleSpringEasyMockTest.java
==============================================================================
--- trunk/example/atunit/example/ExampleSpringEasyMockTest.java (original)
+++ trunk/example/atunit/example/ExampleSpringEasyMockTest.java Sun Nov
11 14:19:10 2007
@@ -1,90 +1,54 @@
-/**
- * Copyright (C) 2007 Logan Johnson
- *
- * 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.
- */
-
package atunit.example;

+import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;

-import org.easymock.EasyMock;
import org.junit.Test;
import org.junit.runner.RunWith;

-import atunit.AtUnit;
-import atunit.Container;
-import atunit.Mock;
-import atunit.MockFramework;
-import atunit.Unit;
+import atunit.*;
+import atunit.example.subjects.*;
import atunit.spring.Bean;

/**
- * Demonstrates use of Spring container in conjunction with a mock
framework, in this case EasyMock.
+ * This example demonstrates the combined integration of Spring and EasyMock.
+ *
+ * See ExampleGuiceAndJMockTest for notes on the advantages of
combined Container/MockFramework integration.
*
- * @author Logan Johnson <logan....@gmail.com>
*/
-
@RunWith(AtUnit.class)
@Container(Container.Option.SPRING)
@MockFramework(MockFramework.Option.EASYMOCK)
public class ExampleSpringEasyMockTest {

- /**
- * Beans are autowired by type, by default.
+ /*
+ * @Bean fields are autowired by type by default.
*/
- @Bean @Unit SimpleManager manager;
+ @Bean @Unit UserManagerImpl manager;
+ /*
+ * You can specify a name in the @Bean annotation, and the field will be
+ * wired to the bean of that name.
+ */
+ @Bean("fred") User fred;

- /**
+ /*
* Mocks and Stubs are automatically placed into the application context
- * like any other bean. If you also annotate them with Bean, you can give
- * them a bean name which will be used in the context.
+ * like any other bean, and are candidates for autowiring by type. If you
+ * also annotate them with Bean, you can give them a bean name which
will be
+ * used in the context.
*
- * In other words: Declare a Mock or Stub field, and it's just like you've
+ * In other words: Declare a @Mock or @Stub field, and it's just like you've
* put it in the XML.
*/
- @Bean("daoBean") @Mock SimpleDao dao;
-
+ @Bean("userDao") @Mock UserDao dao;
+ @Bean("log") @Stub Logger log;

@Test
- public void testMocksAndContext() {
- assertNotNull(manager);
- assertNotNull(dao);
- assertSame(dao, manager.getDao());
-
- EasyMock.expect(dao.loadString()).andReturn("loaded string");
- EasyMock.replay(dao);
-
- assertEquals("loaded string", manager.getString());
-
- EasyMock.verify(dao);
- }
-
- public static class SimpleManager {
- private SimpleDao dao;
- public void setDao(SimpleDao dao) {
- this.dao = dao;
- }
- public SimpleDao getDao() {
- return dao;
- }
- public String getString() {
- return dao.loadString();
- }
- }
-
- public static interface SimpleDao {
- public String loadString();
+ public void testGetUser() {
+ expect(dao.load(1)).andReturn(fred);
+ replay(dao);
+ assertSame(fred, manager.getUser(1));
+ verify(dao);
}

}

Modified: trunk/example/atunit/example/ExampleSpringEasyMockTest.xml
==============================================================================
--- trunk/example/atunit/example/ExampleSpringEasyMockTest.xml (original)
+++ trunk/example/atunit/example/ExampleSpringEasyMockTest.xml Sun Nov
11 14:19:10 2007
@@ -3,9 +3,14 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

-
- <bean id="mgr" class="atunit.example.ExampleSpringEasyMockTest$SimpleManager">
- <property name="dao" ref="daoBean"/> <!-- daoBean is a Mock,
defined in the test class and imported here -->
+ <bean id="userManager" class="atunit.example.subjects.UserManagerImpl">
+ <constructor-arg ref="log"/>
+ <property name="userDao" ref="userDao"/>
</bean>
-
+
+ <bean id="fred" class="atunit.example.subjects.User">
+ <property name="id" value="500"/>
+ <property name="username" value="fred"/>
+ </bean>
+
</beans>

Modified: trunk/example/atunit/example/ExampleSpringTest.java
==============================================================================
--- trunk/example/atunit/example/ExampleSpringTest.java (original)
+++ trunk/example/atunit/example/ExampleSpringTest.java Sun Nov 11
14:19:10 2007
@@ -1,19 +1,3 @@
-/**
- * Copyright (C) 2007 Logan Johnson
- *
- * 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.
- */
-
package atunit.example;

import static org.junit.Assert.*;
@@ -21,51 +5,41 @@
import org.junit.Test;
import org.junit.runner.RunWith;

-import atunit.AtUnit;
-import atunit.Container;
-import atunit.Unit;
+import atunit.*;
import atunit.spring.Bean;
+import atunit.example.subjects.User;

/**
- * Demonstrates automatic Spring configuration loading and injection
of beans
- * from the application context.
+ * This example demonstrates AtUnit's Spring integration.
*
- * By default, AtUnit will look for a configuration file named after
the test
- * class plus a ".xml" suffix, parallel to the test class in the
classpath. In
- * this example, AtUnit looks in the classpath for
- * atunit/spring/ExampleSpringTest.xml.
- *
- * @author Logan Johnson <logan....@gmail.com>
+ * Fields can be populated from the Spring context. By default, AtUnit
will look for
+ * a configuration file named after the test class plus a ".xml" suffix,
+ * parallel to the test class in the classpath. In this example,
AtUnit looks in
+ * the classpath for atunit/spring/ExampleSpringTest.xml.
*
*/
@RunWith(AtUnit.class)
@Container(Container.Option.SPRING)
public class ExampleSpringTest {

- /**
+ /*
* Any fields annotated with Bean will be injected from the context.
By default, annotated fields are
* autowired by type.
*/
- @Bean @Unit StringHolder holder;
+ @Bean @Unit User user;

- /**
- * If autowiring by type is not sufficient, you can specify the name
of the bean to inject.
+ /*
+ * If autowiring by type is not sufficient, you can request a bean by name.
*/
- @Bean("myString") String myString;
+ @Bean("username") String username;

@Test
- public void testInjectionFromContext() {
- assertEquals("string loaded from context", myString);
- assertSame(myString, holder.getString());
+ public void testGetId() {
+ assertEquals(500, user.getId().intValue());
}

- public static class StringHolder {
- private String string;
- public void setString(String string) {
- this.string = string;
- }
- public String getString() {
- return string;
- }
+ @Test
+ public void testGetUsername() {
+ assertEquals("fred", user.getUsername());
}
}

Modified: trunk/example/atunit/example/ExampleSpringTest.xml
==============================================================================
--- trunk/example/atunit/example/ExampleSpringTest.xml (original)
+++ trunk/example/atunit/example/ExampleSpringTest.xml Sun Nov 11
14:19:10 2007
@@ -4,12 +4,13 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">


- <bean id="myString" class="java.lang.String">
- <constructor-arg value="string loaded from context"/>
+ <bean id="user" class="atunit.example.subjects.User">
+ <constructor-arg value="500"/>
+ <constructor-arg ref="username"/>
</bean>

- <bean id="myStringHolder" class="atunit.example.ExampleSpringTest$StringHolder">
- <property name="string" ref="myString"/>
+ <bean id="username" class="java.lang.String">
+ <constructor-arg value="fred"/>
</bean>
-
+
</beans>

Modified: trunk/example/atunit/example/ExampleSpringWithContextLocationTest.java
==============================================================================
---
trunk/example/atunit/example/ExampleSpringWithContextLocationTest.java (original)
+++
trunk/example/atunit/example/ExampleSpringWithContextLocationTest.java
Sun Nov 11 14:19:10 2007
@@ -1,18 +1,3 @@
-/**
- * Copyright (C) 2007 Logan Johnson
- *
- * 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.
- */

package atunit.example;

@@ -21,36 +6,35 @@
import org.junit.Test;
import org.junit.runner.RunWith;

-import atunit.AtUnit;
-import atunit.Container;
-import atunit.Unit;
-import atunit.example.ExampleSpringTest.StringHolder;
+import atunit.*;
+import atunit.example.subjects.User;
import atunit.spring.Bean;
import atunit.spring.Context;

/**
- * If you want to manually specify the location of a Spring
configuration file to use,
- * you can do so with the Context annotation on your test class. This
test is exactly
- * the same as {@link ExampleSpringTest}, except for the Context annotation.
- *
- * Normally AtUnit would look for
ExampleSpringWithContextLocationTest.xml, but I've told
- * it to use ExampleSpringTest.xml instead.
- *
- * @author Logan Johnson <logan....@gmail.com>
+ * This example is exactly the same as ExampleSpringTest, except that it
+ * specifies the location of a context XML file to use.
*
*/
@RunWith(AtUnit.class)
@Container(Container.Option.SPRING)
+/*
+ * The @Context annotation tells AtUnit to use the named context XML file,
+ * instead of looking for a file named after the test class.
+ */
@Context("ExampleSpringTest.xml")
public class ExampleSpringWithContextLocationTest {

- @Bean @Unit StringHolder holder;
- @Bean("myString") String myString;
+ @Bean @Unit User user;
+ @Bean("username") String username;

@Test
- public void testInjectionFromContext() {
- assertEquals("string loaded from context", myString);
- assertSame(myString, holder.getString());
+ public void testGetId() {
+ assertEquals(500, user.getId().intValue());
}

+ @Test
+ public void testGetUsername() {
+ assertEquals("fred", user.getUsername());
+ }
}

Modified: trunk/example/atunit/example/ExampleSpringWithoutXmlTest.java
==============================================================================
--- trunk/example/atunit/example/ExampleSpringWithoutXmlTest.java (original)
+++ trunk/example/atunit/example/ExampleSpringWithoutXmlTest.java Sun
Nov 11 14:19:10 2007
@@ -1,97 +1,37 @@
-/**
- * Copyright (C) 2007 Logan Johnson
- *
- * 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.
- */
-
package atunit.example;

+import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;

-import org.easymock.EasyMock;
import org.junit.Test;
import org.junit.runner.RunWith;

-import atunit.AtUnit;
-import atunit.Container;
-import atunit.Mock;
-import atunit.MockFramework;
-import atunit.Unit;
+import atunit.*;
+import atunit.example.subjects.*;
import atunit.spring.Bean;

/**
- * Because fields annotated with Bean are taken from the Spring
context, and fields annotated with Bean and
- * Mock/Stub are placed into the Spring context, and all beans are
autowired by type by default, you very
- * frequently do not need a Spring XML file at all. Simply define the
Bean fields necessary to sufficiently
- * wire your test, and AtUnit will do the rest.
+ * This example shows Spring integration without an XML file.
*
- * This test is exactly the same as {@link ExampleSpringEasyMockTest},
with one exception: there is no Spring
- * configuration file.
+ * This test is the same as ExampleSpringEasyMockTest, except that
there is no XML file.
*
- * @author Logan Johnson <logan....@gmail.com>
- *
*/
@RunWith(AtUnit.class)
@Container(Container.Option.SPRING)
@MockFramework(MockFramework.Option.EASYMOCK)
public class ExampleSpringWithoutXmlTest {

- /**
- * Beans are autowired by type, by default.
- */
- @Bean @Unit SimpleManager manager;
-
- /**
- * Mocks and Stubs that are also annotated with Bean will be placed
into the application context like
- * any other bean. If a name is specified, it will be used. If no
name is specified, one will be generated.
- * Mock or Stub Beans are candidates for autowiring by type.
- *
- * In other words: Declare a Mock or Stub field, annotate it with
Bean, and it's just like you've put it
- * in the XML.
- *
- */
- @Bean("daoBean") @Mock SimpleDao dao;
-
+ @Bean @Unit UserManagerImpl manager;
+ @Bean("userDao") @Mock UserDao dao;
+ @Bean("log") @Stub Logger log;
+ @Bean("fred") User fred;

@Test
- public void testMocksAndContext() {
- assertNotNull(manager);
- assertNotNull(dao);
- assertSame(dao, manager.getDao());
-
- EasyMock.expect(dao.loadString()).andReturn("loaded string");
- EasyMock.replay(dao);
-
- assertEquals("loaded string", manager.getString());
-
- EasyMock.verify(dao);
- }
-
- public static class SimpleManager {
- private SimpleDao dao;
- public void setDao(SimpleDao dao) {
- this.dao = dao;
- }
- public SimpleDao getDao() {
- return dao;
- }
- public String getString() {
- return dao.loadString();
- }
- }
-
- public static interface SimpleDao {
- public String loadString();
+ public void testGetUser() {
+ expect(dao.load(1)).andReturn(fred);
+ replay(dao);
+ assertSame(fred, manager.getUser(1));
+ verify(dao);
}

}

Added: trunk/example/atunit/example/subjects/GuiceUser.java
==============================================================================
--- (empty file)
+++ trunk/example/atunit/example/subjects/GuiceUser.java Sun Nov 11
14:19:10 2007
@@ -0,0 +1,13 @@
+package atunit.example.subjects;
+
+import com.google.inject.Inject;
+import com.google.inject.name.Named;
+
+public class GuiceUser extends User {
+
+ @Inject
+ public GuiceUser(@Named("user.id") Integer id, @Named("user.name")
String username) {
+ super(id, username);
+ }
+
+}

Added: trunk/example/atunit/example/subjects/GuiceUserManager.java
==============================================================================
--- (empty file)
+++ trunk/example/atunit/example/subjects/GuiceUserManager.java Sun Nov
11 14:19:10 2007
@@ -0,0 +1,13 @@
+package atunit.example.subjects;
+
+import com.google.inject.Inject;
+
+public class GuiceUserManager extends UserManagerImpl {
+
+ @Inject
+ public GuiceUserManager(UserDao dao, Logger log) {
+ super(dao, log);
+ log.debug("GuiceUserManager instantiated");
+ }
+
+}

Added: trunk/example/atunit/example/subjects/Logger.java
==============================================================================
--- (empty file)
+++ trunk/example/atunit/example/subjects/Logger.java Sun Nov 11
14:19:10 2007
@@ -0,0 +1,9 @@
+package atunit.example.subjects;
+
+public interface Logger {
+
+ void debug(String msg);
+ void info(String msg);
+ void warn(String msg);
+ void error(String msg);
+}

Added: trunk/example/atunit/example/subjects/User.java
==============================================================================
--- (empty file)
+++ trunk/example/atunit/example/subjects/User.java Sun Nov 11 14:19:10 2007
@@ -0,0 +1,31 @@
+package atunit.example.subjects;
+
+public class User {
+
+ private Integer id;
+ private String username;
+
+ public User() {
+ }
+
+ public User(Integer id, String username) {
+ this.id = id;
+ this.username = username;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+ public Integer getId() {
+ return id;
+ }
+
+ public void setUsername(String username) {
+ this.username = username;
+ }
+ public String getUsername() {
+ return username;
+ }
+
+
+}

Added: trunk/example/atunit/example/subjects/UserDao.java
==============================================================================
--- (empty file)
+++ trunk/example/atunit/example/subjects/UserDao.java Sun Nov 11
14:19:10 2007
@@ -0,0 +1,7 @@
+package atunit.example.subjects;
+
+public interface UserDao {
+
+ public User load(Integer id);
+
+}

Added: trunk/example/atunit/example/subjects/UserManager.java
==============================================================================
--- (empty file)
+++ trunk/example/atunit/example/subjects/UserManager.java Sun Nov 11
14:19:10 2007
@@ -0,0 +1,7 @@
+package atunit.example.subjects;
+
+public interface UserManager {
+
+ User getUser(Integer id);
+
+}

Added: trunk/example/atunit/example/subjects/UserManagerImpl.java
==============================================================================
--- (empty file)
+++ trunk/example/atunit/example/subjects/UserManagerImpl.java Sun Nov
11 14:19:10 2007
@@ -0,0 +1,26 @@
+package atunit.example.subjects;
+
+public class UserManagerImpl implements UserManager {
+
+ private UserDao dao;
+ final Logger log;
+
+ public UserManagerImpl(UserDao dao, Logger log) {
+ this.dao = dao;
+ this.log = log;
+ }
+
+ public UserManagerImpl(Logger log) {
+ this.log = log;
+ }
+
+ public void setUserDao(UserDao dao) {
+ this.dao = dao;
+ }
+
+ public User getUser(Integer id) {
+ log.debug("user " + id + " retrieved");
+ return dao.load(id);
+ }
+
+}

Added: trunk/lib/core/java2html/java2html.jar
==============================================================================
Binary file. No diff available.

Reply all
Reply to author
Forward
0 new messages