|
| 1 | +package wstxtest.evt; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | + |
| 5 | +import java.util.Objects; |
| 6 | + |
| 7 | +import javax.xml.stream.Location; |
| 8 | +import javax.xml.stream.events.XMLEvent; |
| 9 | + |
| 10 | +import org.hamcrest.Description; |
| 11 | +import org.hamcrest.Matcher; |
| 12 | +import org.hamcrest.TypeSafeMatcher; |
| 13 | +import org.junit.Before; |
| 14 | +import org.junit.Test; |
| 15 | + |
| 16 | +import com.ctc.wstx.io.WstxInputLocation; |
| 17 | +import com.ctc.wstx.stax.WstxEventFactory; |
| 18 | + |
| 19 | +public class TestEventFactoryLocation { |
| 20 | + |
| 21 | + private WstxEventFactory eventFactory; |
| 22 | + |
| 23 | + @Before |
| 24 | + public void setUp() { |
| 25 | + eventFactory = new WstxEventFactory(); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void testDefaultLocation() { |
| 30 | + XMLEvent event = eventFactory.createStartDocument(); |
| 31 | + |
| 32 | + assertThat("event.location", event.getLocation(), unknownLocation()); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testResetLocation() { |
| 37 | + eventFactory.setLocation(new WstxInputLocation(null, null, "about:blank", 3L, 2, 1)); |
| 38 | + eventFactory.setLocation(null); |
| 39 | + |
| 40 | + XMLEvent event = eventFactory.createStartElement("foo", "bar", "baz"); |
| 41 | + |
| 42 | + assertThat("event.location", event.getLocation(), unknownLocation()); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testNonVolatileLocation() { |
| 47 | + VolatileLocation volatileLocation = new VolatileLocation(2, 3); |
| 48 | + eventFactory.setLocation(volatileLocation); |
| 49 | + |
| 50 | + XMLEvent event = eventFactory.createEndElement("foo", "bar", "baz"); |
| 51 | + volatileLocation.line = 4; |
| 52 | + volatileLocation.col = 5; |
| 53 | + |
| 54 | + assertThat("event.location", event.getLocation(), |
| 55 | + locationWithProperties(null, null, -1, 2, 3)); |
| 56 | + } |
| 57 | + |
| 58 | + private static Matcher<Location> unknownLocation() { |
| 59 | + // XXX: Not sure if the empty-string publicId/systemId are conformant |
| 60 | + //return locationWithProperties(null, null, -1, -1, -1); |
| 61 | + return locationWithProperties("", "", -1, -1, -1); |
| 62 | + } |
| 63 | + |
| 64 | + private static Matcher<Location> locationWithProperties(final String pubId, |
| 65 | + final String sysId, final int charOffset, final int row, final int col) { |
| 66 | + return new TypeSafeMatcher<Location>() { |
| 67 | + @Override public void describeTo(Description description) { |
| 68 | + description.appendText("Location(publicId: ").appendValue(pubId) |
| 69 | + .appendText(", systemId: ").appendValue(sysId) |
| 70 | + .appendText(", characterOffset: ").appendValue(charOffset) |
| 71 | + .appendText(", lineNumber: ").appendValue(row) |
| 72 | + .appendText(", columnNumber: ").appendValue(col); |
| 73 | + } |
| 74 | + |
| 75 | + @Override protected boolean matchesSafely(Location item) { |
| 76 | + return Objects.equals(item.getPublicId(), pubId) |
| 77 | + && Objects.equals(item.getSystemId(), sysId) |
| 78 | + && Objects.equals(item.getCharacterOffset(), charOffset) |
| 79 | + && Objects.equals(item.getLineNumber(), row) |
| 80 | + && Objects.equals(item.getColumnNumber(), col); |
| 81 | + } |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + static class VolatileLocation implements Location { |
| 86 | + int line; |
| 87 | + int col; |
| 88 | + VolatileLocation(int line, int col) { |
| 89 | + this.line = line; |
| 90 | + this.col = col; |
| 91 | + } |
| 92 | + @Override public String getPublicId() { return null; } |
| 93 | + @Override public String getSystemId() { return null; } |
| 94 | + @Override public int getLineNumber() { return line; } |
| 95 | + @Override public int getColumnNumber() { return col; } |
| 96 | + @Override public int getCharacterOffset() { return -1; } |
| 97 | + } |
| 98 | +} |
0 commit comments