aboutsummaryrefslogtreecommitdiff
path: root/docs/dev/InstantiatingComponents.md
blob: 80ba6b104653275c9c3c3bbd93c0d93b2d1d290d (plain)
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
# Instantiating Components

There are a few techniques for creating instances classes in unit tests:

* Use a mocking library. **(Depracated)**
* Provide a simple impementation of an interface
* Build a real instance of class using the class's builders / constructors
* Use the [ClusterFixture](ClusterFixture.md) or [OperatorFixture](OperatorFixture.md) classes to create instances of objects.

## Mocking Libraries (Deprecated)

Drill uses two mocking libraries in order to mock classes.

* [Mockito](http://site.mockito.org)
* [JMockit](http://jmockit.github.io/tutorial.html)

These libraries were originally used to work around the lack of well defined interfaces and adequate testing tools. Drill has made significant improvements in these areas
so using mocking libraries are no longer required. Existing tests that use these libraries will be refactored to remove them, and new tests should NOT use these libraries.

## Instantiating Contexts

There are several contexts used throughout Drill, for a complete description of each and how
they are used please see [FragmentContextImpl](../../exec/java-exec/src/main/java/org/apache/drill/exec/ops/FragmentContextImpl.java).

When doing tests you can use the following mock contexts:

  * [MockFragmentContext](../../exec/java-exec/src/test/java/org/apache/drill/test/OperatorFixture.java) is a simple mock implementation of
    the [FragmentContext](../../exec/java-exec/src/main/java/org/apache/drill/exec/ops/FragmentContext.java). A mock instance of the class can also be retrieved from an
    [OperatorFixture](OperatorFixture.md).
    ```
    FragmentContext context = operatorFixture.getFragmentContext();
    ```

## Creating An Instance of [QueryId](../../protocol/src/main/java/org/apache/drill/exec/proto/beans/QueryId.java)

```
UserBitShared.QueryId queryId = UserBitShared.QueryId.newBuilder()
  .setPart1(1L)
  .setPart2(2L)
  .build();
```

## Creating [FragmentHandle](../../protocol/src/main/java/org/apache/drill/exec/proto/beans/FragmentHandle.java)

```
ExecProtos.FragmentHandle fragmentHandle = ExecProtos.FragmentHandle.newBuilder()
  .setQueryId(queryId)
  .setMinorFragmentId(1)
  .setMajorFragmentId(2)
  .build();
```

## Creating A [DrillConfig](../../common/src/main/java/org/apache/drill/common/config/DrillConfig.java)

There are a few ways to create a [DrillConfig](../../common/src/main/java/org/apache/drill/common/config/DrillConfig.java). The simplest way is to
 create a [ClusterFixture](ClusterFixture.md) or [OperatorFixture](OperatorFixture.md) and then do the following:

```
DrillConfig drillConfig = clusterFixture.config();
```

or

```
DrillConfig drillConfig = operatorFixture.config();
```

If you need a [DrillConfig](../../common/src/main/java/org/apache/drill/common/config/DrillConfig.java) and don't want all the extra things provided
by [ClusterFixture](ClusterFixture.md) and [OperatorFixture](OperatorFixture.md), you can use
[ConfigBuilder](../../exec/java-exec/src/test/java/org/apache/drill/test/ConfigBuilder.java).

## Creating A [SpillSet](../../exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/spill/SpillSet.java)

 1. Create a [PhysicalOperator](../../exec/java-exec/src/main/java/org/apache/drill/exec/physical/base/PhysicalOperator.java).
    ```
    HashJoinPOP pop = new HashJoinPOP(null, null, null, JoinRelType.FULL);
    ```
 1. Create a [DrillConfig](../../common/src/main/java/org/apache/drill/common/config/DrillConfig.java).
 1. Create a [FragmentHandle](../../protocol/src/main/java/org/apache/drill/exec/proto/beans/FragmentHandle.java) as described above.
 1. Create a [SpillSet](../../exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/spill/SpillSet.java).
    ```
    SpillSet spillSet = new SpillSet(config, fragmentHandle, pop);
    ```
 
## Creating A [PersistentStoreProvider](../../exec/java-exec/src/main/java/org/apache/drill/exec/store/sys/PersistentStoreProvider.java)

```
LocalPersistentStoreProvider provider = new LocalPersistentStoreProvider(drillConfig);
provider.start();
```
 
## Creating A [LogicalPlanPersistence](../../logical/src/main/java/org/apache/drill/common/config/LogicalPlanPersistence.java)

```
LogicalPlanPersistence logicalPlanPersistence = PhysicalPlanReaderTestFactory.defaultLogicalPlanPersistence(drillConfig);
```

## Creating An Instance Of An Option Manager

You can create an instance of the [SystemOptionManager](../../exec/java-exec/src/main/java/org/apache/drill/exec/server/options/SystemOptionManager.java) by leveraging the
[OperatorFixture](OperatorFixture.md).

 1. Create an [OperatorFixture](OperatorFixture.md).
 1. Retrieve the [SystemOptionManager](../../exec/java-exec/src/main/java/org/apache/drill/exec/server/options/SystemOptionManager.java).
    ```
    operatorFixture.getOptionManager();
    ```