Last active
December 26, 2015 07:39
-
-
Save ricston-git/7116654 to your computer and use it in GitHub Desktop.
Gists for groovy/maven/spock blog post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" | |
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:p="http://www.springframework.org/schema/p" | |
xmlns:tx="http://www.springframework.org/schema/tx" | |
xsi:schemaLocation=" | |
http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd | |
http://www.springframework.org/schema/context | |
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> | |
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"> | |
<property name="driverClassName" value="org.postgresql.Driver" /> | |
<property name="url" value="jdbc:postgresql://localhost:5432/tododbtest" /> | |
<property name="username" value="fillin" /> | |
<property name="password" value="fillin" /> | |
</bean> | |
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:context="http://www.springframework.org/schema/context" | |
xmlns:jdbc="http://www.springframework.org/schema/jdbc" | |
xsi:schemaLocation=" | |
http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd | |
http://www.springframework.org/schema/jdbc | |
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"> | |
<!--Intialize the database schema with test data --> | |
<jdbc:initialize-database data-source="dataSource"> | |
<jdbc:script location="classpath:schema.sql"/> | |
<jdbc:script location="classpath:test-data.sql"/> | |
</jdbc:initialize-database> | |
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '{id=3, email=anon@gmail.com, password=anon, registered=false, confirmationcode=codeA}' with class 'groovy.sql.GroovyRowResult' to class 'com.ricston.blog.sample.model.data.TodoUser' due to: org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack: No such property: confirmationcode for class: com.ricston.blog.sample.model.data.TodoUser | |
Possible solutions: confirmationCode | |
at com.ricston.blog.sample.model.dao.postgre.PostgreTodoUserDAO.findTodoUserByEmail(PostgreTodoUserDAO.groovy:23) | |
at com.ricston.blog.sample.model.spec.PostgreTodoUserDAOSpec.findTodoUserByEmail when user exists in db(PostgreTodoUserDAOSpec.groovy:37) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.ricston.blog.sample.model</groupId> | |
<artifactId>tododb-model</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<properties> | |
<spring.version>3.2.4.RELEASE</spring.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.codehaus.groovy</groupId> | |
<artifactId>groovy-all</artifactId> | |
<version>2.1.5</version> | |
</dependency> | |
<dependency> | |
<groupId>postgresql</groupId> | |
<artifactId>postgresql</artifactId> | |
<version>9.1-901.jdbc4</version> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.tomcat</groupId> | |
<artifactId>tomcat-jdbc</artifactId> | |
<version>7.0.42</version> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.tomcat</groupId> | |
<artifactId>tomcat-dbcp</artifactId> | |
<version>7.0.42</version> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>commons-lang</groupId> | |
<artifactId>commons-lang</artifactId> | |
<version>2.6</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-context</artifactId> | |
<version>${spring.version}</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-jdbc</artifactId> | |
<version>${spring.version}</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-test</artifactId> | |
<version>${spring.version}</version> | |
<scope>test</scope> | |
<exclusions> | |
<exclusion> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
</exclusion> | |
</exclusions> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-beans</artifactId> | |
<version>${spring.version}</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.spockframework</groupId> | |
<artifactId>spock-spring</artifactId> | |
<version>0.7-groovy-2.0</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.spockframework</groupId> | |
<artifactId>spock-core</artifactId> | |
<version>0.7-groovy-2.0</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>2.3.2</version> | |
<configuration> | |
<compilerId>groovy-eclipse-compiler</compilerId> | |
</configuration> | |
<dependencies> | |
<dependency> | |
<groupId>org.codehaus.groovy</groupId> | |
<artifactId>groovy-eclipse-compiler</artifactId> | |
<version>2.7.0-01</version> | |
</dependency> | |
</dependencies> | |
</plugin> | |
<plugin> | |
<groupId>org.codehaus.groovy</groupId> | |
<artifactId>groovy-eclipse-compiler</artifactId> | |
<version>2.7.0-01</version> | |
<extensions>true</extensions> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ricston.blog.sample.model.dao.postgre | |
import groovy.sql.Sql | |
import javax.sql.DataSource | |
import com.ricston.blog.sample.model.dao.TodoUserDAO | |
import com.ricston.blog.sample.model.data.TodoUser | |
class PostgreTodoUserDAO implements TodoUserDAO { | |
private Sql sql | |
public PostgreTodoUserDAO(DataSource dataSource) { | |
sql = new Sql(dataSource) | |
} | |
/** | |
* | |
* @param email | |
* @return the TodoUser with the given email | |
*/ | |
public TodoUser findTodoUserByEmail(String email) { | |
sql.firstRow """SELECT * FROM todouser WHERE email = $email""" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ricston.blog.sample.model.spec; | |
import javax.sql.DataSource | |
import org.springframework.beans.factory.annotation.Autowired | |
import org.springframework.test.annotation.DirtiesContext | |
import org.springframework.test.annotation.DirtiesContext.ClassMode | |
import org.springframework.test.context.ContextConfiguration | |
import spock.lang.Specification | |
import com.ricston.blog.sample.model.data.TodoUser | |
import com.ricston.blog.sample.model.dao.postgre.PostgreTodoUserDAO | |
// because it supplies a new application context after each test, the initialize-database in initdb.xml is | |
// executed for each test/specification | |
@DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD) | |
@ContextConfiguration('classpath:testContext.xml') | |
class PostgreTodoUserDAOSpec extends Specification { | |
@Autowired | |
DataSource dataSource | |
PostgreTodoUserDAO postgreTodoUserDAO | |
def setup() { | |
postgreTodoUserDAO = new PostgreTodoUserDAO(dataSource) | |
} | |
def "findTodoUserByEmail when user exists in db"() { | |
given: "a db populated with a TodoUser with email anon@gmail.com and the password given below" | |
String email = 'anon@gmail.com' | |
String password = 'anon' | |
when: "searching for a TodoUser with that email" | |
TodoUser user = postgreTodoUserDAO.findTodoUserByEmail email | |
then: "the row is found such that the user returned by findTodoUserByEmail has the correct password" | |
user.password == password | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP TABLE IF EXISTS todouser CASCADE; | |
CREATE TABLE todouser | |
( | |
id SERIAL, | |
email varchar(80) UNIQUE NOT NULL, | |
password varchar(80), | |
registered boolean DEFAULT FALSE, | |
confirmationCode varchar(280), | |
CONSTRAINT todouser_pkey PRIMARY KEY (id) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
insert into todouser (email, password, registered, confirmationCode) values ('abc.j123@gmail.com', 'abc123', FALSE, 'abcdefg') | |
insert into todouser (email, password, registered, confirmationCode) values ('def.123@gmail.com', 'pass1516', FALSE, '123456') | |
insert into todouser (email, password, registered, confirmationCode) values ('anon@gmail.com', 'anon', FALSE, 'codeA') | |
insert into todouser (email, password, registered, confirmationCode) values ('anon2@gmail.com', 'anon2', FALSE, 'codeB') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:context="http://www.springframework.org/schema/context" | |
xmlns:jdbc="http://www.springframework.org/schema/jdbc" | |
xmlns:p="http://www.springframework.org/schema/p" | |
xmlns:tx="http://www.springframework.org/schema/tx" | |
xsi:schemaLocation=" | |
http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd | |
http://www.springframework.org/schema/context | |
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> | |
<import resource="classpath:datasource.xml"/> | |
<import resource="classpath:initdb.xml"/> | |
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sample.data | |
import org.apache.commons.lang.builder.ToStringBuilder | |
class TodoUser { | |
long id; | |
String email; | |
String password; | |
String confirmationCode; | |
boolean registered; | |
@Override | |
public String toString() { | |
ToStringBuilder.reflectionToString(this); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def propertyMissing(String name, value) { | |
if(isConfirmationCode(name)) { | |
this.confirmationCode = value | |
} else { | |
unknownProperty(name) | |
} | |
} | |
def propertyMissing(String name) { | |
if(isConfirmationCode(name)) { | |
return confirmationCode | |
} else { | |
unknownProperty(name) | |
} | |
} | |
private boolean isConfirmationCode(String name) { | |
'confirmation_code'.equals(name) | |
} | |
def unknownProperty(String name) { | |
throw new MissingPropertyException(name, this.class) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ricston.blog.sample.model.dao; | |
import com.ricston.blog.sample.model.data.TodoUser; | |
public interface TodoUserDAO { | |
/** | |
* | |
* @param email | |
* @return the TodoUser with the given email | |
*/ | |
public TodoUser findTodoUserByEmail(String email); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment