1 /* Copyright 2004-2005 Graeme Rocher 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 package grails.util; 16 17 import groovy.util.GroovyTestCase; 18 import groovy.util.ObjectGraphBuilder; 19 import org.codehaus.groovy.grails.commons.ApplicationHolder; 20 import org.codehaus.groovy.grails.commons.DefaultGrailsApplication; 21 22 import java.util.ArrayList; 23 import java.util.List; 24 25 public class DomainBuilderTests extends GroovyTestCase { 26 27 private DomainBuilder builder; 28 private ObjectGraphBuilder.ChildPropertySetter childPropertySetter; 29 private Employer employer; 30 31 @Override setUp()32 protected void setUp() throws Exception { 33 ApplicationHolder.setApplication(new DefaultGrailsApplication()); 34 35 builder = new DomainBuilder(); 36 childPropertySetter = builder.getChildPropertySetter(); 37 38 employer = new Employer(); 39 employer.setName("Spacely Space Sprockets"); 40 } 41 42 @SuppressWarnings("rawtypes") testChildIsCollection()43 public void testChildIsCollection() throws Exception { 44 Employee one = new Employee(); 45 one.setName("Cosmo"); 46 47 Employee two = new Employee(); 48 two.setName("George"); 49 50 childPropertySetter.setChild(employer, one, null, "employees"); 51 childPropertySetter.setChild(employer, two, null, "employees"); 52 53 List employees = employer.getEmployees(); 54 55 assertNull(employer.getAddress()); 56 assertEquals(2, employees.size()); 57 58 assertEquals(one.getName(), ((Employee)employees.get(0)).getName()); 59 assertEquals(two.getName(), ((Employee)employees.get(1)).getName()); 60 } 61 testChildIsNotCollection()62 public void testChildIsNotCollection() throws Exception { 63 Address address = new Address(); 64 address.setStreet("Park Pl."); 65 66 childPropertySetter.setChild(employer, address, null, "address"); 67 68 Address a = employer.getAddress(); 69 70 assertEquals(address.getStreet(), a.getStreet()); 71 72 assertEquals(0, employer.getEmployees().size()); 73 } 74 75 @SuppressWarnings({"unchecked","rawtypes"}) 76 public static class Employer { 77 private String name = null; 78 private Address address = null; 79 private List employees = new ArrayList(); 80 addToEmployees(Employee employee)81 public void addToEmployees(Employee employee) { 82 employees.add(employee); 83 } 84 getEmployees()85 public List getEmployees() { 86 return employees; 87 } 88 setAddress(Address a)89 public void setAddress(Address a) { 90 address = a; 91 } 92 getAddress()93 public Address getAddress() { 94 return address; 95 } 96 setName(String n)97 public void setName(String n) { 98 name = n; 99 } 100 getName()101 public String getName() { 102 return name; 103 } 104 } 105 106 public static class Employee { 107 private String name; setName(String n)108 public void setName(String n) { name = n; } getName()109 public String getName() { return name; } 110 } 111 112 public static class Address { 113 private String street; setStreet(String s)114 public void setStreet(String s) { street = s; } getStreet()115 public String getStreet() { return street; } 116 } 117 } 118