Make testing more generic.
[lgpl/argeo-commons.git] / org.argeo.enterprise / ext / test / org / argeo / osgi / useradmin / LdifUserAdminTest.java
index 06f1e6d2e19f5e9b1a5364b6d5edd5dad12f2e7f..1d6b0f3a5d80bcfef29b0af53b3e01fcd7066c1d 100644 (file)
@@ -2,51 +2,91 @@ package org.argeo.osgi.useradmin;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.File;
+import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
+import java.nio.file.FileVisitResult;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
 import java.util.Arrays;
 import java.util.Base64;
 import java.util.Dictionary;
 import java.util.Hashtable;
 import java.util.List;
-import java.util.UUID;
 
 import javax.transaction.TransactionManager;
 
-import junit.framework.TestCase;
-
+import org.argeo.naming.LdapAttrs;
+import org.argeo.transaction.simple.SimpleTransactionManager;
 import org.osgi.service.useradmin.Authorization;
 import org.osgi.service.useradmin.Group;
 import org.osgi.service.useradmin.Role;
 import org.osgi.service.useradmin.User;
 
-import bitronix.tm.BitronixTransactionManager;
-import bitronix.tm.TransactionManagerServices;
-import bitronix.tm.resource.ehcache.EhCacheXAResourceProducer;
+import junit.framework.TestCase;
 
