From edabeee9d82c1e34459be95bc951e3b403ef7407 Mon Sep 17 00:00:00 2001 From: oborkovskyi Date: Mon, 11 Apr 2016 18:59:05 +0300 Subject: [PATCH] Added tests --- spring-spel/pom.xml | 12 +- .../spring/spel/examples/SpelOperators.java | 204 ------------------ .../test/java/com/baeldung/spel/SpelTest.java | 99 +++++++++ .../src/test/resources/applicationContext.xml | 8 + 4 files changed, 111 insertions(+), 212 deletions(-) delete mode 100644 spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelOperators.java create mode 100644 spring-spel/src/test/java/com/baeldung/spel/SpelTest.java create mode 100644 spring-spel/src/test/resources/applicationContext.xml diff --git a/spring-spel/pom.xml b/spring-spel/pom.xml index d7a9f9f7f3..12b7164e27 100644 --- a/spring-spel/pom.xml +++ b/spring-spel/pom.xml @@ -5,15 +5,10 @@ 4.0.0 com.baeldung - guava + spel 1.0-SNAPSHOT - - com.google.guava - guava - 19.0 - junit junit @@ -26,13 +21,14 @@ org.springframework - spring-core + spring-context 4.0.6.RELEASE org.springframework - spring-context + spring-test 4.0.6.RELEASE + test diff --git a/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelOperators.java b/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelOperators.java deleted file mode 100644 index 884ef73f6c..0000000000 --- a/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelOperators.java +++ /dev/null @@ -1,204 +0,0 @@ -package com.baeldung.spring.spel.examples; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; - -@Component("spelOperators") -public class SpelOperators { - - @Value("#{1 == 1}") - private boolean equal; - - @Value("#{1 != 1}") - private boolean notEqual; - - @Value("#{1 < 1}") - private boolean lessThan; - - @Value("#{1 <= 1}") - private boolean lessThanOrEqual; - - @Value("#{1 > 1}") - private boolean greaterThan; - - @Value("#{someCar.engine.numberOfCylinders >= 6}") - private boolean greaterThanOrEqual; - - @Value("#{someCar.horsePower == 250 and someCar.engine.capacity < 4000}") - private boolean and; - - @Value("#{someCar.horsePower > 300 or someCar.engine.capacity > 3000}") - private boolean or; - - @Value("#{!(someCar.engine.numberOfCylinders == 6)}") - private boolean not; - - @Value("#{1 + 1}") - private double add; - - @Value("#{someCar.model + ' manufactored by ' + someCar.make}") - private String addString; - - @Value("#{1 - 1}") - private double subtraction; - - @Value("#{1 * 1}") - private double multiply; - - @Value("#{10 / 2}") - private double divide; - - @Value("#{37 % 10}") - private double modulo; - - @Value("#{2 ^ 2}") - private double powerOf; - - public boolean isEqual() { - return equal; - } - - public void setEqual(boolean equal) { - this.equal = equal; - } - - public boolean isNotEqual() { - return notEqual; - } - - public void setNotEqual(boolean notEqual) { - this.notEqual = notEqual; - } - - public boolean isLessThan() { - return lessThan; - } - - public void setLessThan(boolean lessThan) { - this.lessThan = lessThan; - } - - public boolean isLessThanOrEqual() { - return lessThanOrEqual; - } - - public void setLessThanOrEqual(boolean lessThanOrEqual) { - this.lessThanOrEqual = lessThanOrEqual; - } - - public boolean isGreaterThan() { - return greaterThan; - } - - public void setGreaterThan(boolean greaterThan) { - this.greaterThan = greaterThan; - } - - public boolean isGreaterThanOrEqual() { - return greaterThanOrEqual; - } - - public void setGreaterThanOrEqual(boolean greaterThanOrEqual) { - this.greaterThanOrEqual = greaterThanOrEqual; - } - - public boolean isAnd() { - return and; - } - - public void setAnd(boolean and) { - this.and = and; - } - - public boolean isOr() { - return or; - } - - public void setOr(boolean or) { - this.or = or; - } - - public boolean isNot() { - return not; - } - - public void setNot(boolean not) { - this.not = not; - } - - public double getAdd() { - return add; - } - - public void setAdd(double add) { - this.add = add; - } - - public String getAddString() { - return addString; - } - - public void setAddString(String addString) { - this.addString = addString; - } - - public double getSubtraction() { - return subtraction; - } - - public void setSubtraction(double subtraction) { - this.subtraction = subtraction; - } - - public double getMultiply() { - return multiply; - } - - public void setMultiply(double multiply) { - this.multiply = multiply; - } - - public double getDivide() { - return divide; - } - - public void setDivide(double divide) { - this.divide = divide; - } - - public double getModulo() { - return modulo; - } - - public void setModulo(double modulo) { - this.modulo = modulo; - } - - public double getPowerOf() { - return powerOf; - } - - public void setPowerOf(double powerOf) { - this.powerOf = powerOf; - } - - @Override - public String toString() { - return "[equal=" + equal + ", " + - "notEqual=" + notEqual + ", " + - "lessThan=" + lessThan + ", " + - "lessThanOrEqual=" + lessThanOrEqual + ", " + - "greaterThan=" + greaterThan + ", " + - "greaterThanOrEqual=" + greaterThanOrEqual + ", " + - "and=" + and + ", " + - "or=" + or + ", " + - "not=" + not + ", " + - "add=" + add + ", " + - "addString=" + addString + ", " + - "subtraction=" + subtraction + ", " + - "multiply=" + multiply + ", " + - "divide=" + divide + ", " + - "modulo=" + modulo + ", " + - "powerOf=" + powerOf + "]"; - } -} diff --git a/spring-spel/src/test/java/com/baeldung/spel/SpelTest.java b/spring-spel/src/test/java/com/baeldung/spel/SpelTest.java new file mode 100644 index 0000000000..3ca0a04f5b --- /dev/null +++ b/spring-spel/src/test/java/com/baeldung/spel/SpelTest.java @@ -0,0 +1,99 @@ +package com.baeldung.spel; + +import com.baeldung.spring.spel.examples.*; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.hamcrest.Matchers.equalTo; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = {"/applicationContext.xml"}) +public class SpelTest { + + @Autowired + private SpelArithmetic spelArithmetic = new SpelArithmetic(); + @Autowired + private SpelCollections spelCollections = new SpelCollections(); + @Autowired + private SpelConditional spelConditional = new SpelConditional(); + @Autowired + private SpelLogical spelLogical = new SpelLogical(); + @Autowired + private SpelRegex spelRegex = new SpelRegex(); + @Autowired + private SpelRelational spelRelational = new SpelRelational(); + + @Test + public void testArithmetic() throws Exception { + Assert.assertThat(spelArithmetic.getAdd(), equalTo(20.0)); + Assert.assertThat(spelArithmetic.getAddString(), equalTo("Some string plus other string")); + Assert.assertThat(spelArithmetic.getSubtract(), equalTo(19.0)); + Assert.assertThat(spelArithmetic.getMultiply(), equalTo(20.0)); + Assert.assertThat(spelArithmetic.getDivide(), equalTo(18.0)); + Assert.assertThat(spelArithmetic.getDivideAlphabetic(), equalTo(18.0)); + Assert.assertThat(spelArithmetic.getModulo(), equalTo(7.0)); + Assert.assertThat(spelArithmetic.getModuloAlphabetic(), equalTo(7.0)); + Assert.assertThat(spelArithmetic.getPowerOf(), equalTo(512.0)); + Assert.assertThat(spelArithmetic.getBrackets(), equalTo(17.0)); + } + + @Test + public void testCollections() throws Exception { + Assert.assertThat(spelCollections.getDriver1Car().getModel(), equalTo("Model1")); + Assert.assertThat(spelCollections.getDriver2Car().getModel(), equalTo("Model2")); + Assert.assertThat(spelCollections.getFirstCarInPark().getModel(), equalTo("Model1")); + Assert.assertThat(spelCollections.getNumberOfCarsInPark(), equalTo(2)); + } + + @Test + public void testConditional() throws Exception { + Assert.assertThat(spelConditional.getTernary(), equalTo("Something went wrong. There was false value")); + Assert.assertThat(spelConditional.getTernary2(), equalTo("Some model")); + Assert.assertThat(spelConditional.getElvis(), equalTo("Some model")); + } + + @Test + public void testLogical() throws Exception { + Assert.assertThat(spelLogical.isAnd(), equalTo(true)); + Assert.assertThat(spelLogical.isAndAlphabetic(), equalTo(true)); + Assert.assertThat(spelLogical.isOr(), equalTo(true)); + Assert.assertThat(spelLogical.isOrAlphabetic(), equalTo(true)); + Assert.assertThat(spelLogical.isNot(), equalTo(false)); + Assert.assertThat(spelLogical.isNotAlphabetic(), equalTo(false)); + } + + @Test + public void testRegex() throws Exception { + Assert.assertThat(spelRegex.isValidNumericStringResult(), equalTo(true)); + Assert.assertThat(spelRegex.isInvalidNumericStringResult(), equalTo(false)); + Assert.assertThat(spelRegex.isValidAlphabeticStringResult(), equalTo(true)); + Assert.assertThat(spelRegex.isInvalidAlphabeticStringResult(), equalTo(false)); + Assert.assertThat(spelRegex.isValidFormatOfHorsePower(), equalTo(true)); + } + + @Test + public void testRelational() throws Exception { + Assert.assertThat(spelRelational.isEqual(), equalTo(true)); + Assert.assertThat(spelRelational.isEqualAlphabetic(), equalTo(true)); + Assert.assertThat(spelRelational.isNotEqual(), equalTo(false)); + Assert.assertThat(spelRelational.isNotEqualAlphabetic(), equalTo(false)); + Assert.assertThat(spelRelational.isLessThan(), equalTo(false)); + Assert.assertThat(spelRelational.isLessThanAlphabetic(), equalTo(false)); + Assert.assertThat(spelRelational.isLessThanOrEqual(), equalTo(true)); + Assert.assertThat(spelRelational.isLessThanOrEqualAlphabetic(), equalTo(true)); + Assert.assertThat(spelRelational.isGreaterThan(), equalTo(false)); + Assert.assertThat(spelRelational.isGreaterThanAlphabetic(), equalTo(false)); + Assert.assertThat(spelRelational.isGreaterThanOrEqual(), equalTo(true)); + Assert.assertThat(spelRelational.isGreaterThanOrEqualAlphabetic(), equalTo(true)); + Assert.assertThat(spelRelational.isAnd(), equalTo(true)); + Assert.assertThat(spelRelational.isAndAlphabetic(), equalTo(true)); + Assert.assertThat(spelRelational.isOr(), equalTo(true)); + Assert.assertThat(spelRelational.isOrAlphabetic(), equalTo(true)); + Assert.assertThat(spelRelational.isNot(), equalTo(false)); + Assert.assertThat(spelRelational.isNotAlphabetic(), equalTo(false)); + } +} diff --git a/spring-spel/src/test/resources/applicationContext.xml b/spring-spel/src/test/resources/applicationContext.xml new file mode 100644 index 0000000000..815ac35837 --- /dev/null +++ b/spring-spel/src/test/resources/applicationContext.xml @@ -0,0 +1,8 @@ + + + + + \ No newline at end of file