001/* 002 * Copyright © 2025 CUI-OpenSource-Software (info@cuioss.de) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package de.cuioss.test.generator.domain; 017 018import de.cuioss.test.generator.TypedGenerator; 019 020import java.util.logging.Level; 021import java.util.logging.Logger; 022 023/** 024 * Generates realistic {@link Person} objects for testing purposes. 025 * The generator creates persons with: 026 * <ul> 027 * <li>English first names (male and female) from {@link NameGenerators#FIRSTNAMES_ANY_ENGLISH}</li> 028 * <li>English family names from {@link NameGenerators#FAMILY_NAMES_ENGLISH}</li> 029 * <li>Organization names from {@link OrganizationNameGenerator#READABLE}</li> 030 * <li>Professional titles from {@link TitleGenerator#READABLE}</li> 031 * <li>Email addresses generated from the person's name using {@link EmailGenerator}</li> 032 * </ul> 033 * 034 * <p><em>Example usage:</em></p> 035 * <pre> 036 * var generator = new PersonGenerator(); 037 * Person person = generator.next(); 038 * // person will have realistic first name, last name, title, organization and email 039 * </pre> 040 * 041 * @author Oliver Wolff 042 */ 043public class PersonGenerator implements TypedGenerator<Person> { 044 045 private static final Logger LOGGER = Logger.getLogger(PersonGenerator.class.getName()); 046 private final TypedGenerator<String> firstNames = NameGenerators.FIRSTNAMES_ANY_ENGLISH.generator(); 047 private final TypedGenerator<String> familyNames = NameGenerators.FAMILY_NAMES_ENGLISH.generator(); 048 private final TypedGenerator<String> organizations = OrganizationNameGenerator.READABLE.generator(); 049 private final TypedGenerator<String> titles = TitleGenerator.READABLE.generator(); 050 051 @Override 052 public Person next() { 053 final var firstname = firstNames.next(); 054 final var lastname = familyNames.next(); 055 final var organization = organizations.next(); 056 final var title = titles.next(); 057 058 if (null == firstname || null == lastname) { 059 LOGGER.log(Level.WARNING, "Generated null name components: firstname={0}, lastname={1}", new Object[]{firstname, lastname}); 060 } 061 062 var person = Person.builder() 063 .email(EmailGenerator.createEmail(firstname, lastname)) 064 .firstname(firstname) 065 .lastname(lastname) 066 .organisation(organization) 067 .title(title) 068 .build(); 069 070 LOGGER.log(Level.FINE, "Generated person: {0} {1}", new Object[]{firstname, lastname}); 071 return person; 072 } 073 074}