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