@Before public void setUp() throws RegisterBlueprintException { modelFactory = new ModelFactory(); modelFactory.registerBlueprint(carBlueprint); modelFactory.registerBlueprint(wheelBlueprint); modelFactory.registerBlueprint(driverBlueprint); modelFactory.registerBlueprint(userBlueprint); modelFactory.registerBlueprint(optionBlueprint); }
/** * Create a Model for a registered Blueprint. Values set in the * model will not be overridden by defaults in the Blueprint. * * @param <T> model Class * @param referenceModel Object * @return Model * @throws CreateModelException model failed to create */ public <T> T createModel(T referenceModel) throws CreateModelException { return createModel(referenceModel, true); }
/** * Get complete inherited list of Field for Class, with the exception * that ConstructorCallback fields are not inherited. * * @param clazz Class * @return Map */ private Map<String,Field> getAllFields(Class clazz) { return this.getAllFields(clazz, false); }
createdModel = (T) createNewInstance(erector); } catch (BlueprintTemplateException e) { throw new CreateModelException(e); List<BlueprintPolicy> blueprintPolicies = this.getBlueprintPolicies().get(erector.getTarget()); if (blueprintPolicies != null) { List<FieldPolicy> policiesForSingleField = this.getFieldPolicies().get(modelField.getTarget()); if (policiesForSingleField != null) { value = this.createModel(mappedField.getTarget()); ((List) value).add(this.createModel(listField.getTarget())); ((List) value).add(this.createModel(object)); ((Set) value).add(this.createModel(setField.getTarget())); ((Set) value).add(this.createModel(object));
/** * Register a List of Blueprint, Class, or String * class names of Blueprint * * @param blueprints List * @throws RegisterBlueprintException failed to register blueprint */ public void setRegisterBlueprints(Collection blueprints) throws RegisterBlueprintException { for (Object blueprint : blueprints) { if (blueprint instanceof Class) { registerBlueprint((Class) blueprint); } else if (blueprint instanceof String) { registerBlueprint((String) blueprint); } else if (blueprint instanceof String) { registerBlueprint(blueprint); } else { throw new RegisterBlueprintException("Only supports List comprised of Class<Blueprint>, Blueprint, or String className"); } } }
@Test public void singletonPolicyWithModel() throws ModelFactoryException { Driver driver = modelFactory.createModel(Driver.class); modelFactory.addPolicy(new MappedSingletonPolicy(driver)); Car car1 = modelFactory.createModel(Car.class); Car car2 = modelFactory.createModel(Car.class); Car car3 = modelFactory.createModel(Car.class); assertEquals(car1.getDriver(), driver); assertEquals(car2.getDriver(), driver); assertEquals(car3.getDriver(), driver); } }
@Test public void testBlueprintConstructorOnlyTriggeredOncePerCreateModelInvocation() throws Exception { Erector erector = mock(Erector.class); modelFactory.getErectors().put(Car.class, erector); modelFactory.createModel(Car.class); verify(erector, times(1)).createNewInstance(); } }
@Test public void testRegisterBlueprintsByPackage() throws RegisterBlueprintException { modelFactory = new ModelFactory(); // reset modelFactory to set loading by package modelFactory.setRegisterBlueprintsByPackage("com.tobedevoured.modelcitizen"); List<Class> blueprintClasses = new ArrayList<Class>(); for (Object blueprint : modelFactory.getBlueprints()) { blueprintClasses.add(blueprint.getClass()); } assertTrue("CarBlueprint should be registered", blueprintClasses.contains(CarBlueprint.class)); assertTrue("WheelBlueprint should be registered", blueprintClasses.contains(WheelBlueprint.class)); assertTrue("DriverBlueprint should be registered", blueprintClasses.contains(DriverBlueprint.class)); assertTrue("UserBlueprint should be registered", blueprintClasses.contains(UserBlueprint.class)); }
/** * Register all Blueprint in package. * * @param _package String package to scan * @throws RegisterBlueprintException failed to register blueprint */ public void setRegisterBlueprintsByPackage(String _package) throws RegisterBlueprintException { Set<Class<?>> annotated = null; try { annotated = new ClassesInPackageScanner().findAnnotatedClasses(_package, Blueprint.class); } catch (IOException e) { throw new RegisterBlueprintException(e); } logger.info("Scanned {} and found {}", _package, annotated); this.setRegisterBlueprints(annotated); }
Object instance = super.createNewInstance(erector); beanFactory.autowireBean( instance ); return instance; return super.createNewInstance(erector);
@Test public void testRegisterBlueprint() throws RegisterBlueprintException { assertEquals(carBlueprint, modelFactory.getBlueprints().get(0)); assertEquals(wheelBlueprint, modelFactory.getBlueprints().get(1)); assertEquals(driverBlueprint, modelFactory.getBlueprints().get(2)); assertEquals(userBlueprint, modelFactory.getBlueprints().get(3)); }
/** * Register a Blueprint from a String Class name * * @param className String */ public void registerBlueprint(String className) throws RegisterBlueprintException { try { registerBlueprint(Class.forName(className)); } catch (ClassNotFoundException e) { throw new RegisterBlueprintException(e); } }
@Test public void singletonPolicyWithClass() throws ModelFactoryException { modelFactory.addPolicy(new MappedSingletonPolicy(Driver.class)); Car car1 = modelFactory.createModel(Car.class); Car car2 = modelFactory.createModel(Car.class); Car car3 = modelFactory.createModel(Car.class); Driver driver = car1.getDriver(); assertEquals(car2.getDriver(), driver); assertEquals(car3.getDriver(), driver); }
@Test public void testBlueprintWithPrimitive() throws CreateModelException, BlueprintTemplateException { Car car = modelFactory.createModel(Car.class); Erector erector = modelFactory.getErectors().get(Car.class); ModelField modelField = erector.getModelField("mileage"); assertEquals(new Float(100.1), modelField.getValue()); // Val is zero because primitive initializes as zero Object val = erector.getTemplate().get(new Car(), "mileage"); assertEquals(new Float(0.0), val); // Val is zero because primitive initializes as zero assertEquals(0.0, car.getMileage(), 0); } }
@Test(expected = RegisterBlueprintException.class) public void testSetRegisterBlueprintsWithException() throws RegisterBlueprintException { List blueprints = new ArrayList(); blueprints.add(carBlueprint); blueprints.add(8); modelFactory.setRegisterBlueprints(blueprints); }
@Before public void setUp() throws RegisterBlueprintException { modelFactory = new ModelFactory(); modelFactory.registerBlueprint(carBlueprint); modelFactory.registerBlueprint(wheelBlueprint); modelFactory.registerBlueprint(driverBlueprint); modelFactory.registerBlueprint(userBlueprint); modelFactory.registerBlueprint(optionBlueprint); }
/** * Create a Model for a registered Blueprint * * @param <T> model Class * @param clazz Model class * @return Model * @throws CreateModelException model failed to create */ public <T> T createModel(Class<T> clazz) throws CreateModelException { return createModel(clazz, true); }
/** * Register Blueprints, autowire them from Spring Context if they have the SpringBlueprint annotation * * @param blueprint Blueprint * @throws RegisterBlueprintException failed to register blueprint */ @Override public void registerBlueprint(Object blueprint) throws RegisterBlueprintException { SpringBlueprint springBlueprint = blueprint.getClass().getAnnotation(SpringBlueprint.class); if ( springBlueprint != null && springBlueprint.autowire() ) { logger.debug( "Autowiring blueprint {}", blueprint ); beanFactory.autowireBean( blueprint ); } super.registerBlueprint(blueprint); } }
@Test public void alwaySetFieldPolicy() throws ModelFactoryException { modelFactory.addPolicy(new SkipReferenceFieldPolicy("username", User.class)); User user1 = modelFactory.createModel(User.class); User user2 = modelFactory.createModel(user1); assertNotSame(user1.getUsername(), user2.getUsername()); assertEquals(user1.getEmails(), user2.getEmails()); }
private Map<String,Field> getAllFields(Class clazz, boolean isParent) { Map<String,Field> fieldsMap = new HashMap<String,Field>(); Class superClazz = clazz.getSuperclass(); if(superClazz != null){ fieldsMap.putAll( getAllFields(superClazz, true) ); } for ( Field field: clazz.getDeclaredFields() ) { field.setAccessible(true); if (isParent) { // ConstructorCallbacks are not inherited if (!field.getType().equals( ConstructorCallback.class ) ) { fieldsMap.put( field.getName(), field ); } } else { fieldsMap.put( field.getName(), field ); } } return fieldsMap; } }