1   package com.atlassian.user.impl.delegation.properties;
2   
3   import junit.framework.TestCase;
4   import com.atlassian.user.repository.RepositoryIdentifier;
5   import com.atlassian.user.repository.DefaultRepositoryIdentifier;
6   import com.atlassian.user.properties.PropertySetFactory;
7   import com.atlassian.user.Entity;
8   import com.atlassian.user.EntityException;
9   import com.atlassian.user.impl.DefaultUser;
10  import com.opensymphony.module.propertyset.PropertySet;
11  
12  import java.util.*;
13  
14  public class TestDelegatingPropertySetFactory extends TestCase
15  {
16      DelegatingPropertySetFactory delegatingPropertySetFactory;
17      RepositoryIdentifier config;
18      TestPropertySetFactory psFactory1;
19      TestPropertySetFactory psFactory2;
20      Entity entity1;
21      Entity entity2;
22  
23      public void setUp() throws Exception
24      {
25          config = new DefaultRepositoryIdentifier("default", "Default repo");
26          ArrayList<PropertySetFactory> propertySetFactories = new ArrayList<PropertySetFactory>();
27          entity1 = new DefaultUser("foo1");
28          entity2 = new DefaultUser("foo2");
29  
30          psFactory1 = new TestPropertySetFactory(entity1);
31          psFactory2 = new TestPropertySetFactory(entity2);
32          propertySetFactories.add(psFactory1);
33          propertySetFactories.add(psFactory2);
34  
35          delegatingPropertySetFactory = new DelegatingPropertySetFactory(propertySetFactories);
36          super.setUp();
37      }
38  
39      public void tearDown() throws Exception
40      {
41          super.tearDown();
42      }
43  
44  
45      public void testDelegatingGetPropertySet() throws EntityException
46      {
47          PropertySet propertySet = delegatingPropertySetFactory.getPropertySet(entity2);
48          assertNotNull("delegatingPropertySetFactory is not handling its propertySetFactories properly",
49                  propertySet);
50  
51          assertTrue("delegatingPropertSetFactory is not respecting the order of delegation", psFactory1.called);
52          assertTrue("delegatingPropertSetFactory is not returning propertySets for handled entities", psFactory2.called);
53  
54          psFactory1.reset();
55          psFactory2.reset();
56  
57          propertySet = delegatingPropertySetFactory.getPropertySet(entity1);
58  
59          assertNotNull("delegatingPropertySetFactory is not handling its propertySetFactories properly",
60                  propertySet);
61  
62          assertTrue("delegatingPropertSetFactory is not returning propertySets for handled entitiesn", psFactory1.called);
63          assertFalse("delegatingPropertSetFactory is called the entire chain of factories needlessly", psFactory2.called);
64      }
65  
66      private static class TestPropertySetFactory implements PropertySetFactory
67      {
68          private final Entity entity;
69          private boolean called;
70  
71          public TestPropertySetFactory(Entity entity)
72          {
73              this.entity = entity;
74          }
75  
76          void reset()
77          {
78              this.called = false;
79          }
80  
81          public PropertySet getPropertySet(Entity entity) throws EntityException
82          {
83              this.called = true;
84  
85              if (this.entity != entity)
86                  return null;
87  
88              return new NullPropertySet();
89  
90          }
91      }
92  
93  }