|
| 1 | +/* |
| 2 | + * Copyright 2016 Deutsche Nationalbibliothek |
| 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.culturegraph.mf.stream.sink; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertNull; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | + |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +/** |
| 27 | + * Tests for class {@link EntityPathTracker}. |
| 28 | + * |
| 29 | + * @author Christoph Böhme |
| 30 | + */ |
| 31 | +public class EntityPathTrackerTest { |
| 32 | + |
| 33 | + private EntityPathTracker pathTracker; |
| 34 | + |
| 35 | + @Before |
| 36 | + public void initSystemUnderTest() { |
| 37 | + pathTracker = new EntityPathTracker(); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void getCurrentPath_shouldReturnEmptyPathIfProcessingHasNotStarted() { |
| 42 | + assertTrue(pathTracker.getCurrentPath().isEmpty()); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void getCurrentPath_shouldReturnEmptyPathIfNotInRecord() { |
| 47 | + pathTracker.startRecord("1"); |
| 48 | + pathTracker.endRecord(); |
| 49 | + assertTrue(pathTracker.getCurrentPath().isEmpty()); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void getCurrentPath_shouldReturnPathToCurrentEntity() { |
| 54 | + pathTracker.startRecord("1"); |
| 55 | + assertEquals("", pathTracker.getCurrentPath()); |
| 56 | + pathTracker.startEntity("granny"); |
| 57 | + assertEquals("granny", pathTracker.getCurrentPath()); |
| 58 | + pathTracker.startEntity("mommy"); |
| 59 | + assertEquals("granny.mommy", pathTracker.getCurrentPath()); |
| 60 | + pathTracker.startEntity("me"); |
| 61 | + assertEquals("granny.mommy.me", pathTracker.getCurrentPath()); |
| 62 | + pathTracker.endEntity(); |
| 63 | + assertEquals("granny.mommy", pathTracker.getCurrentPath()); |
| 64 | + pathTracker.startEntity("my-sister"); |
| 65 | + assertEquals("granny.mommy.my-sister", pathTracker.getCurrentPath()); |
| 66 | + pathTracker.endEntity(); |
| 67 | + assertEquals("granny.mommy", pathTracker.getCurrentPath()); |
| 68 | + pathTracker.endEntity(); |
| 69 | + assertEquals("granny", pathTracker.getCurrentPath()); |
| 70 | + pathTracker.endEntity(); |
| 71 | + assertEquals("", pathTracker.getCurrentPath()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void startRecord_shouldResetPath() { |
| 76 | + pathTracker.startRecord("1"); |
| 77 | + pathTracker.startEntity("entity"); |
| 78 | + assertEquals("entity", pathTracker.getCurrentPath()); |
| 79 | + |
| 80 | + pathTracker.startRecord("2"); |
| 81 | + assertTrue(pathTracker.getCurrentPath().isEmpty()); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void resetStream_shouldResetPath() { |
| 86 | + pathTracker.startRecord("1"); |
| 87 | + pathTracker.startEntity("entity"); |
| 88 | + assertEquals("entity", pathTracker.getCurrentPath()); |
| 89 | + |
| 90 | + pathTracker.resetStream(); |
| 91 | + assertTrue(pathTracker.getCurrentPath().isEmpty()); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void closeStream_shouldResetPath() { |
| 96 | + pathTracker.startRecord("1"); |
| 97 | + pathTracker.startEntity("entity"); |
| 98 | + assertEquals("entity", pathTracker.getCurrentPath()); |
| 99 | + |
| 100 | + pathTracker.closeStream(); |
| 101 | + assertTrue(pathTracker.getCurrentPath().isEmpty()); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void getCurrentPathWith_shouldAppendLiteralNameToPath() { |
| 106 | + pathTracker.startRecord("1"); |
| 107 | + pathTracker.startEntity("entity"); |
| 108 | + |
| 109 | + assertEquals("entity.literal", pathTracker.getCurrentPathWith("literal")); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void getCurrentPathWith_shouldReturnOnlyLiteralNameIfNotInEntity() { |
| 114 | + pathTracker.startRecord("1"); |
| 115 | + |
| 116 | + assertEquals("literal", pathTracker.getCurrentPathWith("literal")); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void getCurrentEntityName_shouldReturnNullIfProcessingNotStarted() { |
| 121 | + assertNull(pathTracker.getCurrentEntityName()); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void getCurrentEntityName_shouldReturnNullIfNotInRecord() { |
| 126 | + pathTracker.startRecord("1"); |
| 127 | + pathTracker.endRecord(); |
| 128 | + |
| 129 | + assertNull(pathTracker.getCurrentEntityName()); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void getCurrentEntityName_shouldReturnNameOfCurrentEntity() { |
| 134 | + pathTracker.startRecord("1"); |
| 135 | + assertNull(pathTracker.getCurrentEntityName()); |
| 136 | + pathTracker.startEntity("grandad"); |
| 137 | + assertEquals("grandad", pathTracker.getCurrentEntityName()); |
| 138 | + pathTracker.startEntity("daddy"); |
| 139 | + assertEquals("daddy", pathTracker.getCurrentEntityName()); |
| 140 | + pathTracker.endEntity(); |
| 141 | + assertEquals("grandad", pathTracker.getCurrentEntityName()); |
| 142 | + pathTracker.endEntity(); |
| 143 | + assertNull(pathTracker.getCurrentEntityName()); |
| 144 | + pathTracker.endRecord(); |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments