-
Notifications
You must be signed in to change notification settings - Fork 54k
[BAEL-9235] - Introduction to MVEL #19018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LordMaduz
wants to merge
3
commits into
eugenp:master
Choose a base branch
from
LordMaduz:BAEL-9235
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <artifactId>mvel</artifactId> | ||
| <name>mvel</name> | ||
|
|
||
| <parent> | ||
| <groupId>com.baeldung</groupId> | ||
| <artifactId>parent-modules</artifactId> | ||
| <version>1.0.0-SNAPSHOT</version> | ||
| </parent> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.mvel</groupId> | ||
| <artifactId>mvel2</artifactId> | ||
| <version>${mvel.version}</version> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <properties> | ||
| <mvel.version>2.5.2.Final</mvel.version> | ||
| </properties> | ||
|
|
||
| </project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| package com.baeldung.mvel.model; | ||
|
|
||
| public class Employee { | ||
| private String name; | ||
| private Job job; | ||
|
|
||
| public Employee(String name, Job job) { | ||
| this.name = name; | ||
| this.job = job; | ||
| } | ||
|
|
||
| public Employee(String name, int experienceInYears, double salary, String department) { | ||
| this.name = name; | ||
| this.job = new Job(experienceInYears, salary, department); | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public void setJob(Job job) { | ||
| this.job = job; | ||
| } | ||
|
|
||
| public Job getJob() { | ||
| return job; | ||
| } | ||
|
|
||
|
|
||
| public static class Job { | ||
| private int experienceInYears; | ||
| private double salary; | ||
| private String department; | ||
|
|
||
| public Job(int experienceInYears, double salary) { | ||
| this.experienceInYears = experienceInYears; | ||
| this.salary = salary; | ||
| } | ||
|
|
||
| public Job(int experienceInYears, double salary, String department) { | ||
| this.experienceInYears = experienceInYears; | ||
| this.salary = salary; | ||
| this.department = department; | ||
| } | ||
|
|
||
| public int getExperienceInYears() { | ||
| return experienceInYears; | ||
| } | ||
|
|
||
| public void setExperienceInYears(int experienceInYears) { | ||
| this.experienceInYears = experienceInYears; | ||
| } | ||
|
|
||
| public double getSalary() { | ||
| return salary; | ||
| } | ||
|
|
||
| public void setSalary(double salary) { | ||
| this.salary = salary; | ||
| } | ||
|
|
||
| public String getDepartment() { | ||
| return department; | ||
| } | ||
|
|
||
| public void setDepartment(String department) { | ||
| this.department = department; | ||
| } | ||
|
|
||
| public double calculateAnnualBonus() { | ||
| if (experienceInYears > 10) { | ||
| return salary * 0.20; | ||
| } else if (experienceInYears > 5) { | ||
| return salary * 0.10; | ||
| } else { | ||
| return salary * 0.05; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } |
142 changes: 142 additions & 0 deletions
142
mvel/src/test/java/com/baeldung/mvel/MvelServiceUnitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| package com.baeldung.mvel; | ||
|
|
||
| import com.baeldung.mvel.model.Employee; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mvel2.MVEL; | ||
|
|
||
| import java.io.Serializable; | ||
| import java.util.Arrays; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| public class MvelServiceUnitTest { | ||
|
|
||
| @Test | ||
| public void givenSimpleExpression_whenEvaluate_thenCorrectResult() { | ||
| String expression = "5 + 3 * 2"; | ||
| Object result = MVEL.eval(expression); | ||
|
|
||
| assertEquals(11, result); | ||
| } | ||
|
|
||
| @Test | ||
| public void givenVariables_whenEvaluateExpression_thenCorrectResult() { | ||
| Map<String, Object> variables = new HashMap<>(); | ||
| variables.put("salary", 75000.0); | ||
| variables.put("experience", 8); | ||
|
|
||
| String expression = "salary * 0.10"; | ||
| Object result = MVEL.eval(expression, variables); | ||
|
|
||
| assertEquals(7500.0, result); | ||
| } | ||
|
|
||
| @Test | ||
| public void givenBooleanExpression_whenEvaluate_thenCorrectResult() { | ||
| Map<String, Object> variables = new HashMap<>(); | ||
| variables.put("experience", 12); | ||
| variables.put("salary", 95000.0); | ||
|
|
||
| String expression = "experience > 10 && salary > 80000"; | ||
| Object result = MVEL.eval(expression, variables); | ||
|
|
||
| assertTrue((Boolean) result); | ||
| } | ||
|
|
||
| @Test | ||
| public void givenNullSafeNavigation_whenPropertyIsNull_thenNoException() { | ||
| Employee.Job job = new Employee.Job(5, 5000); | ||
| Employee employee = new Employee("David", job); | ||
| employee.getJob().setDepartment(null); | ||
| String expression = " (job.?department.length) "; | ||
| Object result = MVEL.eval(expression, employee); | ||
|
|
||
| assertNull(result); | ||
| } | ||
|
|
||
| @Test | ||
| public void givenEmployee_whenAccessProperties_thenCorrectValues() { | ||
| Employee.Job job = new Employee.Job(8, 75000.0); | ||
| Employee employee = new Employee("John Doe", job); | ||
|
|
||
| String expression = "name + ' has ' + job.experienceInYears + ' years of experience'"; | ||
| Object result = MVEL.eval(expression, employee); | ||
|
|
||
| assertEquals("John Doe has 8 years of experience", result); | ||
| } | ||
|
|
||
| @Test | ||
| public void givenEmployee_whenCallMethod_thenCorrectResult() { | ||
| Employee.Job job = new Employee.Job(12, 90000.0); | ||
| Employee employee = new Employee("Jane Smith", job); | ||
|
|
||
| String expression = "job.calculateAnnualBonus()"; | ||
| Object result = MVEL.eval(expression, employee); | ||
|
|
||
| assertEquals(18000.0, (Double) result, 0.01); | ||
| } | ||
|
|
||
| @Test | ||
| public void givenEmployee_whenEvaluateConditional_thenCorrectResult() { | ||
| Employee.Job job = new Employee.Job(15, 120000.0); | ||
| Employee employee = new Employee("Bob Johnson", job); | ||
|
|
||
| String expression = "if (job.experienceInYears > 10) { job.salary * 0.20 } " + | ||
| "else if (job.experienceInYears > 5) { job.salary * 0.10 } " + | ||
| "else { job.salary * 0.05 }"; | ||
| Object result = MVEL.eval(expression, employee); | ||
|
|
||
| assertEquals(24000.0, (Double) result, 0.01); | ||
| } | ||
|
|
||
| @Test | ||
| public void givenCompiledExpression_whenExecuteMultipleTimes_thenCorrectResults() { | ||
| String expression = "job.experienceInYears > 5 && job.salary > 50000"; | ||
| Serializable compiledExpression = MVEL.compileExpression(expression); | ||
|
|
||
| Employee.Job job1 = new Employee.Job(7, 60000.0); | ||
| Employee employee1 = new Employee("Alice", job1); | ||
|
|
||
| Employee.Job job2 = new Employee.Job(3, 45000.0); | ||
| Employee employee2 = new Employee("Charlie",job2); | ||
|
|
||
| boolean result1 = (Boolean) MVEL.executeExpression(compiledExpression, employee1); | ||
| boolean result2 = (Boolean) MVEL.executeExpression(compiledExpression, employee2); | ||
|
|
||
| assertTrue(result1); | ||
| assertFalse(result2); | ||
| } | ||
|
|
||
| @Test | ||
| public void givenListProjection_whenFilter_thenCorrectResult() { | ||
| Map<String, Object> variables = new HashMap<>(); | ||
| List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); | ||
| variables.put("numbers", numbers); | ||
|
|
||
| String expression = "($ in numbers if $ > 5)"; | ||
| Object result = MVEL.eval(expression, variables); | ||
|
|
||
| assertInstanceOf(List.class, result); | ||
| List<?> filteredList = (List<?>) result; | ||
| assertEquals(5, filteredList.size()); | ||
| } | ||
|
|
||
| @Test | ||
| public void givenListTransformation_whenProject_thenCorrectResult() { | ||
| Map<String, Object> variables = new HashMap<>(); | ||
| List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); | ||
| variables.put("numbers", numbers); | ||
|
|
||
| String expression = "($ * 2 in numbers)"; | ||
| Object result = MVEL.eval(expression, variables); | ||
|
|
||
| assertInstanceOf(List.class, result); | ||
| List<?> transformedList = (List<?>) result; | ||
| assertEquals(10, transformedList.get(4)); | ||
| } | ||
|
|
||
|
|
||
| } |
79 changes: 79 additions & 0 deletions
79
mvel/src/test/java/com/baeldung/mvel/MvelTemplateServiceUnitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package com.baeldung.mvel; | ||
|
|
||
| import com.baeldung.mvel.model.Employee; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mvel2.templates.CompiledTemplate; | ||
| import org.mvel2.templates.TemplateCompiler; | ||
| import org.mvel2.templates.TemplateRuntime; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| public class MvelTemplateServiceUnitTest { | ||
|
|
||
| @Test | ||
| public void givenEmployeeData_whenGenerateReport_thenCorrectOutput() { | ||
| String template = "Employee Report\n" + | ||
| "================\n" + | ||
| "Name: @{name}\n" + | ||
| "Experience: @{experience} years\n" + | ||
| "Salary: $@{salary}\n"; | ||
|
|
||
| Map<String, Object> variables = new HashMap<>(); | ||
| variables.put("name", "John Doe"); | ||
| variables.put("experience", 12); | ||
| variables.put("salary", 95000.0); | ||
|
|
||
| CompiledTemplate compiledTemplate = TemplateCompiler.compileTemplate(template); | ||
| String result = (String) TemplateRuntime.execute(compiledTemplate, variables); | ||
|
|
||
| assertTrue(result.contains("John Doe")); | ||
| assertTrue(result.contains("12 years")); | ||
| } | ||
|
|
||
| @Test | ||
| public void givenSeniorEmployee_whenGenerateReport_thenCorrectStatus() { | ||
| String template = "Employee Report\n" + | ||
| "@if{experience > 10}" + | ||
| "Status: Senior Employee\n" + | ||
| "@else{}" + | ||
| "Status: Junior/Mid-level Employee\n" + | ||
| "@end{}"; | ||
|
|
||
| Map<String, Object> variables = new HashMap<>(); | ||
| variables.put("experience", 12); | ||
|
|
||
| CompiledTemplate compiledTemplate = TemplateCompiler.compileTemplate(template); | ||
| String result = (String) TemplateRuntime.execute(compiledTemplate, variables); | ||
|
|
||
| assertTrue(result.contains("Senior Employee")); | ||
| } | ||
|
|
||
| @Test | ||
| public void givenEmployeeList_whenGenerateListReport_thenCorrectOutput() { | ||
| String template = "Employee List:\n" + | ||
| "@foreach{emp : employees}" + | ||
| "- @{emp.name} (@{emp.job.department})\n" + | ||
| "@end{}"; | ||
|
|
||
|
|
||
| List<Employee> employees = Arrays.asList( | ||
| new Employee("John", 10, 80000.0, "IT"), | ||
| new Employee("Jane", 8, 75000.0, "HR"), | ||
| new Employee("Bob", 12, 95000.0, "Finance") | ||
| ); | ||
|
|
||
| Map<String, Object> variables = new HashMap<>(); | ||
| variables.put("employees", employees); | ||
|
|
||
| CompiledTemplate compiledTemplate = TemplateCompiler.compileTemplate(template); | ||
| String result = (String) TemplateRuntime.execute(compiledTemplate, variables); | ||
|
|
||
| assertTrue(result.contains("John (IT)")); | ||
| assertTrue(result.contains("Jane (HR)")); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use the Baeldung IDE code formatter profile, which has 4-space indents