|
| 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 | +} |
0 commit comments