Skip to content

Commit 32331ee

Browse files
committed
Test all ORMs and override equals for Device
1 parent d82e780 commit 32331ee

File tree

4 files changed

+123
-8
lines changed

4 files changed

+123
-8
lines changed

src/main/java/nl/bascoder/keymanager/entity/Device.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,26 @@ public void setOwner(Owner owner) {
7575
public String toString() {
7676
return name;
7777
}
78-
}
7978

79+
@Override
80+
public boolean equals(Object o) {
81+
if (this == o) return true;
82+
if (!(o instanceof Device)) return false;
83+
84+
Device device = (Device) o;
85+
86+
if (id != device.id) return false;
87+
if (name != null ? !name.equals(device.name) : device.name != null) return false;
88+
if (owner != null ? !owner.equals(device.owner) : device.owner != null) return false;
89+
90+
return true;
91+
}
92+
93+
@Override
94+
public int hashCode() {
95+
int result = id;
96+
result = 31 * result + (name != null ? name.hashCode() : 0);
97+
result = 31 * result + (owner != null ? owner.hashCode() : 0);
98+
return result;
99+
}
100+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package nl.bascoder.keymanager.test.orm;
2+
3+
import com.j256.ormlite.dao.Dao;
4+
import com.j256.ormlite.dao.DaoManager;
5+
import com.j256.ormlite.support.ConnectionSource;
6+
import nl.bascoder.keymanager.DatabaseManager;
7+
import nl.bascoder.keymanager.entity.Device;
8+
import nl.bascoder.keymanager.entity.Owner;
9+
import org.junit.After;
10+
import org.junit.Assert;
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
14+
import java.util.logging.Level;
15+
import java.util.logging.Logger;
16+
17+
/**
18+
* Created by Bas for project licenseKey-manager.
19+
*
20+
* @author bascoder
21+
* @since 12-7-2015
22+
*/
23+
public class DeviceTestCase {
24+
Device mDevice;
25+
Dao<Device, Integer> mDao;
26+
Dao<Owner, Integer> mOwnerDao;
27+
28+
@Before
29+
public void setUp() throws Exception {
30+
mDevice = new Device();
31+
mDevice.setName("Test device1");
32+
33+
final ConnectionSource connectionSource
34+
= DatabaseManager.getInstance().getConnectionSource();
35+
mDao = DaoManager.createDao(connectionSource, Device.class);
36+
mOwnerDao = DaoManager.createDao(connectionSource, Owner.class);
37+
38+
Owner owner = new Owner();
39+
owner.setName("Test owner1");
40+
mDevice.setOwner(owner);
41+
42+
mOwnerDao.create(owner);
43+
mDao.create(mDevice);
44+
}
45+
46+
@After
47+
public void tearDown() throws Exception {
48+
try {
49+
mOwnerDao.delete(mDevice.getOwner());
50+
mDao.delete(mDevice);
51+
} catch (Exception e) {
52+
Logger.getGlobal().log(Level.WARNING, "Could not clean up device", e);
53+
}
54+
}
55+
56+
@Test
57+
public void testCreate() throws Exception {
58+
Assert.assertNotNull(mDevice);
59+
Assert.assertNotNull(mDao);
60+
}
61+
62+
@Test
63+
public void testRead() throws Exception {
64+
int id = mDevice.getId();
65+
66+
Device queried = mDao.queryForId(id);
67+
68+
Assert.assertEquals(mDevice.getId(), queried.getId());
69+
}
70+
71+
@Test
72+
public void testUpdate() throws Exception {
73+
final String NEW_NAME = "test device 2";
74+
mDevice.setName(NEW_NAME);
75+
76+
mDao.update(mDevice);
77+
78+
Device queried = mDao.queryForSameId(mDevice);
79+
80+
Assert.assertEquals(NEW_NAME, queried.getName());
81+
}
82+
83+
@Test
84+
public void testDelete() throws Exception {
85+
Device toDelete = new Device();
86+
toDelete.setName("test device to delete1");
87+
toDelete.setOwner(mDevice.getOwner());
88+
89+
mDao.create(toDelete);
90+
int id = toDelete.getId();
91+
92+
Assert.assertNotNull(mDao.queryForId(id));
93+
mDao.delete(toDelete);
94+
95+
Assert.assertNull(mDao.queryForId(id));
96+
}
97+
}

test/nl/bascoder/keymanager/test/orm/KeyTestCase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void tearDown() throws Exception {
4343
try {
4444
mKeyDao.delete(mKey);
4545
} catch (Exception e) {
46-
Logger.getGlobal().log(Level.WARNING, "Could not clean up owner", e);
46+
Logger.getGlobal().log(Level.WARNING, "Could not clean up key", e);
4747
}
4848
}
4949

test/nl/bascoder/keymanager/test/orm/OwnerTestCase.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import com.j256.ormlite.dao.Dao;
55
import com.j256.ormlite.dao.DaoManager;
66
import com.j256.ormlite.support.ConnectionSource;
7-
8-
import org.junit.Assert;
9-
7+
import nl.bascoder.keymanager.DatabaseManager;
8+
import nl.bascoder.keymanager.entity.Owner;
109
import org.junit.After;
10+
import org.junit.Assert;
1111
import org.junit.Before;
1212
import org.junit.Test;
1313

@@ -16,9 +16,6 @@
1616
import java.util.logging.Level;
1717
import java.util.logging.Logger;
1818

19-
import nl.bascoder.keymanager.DatabaseManager;
20-
import nl.bascoder.keymanager.entity.Owner;
21-
2219
/**
2320
* Created by Bas on 7-6-2015.
2421
*

0 commit comments

Comments
 (0)