Search in sources :

Example 21 with Order

use of org.apache.tapestry5.ioc.annotations.Order in project tapestry-5 by apache.

the class AbstractBeanModelSourceImplTest method reorder.

@Test
public void reorder() {
    Messages messages = mockMessages();
    stub_contains(messages, false);
    replay();
    BeanModel model = source.create(SimpleBean.class, true, messages);
    assertSame(model.getBeanType(), SimpleBean.class);
    // Based on order of the getter methods (no longer alphabetical)
    assertEquals(model.getPropertyNames(), Arrays.asList("firstName", "lastName", "age"));
    // Testing a couple of things here:
    // 1) case insensitive
    // 2) unreferenced property names added to the end.
    model.reorder("lastname", "AGE");
    assertEquals(model.getPropertyNames(), Arrays.asList("lastName", "age", "firstName"));
    verify();
}
Also used : Messages(org.apache.tapestry5.commons.Messages) BeanModel(org.apache.tapestry5.beanmodel.BeanModel) Test(org.testng.annotations.Test)

Example 22 with Order

use of org.apache.tapestry5.ioc.annotations.Order in project tapestry-5 by apache.

the class AbstractBeanModelSourceImplTest method include_properties.

@Test
public void include_properties() {
    Messages messages = mockMessages();
    stub_contains(messages, false);
    replay();
    BeanModel model = source.create(SimpleBean.class, true, messages);
    assertSame(model.getBeanType(), SimpleBean.class);
    model.include("lastname", "firstname");
    // Based on order of the getter methods (no longer alphabetical)
    assertEquals(model.getPropertyNames(), Arrays.asList("lastName", "firstName"));
    verify();
}
Also used : Messages(org.apache.tapestry5.commons.Messages) BeanModel(org.apache.tapestry5.beanmodel.BeanModel) Test(org.testng.annotations.Test)

Example 23 with Order

use of org.apache.tapestry5.ioc.annotations.Order in project tapestry-5 by apache.

the class ModuleImpl method instantiateModuleInstance.

private Object instantiateModuleInstance() {
    Class moduleClass = moduleDef.getBuilderClass();
    Constructor[] constructors = moduleClass.getConstructors();
    if (constructors.length == 0)
        throw new RuntimeException(IOCMessages.noPublicConstructors(moduleClass));
    if (constructors.length > 1) {
        // Sort the constructors ascending by number of parameters (descending); this is really
        // just to allow the test suite to work properly across different JVMs (which will
        // often order the constructors differently).
        Comparator<Constructor> comparator = new Comparator<Constructor>() {

            @Override
            public int compare(Constructor c1, Constructor c2) {
                return c2.getParameterTypes().length - c1.getParameterTypes().length;
            }
        };
        Arrays.sort(constructors, comparator);
        logger.warn(IOCMessages.tooManyPublicConstructors(moduleClass, constructors[0]));
    }
    Constructor constructor = constructors[0];
    if (insideConstructor)
        throw new RuntimeException(IOCMessages.recursiveModuleConstructor(moduleClass, constructor));
    ObjectLocator locator = new ObjectLocatorImpl(registry, this);
    Map<Class, Object> resourcesMap = CollectionFactory.newMap();
    resourcesMap.put(Logger.class, logger);
    resourcesMap.put(ObjectLocator.class, locator);
    resourcesMap.put(OperationTracker.class, registry);
    InjectionResources resources = new MapInjectionResources(resourcesMap);
    Throwable fail = null;
    try {
        insideConstructor = true;
        ObjectCreator[] parameterValues = InternalUtils.calculateParameters(locator, resources, constructor.getParameterTypes(), constructor.getGenericParameterTypes(), constructor.getParameterAnnotations(), registry);
        Object[] realized = InternalUtils.realizeObjects(parameterValues);
        Object result = constructor.newInstance(realized);
        InternalUtils.injectIntoFields(result, locator, resources, registry);
        return result;
    } catch (InvocationTargetException ex) {
        fail = ex.getTargetException();
    } catch (Exception ex) {
        fail = ex;
    } finally {
        insideConstructor = false;
    }
    throw new RuntimeException(IOCMessages.instantiateBuilderError(moduleClass, fail), fail);
}
Also used : Constructor(java.lang.reflect.Constructor) MapInjectionResources(org.apache.tapestry5.ioc.internal.util.MapInjectionResources) InjectionResources(org.apache.tapestry5.ioc.internal.util.InjectionResources) MapInjectionResources(org.apache.tapestry5.ioc.internal.util.MapInjectionResources) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ObjectStreamException(java.io.ObjectStreamException) JustInTimeObjectCreator(org.apache.tapestry5.ioc.internal.services.JustInTimeObjectCreator)

Example 24 with Order

use of org.apache.tapestry5.ioc.annotations.Order in project tapestry-5 by apache.

the class PersistentFieldManagerImplTest method gather_changes.

