|
| 1 | +/* |
| 2 | + * Copyright 2021 hbz NRW |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 the "License"; |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.metafacture.triples; |
| 18 | + |
| 19 | +import org.metafacture.framework.ObjectReceiver; |
| 20 | +import org.metafacture.framework.objects.Triple; |
| 21 | + |
| 22 | +import org.junit.Rule; |
| 23 | +import org.junit.Test; |
| 24 | +import org.mockito.InOrder; |
| 25 | +import org.mockito.Mock; |
| 26 | +import org.mockito.Mockito; |
| 27 | +import org.mockito.exceptions.base.MockitoAssertionError; |
| 28 | +import org.mockito.junit.MockitoJUnit; |
| 29 | +import org.mockito.junit.MockitoRule; |
| 30 | + |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.function.BiConsumer; |
| 33 | +import java.util.function.Consumer; |
| 34 | + |
| 35 | +public final class TripleSortTest { |
| 36 | + |
| 37 | + @Rule |
| 38 | + public final MockitoRule mockitoRule = MockitoJUnit.rule(); |
| 39 | + |
| 40 | + @Mock |
| 41 | + private ObjectReceiver<Triple> receiver; |
| 42 | + |
| 43 | + @Test |
| 44 | + public void shouldSortByIncreasingSubject() { |
| 45 | + assertSort( |
| 46 | + t -> { |
| 47 | + }, |
| 48 | + "s0 p1 o2", |
| 49 | + "s2 p1 o0", |
| 50 | + "s0 p1 o1", |
| 51 | + "s1 p0 o2", |
| 52 | + "s0 p2 o1", |
| 53 | + // |
| 54 | + "s0 p1 o2", |
| 55 | + "s0 p1 o1", |
| 56 | + "s0 p2 o1", |
| 57 | + "s1 p0 o2", |
| 58 | + "s2 p1 o0" |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void shouldSortByDecreasingSubject() { |
| 64 | + assertSort( |
| 65 | + t -> { |
| 66 | + t.setOrder(AbstractTripleSort.Order.DECREASING); |
| 67 | + }, |
| 68 | + "s0 p1 o2", |
| 69 | + "s2 p1 o0", |
| 70 | + "s0 p1 o1", |
| 71 | + "s1 p0 o2", |
| 72 | + "s0 p2 o1", |
| 73 | + // |
| 74 | + "s2 p1 o0", |
| 75 | + "s1 p0 o2", |
| 76 | + "s0 p1 o2", |
| 77 | + "s0 p1 o1", |
| 78 | + "s0 p2 o1" |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void shouldSortByIncreasingPredicate() { |
| 84 | + assertSort( |
| 85 | + t -> { |
| 86 | + t.setBy(AbstractTripleSort.Compare.PREDICATE); |
| 87 | + }, |
| 88 | + "s0 p1 o2", |
| 89 | + "s2 p1 o0", |
| 90 | + "s0 p1 o1", |
| 91 | + "s1 p0 o2", |
| 92 | + "s0 p2 o1", |
| 93 | + // |
| 94 | + "s1 p0 o2", |
| 95 | + "s0 p1 o2", |
| 96 | + "s2 p1 o0", |
| 97 | + "s0 p1 o1", |
| 98 | + "s0 p2 o1" |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void shouldSortByDecreasingPredicate() { |
| 104 | + assertSort( |
| 105 | + t -> { |
| 106 | + t.setBy(AbstractTripleSort.Compare.PREDICATE); |
| 107 | + t.setOrder(AbstractTripleSort.Order.DECREASING); |
| 108 | + }, |
| 109 | + "s0 p1 o2", |
| 110 | + "s2 p1 o0", |
| 111 | + "s0 p1 o1", |
| 112 | + "s1 p0 o2", |
| 113 | + "s0 p2 o1", |
| 114 | + // |
| 115 | + "s0 p2 o1", |
| 116 | + "s0 p1 o2", |
| 117 | + "s2 p1 o0", |
| 118 | + "s0 p1 o1", |
| 119 | + "s1 p0 o2" |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void shouldSortByIncreasingObject() { |
| 125 | + assertSort( |
| 126 | + t -> { |
| 127 | + t.setBy(AbstractTripleSort.Compare.OBJECT); |
| 128 | + }, |
| 129 | + "s0 p1 o2", |
| 130 | + "s2 p1 o0", |
| 131 | + "s0 p1 o1", |
| 132 | + "s1 p0 o2", |
| 133 | + "s0 p2 o1", |
| 134 | + // |
| 135 | + "s2 p1 o0", |
| 136 | + "s0 p1 o1", |
| 137 | + "s0 p2 o1", |
| 138 | + "s0 p1 o2", |
| 139 | + "s1 p0 o2" |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void shouldSortByDecreasingObject() { |
| 145 | + assertSort( |
| 146 | + t -> { |
| 147 | + t.setBy(AbstractTripleSort.Compare.OBJECT); |
| 148 | + t.setOrder(AbstractTripleSort.Order.DECREASING); |
| 149 | + }, |
| 150 | + "s0 p1 o2", |
| 151 | + "s2 p1 o0", |
| 152 | + "s0 p1 o1", |
| 153 | + "s1 p0 o2", |
| 154 | + "s0 p2 o1", |
| 155 | + // |
| 156 | + "s0 p1 o2", |
| 157 | + "s1 p0 o2", |
| 158 | + "s0 p1 o1", |
| 159 | + "s0 p2 o1", |
| 160 | + "s2 p1 o0" |
| 161 | + ); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void shouldSortByIncreasingTriple() { |
| 166 | + assertSort( |
| 167 | + t -> { |
| 168 | + t.setBy(AbstractTripleSort.Compare.ALL); |
| 169 | + }, |
| 170 | + "s0 p1 o2", |
| 171 | + "s2 p1 o0", |
| 172 | + "s0 p1 o1", |
| 173 | + "s1 p0 o2", |
| 174 | + "s0 p2 o1", |
| 175 | + // |
| 176 | + "s0 p1 o1", |
| 177 | + "s0 p1 o2", |
| 178 | + "s0 p2 o1", |
| 179 | + "s1 p0 o2", |
| 180 | + "s2 p1 o0" |
| 181 | + ); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + public void shouldSortByDecreasingTriple() { |
| 186 | + assertSort( |
| 187 | + t -> { |
| 188 | + t.setBy(AbstractTripleSort.Compare.ALL); |
| 189 | + t.setOrder(AbstractTripleSort.Order.DECREASING); |
| 190 | + }, |
| 191 | + "s0 p1 o2", |
| 192 | + "s2 p1 o0", |
| 193 | + "s0 p1 o1", |
| 194 | + "s1 p0 o2", |
| 195 | + "s0 p2 o1", |
| 196 | + // |
| 197 | + "s2 p1 o0", |
| 198 | + "s1 p0 o2", |
| 199 | + "s0 p2 o1", |
| 200 | + "s0 p1 o2", |
| 201 | + "s0 p1 o1" |
| 202 | + ); |
| 203 | + } |
| 204 | + |
| 205 | + @Test |
| 206 | + public void shouldNotSortNumerically() { |
| 207 | + assertSort( |
| 208 | + t -> { |
| 209 | + }, |
| 210 | + "10 p1 o2", |
| 211 | + "2 p1 o0", |
| 212 | + "0 p1 o1", |
| 213 | + "101 p0 o2", |
| 214 | + "11 p2 o1", |
| 215 | + // |
| 216 | + "0 p1 o1", |
| 217 | + "10 p1 o2", |
| 218 | + "101 p0 o2", |
| 219 | + "11 p2 o1", |
| 220 | + "2 p1 o0" |
| 221 | + ); |
| 222 | + } |
| 223 | + |
| 224 | + public void assertSort(final Consumer<TripleSort> consumer, final String... triples) { |
| 225 | + final BiConsumer<Integer, Consumer<Triple>> processor = (i, c) -> { |
| 226 | + final int j = triples.length / 2; |
| 227 | + |
| 228 | + Arrays.stream(triples, i * j, (i + 1) * j).map(s -> { |
| 229 | + final String[] t = s.split("\\s+"); |
| 230 | + return new Triple(t[0], t[1], t[2]); |
| 231 | + }).forEach(c); |
| 232 | + }; |
| 233 | + |
| 234 | + final InOrder ordered = Mockito.inOrder(receiver); |
| 235 | + |
| 236 | + final TripleSort tripleSort = new TripleSort(); |
| 237 | + tripleSort.setReceiver(receiver); |
| 238 | + consumer.accept(tripleSort); |
| 239 | + |
| 240 | + processor.accept(0, tripleSort::process); |
| 241 | + tripleSort.closeStream(); |
| 242 | + |
| 243 | + try { |
| 244 | + processor.accept(1, t -> ordered.verify(receiver).process(t)); |
| 245 | + ordered.verify(receiver).closeStream(); |
| 246 | + |
| 247 | + ordered.verifyNoMoreInteractions(); |
| 248 | + Mockito.verifyNoMoreInteractions(receiver); |
| 249 | + } |
| 250 | + catch (final MockitoAssertionError e) { |
| 251 | + System.out.println(Mockito.mockingDetails(receiver).printInvocations()); |
| 252 | + throw e; |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | +} |
0 commit comments