View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jxpath.ri.model.dynabeans;
19  
20  import org.apache.commons.beanutils.DynaBean;
21  import org.apache.commons.jxpath.AbstractFactory;
22  import org.apache.commons.jxpath.JXPathContext;
23  import org.apache.commons.jxpath.NestedTestBean;
24  import org.apache.commons.jxpath.Pointer;
25  
26  /**
27   * Test AbstractFactory.
28   */
29  public class TestDynaBeanFactory extends AbstractFactory {
30  
31      /**
32       */
33      @Override
34      public boolean createObject(final JXPathContext context, final Pointer pointer, final Object parent, final String name, final int index) {
35          switch (name) {
36          case "nestedBean":
37              ((DynaBean) parent).set("nestedBean", new NestedTestBean("newName"));
38              return true;
39          case "beans": {
40              final DynaBean bean = (DynaBean) parent;
41              Object beans[] = (Object[]) bean.get("beans");
42              if (beans == null || index >= beans.length) {
43                  beans = new NestedTestBean[index + 1];
44                  bean.set("beans", beans);
45              }
46              beans[index] = new NestedTestBean("newName");
47              return true;
48          }
49          case "integers": {
50              final DynaBean bean = (DynaBean) parent;
51              bean.set("integers", index, Integer.valueOf(0));
52              return true;
53          }
54          default:
55              break;
56          }
57          return false;
58      }
59  
60      /**
61       */
62      @Override
63      public boolean declareVariable(final JXPathContext context, final String name) {
64          context.getVariables().declareVariable(name, null);
65          return true;
66      }
67  }