Michael Hanl | 0320dca | 2016-01-27 21:08:25 +0100 | [diff] [blame] | 1 | import de.ids_mannheim.korap.config.BeanConfiguration; |
Michael Hanl | d034af0 | 2016-01-28 14:02:41 +0100 | [diff] [blame] | 2 | import de.ids_mannheim.korap.user.*; |
| 3 | import org.junit.AfterClass; |
| 4 | import org.junit.Before; |
| 5 | import org.junit.BeforeClass; |
| 6 | import org.junit.Test; |
Michael Hanl | 0320dca | 2016-01-27 21:08:25 +0100 | [diff] [blame] | 7 | |
| 8 | /** |
| 9 | * @author hanl |
| 10 | * @date 27/01/2016 |
| 11 | */ |
| 12 | public class UserdataTest { |
| 13 | |
| 14 | @BeforeClass |
| 15 | public static void init() { |
| 16 | BeanConfiguration.loadClasspathContext("default-config.xml"); |
| 17 | } |
| 18 | |
| 19 | @AfterClass |
| 20 | public static void drop() { |
| 21 | BeanConfiguration.closeApplication(); |
| 22 | } |
| 23 | |
| 24 | @Before |
| 25 | public void clear() { |
| 26 | UserDetailsDao dao = new UserDetailsDao( |
| 27 | BeanConfiguration.getBeans().getPersistenceClient()); |
| 28 | assert dao.deleteAll() != -1; |
| 29 | } |
| 30 | |
| 31 | @Test |
| 32 | public void testDataStore() { |
| 33 | User user = new KorAPUser(); |
| 34 | user.setId(1); |
| 35 | UserDetailsDao dao = new UserDetailsDao( |
| 36 | BeanConfiguration.getBeans().getPersistenceClient()); |
| 37 | Userdetails2 d = new Userdetails2(1); |
| 38 | d.addField("key_1", "value is a value"); |
| 39 | assert dao.store(d) != -1; |
| 40 | } |
| 41 | |
| 42 | @Test |
| 43 | public void testDataGet() { |
| 44 | User user = new KorAPUser(); |
| 45 | user.setId(1); |
| 46 | UserDetailsDao dao = new UserDetailsDao( |
| 47 | BeanConfiguration.getBeans().getPersistenceClient()); |
| 48 | Userdetails2 d = new Userdetails2(1); |
| 49 | d.addField("key_1", "value is a value"); |
| 50 | assert dao.store(d) != -1; |
| 51 | |
| 52 | d = dao.get(d.getId()); |
| 53 | assert d != null; |
| 54 | assert "value is a value".equals(d.get("key_1")); |
| 55 | |
| 56 | d = dao.get(user); |
| 57 | assert d != null; |
| 58 | assert "value is a value".equals(d.get("key_1")); |
| 59 | } |
| 60 | |
Michael Hanl | d034af0 | 2016-01-28 14:02:41 +0100 | [diff] [blame] | 61 | @Test |
Michael Hanl | c2a9f62 | 2016-01-28 16:40:06 +0100 | [diff] [blame] | 62 | public void testDataValidation() { |
| 63 | Userdata data = new Userdetails2(1); |
| 64 | data.addField(Attributes.COUNTRY, "Germany"); |
| 65 | |
| 66 | String[] req = data.requiredFields(); |
| 67 | String[] r = data.validationReturn(); |
| 68 | assert r.length > 0; |
| 69 | assert r.length == req.length; |
| 70 | assert !data.isValid(); |
| 71 | } |
| 72 | |
| 73 | @Test |
Michael Hanl | d034af0 | 2016-01-28 14:02:41 +0100 | [diff] [blame] | 74 | public void testUserdatafactory() { |
| 75 | UserDataDbIface dao = UserdataFactory |
| 76 | .getDaoInstance(Userdetails2.class); |
| 77 | assert UserDetailsDao.class.equals(dao.getClass()); |
| 78 | } |
| 79 | |
| 80 | @Test(expected = RuntimeException.class) |
| 81 | public void testUserdatafactoryError() { |
| 82 | UserdataFactory.getDaoInstance(new Userdata(1) { |
| 83 | @Override |
| 84 | public String[] requiredFields() { |
| 85 | return new String[0]; |
| 86 | } |
| 87 | }.getClass()); |
| 88 | } |
| 89 | |
Michael Hanl | 0320dca | 2016-01-27 21:08:25 +0100 | [diff] [blame] | 90 | } |