|
| 1 | +package com.baeldung.spel; |
| 2 | + |
| 3 | +import com.baeldung.spring.spel.examples.*; |
| 4 | +import org.junit.Assert; |
| 5 | +import org.junit.Test; |
| 6 | +import org.junit.runner.RunWith; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.test.context.ContextConfiguration; |
| 9 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 10 | + |
| 11 | +import static org.hamcrest.Matchers.equalTo; |
| 12 | + |
| 13 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 14 | +@ContextConfiguration(locations = {"/applicationContext.xml"}) |
| 15 | +public class SpelTest { |
| 16 | + |
| 17 | + @Autowired |
| 18 | + private SpelArithmetic spelArithmetic = new SpelArithmetic(); |
| 19 | + @Autowired |
| 20 | + private SpelCollections spelCollections = new SpelCollections(); |
| 21 | + @Autowired |
| 22 | + private SpelConditional spelConditional = new SpelConditional(); |
| 23 | + @Autowired |
| 24 | + private SpelLogical spelLogical = new SpelLogical(); |
| 25 | + @Autowired |
| 26 | + private SpelRegex spelRegex = new SpelRegex(); |
| 27 | + @Autowired |
| 28 | + private SpelRelational spelRelational = new SpelRelational(); |
| 29 | + |
| 30 | + @Test |
| 31 | + public void testArithmetic() throws Exception { |
| 32 | + Assert.assertThat(spelArithmetic.getAdd(), equalTo(20.0)); |
| 33 | + Assert.assertThat(spelArithmetic.getAddString(), equalTo("Some string plus other string")); |
| 34 | + Assert.assertThat(spelArithmetic.getSubtract(), equalTo(19.0)); |
| 35 | + Assert.assertThat(spelArithmetic.getMultiply(), equalTo(20.0)); |
| 36 | + Assert.assertThat(spelArithmetic.getDivide(), equalTo(18.0)); |
| 37 | + Assert.assertThat(spelArithmetic.getDivideAlphabetic(), equalTo(18.0)); |
| 38 | + Assert.assertThat(spelArithmetic.getModulo(), equalTo(7.0)); |
| 39 | + Assert.assertThat(spelArithmetic.getModuloAlphabetic(), equalTo(7.0)); |
| 40 | + Assert.assertThat(spelArithmetic.getPowerOf(), equalTo(512.0)); |
| 41 | + Assert.assertThat(spelArithmetic.getBrackets(), equalTo(17.0)); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testCollections() throws Exception { |
| 46 | + Assert.assertThat(spelCollections.getDriver1Car().getModel(), equalTo("Model1")); |
| 47 | + Assert.assertThat(spelCollections.getDriver2Car().getModel(), equalTo("Model2")); |
| 48 | + Assert.assertThat(spelCollections.getFirstCarInPark().getModel(), equalTo("Model1")); |
| 49 | + Assert.assertThat(spelCollections.getNumberOfCarsInPark(), equalTo(2)); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testConditional() throws Exception { |
| 54 | + Assert.assertThat(spelConditional.getTernary(), equalTo("Something went wrong. There was false value")); |
| 55 | + Assert.assertThat(spelConditional.getTernary2(), equalTo("Some model")); |
| 56 | + Assert.assertThat(spelConditional.getElvis(), equalTo("Some model")); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testLogical() throws Exception { |
| 61 | + Assert.assertThat(spelLogical.isAnd(), equalTo(true)); |
| 62 | + Assert.assertThat(spelLogical.isAndAlphabetic(), equalTo(true)); |
| 63 | + Assert.assertThat(spelLogical.isOr(), equalTo(true)); |
| 64 | + Assert.assertThat(spelLogical.isOrAlphabetic(), equalTo(true)); |
| 65 | + Assert.assertThat(spelLogical.isNot(), equalTo(false)); |
| 66 | + Assert.assertThat(spelLogical.isNotAlphabetic(), equalTo(false)); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testRegex() throws Exception { |
| 71 | + Assert.assertThat(spelRegex.isValidNumericStringResult(), equalTo(true)); |
| 72 | + Assert.assertThat(spelRegex.isInvalidNumericStringResult(), equalTo(false)); |
| 73 | + Assert.assertThat(spelRegex.isValidAlphabeticStringResult(), equalTo(true)); |
| 74 | + Assert.assertThat(spelRegex.isInvalidAlphabeticStringResult(), equalTo(false)); |
| 75 | + Assert.assertThat(spelRegex.isValidFormatOfHorsePower(), equalTo(true)); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testRelational() throws Exception { |
| 80 | + Assert.assertThat(spelRelational.isEqual(), equalTo(true)); |
| 81 | + Assert.assertThat(spelRelational.isEqualAlphabetic(), equalTo(true)); |
| 82 | + Assert.assertThat(spelRelational.isNotEqual(), equalTo(false)); |
| 83 | + Assert.assertThat(spelRelational.isNotEqualAlphabetic(), equalTo(false)); |
| 84 | + Assert.assertThat(spelRelational.isLessThan(), equalTo(false)); |
| 85 | + Assert.assertThat(spelRelational.isLessThanAlphabetic(), equalTo(false)); |
| 86 | + Assert.assertThat(spelRelational.isLessThanOrEqual(), equalTo(true)); |
| 87 | + Assert.assertThat(spelRelational.isLessThanOrEqualAlphabetic(), equalTo(true)); |
| 88 | + Assert.assertThat(spelRelational.isGreaterThan(), equalTo(false)); |
| 89 | + Assert.assertThat(spelRelational.isGreaterThanAlphabetic(), equalTo(false)); |
| 90 | + Assert.assertThat(spelRelational.isGreaterThanOrEqual(), equalTo(true)); |
| 91 | + Assert.assertThat(spelRelational.isGreaterThanOrEqualAlphabetic(), equalTo(true)); |
| 92 | + Assert.assertThat(spelRelational.isAnd(), equalTo(true)); |
| 93 | + Assert.assertThat(spelRelational.isAndAlphabetic(), equalTo(true)); |
| 94 | + Assert.assertThat(spelRelational.isOr(), equalTo(true)); |
| 95 | + Assert.assertThat(spelRelational.isOrAlphabetic(), equalTo(true)); |
| 96 | + Assert.assertThat(spelRelational.isNot(), equalTo(false)); |
| 97 | + Assert.assertThat(spelRelational.isNotAlphabetic(), equalTo(false)); |
| 98 | + } |
| 99 | +} |
0 commit comments