|
24 | 24 | package cz.jirutka.rsql.parser |
25 | 25 |
|
26 | 26 | import cz.jirutka.rsql.parser.ast.ComparisonNode |
| 27 | +import cz.jirutka.rsql.parser.ast.ComparisonOperator |
27 | 28 | import cz.jirutka.rsql.parser.ast.LogicalOperator |
28 | 29 | import cz.jirutka.rsql.parser.ast.NodesFactory |
29 | 30 | import spock.lang.Specification |
@@ -81,4 +82,61 @@ class NodesFactoryAccessSpec extends Specification { |
81 | 82 |
|
82 | 83 | actual.arguments.size() == arguments.size() |
83 | 84 | } |
| 85 | + |
| 86 | + def 'Should not use trusted constructors when NodesFactory is inherited'(LogicalOperator op) { |
| 87 | + given: |
| 88 | + def factory = new NodesFactoryExtended(defaultOperators()) |
| 89 | + def children = [ |
| 90 | + new ComparisonNode(EQUAL, 'a1', ['b1']), |
| 91 | + new ComparisonNode(NOT_EQUAL, 'a2', ['b2']) |
| 92 | + ] |
| 93 | + |
| 94 | + when: |
| 95 | + def actual = NodesFactoryAccess.create(factory, op, children) |
| 96 | + |
| 97 | + then: |
| 98 | + actual.children.size() == children.size() |
| 99 | + |
| 100 | + and: 'original is not modified by modifying copy' |
| 101 | + def childrenCopy = actual.children |
| 102 | + childrenCopy.removeLast() |
| 103 | + childrenCopy.size() + 1 == children.size() |
| 104 | + |
| 105 | + and: 'modifying original it should not change in the LogicalNode' |
| 106 | + children.removeLast() |
| 107 | + |
| 108 | + actual.children.size() == children.size() + 1 |
| 109 | + |
| 110 | + where: |
| 111 | + op << LogicalOperator.values() |
| 112 | + } |
| 113 | + |
| 114 | + def 'Should not use trusted constructors when NodesFactory is inherited and creating comparison node'() { |
| 115 | + given: |
| 116 | + def factory = new NodesFactoryExtended(defaultOperators()) |
| 117 | + def arguments = ['x', 'y', 'z'] |
| 118 | + |
| 119 | + when: |
| 120 | + def actual = NodesFactoryAccess.create(factory, '=in=', 'a1', arguments) |
| 121 | + |
| 122 | + then: |
| 123 | + actual.arguments.size() == arguments.size() |
| 124 | + |
| 125 | + and: 'original is not modified by modifying copy' |
| 126 | + def childrenCopy = actual.arguments |
| 127 | + childrenCopy.removeLast() |
| 128 | + childrenCopy.size() + 1 == arguments.size() |
| 129 | + |
| 130 | + and: 'modifying original it should not change in the ComparisonNode' |
| 131 | + arguments.removeLast() |
| 132 | + |
| 133 | + actual.arguments.size() == arguments.size() + 1 |
| 134 | + } |
| 135 | + |
| 136 | + static class NodesFactoryExtended extends NodesFactory { |
| 137 | + |
| 138 | + NodesFactoryExtended(Set<ComparisonOperator> operators) { |
| 139 | + super(operators) |
| 140 | + } |
| 141 | + } |
84 | 142 | } |
0 commit comments