OVal (framework)

from Wikipedia, the free encyclopedia
Oval

OVal-banner.png
Basic data

Maintainer Sebastian Thomschke, Holger Riegel
Publishing year August 17, 2005
Current  version 2.1.6
( June 29, 2020 )
operating system cross-platform
programming language Java
category Validation and design-by-contract framework
License Eclipse Public License 2.0
sebthom.github.io/oval/

OVal is a validation - and Design-by-Contract - Framework for the programming language Java . It enables the validation of attributes of any Java objects using predefined or self-written constraints . In addition, it also allows the testing of invariants , pre - and post-conditions for methods and validation of arguments of methods using AspectJ -Aspekten.

This functionality and the fact that OVal can validate any objects and not just JavaBeans sets OVal apart from other validation and design-by-contract frameworks for Java.

Functionality

Constraints can be declared using annotations (for example @NotNull, @MaxLength), POJOs or XML . OVal offers more than 35 built-in constraints for this. Custom constraints can be defined as Java classes or using script languages ​​such as JavaScript , Groovy , Ruby , BeanShell , OGNL or MVEL . These constraints can not only be defined for attributes, but also as preconditions or postconditions of methods or arguments of methods. Since this is not yet provided for in the Java syntax, OVal uses AspectJ -based aspects for it . Alternatively, Spring AOP or your own AOP implementations can be used.

Constraints are either checked directly when methods are called (preconditions and postconditions, constraints on arguments of methods, invariants) or by means validator.validate(Object)of the entire object. The check of pre- and post-conditions as well as the check of the invariants can be switched on and off globally (for example for performance reasons). In addition, constraint profiles can be used to check different constraints in different situations.

OVal itself only depends on the Java class library. Additional jars only need to be linked if necessary. (For example AspectJ for Design by Contract, XStream , if OVal is configured using XML or JRuby if constraints are defined using Ruby.)

example

Declaration:

// Die Parameter der Konstruktoren und Setter-Methoden werden mittels AspectJ analog zu den Constraints der Attribute geprüft.
@net.sf.oval.guard.Guarded(applyFieldConstraintsToConstructors=true, applyFieldConstraintsToSetters=true)
public class BusinessObject {

  // Das Attribut name darf weder null, noch leer sein und darf maximal 32 Zeichen lang sein.
  @NotNull @NotEmpty @Length(max=32)
  private String name;

  ...

  // Das Attribut mailingAddress muss entweder gleich der deliveryAddress oder der invoiceAddress sein.
  // Dies wird mittels eines Groovy-Skriptes geprüft.
  @Assert(expr = "_value ==_this.deliveryAddress || _value == _this.invoiceAddress", lang = "groovy")
  private String mailingAddress;

  ...

  // Der Parameter name wird automatisch analog wie das Attribut name geprüft,
  // da für die ganze Klasse "applyFieldConstraintsToSetters" gesetzt wurde.
  public void setName(String name) {
    this.name = name;
  }

  ...

  // Vor dem Aufruf der Methode wird mittels eines Groovy-Skriptes sichergestellt, dass this.amount nicht null ist und
  // amount2add größer als this.amount ist.
  @Pre(expr = "_this.amount!=null && amount2add>_this.amount", lang = "groovy")
  public void increase(BigDecimal amount2add) {
    amount = amount.add(amount2add);
  }
}

Use:

  ...
  BusinessObject bo = new BusinessObject();
  bo.setName(""); // Wirft eine ConstraintsViolatedException weil der Parameter leer ist.
  ...
  bo.increase(null); // Wirft eine Exception, wenn this.amount noch nicht gesetzt wurde.
  ...
  List<ConstraintViolation> violations = validator.validate(bo); // Liefert die Liste aller Verletzungen.

history

OVal was started by Sebastian Thomschke out of the motivation to have a framework that extends Java with simple techniques for validation and design by contract - based on the same constraints. In addition, he wanted a validation framework based on annotations, so that the validations are automatically moved along with the attributes and methods during refactoring . These features are still OVal's unique selling points today.

The first version of OVal (0.1alpha) was released on August 17th, 2005. This version already enabled the validation of arguments and return values ​​using AspectJ. Version 1.0 was released on July 22, 2007. Among other things, it contained a number of other constraints as well as support for scripting your own constraints. From version 1.50, OVal can interpret the standard JSR303 constraints and validate them accordingly. As of version 1.60, constraints for method arguments can already be declared at the interface level. From version 1.70 OVal can be configured via the Spring Framework. JSR223 scripting is supported from version 1.82. From version 1.90 Bean Validation 2.0 Constraints.

Various frameworks like Struts 2 OVal Plug-in, Play! Framework or Apache Cayenne annotations use OVal.

OVal 1 has been downloaded more than 18,000 times from the Sourceforge site alone since 2005. Usually, however, open source Java frameworks are obtained from Maven repositories. Oval can be found in the JFrog Bintray Repository from version 2.1.X onwards.

With version 1.86, OVal switched from sourceforge to github in 2016. From version 2.0.0 from May 2019, OVal requires at least Java 8. For OVal 1, at least Java 5 is required. The last version of OVal 1 is 1.90 from October 29, 2017.

Alternatives

Validation frameworks:

  • Apache Commons Validator - Apache Commons project for bean validation
  • Hibernate Validator
  • Spring Modules Validator - Spring Modules (extension to Spring ) Bean validation project
  • JValidate - Beans Validation
  • JValidations - Object Validation
With Java 7, the so-called Bean Validation Framework (JSR 303) was introduced, so bean validation (not object and also not parameter validation) is a component of Java. However, the validation itself must be initiated at a suitable point - for example when storing beans in the database using Hibernate .
Frameworks for implementing the design-by-contract principle in Java
There are a number of design-by-contract frameworks for Java, a few such as B. Contracts for Java (Cofoja) , but seem to be still active. A list of all design-by-contract frameworks for Java can be found under en: Design by contract # Languages ​​with third-party support in the English language Wikipedia.

literature

Web links

Individual evidence

  1. sourceforge.net .
  2. Release 2.1.6 . June 29, 2020 (accessed June 30, 2020).
  3. sebthom.github.io .
  4. OVal Dependencies
  5. OVal Changelog
  6. OVal References
  7. OVal Download Statistics
  8. https://bintray.com/sebthom/maven/oval
  9. https://github.com/sebthom/oval/releases
  10. https://sebthom.github.io/oval/
  11. https://sourceforge.net/projects/oval/files/oval/