Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 693d0eb7246a2d03052eefeeb8355017b3b3ab83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright (c) 2013 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Christian W. Damus (CEA) - initial API and implementation
 */
package org.eclipse.emf.cdo.tests.bugzilla;

import org.eclipse.emf.cdo.internal.net4j.Net4jSessionFactory;
import org.eclipse.emf.cdo.tests.AbstractCDOTest;

import org.eclipse.net4j.util.container.ContainerUtil;
import org.eclipse.net4j.util.container.IManagedContainer;
import org.eclipse.net4j.util.container.IManagedContainerFactory;
import org.eclipse.net4j.util.factory.Factory;
import org.eclipse.net4j.util.factory.IFactory;
import org.eclipse.net4j.util.factory.ProductCreationException;
import org.eclipse.net4j.util.lifecycle.LifecycleUtil;

/**
 * Bug 399641: Tests that factories in managed containers can use their containers
 * to get dependencies.
 */
public class Bugzilla_399641_Test extends AbstractCDOTest
{
  public void testContainerAwareFactories() throws Exception
  {
    IManagedContainer container = ContainerUtil.createContainer();
    LifecycleUtil.activate(container);

    try
    {
      container.registerFactory(new MyFactory());
      container.registerFactory(new MyOtherFactory());

      Object product = container.getElement(MyFactory.PRODUCT_GROUP, MyFactory.TYPE, null);
      assertInstanceOf(MyProduct.class, product);
      assertNotNull(((MyProduct)product).getOther());
    }
    finally
    {
      LifecycleUtil.deactivate(container);
    }
  }

  public void testNet4jSessionFactory() throws Exception
  {
    IManagedContainer container = getClientContainer();

    IFactory factory = container.getFactory(Net4jSessionFactory.PRODUCT_GROUP, Net4jSessionFactory.TYPE);
    assertInstanceOf(IManagedContainerFactory.class, factory);

    IManagedContainer actualContainer = ((IManagedContainerFactory)factory).getManagedContainer();
    assertSame(container, actualContainer);
  }

  private static class MyProduct
  {
    private final MyOtherProduct other;

    MyProduct(MyOtherProduct other)
    {
      this.other = other;
    }

    public MyOtherProduct getOther()
    {
      return other;
    }
  }

  private static class MyFactory extends Factory implements IManagedContainerFactory
  {
    static final String PRODUCT_GROUP = MyFactory.class.getName();

    static final String TYPE = "default";

    private IManagedContainer container;

    public MyFactory()
    {
      super(PRODUCT_GROUP, TYPE);
    }

    public Object create(String description) throws ProductCreationException
    {
      return new MyProduct(
          (MyOtherProduct)getManagedContainer().getElement(MyOtherFactory.PRODUCT_GROUP, MyOtherFactory.TYPE, null));
    }

    public IManagedContainer getManagedContainer()
    {
      return container;
    }

    public void setManagedContainer(IManagedContainer container)
    {
      this.container = container;
    }

  }

  private static class MyOtherProduct
  {
    MyOtherProduct()
    {
      super();
    }
  }

  private static class MyOtherFactory extends Factory
  {
    static final String PRODUCT_GROUP = MyOtherFactory.class.getName();

    static final String TYPE = "default";

    public MyOtherFactory()
    {
      super(PRODUCT_GROUP, TYPE);
    }

    public Object create(String description) throws ProductCreationException
    {
      return new MyOtherProduct();
    }

  }
}

Back to the top