Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
daaexample
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Iago Gómez Salgado
daaexample
Commits
a5f9bd9e
Commit
a5f9bd9e
authored
Mar 11, 2017
by
Iago Gómez Salgado
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add PetDAO tests
parent
bc5b27b1
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
318 additions
and
0 deletions
+318
-0
PetsDAOTest.java
src/test/java/es/uvigo/esei/daa/dao/PetsDAOTest.java
+125
-0
PetsDAOUnitTest.java
src/test/java/es/uvigo/esei/daa/dao/PetsDAOUnitTest.java
+193
-0
No files found.
src/test/java/es/uvigo/esei/daa/dao/PetsDAOTest.java
0 → 100644
View file @
a5f9bd9e
package
es
.
uvigo
.
esei
.
daa
.
dao
;
import
static
es
.
uvigo
.
esei
.
daa
.
dataset
.
PetsDataset
.*;
import
static
es
.
uvigo
.
esei
.
daa
.
matchers
.
IsEqualToPet
.*;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
javax.sql.DataSource
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.TestExecutionListeners
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
com.github.springtestdbunit.DbUnitTestExecutionListener
;
import
com.github.springtestdbunit.annotation.DatabaseSetup
;
import
com.github.springtestdbunit.annotation.ExpectedDatabase
;
import
es.uvigo.esei.daa.entities.Pet
;
import
es.uvigo.esei.daa.listeners.ApplicationContextBinding
;
import
es.uvigo.esei.daa.listeners.ApplicationContextJndiBindingTestExecutionListener
;
import
es.uvigo.esei.daa.listeners.DbManagement
;
import
es.uvigo.esei.daa.listeners.DbManagementTestExecutionListener
;
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@ContextConfiguration
(
"classpath:contexts/mem-context.xml"
)
@TestExecutionListeners
({
DbUnitTestExecutionListener
.
class
,
DbManagementTestExecutionListener
.
class
,
ApplicationContextJndiBindingTestExecutionListener
.
class
})
@ApplicationContextBinding
(
jndiUrl
=
"java:/comp/env/jdbc/daaexample"
,
type
=
DataSource
.
class
)
@DbManagement
(
create
=
"classpath:db/hsqldb.sql"
,
drop
=
"classpath:db/hsqldb-drop.sql"
)
@DatabaseSetup
(
"/datasets/dataset.xml"
)
@ExpectedDatabase
(
"/datasets/dataset.xml"
)
public
class
PetsDAOTest
{
private
PetsDAO
dao
;
@Before
public
void
setUp
()
throws
Exception
{
this
.
dao
=
new
PetsDAO
();
}
@Test
public
void
testList
()
throws
DAOException
{
assertThat
(
this
.
dao
.
list
(),
containsPetsInAnyOrder
(
pets
()));
}
@Test
public
void
testGet
()
throws
DAOException
{
final
Pet
pet
=
this
.
dao
.
get
(
existentId
());
assertThat
(
pet
,
is
(
equalsToPet
(
existentPet
())));
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
testGetNonExistentId
()
throws
DAOException
{
this
.
dao
.
get
(
nonExistentId
());
}
@Test
@ExpectedDatabase
(
"/datasets/dataset-delete.xml"
)
public
void
testDelete
()
throws
DAOException
{
this
.
dao
.
delete
(
existentId
());
assertThat
(
this
.
dao
.
list
(),
containsPetsInAnyOrder
(
petWithout
(
existentId
())));
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
testDeleteNonExistentId
()
throws
DAOException
{
this
.
dao
.
delete
(
nonExistentId
());
}
@Test
@ExpectedDatabase
(
"/datasets/dataset-modify.xml"
)
public
void
testModify
()
throws
DAOException
{
final
Pet
pet
=
existentPet
();
pet
.
setId
(
newId
());
pet
.
setName
(
newName
());
pet
.
setKind
(
newKind
());
pet
.
setBreed
(
newBreed
());
pet
.
setOwner
(
newOwner
());
this
.
dao
.
modify
(
pet
);
final
Pet
persistentPet
=
this
.
dao
.
get
(
pet
.
getId
());
assertThat
(
persistentPet
,
is
(
equalsToPet
(
pet
)));
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
testModifyNonExistentId
()
throws
DAOException
{
this
.
dao
.
modify
(
nonExistentPet
());
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
testModifyNullPet
()
throws
DAOException
{
this
.
dao
.
modify
(
null
);
}
@Test
@ExpectedDatabase
(
"/datasets/dataset-add.xml"
)
public
void
testAdd
()
throws
DAOException
{
final
Pet
pet
=
this
.
dao
.
add
(
new
Pet
(
newId
(),
newName
(),
newKind
(),
newBreed
(),
newOwner
()));
assertThat
(
pet
,
is
(
equalsToPet
(
newPet
())));
final
Pet
persistentPet
=
this
.
dao
.
get
(
pet
.
getId
());
assertThat
(
persistentPet
,
is
(
equalsToPet
(
newPet
())));
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
testAddNullPet
()
throws
DAOException
{
this
.
dao
.
add
(
null
);
}
}
src/test/java/es/uvigo/esei/daa/dao/PetsDAOUnitTest.java
0 → 100644
View file @
a5f9bd9e
package
es
.
uvigo
.
esei
.
daa
.
dao
;
import
static
es
.
uvigo
.
esei
.
daa
.
dataset
.
PetsDataset
.*;
import
static
es
.
uvigo
.
esei
.
daa
.
matchers
.
IsEqualToPet
.*;
import
static
org
.
easymock
.
EasyMock
.
anyString
;
import
static
org
.
easymock
.
EasyMock
.
eq
;
import
static
org
.
easymock
.
EasyMock
.
expect
;
import
static
org
.
easymock
.
EasyMock
.
reset
;
import
static
org
.
hamcrest
.
CoreMatchers
.
equalTo
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
java.sql.SQLException
;
import
org.junit.Test
;
import
com.mysql.jdbc.Statement
;
import
es.uvigo.esei.daa.entities.Pet
;
import
es.uvigo.esei.daa.util.DatabaseQueryUnitTest
;
public
class
PetsDAOUnitTest
extends
DatabaseQueryUnitTest
{
@Test
public
void
testList
()
throws
Exception
{
final
Pet
[]
pets
=
pets
();
for
(
Pet
pet
:
pets
)
{
expectPetRow
(
pet
);
}
expect
(
result
.
next
()).
andReturn
(
false
);
result
.
close
();
replayAll
();
final
PetsDAO
petsDAO
=
new
PetsDAO
();
assertThat
(
petsDAO
.
list
(),
containsPetsInAnyOrder
(
pets
));
}
@Test
(
expected
=
DAOException
.
class
)
public
void
testListUnexpectedException
()
throws
Exception
{
expect
(
result
.
next
()).
andThrow
(
new
SQLException
());
result
.
close
();
replayAll
();
final
PetsDAO
petsDAO
=
new
PetsDAO
();
petsDAO
.
list
();
}
@Test
public
void
testGet
()
throws
Exception
{
final
Pet
existentPet
=
existentPet
();
expectPetRow
(
existentPet
);
result
.
close
();
replayAll
();
final
PetsDAO
petsDAO
=
new
PetsDAO
();
assertThat
(
petsDAO
.
get
(
existentId
()),
is
(
equalTo
(
existentPet
)));
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
testGetMissing
()
throws
Exception
{
expect
(
result
.
next
()).
andReturn
(
false
);
result
.
close
();
replayAll
();
final
PetsDAO
petsDAO
=
new
PetsDAO
();
petsDAO
.
get
(
existentId
());
}
@Test
(
expected
=
DAOException
.
class
)
public
void
testGetUnexpectedException
()
throws
Exception
{
expect
(
result
.
next
()).
andThrow
(
new
SQLException
());
result
.
close
();
replayAll
();
final
PetsDAO
petsDAO
=
new
PetsDAO
();
petsDAO
.
get
(
existentId
());
}
@Test
public
void
testAdd
()
throws
Exception
{
final
Pet
pet
=
newPet
();
reset
(
connection
);
expect
(
connection
.
prepareStatement
(
anyString
(),
eq
(
Statement
.
RETURN_GENERATED_KEYS
)))
.
andReturn
(
statement
);
expect
(
statement
.
executeUpdate
()).
andReturn
(
1
);
connection
.
close
();
replayAll
();
final
PetsDAO
petsDAO
=
new
PetsDAO
();
final
Pet
newPet
=
petsDAO
.
add
(
new
Pet
(
pet
.
getId
(),
pet
.
getName
(),
pet
.
getKind
(),
pet
.
getBreed
(),
pet
.
getOwner
()));
assertThat
(
newPet
,
is
(
equalsToPet
(
pet
)));
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
testAddNullPet
()
throws
Exception
{
replayAll
();
final
PetsDAO
petsDAO
=
new
PetsDAO
();
resetAll
();
// No expectations
petsDAO
.
add
(
null
);
}
@Test
public
void
testDelete
()
throws
Exception
{
expect
(
statement
.
executeUpdate
()).
andReturn
(
1
);
replayAll
();
final
PetsDAO
petsDAO
=
new
PetsDAO
();
petsDAO
.
delete
(
existentId
());
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
testDeleteInvalidId
()
throws
Exception
{
expect
(
statement
.
executeUpdate
()).
andReturn
(
0
);
replayAll
();
final
PetsDAO
petsDAO
=
new
PetsDAO
();
petsDAO
.
delete
(
existentId
());
}
@Test
(
expected
=
DAOException
.
class
)
public
void
testDeleteUnexpectedException
()
throws
Exception
{
expect
(
statement
.
executeUpdate
()).
andThrow
(
new
SQLException
());
replayAll
();
final
PetsDAO
petsDAO
=
new
PetsDAO
();
petsDAO
.
delete
(
existentId
());
}
@Test
public
void
testModify
()
throws
Exception
{
expect
(
statement
.
executeUpdate
()).
andReturn
(
1
);
replayAll
();
final
PetsDAO
petsDAO
=
new
PetsDAO
();
petsDAO
.
modify
(
existentPet
());
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
testModifyNullPet
()
throws
Exception
{
replayAll
();
final
PetsDAO
petsDAO
=
new
PetsDAO
();
resetAll
();
// No expectations
petsDAO
.
modify
(
null
);
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
testModifyZeroUpdatedRows
()
throws
Exception
{
expect
(
statement
.
executeUpdate
()).
andReturn
(
0
);
replayAll
();
final
PetsDAO
petsDAO
=
new
PetsDAO
();
petsDAO
.
modify
(
existentPet
());
}
@Test
(
expected
=
DAOException
.
class
)
public
void
testModifyUnexpectedException
()
throws
Exception
{
expect
(
statement
.
executeUpdate
()).
andThrow
(
new
SQLException
());
replayAll
();
final
PetsDAO
petsDAO
=
new
PetsDAO
();
petsDAO
.
modify
(
existentPet
());
}
private
void
expectPetRow
(
Pet
pet
)
throws
SQLException
{
expect
(
result
.
next
()).
andReturn
(
true
);
expect
(
result
.
getInt
(
"id"
)).
andReturn
(
pet
.
getId
());
expect
(
result
.
getString
(
"name"
)).
andReturn
(
pet
.
getName
());
expect
(
result
.
getString
(
"kind"
)).
andReturn
(
pet
.
getKind
());
expect
(
result
.
getString
(
"breed"
)).
andReturn
(
pet
.
getBreed
());
expect
(
result
.
getInt
(
"owner"
)).
andReturn
(
pet
.
getOwner
());
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment