Added tests

This commit is contained in:
oborkovskyi 2016-04-11 18:59:05 +03:00
parent b05648e02f
commit edabeee9d8
4 changed files with 111 additions and 212 deletions

View File

@ -5,15 +5,10 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>guava</artifactId>
<artifactId>spel</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@ -26,13 +21,14 @@
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<artifactId>spring-context</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<artifactId>spring-test</artifactId>
<version>4.0.6.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -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 + "]";
}
}

View File

@ -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));
}
}

View File

@ -0,0 +1,8 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.baeldung.spring.spel" />
</beans>