@Test
public void gather_changes() {
    Object value1 = new Object();
    Object value2 = new Object();
    PersistentFieldStrategy strat1 = newPersistentFieldStrategy();
    Collection<PersistentFieldChange> changes1 = newList();
    changes1.add(new PersistentFieldChangeImpl("component", "field1", value1));
    PersistentFieldStrategy strat2 = newPersistentFieldStrategy();
    Collection<PersistentFieldChange> changes2 = newList();
    changes2.add(new PersistentFieldChangeImpl("component", "field2", value2));
    // We don't know the exact order the strategies will be ordered in the map,
    // so we can't guarantee the order the strategies will be invoked.
    getMocksControl().checkOrder(false);
    expect(strat1.gatherFieldChanges("foo.Bar")).andReturn(changes1);
    expect(strat2.gatherFieldChanges("foo.Bar")).andReturn(changes2);
    replay();
    Map<String, PersistentFieldStrategy> strategies = newMap();
    strategies.put("alpha", strat1);
    strategies.put("beta", strat2);
    PersistentFieldManager manager = new PersistentFieldManagerImpl(null, strategies);
    PersistentFieldBundle bundle = manager.gatherChanges("foo.Bar");
    assertSame(bundle.getValue("component", "field1"), value1);
    assertSame(bundle.getValue("component", "field2"), value2);
    verify();
}
Also used : PersistentFieldStrategy(org.apache.tapestry5.services.PersistentFieldStrategy) PersistentFieldBundle(org.apache.tapestry5.services.PersistentFieldBundle) PersistentFieldChange(org.apache.tapestry5.services.PersistentFieldChange) Test(org.testng.annotations.Test)

Example 25 with Order

use of org.apache.tapestry5.ioc.annotations.Order in project tapestry-5 by apache.

the class Tree method toRenderCommand.

/**
 * Renders a single node (which may be the last within its containing node).
 * This is a mix of immediate rendering, and queuing up various Blocks and Render commands
 * to do the rest. May recursively render child nodes of the active node. The label part
 * of the node is rendered inside a {@linkplain org.apache.tapestry5.services.Heartbeat heartbeat}.
 *
 * @param node
 *         to render
 * @param isLast
 *         if true, add "last" attribute to the LI element
 * @return command to render the node
 */
private RenderCommand toRenderCommand(final TreeNode node, final boolean isLast) {
    return new RenderCommand() {

        public void render(MarkupWriter writer, RenderQueue queue) {
            // Inform the component's container about what value is being rendered
            // (this may be necessary to generate the correct label for the node).
            Tree.this.node = node;
            value = node.getValue();
            boolean isLeaf = node.isLeaf();
            writer.element("li");
            if (isLast) {
                writer.attributes("class", "last");
            }
            if (isLeaf) {
                writer.getElement().attribute("class", "leaf-node");
            }
            Element e = writer.element("span", "class", "tree-icon");
            if (!isLeaf && !node.getHasChildren()) {
                e.addClassName("empty-node");
            }
            boolean hasChildren = !isLeaf && node.getHasChildren();
            boolean expanded = hasChildren && expansionModel.isExpanded(node);
            writer.attributes("data-node-id", node.getId());
            if (expanded) {
                // Inform the client side, so it doesn't try to fetch it a second time.
                e.addClassName("tree-expanded");
            }
            // span.tree-icon
            writer.end();
            // From here on in, we're pushing things onto the queue. Remember that
            // execution order is reversed from order commands are pushed.
            // li
            queue.push(RENDER_CLOSE_TAG);
            if (expanded) {
                queue.push(new RenderNodes(node.getChildren()));
            }
            queue.push(RENDER_CLOSE_TAG);
            final RenderCommand startHeartbeat = new RenderCommand() {

                @Override
                public void render(MarkupWriter writer, RenderQueue queue) {
                    heartbeat.begin();
                }
            };
            final RenderCommand endHeartbeat = new RenderCommand() {

                @Override
                public void render(MarkupWriter writer, RenderQueue queue) {
                    heartbeat.end();
                }
            };
            queue.push(endHeartbeat);
            queue.push(label);
            queue.push(startHeartbeat);
            if (isLeaf && selectionModel != null && selectionModel.isSelected(node)) {
                queue.push(MARK_SELECTED);
            }
            queue.push(RENDER_LABEL_SPAN);
        }
    };
}
Also used : RenderCommand(org.apache.tapestry5.runtime.RenderCommand) Element(org.apache.tapestry5.dom.Element) RenderQueue(org.apache.tapestry5.runtime.RenderQueue)

Aggregations

Test (org.testng.annotations.Test)10 BeanModel (org.apache.tapestry5.beanmodel.BeanModel)6 Messages (org.apache.tapestry5.commons.Messages)6 PropertyModel (org.apache.tapestry5.beanmodel.PropertyModel)3 ToDoItem (org.apache.tapestry5.integration.app1.data.ToDoItem)3 Method (java.lang.reflect.Method)2 EventContext (org.apache.tapestry5.EventContext)2 PropertyConduit (org.apache.tapestry5.beanmodel.PropertyConduit)2 Location (org.apache.tapestry5.commons.Location)2 ClassPropertyAdapter (org.apache.tapestry5.commons.services.ClassPropertyAdapter)2 PropertyAdapter (org.apache.tapestry5.commons.services.PropertyAdapter)2 Link (org.apache.tapestry5.http.Link)2 ComponentEventLinkEncoder (org.apache.tapestry5.services.ComponentEventLinkEncoder)2 ObjectStreamException (java.io.ObjectStreamException)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1