Skip to content

Commit 697f6b4

Browse files
committed
Use new Assert class and add unit tests for Key and override equals for Key
1 parent d4928e3 commit 697f6b4

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

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

+22
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,27 @@ public void setDevice(Device device) {
9797
public String toString() {
9898
return licenseKey;
9999
}
100+
101+
@Override
102+
public boolean equals(Object o) {
103+
if (this == o) return true;
104+
if (!(o instanceof Key)) return false;
105+
106+
Key key = (Key) o;
107+
108+
if (id != key.id) return false;
109+
if (inUse != key.inUse) return false;
110+
if (licenseKey != null ? !licenseKey.equals(key.licenseKey) : key.licenseKey != null) return false;
111+
112+
return true;
113+
}
114+
115+
@Override
116+
public int hashCode() {
117+
int result = id;
118+
result = 31 * result + (licenseKey != null ? licenseKey.hashCode() : 0);
119+
result = 31 * result + (inUse ? 1 : 0);
120+
return result;
121+
}
100122
}
101123

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package nl.bascoder.keymanager.test.orm;
2+
3+
4+
import com.j256.ormlite.dao.Dao;
5+
import com.j256.ormlite.dao.DaoManager;
6+
import com.j256.ormlite.support.ConnectionSource;
7+
import nl.bascoder.keymanager.DatabaseManager;
8+
import nl.bascoder.keymanager.entity.Key;
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.UUID;
15+
import java.util.logging.Level;
16+
import java.util.logging.Logger;
17+
18+
/**
19+
* Created by Bas for project licenseKey-manager.
20+
*
21+
* @author bascoder
22+
* @since 12-7-2015
23+
*/
24+
public class KeyTestCase {
25+
26+
private Key mKey;
27+
private Dao<Key, Integer> mKeyDao;
28+
29+
@Before
30+
public void setUp() throws Exception {
31+
mKey = new Key();
32+
mKey.setLicenseKey("test key");
33+
34+
final ConnectionSource connectionSource
35+
= DatabaseManager.getInstance().getConnectionSource();
36+
37+
mKeyDao = DaoManager.createDao(connectionSource, Key.class);
38+
mKeyDao.create(mKey);
39+
}
40+
41+
@After
42+
public void tearDown() throws Exception {
43+
try {
44+
mKeyDao.delete(mKey);
45+
} catch (Exception e) {
46+
Logger.getGlobal().log(Level.WARNING, "Could not clean up owner", e);
47+
}
48+
}
49+
50+
@Test
51+
public void testCreate() throws Exception {
52+
Assert.assertNotNull(mKey);
53+
Assert.assertNotNull(mKeyDao);
54+
}
55+
56+
@Test
57+
public void testUpdate() throws Exception {
58+
final String LICENSE_KEY_NEW = "Test change";
59+
mKey.setLicenseKey(LICENSE_KEY_NEW);
60+
mKeyDao.update(mKey);
61+
62+
Key queried = mKeyDao.queryForId(mKey.getId());
63+
64+
Assert.assertEquals(LICENSE_KEY_NEW, queried.getLicenseKey());
65+
}
66+
67+
@Test
68+
public void testDelete() throws Exception {
69+
Key k = new Key();
70+
k.setLicenseKey(UUID.randomUUID().toString());
71+
72+
mKeyDao.create(k);
73+
int id = k.getId();
74+
75+
Key toDelete = mKeyDao.queryForId(id);
76+
mKeyDao.delete(toDelete);
77+
78+
Key queried = mKeyDao.queryForId(id);
79+
Assert.assertNull(queried);
80+
}
81+
82+
@Test
83+
public void testRead() throws Exception {
84+
int id = mKey.getId();
85+
86+
Key queried = mKeyDao.queryForId(id);
87+
88+
Assert.assertEquals(mKey, queried);
89+
}
90+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.j256.ormlite.dao.DaoManager;
66
import com.j256.ormlite.support.ConnectionSource;
77

8-
import junit.framework.Assert;
8+
import org.junit.Assert;
99

1010
import org.junit.After;
1111
import org.junit.Before;

0 commit comments

Comments
 (0)