Skip to content

Commit 4ebf61f

Browse files
committed
Added tests for WellformednessChecker.
1 parent 812fa89 commit 4ebf61f

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* Copyright 2013 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+
package org.culturegraph.mf.stream.sink;
17+
18+
import org.culturegraph.mf.exceptions.WellformednessException;
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
22+
/**
23+
* Tests for {@link WellFormednessChecker}.
24+
*
25+
* @author Christoph Böhme
26+
*
27+
*/
28+
public final class WellFormednessCheckerTest {
29+
30+
private static final String RECORD_ID1 = "id1";
31+
private static final String RECORD_ID2 = "id2";
32+
private static final String ENTITY1 = "entity1";
33+
private static final String ENTITY2 = "entity2";
34+
private static final String ENTITY3 = "entity3";
35+
private static final String LITERAL1 = "literal1";
36+
private static final String LITERAL2 = "literal2";
37+
private static final String LITERAL3 = "literal3";
38+
private static final String LITERAL4 = "literal4";
39+
private static final String VALUE1 = "value1";
40+
private static final String VALUE2 = "value2";
41+
private static final String VALUE3 = "value3";
42+
private static final String VALUE4 = "value4";
43+
44+
private WellFormednessChecker wellFormednessChecker;
45+
46+
@Before
47+
public void setup() {
48+
wellFormednessChecker = new WellFormednessChecker();
49+
}
50+
51+
@Test
52+
public void testShouldAcceptValidStream() {
53+
wellFormednessChecker.startRecord(RECORD_ID1);
54+
wellFormednessChecker.literal(LITERAL1, VALUE1);
55+
wellFormednessChecker.startEntity(ENTITY1);
56+
wellFormednessChecker.literal(LITERAL2, VALUE2);
57+
wellFormednessChecker.startEntity(ENTITY2);
58+
wellFormednessChecker.literal(LITERAL3, VALUE3);
59+
wellFormednessChecker.endEntity();
60+
wellFormednessChecker.endEntity();
61+
wellFormednessChecker.endRecord();
62+
wellFormednessChecker.startRecord(RECORD_ID2);
63+
wellFormednessChecker.startEntity(ENTITY3);
64+
wellFormednessChecker.literal(LITERAL4, VALUE4);
65+
wellFormednessChecker.endEntity();
66+
wellFormednessChecker.endRecord();
67+
wellFormednessChecker.closeStream();
68+
}
69+
70+
@Test
71+
public void testShouldAcceptEmptyStream() {
72+
wellFormednessChecker.closeStream();
73+
}
74+
75+
@Test(expected=WellformednessException.class)
76+
public void testShouldFailOnNullRecordId() {
77+
wellFormednessChecker.startRecord(null);
78+
wellFormednessChecker.endRecord();
79+
wellFormednessChecker.closeStream();
80+
}
81+
82+
@Test(expected=WellformednessException.class)
83+
public void testShouldFailOnNullEntityName() {
84+
wellFormednessChecker.startRecord(RECORD_ID1);
85+
wellFormednessChecker.startEntity(null);
86+
wellFormednessChecker.endEntity();
87+
wellFormednessChecker.endRecord();
88+
wellFormednessChecker.closeStream();
89+
}
90+
91+
@Test(expected=WellformednessException.class)
92+
public void testShouldFailOnNullLiteralName() {
93+
wellFormednessChecker.startRecord(RECORD_ID1);
94+
wellFormednessChecker.literal(null, VALUE1);
95+
wellFormednessChecker.endRecord();
96+
wellFormednessChecker.closeStream();
97+
}
98+
99+
@Test(expected=WellformednessException.class)
100+
public void testShouldFailOnStartRecordInsideRecord() {
101+
wellFormednessChecker.startRecord(RECORD_ID1);
102+
wellFormednessChecker.startRecord(RECORD_ID2);
103+
wellFormednessChecker.endRecord();
104+
wellFormednessChecker.closeStream();
105+
}
106+
107+
@Test(expected=WellformednessException.class)
108+
public void testShouldFailOnEndRecordOutsideRecord() {
109+
wellFormednessChecker.endRecord();
110+
wellFormednessChecker.closeStream();
111+
}
112+
113+
@Test(expected=WellformednessException.class)
114+
public void testShouldFailOnStartEntityOutsideRecord() {
115+
wellFormednessChecker.startEntity(ENTITY1);
116+
wellFormednessChecker.closeStream();
117+
}
118+
119+
@Test(expected=WellformednessException.class)
120+
public void testShouldFailOnEndEntityOutsideRecord() {
121+
wellFormednessChecker.endEntity();
122+
wellFormednessChecker.closeStream();
123+
}
124+
125+
@Test(expected=WellformednessException.class)
126+
public void testShouldFailOnUnmatchedEndEntity() {
127+
wellFormednessChecker.startRecord(RECORD_ID1);
128+
wellFormednessChecker.endEntity();
129+
wellFormednessChecker.endRecord();
130+
wellFormednessChecker.closeStream();
131+
}
132+
133+
@Test(expected=WellformednessException.class)
134+
public void testShouldFailOnLiteralOutsideRecord() {
135+
wellFormednessChecker.literal(LITERAL1, VALUE1);
136+
wellFormednessChecker.closeStream();
137+
}
138+
139+
@Test(expected=WellformednessException.class)
140+
public void testShouldFailOnUnclosedRecord() {
141+
wellFormednessChecker.startRecord(RECORD_ID1);
142+
wellFormednessChecker.closeStream();
143+
}
144+
145+
@Test(expected=WellformednessException.class)
146+
public void testShouldFailOnUnclosedEntityAtEndRecord() {
147+
wellFormednessChecker.startRecord(RECORD_ID1);
148+
wellFormednessChecker.startEntity(ENTITY1);
149+
wellFormednessChecker.endRecord();
150+
wellFormednessChecker.closeStream();
151+
}
152+
153+
@Test(expected=WellformednessException.class)
154+
public void testShouldFailOnUnclosedEntityAtCloseStream() {
155+
wellFormednessChecker.startRecord(RECORD_ID1);
156+
wellFormednessChecker.startEntity(ENTITY1);
157+
wellFormednessChecker.closeStream();
158+
}
159+
160+
}

0 commit comments

Comments
 (0)