|
| 1 | +package algorithms; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | +import org.junit.Test; |
| 5 | +import com.baeldung.automata.*; |
| 6 | + |
| 7 | +/** |
| 8 | + * Tests for {@link RtFiniteStateMachine} |
| 9 | + */ |
| 10 | +public final class RtFiniteStateMachineTest { |
| 11 | + |
| 12 | + @Test |
| 13 | + public void acceptsSimplePair() { |
| 14 | + String json = "{\"key\":\"value\"}"; |
| 15 | + FiniteStateMachine machine = this.buildJsonStateMachine(); |
| 16 | + for (int i=0;i<json.length();i++) { |
| 17 | + machine = machine.switchState(String.valueOf(json.charAt(i))); |
| 18 | + } |
| 19 | + assertTrue(machine.canStop()); |
| 20 | + } |
| 21 | + @Test |
| 22 | + public void acceptsMorePairs() { |
| 23 | + String json = "{\"key1\":\"value1\",\"key2\":\"value2\"}"; |
| 24 | + FiniteStateMachine machine = this.buildJsonStateMachine(); |
| 25 | + for (int i=0;i<json.length();i++) { |
| 26 | + machine = machine.switchState(String.valueOf(json.charAt(i))); |
| 27 | + } |
| 28 | + assertTrue(machine.canStop()); |
| 29 | + } |
| 30 | + |
| 31 | + @Test(expected = IllegalArgumentException.class) |
| 32 | + public void missingColon() { |
| 33 | + String json = "{\"key\"\"value\"}"; |
| 34 | + FiniteStateMachine machine = this.buildJsonStateMachine(); |
| 35 | + for (int i=0;i<json.length();i++) { |
| 36 | + machine = machine.switchState(String.valueOf(json.charAt(i))); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Builds a finite state machine to validate a simple |
| 42 | + * Json object. |
| 43 | + * @return |
| 44 | + */ |
| 45 | + private FiniteStateMachine buildJsonStateMachine() { |
| 46 | + State first = new RtState(); |
| 47 | + State second = new RtState(); |
| 48 | + State third = new RtState(); |
| 49 | + State fourth = new RtState(); |
| 50 | + State fifth = new RtState(); |
| 51 | + State sixth = new RtState(); |
| 52 | + State seventh = new RtState(); |
| 53 | + State eighth = new RtState(true); |
| 54 | + |
| 55 | + first.with(new RtTransition("{", second)); |
| 56 | + second.with(new RtTransition("\"", third)); |
| 57 | + //Add transitions with chars 0-9 and a-z |
| 58 | + for (int i = 0; i < 26; i++) { |
| 59 | + if(i<10) { |
| 60 | + third = third.with( |
| 61 | + new RtTransition(String.valueOf(i), third) |
| 62 | + ); |
| 63 | + sixth = sixth.with( |
| 64 | + new RtTransition(String.valueOf(i), sixth) |
| 65 | + ); |
| 66 | + } |
| 67 | + third = third.with( |
| 68 | + new RtTransition(String.valueOf((char) ('a' + i)), third) |
| 69 | + ); |
| 70 | + sixth = sixth.with( |
| 71 | + new RtTransition(String.valueOf((char) ('a' + i)), sixth) |
| 72 | + ); |
| 73 | + } |
| 74 | + third.with(new RtTransition("\"", fourth)); |
| 75 | + fourth.with(new RtTransition(":", fifth)); |
| 76 | + fifth.with(new RtTransition("\"", sixth)); |
| 77 | + sixth.with(new RtTransition("\"", seventh)); |
| 78 | + seventh.with(new RtTransition(",", second)); |
| 79 | + seventh.with(new RtTransition("}", eighth)); |
| 80 | + return new RtFiniteStateMachine(first); |
| 81 | + } |
| 82 | +} |
0 commit comments