protected Object createNewInstance(Erector erector) throws BlueprintTemplateException { return erector.createNewInstance(); }
/** * Create new instance of {@link Erector#getTarget()}. Uses {@link #getNewInstance()} * if defined, otherwise fails back to {@link BlueprintTemplate#construct} * * @return Object new instance * @throws BlueprintTemplateException */ public Object createNewInstance() throws BlueprintTemplateException { if ( this.getNewInstance() != null ) { // XXX: warn if the return of the callback is not the same class as the target? return this.getNewInstance().createInstance(); } else { return this.getTemplate().construct( this.getTarget() ); } }
@Before public void setUp() throws RegisterBlueprintException { defaultField = new DefaultField(); defaultField.setName("milage"); defaultField.setForce(false); defaultField.setValue(carBlueprint.mileage); defaultField.setTarget(Float.class); defaultField.setFieldClass(Float.class); erector = new Erector(); erector.setTemplate(new JavaBeanTemplate()); erector.setBlueprint(carBlueprint); erector.setModelFields(Arrays.<ModelField>asList(defaultField)); erector.setTarget(Car.class); erector.setNewInstance(null); }
erector.clearCommands(); erector.setReference(nonNullReferenceModel); List<BlueprintPolicy> blueprintPolicies = this.getBlueprintPolicies().get(erector.getTarget()); if (blueprintPolicies != null) { erector.addCommands(modelField, modelFieldCommands.get(modelField)); for (ModelField modelField : erector.getModelFields()) { Command command = policy.process(this, erector, modelField, createdModel); if (command != null) { erector.addCommand(modelField, command); if (erector.getCommands(modelField).size() > 0) { logger.debug(" ModelField commands: {}", erector.getCommands(modelField)); if (!erector.getCommands(modelField).contains(Command.SKIP_INJECTION)) { if (!erector.getCommands(modelField).contains(Command.SKIP_REFERENCE_INJECTION)) { try { value = erector.getTemplate().get(nonNullReferenceModel, defaultField.getName()); } catch (BlueprintTemplateException e) { throw new CreateModelException(e); if (!erector.getCommands(modelField).contains(Command.SKIP_BLUEPRINT_INJECTION) && (value == null || defaultField.isForce())) { value = defaultField.getValue();
SpringBlueprint springBlueprint = erector.getBlueprint().getClass().getAnnotation(SpringBlueprint.class); if ( springBlueprint != null && springBlueprint.bean() ) { Class beanClass = springBlueprint.beanClass(); beanClass = erector.getTarget();
@Test public void customTemplate() throws RegisterBlueprintException, CreateModelException { assertEquals( CustomTemplate.class, modelFactory.getErectors().get( Wheel.class ).getTemplate().getClass() ); } }
@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 public void springShouldInjectsBlueprint() { Erector erector = modelFactory.getErectors().get(SportsCar.class); assertNotNull( "ModelFactory should get injected from spring into Blueprint", ((SportsCarBlueprint) erector.getBlueprint()).modelFactory ); }
Erector erector = new Erector(); erector.setTemplate(template); erector.setBlueprint(blueprint); erector.setModelFields(modelFields); erector.setTarget(target); erector.setNewInstance(newInstance); erector.setCallbacks("afterCreate", afterCreateCallbacks);
@Test public void testGet() throws BlueprintTemplateException { Car car = new Car(); car.setMileage(new Float(123.456)); Float val = (Float) erector.getTemplate().get(car, "mileage"); assertEquals(new Float(123.456), val); } }
public Command process(ModelFactory modelFactory, Erector erector, ModelField modelField, Object model) throws PolicyException { logger.debug( "processing {} for {}", modelField, model ); // If Model has not be set, create a new one from ModelFactory if ( modelField instanceof MappedField ) { if ( this.getSingleton() == null ) { logger.debug(" creating singleton for {}", this.getTarget() ); try { this.setSingleton( modelFactory.createModel( this.getTarget(), false ) ); } catch (CreateModelException e) { throw new PolicyException( e ); } } // Set Singleton into model try { erector.getTemplate().set( model, modelField.getName(), this.getSingleton() ); } catch (BlueprintTemplateException e) { throw new PolicyException(e); } } return Command.SKIP_INJECTION; }
@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(); } }