1 package org.jellyfish.implementation; 2 3 import java.util.ArrayList; 4 import java.util.Collection; 5 import java.util.Iterator; 6 7 import javax.naming.NameNotFoundException; 8 import javax.naming.NamingEnumeration; 9 import javax.naming.NamingException; 10 import javax.naming.directory.Attributes; 11 import javax.naming.directory.BasicAttributes; 12 import javax.naming.directory.DirContext; 13 import javax.naming.directory.SearchResult; 14 15 import junit.framework.TestCase; 16 import mock.javax.naming.directory.DirContextMock; 17 import mock 18 .org 19 .apache 20 .avalon 21 .framework 22 .service 23 .ServiceManagerMock 24 .ServiceManagerMock; 25 26 import org.jellyfish.ConnectionManager; 27 import org.jellyfish.ConnectionManagerMock; 28 import org.jellyfish.MappingManager; 29 import org.jellyfish.MappingManagerMock; 30 import org.jellyfish.config.AttributeMapping; 31 import org.jellyfish.config.ObjectMapping; 32 import org.jellyfish.model.Person; 33 34 public class DirectoryManagerDefaultTest extends TestCase { 35 36 private DirectoryManagerDefault directoryManager; 37 private ServiceManagerMock serviceManagerMock; 38 private ConnectionManagerMock connectionManagerMock; 39 private MappingManagerMock mappingManagerMock; 40 41 private String PERSON = "person"; 42 private Person person; 43 44 public DirectoryManagerDefaultTest(String arg0) { 45 super(arg0); 46 } 47 48 protected void setUp() throws Exception { 49 connectionManagerMock = new ConnectionManagerMock(); 50 connectionManagerMock.getContextReturn = new DirContextMock(); 51 serviceManagerMock = new ServiceManagerMock(); 52 serviceManagerMock.lookupMap.put( 53 ConnectionManager.ROLE, 54 connectionManagerMock); 55 mappingManagerMock = createMappingManagerMock(); 56 serviceManagerMock.lookupMap.put( 57 MappingManager.ROLE, 58 mappingManagerMock); 59 directoryManager = new DirectoryManagerDefault(); 60 directoryManager.service(serviceManagerMock); 61 person = createPerson(); 62 } 63 64 private MappingManagerMock createMappingManagerMock() { 65 MappingManagerMock mock = new MappingManagerMock(); 66 mock.mapToAttributesReturn = createAttributes(); 67 mock.getObjectDnReturn = "cn=joe,ou=people,dc=jellyfish,dc=org"; 68 mock.getObjectMappingReturn = createObjectMapping(); 69 return mock; 70 } 71 72 private ObjectMapping createObjectMapping() { 73 ObjectMapping objectMapping = new ObjectMapping(); 74 objectMapping.setJavaName(Person.class.getName()); 75 objectMapping.setLdapName(PERSON); 76 objectMapping.setDn("cn=${firstName},ou=people"); 77 objectMapping.setAttributeMappings(new ArrayList()); 78 objectMapping.getAttributeMappings().add( 79 new AttributeMapping("firstName", "cn")); 80 objectMapping.getAttributeMappings().add( 81 new AttributeMapping("lastName", "sn")); 82 return objectMapping; 83 } 84 85 private Person createPerson() { 86 return new Person("Joe", "Smith"); 87 } 88 89 private Attributes createAttributes() { 90 Attributes attributes = new BasicAttributes(); 91 attributes.put(MappingManager.OBJECTCLASS, "person"); 92 attributes.put("cn", "Joe"); 93 attributes.put("sn", "Smith"); 94 return attributes; 95 } 96 97 private NamingEnumeration createNamingEnumeration() { 98 final Collection results = new ArrayList(); 99 SearchResult searchResultOne = 100 new SearchResult( 101 "cn=joe,ou=people,dc=jellyfish,dc=org", 102 new Person(), 103 createAttributes()); 104 results.add(searchResultOne); 105 NamingEnumeration namingEnumeration = new NamingEnumeration() { 106 private Iterator iterator = results.iterator(); 107 public void close() throws NamingException { 108 throw new UnsupportedOperationException("Implement me."); 109 } 110 111 public boolean hasMore() throws NamingException { 112 return hasMoreElements(); 113 } 114 115 public Object next() throws NamingException { 116 return nextElement(); 117 } 118 119 public boolean hasMoreElements() { 120 return iterator.hasNext(); 121 } 122 123 public Object nextElement() { 124 return iterator.next(); 125 126 } 127 }; 128 return namingEnumeration; 129 } 130 131 public void testStore() { 132 directoryManager.store(person); 133 assertTrue(mappingManagerMock.mapToAttributesCalled); 134 assertTrue(mappingManagerMock.getObjectDnCalled); 135 assertTrue(connectionManagerMock.getContextCalled); 136 assertTrue( 137 connectionManagerMock.getContextReturn.modifyAttributesCalled); 138 assertEquals( 139 mappingManagerMock.getObjectDnReturn, 140 connectionManagerMock.getContextReturn.modifyAttributesName); 141 assertEquals( 142 DirContext.REPLACE_ATTRIBUTE, 143 connectionManagerMock.getContextReturn.modifyAttributesModOp); 144 assertEquals( 145 mappingManagerMock.mapToAttributesReturn, 146 connectionManagerMock.getContextReturn.modifyAttributesAttributes); 147 } 148 149 public void testStoreNameNotFound() throws NamingException { 150 connectionManagerMock 151 .getContextReturn 152 .modifyAttributesNameNotFoundException = 153 new NameNotFoundException(); 154 directoryManager.store(person); 155 assertTrue( 156 connectionManagerMock.getContextReturn.createSubcontextCalled); 157 assertEquals( 158 PERSON, 159 connectionManagerMock 160 .getContextReturn 161 .createSubcontextAttributes 162 .get("objectclass") 163 .get()); 164 } 165 166 public void testLookup() { 167 connectionManagerMock.getContextReturn.getAttributesReturn = 168 createAttributes(); 169 mappingManagerMock.mapFromAttributesReturn = person; 170 171 Person personReturned = (Person) directoryManager.lookup(person); 172 173 assertTrue(mappingManagerMock.getObjectDnCalled); 174 assertTrue(mappingManagerMock.mapFromAttributesCalled); 175 assertTrue(connectionManagerMock.getContextCalled); 176 assertTrue(connectionManagerMock.getContextReturn.getAttributesCalled); 177 assertEquals( 178 connectionManagerMock.getContextReturn.getAttributesReturn, 179 mappingManagerMock.mapFromAttributesAttributes); 180 assertEquals(person.getFirstName(), personReturned.getFirstName()); 181 } 182 183 public void testDelete() { 184 directoryManager.delete((Object) person); 185 assertTrue(mappingManagerMock.getObjectDnCalled); 186 assertTrue( 187 connectionManagerMock.getContextReturn.destroySubcontextCalled); 188 } 189 190 public void testSearch() { 191 connectionManagerMock.getContextReturn.searchReturn = 192 createNamingEnumeration(); 193 mappingManagerMock.mapFromAttributesReturn = createPerson(); 194 Collection matches = directoryManager.search((Object) person); 195 assertTrue(mappingManagerMock.mapToAttributesCalled); 196 assertTrue(mappingManagerMock.getObjectDnCalled); 197 assertTrue(connectionManagerMock.getContextReturn.searchCalled); 198 assertEquals( 199 "ou=people,dc=jellyfish,dc=org", 200 connectionManagerMock.getContextReturn.searchName); 201 assertEquals( 202 mappingManagerMock.mapToAttributesReturn, 203 connectionManagerMock.getContextReturn.searchMatchingAttributes); 204 assertTrue(mappingManagerMock.mapFromAttributesCalled); 205 } 206 }

This page was automatically generated by Maven