+/** {@link LdifUserAdmin} tests. */
 public class LdifUserAdminTest extends TestCase implements BasicTestConstants {
-       private BitronixTransactionManager tm;
+       // We have to keep using JUnit because of
+       // https://issues.apache.org/jira/browse/SUREFIRE-1669
+       
+       final static int TM_SIMPLE = 0;
+       final static int TM_BITRONIX = 1;
+
+       private int tmType = TM_SIMPLE;
+       private TransactionManager tm;
        private URI uri;
        private AbstractUserDirectory userAdmin;
+       private Path tempDir;
+
+       public void setUp() {
+               System.out.println("Enter setUp()");
+               try {
+                       tempDir = Files.createTempDirectory(getClass().getName());
+                       tempDir.toFile().deleteOnExit();
+                       String uriProp = System.getProperty("argeo.userdirectory.uri");
+                       if (uriProp != null)
+                               uri = new URI(uriProp);
+                       else {
+                               tempDir.toFile().deleteOnExit();
+                               Path ldifPath = tempDir.resolve(BASE_DN + ".ldif");
+                               try (InputStream in = getClass().getResource("basic.ldif").openStream()) {
+                                       Files.copy(in, ldifPath);
+                               }
+                               uri = ldifPath.toUri();
+                       }
 
-       public void testConcurrent() throws Exception {
+                       // Init transaction manager
+                       if (TM_SIMPLE == tmType) {
+                               tm = new SimpleTransactionManager();
+                       }
+//             else if (TM_BITRONIX == tmType) {
+//                     bitronix.tm.Configuration tmConf = TransactionManagerServices.getConfiguration();
+//                     tmConf.setServerId(UUID.randomUUID().toString());
+//                     tmConf.setLogPart1Filename(new File(tempDir.toFile(), "btm1.tlog").getAbsolutePath());
+//                     tmConf.setLogPart2Filename(new File(tempDir.toFile(), "btm2.tlog").getAbsolutePath());
+//                     tm = TransactionManagerServices.getTransactionManager();
+//             }
+
+                       userAdmin = initUserAdmin(uri, tm);
+               } catch (Exception e) {
+                       throw new RuntimeException(e);
+               }
        }
 
-       @SuppressWarnings("unchecked")
        public void testEdition() throws Exception {
                User demoUser = (User) userAdmin.getRole(DEMO_USER_DN);
-               assertNotNull(demoUser);
+               assert demoUser != null;
 
                tm.begin();
                String newName = "demo";
                demoUser.getProperties().put("cn", newName);
-               assertEquals(newName, demoUser.getProperties().get("cn"));
+               assert newName.equals(demoUser.getProperties().get("cn"));
                tm.commit();
                persistAndRestart();
-               assertEquals(newName, demoUser.getProperties().get("cn"));
+               assert newName.equals(demoUser.getProperties().get("cn"));
 
                tm.begin();
                userAdmin.removeRole(DEMO_USER_DN);
@@ -55,59 +95,59 @@ public class LdifUserAdminTest extends TestCase implements BasicTestConstants {
 
                // check data
                Role[] search = userAdmin.getRoles("(objectclass=inetOrgPerson)");
-               assertEquals(1, search.length);
+               assert 1 == search.length;
                Group editorGroup = (Group) userAdmin.getRole(EDITORS_GROUP_DN);
-               assertNotNull(editorGroup);
+               assert editorGroup != null;
                Role[] members = editorGroup.getMembers();
-               assertEquals(1, members.length);
+               assert 1 == members.length;
        }
 
        public void testRetrieve() throws Exception {
                // users
                User rootUser = (User) userAdmin.getRole(ROOT_USER_DN);
-               assertNotNull(rootUser);
+               assert rootUser != null;
                User demoUser = (User) userAdmin.getRole(DEMO_USER_DN);
-               assertNotNull(demoUser);
+               assert demoUser != null;
 
                // groups
                Group adminGroup = (Group) userAdmin.getRole(ADMIN_GROUP_DN);
-               assertNotNull(adminGroup);
+               assert adminGroup != null;
                Role[] members = adminGroup.getMembers();
-               assertEquals(1, members.length);
-               assertEquals(rootUser, members[0]);
+               assert 1 == members.length;
+               assert rootUser.equals(members[0]);
 
                Group editorGroup = (Group) userAdmin.getRole(EDITORS_GROUP_DN);
-               assertNotNull(editorGroup);
+               assert editorGroup != null;
                members = editorGroup.getMembers();
-               assertEquals(2, members.length);
-               assertEquals(adminGroup, members[0]);
-               assertEquals(demoUser, members[1]);
+               assert 2 == members.length;
+               assert adminGroup.equals(members[0]);
+               assert demoUser.equals(members[1]);
 
                Authorization rootAuth = userAdmin.getAuthorization(rootUser);
                List<String> rootRoles = Arrays.asList(rootAuth.getRoles());
-               assertEquals(3, rootRoles.size());
-               assertTrue(rootRoles.contains(ROOT_USER_DN));
-               assertTrue(rootRoles.contains(ADMIN_GROUP_DN));
-               assertTrue(rootRoles.contains(EDITORS_GROUP_DN));
+               assert 3 == rootRoles.size();
+               assert rootRoles.contains(ROOT_USER_DN);
+               assert rootRoles.contains(ADMIN_GROUP_DN);
+               assert rootRoles.contains(EDITORS_GROUP_DN);
 
                // properties
-               assertEquals("root@localhost", rootUser.getProperties().get("mail"));
+               assert "root@localhost".equals(rootUser.getProperties().get("mail"));
 
                // credentials
                byte[] hashedPassword = ("{SHA}" + Base64.getEncoder().encodeToString(DigestUtils.sha1("demo".getBytes())))
                                .getBytes();
-               assertTrue(rootUser.hasCredential(LdifName.userPassword.name(), hashedPassword));
-               assertTrue(demoUser.hasCredential(LdifName.userPassword.name(), hashedPassword));
+               assert rootUser.hasCredential(LdapAttrs.userPassword.name(), hashedPassword);
+               assert demoUser.hasCredential(LdapAttrs.userPassword.name(), hashedPassword);
 
                // search
                Role[] search = userAdmin.getRoles(null);
-               assertEquals(4, search.length);
+               assert 4 == search.length;
                search = userAdmin.getRoles("(objectClass=groupOfNames)");
-               assertEquals(2, search.length);
+               assert 2 == search.length;
                search = userAdmin.getRoles("(objectclass=inetOrgPerson)");
-               assertEquals(2, search.length);
+               assert 2 == search.length;
                search = userAdmin.getRoles("(&(objectclass=inetOrgPerson)(uid=demo))");
-               assertEquals(1, search.length);
+               assert 1 == search.length;
        }
 
        public void testReadWriteRead() throws Exception {
@@ -125,36 +165,12 @@ public class LdifUserAdminTest extends TestCase implements BasicTestConstants {
                                ((LdifUserAdmin) userAdmin).load(in);
                        }
                        Role[] search = userAdmin.getRoles(null);
-                       assertEquals(4, search.length);
+                       assert 4 == search.length;
                } else {
                        // test not relevant for LDAP
                }
        }
 
-       @Override
-       protected void setUp() throws Exception {
-               Path tempDir = Files.createTempDirectory(getClass().getName());
-               String uriProp = System.getProperty("argeo.userdirectory.uri");
-               if (uriProp != null)
-                       uri = new URI(uriProp);
-               else {
-                       tempDir.toFile().deleteOnExit();
-                       Path ldifPath = tempDir.resolve(BASE_DN + ".ldif");
-                       try (InputStream in = getClass().getResource("basic.ldif").openStream()) {
-                               Files.copy(in, ldifPath);
-                       }
-                       uri = ldifPath.toUri();
-               }
-
-               bitronix.tm.Configuration tmConf = TransactionManagerServices.getConfiguration();
-               tmConf.setServerId(UUID.randomUUID().toString());
-               tmConf.setLogPart1Filename(new File(tempDir.toFile(), "btm1.tlog").getAbsolutePath());
-               tmConf.setLogPart2Filename(new File(tempDir.toFile(), "btm2.tlog").getAbsolutePath());
-               tm = TransactionManagerServices.getTransactionManager();
-
-               userAdmin = initUserAdmin(uri, tm);
-       }
-
        private AbstractUserDirectory initUserAdmin(URI uri, TransactionManager tm) {
                Dictionary<String, Object> props = new Hashtable<>();
                props.put(UserAdminConf.uri.name(), uri.toString());
@@ -168,25 +184,43 @@ public class LdifUserAdminTest extends TestCase implements BasicTestConstants {
                        userAdmin = new LdifUserAdmin(props);
                userAdmin.init();
                // JTA
-               EhCacheXAResourceProducer.registerXAResource(UserDirectory.class.getName(), userAdmin.getXaResource());
+//             if (TM_BITRONIX == tmType)
+//                     EhCacheXAResourceProducer.registerXAResource(UserDirectory.class.getName(), userAdmin.getXaResource());
                userAdmin.setTransactionManager(tm);
                return userAdmin;
        }
 
        private void persistAndRestart() {
-               EhCacheXAResourceProducer.unregisterXAResource(UserDirectory.class.getName(), userAdmin.getXaResource());
+//             if (TM_BITRONIX == tmType)
+//                     EhCacheXAResourceProducer.unregisterXAResource(UserDirectory.class.getName(), userAdmin.getXaResource());
                if (userAdmin instanceof LdifUserAdmin)
                        ((LdifUserAdmin) userAdmin).save();
                userAdmin.destroy();
                userAdmin = initUserAdmin(uri, tm);
        }
 
-       @Override
-       protected void tearDown() throws Exception {
-               EhCacheXAResourceProducer.unregisterXAResource(UserDirectory.class.getName(), userAdmin.getXaResource());
-               tm.shutdown();
+       public void tearDown() throws Exception {
+//             if (TM_BITRONIX == tmType) {
+//                     EhCacheXAResourceProducer.unregisterXAResource(UserDirectory.class.getName(), userAdmin.getXaResource());
+//                     ((BitronixTransactionManager) tm).shutdown();
+//             }
                if (userAdmin != null)
                        userAdmin.destroy();
+               if (tempDir != null)
+                       Files.walkFileTree(tempDir, new SimpleFileVisitor<Path>() {
+                               @Override
+                               public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
+                                       Files.delete(file);
+                                       return FileVisitResult.CONTINUE;
+                               }
+
+                               @Override
+                               public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
+                                       Files.delete(dir);
+                                       return FileVisitResult.CONTINUE;
+                               }
+
+                       });
        }
 
 }