diff --git a/spring-spel/pom.xml b/spring-spel/pom.xml new file mode 100644 index 0000000000..12b7164e27 --- /dev/null +++ b/spring-spel/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + + com.baeldung + spel + 1.0-SNAPSHOT + + + + junit + junit + 4.12 + + + org.hamcrest + hamcrest-all + 1.3 + + + org.springframework + spring-context + 4.0.6.RELEASE + + + org.springframework + spring-test + 4.0.6.RELEASE + test + + + + + + maven-compiler-plugin + 3.3 + + true + true + 1.8 + 1.8 + UTF-8 + true + true + + + + + + \ No newline at end of file diff --git a/spring-spel/src/main/java/com/baeldung/spring/spel/SpelProgram.java b/spring-spel/src/main/java/com/baeldung/spring/spel/SpelProgram.java new file mode 100644 index 0000000000..f399ce4a9c --- /dev/null +++ b/spring-spel/src/main/java/com/baeldung/spring/spel/SpelProgram.java @@ -0,0 +1,17 @@ +package com.baeldung.spring.spel; + +import com.baeldung.spring.spel.examples.SpelConditional; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class SpelProgram { + public static void main(String[] args) { + ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); + SpelConditional spelCollections = (SpelConditional) context.getBean("spelConditional"); + + // Here you can choose which bean do you want to load insted of spelConditional: spelCollections, spelLogical, etc. + + System.out.println(spelCollections); + } + +} diff --git a/spring-spel/src/main/java/com/baeldung/spring/spel/entity/Car.java b/spring-spel/src/main/java/com/baeldung/spring/spel/entity/Car.java new file mode 100644 index 0000000000..6ad665bcba --- /dev/null +++ b/spring-spel/src/main/java/com/baeldung/spring/spel/entity/Car.java @@ -0,0 +1,91 @@ +package com.baeldung.spring.spel.entity; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component("someCar") +public class Car { + @Value("Some make") + private String make; + @Value("Some model") + private String model; + @Value("#{engine}") + private Engine engine; + @Value("#{engine.horsePower}") + private int horsePower; + @Value("2007") + private int yearOfProduction; + @Value("#{engine.capacity >= 3000}") + private boolean isBigEngine; + @Value("#{someCar.yearOfProduction > 2016}") + private boolean isNewCar; + + public String getMake() { + return make; + } + + public void setMake(String make) { + this.make = make; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public Engine getEngine() { + return engine; + } + + public void setEngine(Engine engine) { + this.engine = engine; + } + + public int getHorsePower() { + return horsePower; + } + + public void setHorsePower(int horsePower) { + this.horsePower = horsePower; + } + + public int getYearOfProduction() { + return yearOfProduction; + } + + public void setYearOfProduction(int yearOfProduction) { + this.yearOfProduction = yearOfProduction; + } + + public boolean isBigEngine() { + return isBigEngine; + } + + public void setBigEngine(boolean bigEngine) { + isBigEngine = bigEngine; + } + + public boolean isNewCar() { + return isNewCar; + } + + public void setNewCar(boolean newCar) { + isNewCar = newCar; + } + + @Override + public String toString() { + return "Car{" + + "make='" + make + '\'' + + ", model='" + model + '\'' + + ", engine=" + engine + + ", horsePower=" + horsePower + + ", yearOfProduction=" + yearOfProduction + + ", isBigEngine=" + isBigEngine + + ", isNewCar=" + isNewCar + + '}'; + } +} diff --git a/spring-spel/src/main/java/com/baeldung/spring/spel/entity/CarPark.java b/spring-spel/src/main/java/com/baeldung/spring/spel/entity/CarPark.java new file mode 100644 index 0000000000..6475405383 --- /dev/null +++ b/spring-spel/src/main/java/com/baeldung/spring/spel/entity/CarPark.java @@ -0,0 +1,48 @@ +package com.baeldung.spring.spel.entity; + +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Component("carPark") +public class CarPark { + private List cars = new ArrayList<>(); + private Map carsByDriver = new HashMap<>(); + + public CarPark() { + Car model1 = new Car(); + model1.setMake("Good company"); + model1.setModel("Model1"); + model1.setYearOfProduction(1998); + + Car model2 = new Car(); + model2.setMake("Good company"); + model2.setModel("Model2"); + model2.setYearOfProduction(2005); + + cars.add(model1); + cars.add(model2); + + carsByDriver.put("Driver1", model1); + carsByDriver.put("Driver2", model2); + } + + public List getCars() { + return cars; + } + + public void setCars(List cars) { + this.cars = cars; + } + + public Map getCarsByDriver() { + return carsByDriver; + } + + public void setCarsByDriver(Map carsByDriver) { + this.carsByDriver = carsByDriver; + } +} diff --git a/spring-spel/src/main/java/com/baeldung/spring/spel/entity/Engine.java b/spring-spel/src/main/java/com/baeldung/spring/spel/entity/Engine.java new file mode 100644 index 0000000000..9b9d125170 --- /dev/null +++ b/spring-spel/src/main/java/com/baeldung/spring/spel/entity/Engine.java @@ -0,0 +1,47 @@ +package com.baeldung.spring.spel.entity; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component("engine") +public class Engine { + @Value("3200") + private int capacity; + @Value("250") + private int horsePower; + @Value("6") + private int numberOfCylinders; + + public int getCapacity() { + return capacity; + } + + public void setCapacity(int capacity) { + this.capacity = capacity; + } + + public int getHorsePower() { + return horsePower; + } + + public void setHorsePower(int horsePower) { + this.horsePower = horsePower; + } + + public int getNumberOfCylinders() { + return numberOfCylinders; + } + + public void setNumberOfCylinders(int numberOfCylinders) { + this.numberOfCylinders = numberOfCylinders; + } + + @Override + public String toString() { + return "Engine{" + + "capacity=" + capacity + + ", horsePower=" + horsePower + + ", numberOfCylinders=" + numberOfCylinders + + '}'; + } +} diff --git a/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelArithmetic.java b/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelArithmetic.java new file mode 100644 index 0000000000..ba21c87931 --- /dev/null +++ b/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelArithmetic.java @@ -0,0 +1,131 @@ +package com.baeldung.spring.spel.examples; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component("spelArithmetic") +public class SpelArithmetic { + @Value("#{19 + 1}") + private double add; + + @Value("#{'Some string ' + 'plus other string'}") + private String addString; + + @Value("#{20 - 1}") + private double subtract; + + @Value("#{10 * 2}") + private double multiply; + + @Value("#{36 / 2}") + private double divide; + @Value("#{36 div 2}") + private double divideAlphabetic; + + @Value("#{37 % 10}") + private double modulo; + @Value("#{37 mod 10}") + private double moduloAlphabetic; + + @Value("#{2 ^ 9}") + private double powerOf; + + @Value("#{(2 + 2) * 2 + 9}") + private double brackets; + + 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 getSubtract() { + return subtract; + } + + public void setSubtract(double subtract) { + this.subtract = subtract; + } + + 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 getDivideAlphabetic() { + return divideAlphabetic; + } + + public void setDivideAlphabetic(double divideAlphabetic) { + this.divideAlphabetic = divideAlphabetic; + } + + public double getModulo() { + return modulo; + } + + public void setModulo(double modulo) { + this.modulo = modulo; + } + + public double getModuloAlphabetic() { + return moduloAlphabetic; + } + + public void setModuloAlphabetic(double moduloAlphabetic) { + this.moduloAlphabetic = moduloAlphabetic; + } + + public double getPowerOf() { + return powerOf; + } + + public void setPowerOf(double powerOf) { + this.powerOf = powerOf; + } + + public double getBrackets() { + return brackets; + } + + public void setBrackets(double brackets) { + this.brackets = brackets; + } + + @Override + public String toString() { + return "SpelArithmetic{" + + "add=" + add + + ", addString='" + addString + '\'' + + ", subtract=" + subtract + + ", multiply=" + multiply + + ", divide=" + divide + + ", divideAlphabetic=" + divideAlphabetic + + ", modulo=" + modulo + + ", moduloAlphabetic=" + moduloAlphabetic + + ", powerOf=" + powerOf + + ", brackets=" + brackets + + '}'; + } +} diff --git a/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelCollections.java b/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelCollections.java new file mode 100644 index 0000000000..6c606bbd75 --- /dev/null +++ b/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelCollections.java @@ -0,0 +1,59 @@ +package com.baeldung.spring.spel.examples; + +import com.baeldung.spring.spel.entity.Car; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component("spelCollections") +public class SpelCollections { + @Value("#{carPark.carsByDriver['Driver1']}") + private Car driver1Car; + @Value("#{carPark.carsByDriver['Driver2']}") + private Car driver2Car; + @Value("#{carPark.cars[0]}") + private Car firstCarInPark; + @Value("#{carPark.cars.size()}") + private Integer numberOfCarsInPark; + + public Car getDriver1Car() { + return driver1Car; + } + + public void setDriver1Car(Car driver1Car) { + this.driver1Car = driver1Car; + } + + public Car getDriver2Car() { + return driver2Car; + } + + public void setDriver2Car(Car driver2Car) { + this.driver2Car = driver2Car; + } + + public Car getFirstCarInPark() { + return firstCarInPark; + } + + public void setFirstCarInPark(Car firstCarInPark) { + this.firstCarInPark = firstCarInPark; + } + + public Integer getNumberOfCarsInPark() { + return numberOfCarsInPark; + } + + public void setNumberOfCarsInPark(Integer numberOfCarsInPark) { + this.numberOfCarsInPark = numberOfCarsInPark; + } + + @Override + public String toString() { + return "[" + + "driver1Car=" + driver1Car + + ", driver2Car=" + driver2Car + + ", firstCarInPark=" + firstCarInPark + + ", numberOfCarsInPark=" + numberOfCarsInPark + + ']'; + } +} diff --git a/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelConditional.java b/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelConditional.java new file mode 100644 index 0000000000..6e255c0425 --- /dev/null +++ b/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelConditional.java @@ -0,0 +1,49 @@ +package com.baeldung.spring.spel.examples; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component("spelConditional") +public class SpelConditional { + @Value("#{false ? 'There was true value' : 'Something went wrong. There was false value'}") + private String ternary; + + @Value("#{someCar.model != null ? someCar.model : 'Unknown model'}") + private String ternary2; + + @Value("#{someCar.model?:'Unknown model'}") + private String elvis; + + public String getTernary() { + return ternary; + } + + public void setTernary(String ternary) { + this.ternary = ternary; + } + + public String getTernary2() { + return ternary2; + } + + public void setTernary2(String ternary2) { + this.ternary2 = ternary2; + } + + public String getElvis() { + return elvis; + } + + public void setElvis(String elvis) { + this.elvis = elvis; + } + + @Override + public String toString() { + return "SpelConditional{" + + "ternary='" + ternary + '\'' + + ", ternary2='" + ternary2 + '\'' + + ", elvis='" + elvis + '\'' + + '}'; + } +} diff --git a/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelLogical.java b/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelLogical.java new file mode 100644 index 0000000000..09b4e35c9d --- /dev/null +++ b/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelLogical.java @@ -0,0 +1,82 @@ +package com.baeldung.spring.spel.examples; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component("spelLogical") +public class SpelLogical { + @Value("#{250 > 200 && 200 < 4000}") + private boolean and; + @Value("#{250 > 200 and 200 < 4000}") + private boolean andAlphabetic; + + @Value("#{400 > 300 || 150 < 100}") + private boolean or; + @Value("#{400 > 300 or 150 < 100}") + private boolean orAlphabetic; + + @Value("#{!true}") + private boolean not; + @Value("#{not true}") + private boolean notAlphabetic; + + public boolean isAnd() { + return and; + } + + public void setAnd(boolean and) { + this.and = and; + } + + public boolean isAndAlphabetic() { + return andAlphabetic; + } + + public void setAndAlphabetic(boolean andAlphabetic) { + this.andAlphabetic = andAlphabetic; + } + + public boolean isOr() { + return or; + } + + public void setOr(boolean or) { + this.or = or; + } + + public boolean isOrAlphabetic() { + return orAlphabetic; + } + + public void setOrAlphabetic(boolean orAlphabetic) { + this.orAlphabetic = orAlphabetic; + } + + public boolean isNot() { + return not; + } + + public void setNot(boolean not) { + this.not = not; + } + + public boolean isNotAlphabetic() { + return notAlphabetic; + } + + public void setNotAlphabetic(boolean notAlphabetic) { + this.notAlphabetic = notAlphabetic; + } + + @Override + public String toString() { + return "SpelLogical{" + + "and=" + and + + ", andAlphabetic=" + andAlphabetic + + ", or=" + or + + ", orAlphabetic=" + orAlphabetic + + ", not=" + not + + ", notAlphabetic=" + notAlphabetic + + '}'; + } +} diff --git a/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelParser.java b/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelParser.java new file mode 100644 index 0000000000..f7063d4c46 --- /dev/null +++ b/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelParser.java @@ -0,0 +1,30 @@ +package com.baeldung.spring.spel.examples; + +import com.baeldung.spring.spel.entity.Car; +import com.baeldung.spring.spel.entity.CarPark; +import org.springframework.expression.ExpressionParser; +import org.springframework.expression.spel.SpelParserConfiguration; +import org.springframework.expression.spel.standard.SpelExpressionParser; +import org.springframework.expression.spel.support.StandardEvaluationContext; + +public class SpelParser { + public static void main(String[] args) { + Car car = new Car(); + car.setMake("Good manufacturer"); + car.setModel("Model 3"); + car.setYearOfProduction(2014); + + CarPark carPark = new CarPark(); + + SpelParserConfiguration config = new SpelParserConfiguration(true, true); + + StandardEvaluationContext context = new StandardEvaluationContext(carPark); + + ExpressionParser expressionParser = new SpelExpressionParser(config); + expressionParser.parseExpression("cars[0]").setValue(context, car); + + Car result = carPark.getCars().get(0); + + System.out.println(result); + } +} diff --git a/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelRegex.java b/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelRegex.java new file mode 100644 index 0000000000..a08142ca1c --- /dev/null +++ b/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelRegex.java @@ -0,0 +1,74 @@ +package com.baeldung.spring.spel.examples; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component("spelRegex") +public class SpelRegex { + + @Value("#{100 matches '\\d+' }") + private boolean validNumericStringResult; + + @Value("#{'100fghdjf' matches '\\d+' }") + private boolean invalidNumericStringResult; + + @Value("#{'valid alphabetic string' matches '[a-zA-Z\\s]+' }") + private boolean validAlphabeticStringResult; + + @Value("#{'invalid alphabetic string #$1' matches '[a-zA-Z\\s]+' }") + private boolean invalidAlphabeticStringResult; + + @Value("#{engine.horsePower matches '\\d+'}") + private boolean validFormatOfHorsePower; + + public boolean isValidNumericStringResult() { + return validNumericStringResult; + } + + public void setValidNumericStringResult(boolean validNumericStringResult) { + this.validNumericStringResult = validNumericStringResult; + } + + public boolean isInvalidNumericStringResult() { + return invalidNumericStringResult; + } + + public void setInvalidNumericStringResult(boolean invalidNumericStringResult) { + this.invalidNumericStringResult = invalidNumericStringResult; + } + + public boolean isValidAlphabeticStringResult() { + return validAlphabeticStringResult; + } + + public void setValidAlphabeticStringResult(boolean validAlphabeticStringResult) { + this.validAlphabeticStringResult = validAlphabeticStringResult; + } + + public boolean isInvalidAlphabeticStringResult() { + return invalidAlphabeticStringResult; + } + + public void setInvalidAlphabeticStringResult(boolean invalidAlphabeticStringResult) { + this.invalidAlphabeticStringResult = invalidAlphabeticStringResult; + } + + public boolean isValidFormatOfHorsePower() { + return validFormatOfHorsePower; + } + + public void setValidFormatOfHorsePower(boolean validFormatOfHorsePower) { + this.validFormatOfHorsePower = validFormatOfHorsePower; + } + + @Override + public String toString() { + return "SpelRegex{" + + "validNumericStringResult=" + validNumericStringResult + + ", invalidNumericStringResult=" + invalidNumericStringResult + + ", validAlphabeticStringResult=" + validAlphabeticStringResult + + ", invalidAlphabeticStringResult=" + invalidAlphabeticStringResult + + ", validFormatOfHorsePower=" + validFormatOfHorsePower + + '}'; + } +} diff --git a/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelRelational.java b/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelRelational.java new file mode 100644 index 0000000000..b811595429 --- /dev/null +++ b/spring-spel/src/main/java/com/baeldung/spring/spel/examples/SpelRelational.java @@ -0,0 +1,229 @@ +package com.baeldung.spring.spel.examples; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component("spelRelational") +public class SpelRelational { + @Value("#{1 == 1}") + private boolean equal; + + @Value("#{1 eq 1}") + private boolean equalAlphabetic; + + @Value("#{1 != 1}") + private boolean notEqual; + + @Value("#{1 ne 1}") + private boolean notEqualAlphabetic; + + @Value("#{1 < 1}") + private boolean lessThan; + + @Value("#{1 lt 1}") + private boolean lessThanAlphabetic; + + @Value("#{1 <= 1}") + private boolean lessThanOrEqual; + + @Value("#{1 le 1}") + private boolean lessThanOrEqualAlphabetic; + + @Value("#{1 > 1}") + private boolean greaterThan; + + @Value("#{1 gt 1}") + private boolean greaterThanAlphabetic; + + @Value("#{9 >= 6}") + private boolean greaterThanOrEqual; + + @Value("#{9 ge 6}") + private boolean greaterThanOrEqualAlphabetic; + + @Value("#{250 > 200 && 200 < 4000}") + private boolean and; + + @Value("#{250 > 200 and 200 < 4000}") + private boolean andAlphabetic; + + @Value("#{400 > 300 || 150 < 100}") + private boolean or; + + @Value("#{400 > 300 or 150 < 100}") + private boolean orAlphabetic; + + @Value("#{!true}") + private boolean not; + + @Value("#{not true}") + private boolean notAlphabetic; + + public boolean isEqual() { + return equal; + } + + public void setEqual(boolean equal) { + this.equal = equal; + } + + public boolean isEqualAlphabetic() { + return equalAlphabetic; + } + + public void setEqualAlphabetic(boolean equalAlphabetic) { + this.equalAlphabetic = equalAlphabetic; + } + + public boolean isNotEqual() { + return notEqual; + } + + public void setNotEqual(boolean notEqual) { + this.notEqual = notEqual; + } + + public boolean isNotEqualAlphabetic() { + return notEqualAlphabetic; + } + + public void setNotEqualAlphabetic(boolean notEqualAlphabetic) { + this.notEqualAlphabetic = notEqualAlphabetic; + } + + public boolean isLessThan() { + return lessThan; + } + + public void setLessThan(boolean lessThan) { + this.lessThan = lessThan; + } + + public boolean isLessThanAlphabetic() { + return lessThanAlphabetic; + } + + public void setLessThanAlphabetic(boolean lessThanAlphabetic) { + this.lessThanAlphabetic = lessThanAlphabetic; + } + + public boolean isLessThanOrEqual() { + return lessThanOrEqual; + } + + public void setLessThanOrEqual(boolean lessThanOrEqual) { + this.lessThanOrEqual = lessThanOrEqual; + } + + public boolean isLessThanOrEqualAlphabetic() { + return lessThanOrEqualAlphabetic; + } + + public void setLessThanOrEqualAlphabetic(boolean lessThanOrEqualAlphabetic) { + this.lessThanOrEqualAlphabetic = lessThanOrEqualAlphabetic; + } + + public boolean isGreaterThan() { + return greaterThan; + } + + public void setGreaterThan(boolean greaterThan) { + this.greaterThan = greaterThan; + } + + public boolean isGreaterThanAlphabetic() { + return greaterThanAlphabetic; + } + + public void setGreaterThanAlphabetic(boolean greaterThanAlphabetic) { + this.greaterThanAlphabetic = greaterThanAlphabetic; + } + + public boolean isGreaterThanOrEqual() { + return greaterThanOrEqual; + } + + public void setGreaterThanOrEqual(boolean greaterThanOrEqual) { + this.greaterThanOrEqual = greaterThanOrEqual; + } + + public boolean isGreaterThanOrEqualAlphabetic() { + return greaterThanOrEqualAlphabetic; + } + + public void setGreaterThanOrEqualAlphabetic(boolean greaterThanOrEqualAlphabetic) { + this.greaterThanOrEqualAlphabetic = greaterThanOrEqualAlphabetic; + } + + public boolean isAnd() { + return and; + } + + public void setAnd(boolean and) { + this.and = and; + } + + public boolean isAndAlphabetic() { + return andAlphabetic; + } + + public void setAndAlphabetic(boolean andAlphabetic) { + this.andAlphabetic = andAlphabetic; + } + + public boolean isOr() { + return or; + } + + public void setOr(boolean or) { + this.or = or; + } + + public boolean isOrAlphabetic() { + return orAlphabetic; + } + + public void setOrAlphabetic(boolean orAlphabetic) { + this.orAlphabetic = orAlphabetic; + } + + public boolean isNot() { + return not; + } + + public void setNot(boolean not) { + this.not = not; + } + + public boolean isNotAlphabetic() { + return notAlphabetic; + } + + public void setNotAlphabetic(boolean notAlphabetic) { + this.notAlphabetic = notAlphabetic; + } + + @Override + public String toString() { + return "SpelRelational{" + + "equal=" + equal + + ", equalAlphabetic=" + equalAlphabetic + + ", notEqual=" + notEqual + + ", notEqualAlphabetic=" + notEqualAlphabetic + + ", lessThan=" + lessThan + + ", lessThanAlphabetic=" + lessThanAlphabetic + + ", lessThanOrEqual=" + lessThanOrEqual + + ", lessThanOrEqualAlphabetic=" + lessThanOrEqualAlphabetic + + ", greaterThan=" + greaterThan + + ", greaterThanAlphabetic=" + greaterThanAlphabetic + + ", greaterThanOrEqual=" + greaterThanOrEqual + + ", greaterThanOrEqualAlphabetic=" + greaterThanOrEqualAlphabetic + + ", and=" + and + + ", andAlphabetic=" + andAlphabetic + + ", or=" + or + + ", orAlphabetic=" + orAlphabetic + + ", not=" + not + + ", notAlphabetic=" + notAlphabetic + + '}'; + } +} diff --git a/spring-spel/src/main/resources/applicationContext.xml b/spring-spel/src/main/resources/applicationContext.xml new file mode 100644 index 0000000000..998addae67 --- /dev/null +++ b/spring-spel/src/main/resources/applicationContext.xml @@ -0,0 +1,8 @@ + + + + + \ No newline at end of file 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..998addae67 --- /dev/null +++ b/spring-spel/src/test/resources/applicationContext.xml @@ -0,0 +1,8 @@ + + + + + \ No newline at end of file