1   package com.atlassian.user.impl.hibernate.properties;
2   
3   import com.atlassian.user.EntityException;
4   import com.atlassian.user.GroupManager;
5   import com.atlassian.user.User;
6   import com.atlassian.user.UserManager;
7   import com.atlassian.user.generic.AbstractSpringTest;
8   import com.atlassian.user.impl.hibernate.DefaultHibernateExternalEntity;
9   import com.atlassian.user.impl.hibernate.ExternalEntityDAO;
10  import com.atlassian.user.impl.hibernate.HibernateGroupManager;
11  import com.atlassian.user.impl.hibernate.HibernateUserManager;
12  import com.atlassian.user.properties.PropertySetFactory;
13  import com.opensymphony.module.propertyset.PropertySet;
14  import com.opensymphony.module.propertyset.PropertySetManager;
15  import com.opensymphony.module.propertyset.hibernate.HibernateConfigurationProvider;
16  import net.sf.hibernate.HibernateException;
17  
18  import java.sql.SQLException;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  
22  public class TestExternalEntityPropertySetFactory extends AbstractSpringTest
23  {
24      private PropertySetFactory propertySetFactory;
25      private ExternalEntityDAO externalEntityDao;
26      private HibernateUserManager hibernateUserManager;
27      private HibernateGroupManager hibernateGroupManager;
28      private HibernateConfigurationProvider propertySetConfigurationProvider;
29  
30      protected UserManager getUserManager()
31      {
32          return hibernateUserManager;
33      }
34  
35      protected GroupManager getGroupManager()
36      {
37          return hibernateGroupManager;
38      }
39  
40      public void setExternalEntityDao(ExternalEntityDAO externalEntityDao)
41      {
42          this.externalEntityDao = externalEntityDao;
43      }
44  
45      public void setHibernateUserManager(HibernateUserManager hibernateUserManager)
46      {
47          this.hibernateUserManager = hibernateUserManager;
48      }
49  
50      public void setHibernateGroupManager(HibernateGroupManager hibernateGroupManager)
51      {
52          this.hibernateGroupManager = hibernateGroupManager;
53      }
54  
55      public void setPropertySetFactory(PropertySetFactory propertySetFactory)
56      {
57          this.propertySetFactory = propertySetFactory;
58      }
59  
60      public void setPropertySetConfigurationProvider(HibernateConfigurationProvider propertySetConfigurationProvider)
61      {
62          this.propertySetConfigurationProvider = propertySetConfigurationProvider;
63      }
64  
65      protected String[] getConfigLocations()
66      {
67          return new String[]{
68              "classpath:com/atlassian/user/impl/hibernate/hibernateTestContext.xml",
69              "classpath:com/atlassian/user/dataSourceTestContext.xml",
70          };
71      }
72  
73      public void testCreateMemoryPropertySet()
74      {
75          PropertySet ps = PropertySetManager.getInstance("memory", null);
76          assertNotNull("is propertyset.xml on the classpath?", ps);
77      }
78  
79      public void testCreateHibernatePropertySet()
80      {
81          HashMap<String, Object> args = new HashMap<String, Object>();
82          args.put("entityId", new Long(1));
83          args.put("entityName", "_" + "testing");
84          args.put("configurationProvider", propertySetConfigurationProvider);
85  
86          PropertySet ps = PropertySetManager.getInstance("hibernate", args);
87          assertNotNull("is propertyset.xml on the classpath?", ps);
88      }
89  
90      public void testAddPropertyForExternalUser() throws HibernateException, SQLException, EntityException
91      {
92          DefaultHibernateExternalEntity externalEntity = new DefaultHibernateExternalEntity();
93          externalEntity.setName("user");
94          externalEntity.setType(User.class.getName());
95  
96          externalEntityDao.saveExternalEntity(externalEntity);
97  
98          PropertySet propertySet = propertySetFactory.getPropertySet(externalEntity);
99          assertNull(hibernateUserManager.getUser("user"));
100         assertNotNull(propertySet);
101 
102         propertySet.setString("testString", "helloworld");
103         propertySet.setBoolean("test", true);
104 
105         PropertySet retrievedPropertySet = propertySetFactory.getPropertySet(externalEntity);
106 
107         assertEquals("Propertyset for external user is not recording data.", "helloworld",
108             retrievedPropertySet.getString("testString"));
109         assertTrue("Propertyset for external user is not recording data.", retrievedPropertySet.getBoolean("test"));
110 
111         // remove left over properties
112         for (Iterator i = retrievedPropertySet.getKeys().iterator(); i.hasNext();)
113         {
114             String key = (String) i.next();
115             retrievedPropertySet.remove(key);
116         }
117     }
118 }