FieldCheck

Mit der Tool-Klasse FieldCheck, können die Werte von Optionalen und/oder Pflichtfeldern getestet werden. Benutzt werden kann die Klasse FieldCheck, zum Beispiel vor dem Speichern in Datenbanken, oder vor dem Senden an einen Webservice.

class FieldCheck


package de.snowbits.tools;

import de.snowbits.tools.FieldCheckException.FieldError;


public class FieldCheck {

	private enum Validity {
		Number, PositiveNumber, GreaterThanZero;
	}
	
	private FieldCheck() {
	}
	
	public static final void isNotEmptyRequired(String value) throws FieldCheckException {
		if (! isNotEmpty(value))
			throw new FieldCheckException(FieldError.Required);
	}	

	public static final void isNotEmptyRequired(String value, String fieldname) throws FieldCheckException {
		if (! isNotEmpty(value))
			throw new FieldCheckException(fieldname, FieldError.Required);
	}	
	
	public static final void isNotEmpty(String value, FieldCheckException exception) throws FieldCheckException {
		if (! isNotEmpty(value))
			throw exception;
	}

	public static final boolean isNotEmpty(String value) {
		return value != null && value.trim().length() > 0;
	}

	public static final void isIntRequired(Integer number) throws FieldCheckException {
		isNumber(number, Validity.Number, new FieldCheckException(FieldError.Number));
	}
	
	public static final void isIntPositiveRequired(Integer number) throws FieldCheckException {
		isNumber(number, Validity.PositiveNumber, new FieldCheckException(FieldError.PositiveNumber));
	}
	
	public static final void isIntGreaterThanZeroRequired(Integer number) throws FieldCheckException {
		isNumber(number, Validity.GreaterThanZero, new FieldCheckException(FieldError.GreaterThanZero));
	}

	public static final void isIntRequired(Integer number, String fieldname) throws FieldCheckException {
		isNumber(number, Validity.Number, new FieldCheckException(fieldname, FieldError.Number));
	}

	public static final void isIntPositiveRequired(Integer number, String fieldname) throws FieldCheckException {
		isNumber(number, Validity.PositiveNumber, new FieldCheckException(fieldname, FieldError.PositiveNumber));
	}

	public static final void isIntGreaterThanZeroRequired(Integer number, String fieldname) throws FieldCheckException {
		isNumber(number, Validity.GreaterThanZero, new FieldCheckException(fieldname, FieldError.GreaterThanZero));
	}
			
	public static final boolean isInt(Integer number) {
		try {
			return isNumber(number, Validity.Number, null);
		} catch (FieldCheckException e) {
			return false;
		} 
	}

	public static final boolean isIntPositive(Integer number) {
		try {
			return isNumber(number, Validity.PositiveNumber, null);
		} catch (FieldCheckException e) {
			return false;
		} 
	}

	public static final boolean isIntGreaterThanZero(Integer number) {
		try {
			return isNumber(number, Validity.GreaterThanZero, null);
		} catch (FieldCheckException e) {
			return false;
		} 
	}
	
	public static final void isInt(Integer number, FieldCheckException exception) throws FieldCheckException {
		isNumber(number, Validity.Number, exception);
	}
		
	public static final void isIntPositive(Integer number, FieldCheckException exception) throws FieldCheckException {
		isNumber(number, Validity.PositiveNumber, exception);
	}	
	
	public static final void isIntGreaterThanZero(Integer number, FieldCheckException exception) throws FieldCheckException {
		isNumber(number, Validity.GreaterThanZero, exception);
	}	
	// TODO not yet implemented: isLong ...
	// TODO not yet implemented: isFloat ...
	// TODO not yet implemented: isDouble ...
	// TODO not yet implemented: ... usw. ...
	
	private static boolean isNumber(Number number, Validity validity, FieldCheckException exception) throws FieldCheckException {
		switch (validity) {
		case Number:
			if (exception != null && number == null) 
				throw exception;
			
			return number != null;
		case PositiveNumber:
			if (exception != null && number == null) 
				throw exception;
			else if (exception != null && ((Number) number).doubleValue() < 0d) 
				throw exception;

			return number != null && ((Number) number).doubleValue() >= 0d;
		case GreaterThanZero:
			if (exception != null && number == null) 
				throw exception;
			else if (exception != null && ((Number) number).doubleValue() <= 0d) 
				throw exception;
			
			return number != null && ((Number) number).doubleValue() > 0d;
		}
		
		return false; 
	}
}
	

class FieldCheckException


package de.snowbits.tools;


@SuppressWarnings("serial")
public class FieldCheckException extends Exception {

	public enum FieldError {
		Unknown(1, "unknown error"),
		Required(2, "missing value"),
		Number(3, "not a number"),
		PositiveNumber(3, "number not positive or zero"),
		GreaterThanZero(4, "number not greater than zero");
		
		private Integer _code;
		private String _error;
		
		private FieldError(Integer code, String error) {
			_code = code;
			_error = error;
		}
		
		public static final FieldError getValue(Integer code) {
			FieldError[] result = values();

			for (FieldError element : result) {
				if (element.getCode() == code)
					return element;
			}

			throw new IllegalArgumentException("Error this type is not supported: code=" + code);
		}

		public Integer getCode() {
			return _code;
		}

		public String getError() {
			return _error;
		}
	}
	
	private Integer _code;
	private String _error;

	public FieldCheckException(FieldError error) {
		this(error, error.getError());
	}

	public FieldCheckException(String fieldname, FieldError error) {
		this(error, "Field=\"" + fieldname + "\": " + error.getError());
	}
	
	public FieldCheckException(FieldError error, String message) {
		this(error.getCode(), error.getError(), message);
	}

	public FieldCheckException(Integer code, String error) {
		this(code, error, error);
	}	
	
	public FieldCheckException(Integer code, String error, String message) {
		super(message);
		_code = code;
		_error = error;
	}
	
	public Integer getCode() {
		return _code;
	}

	public String getError() {
		return _error;
	}

}
	

class FieldCheckTest


package de.snowbits.tools;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;

import de.snowbits.tools.FieldCheckException.FieldError;

public class FieldCheckTest {

	private static final String TESTEE_ERROR  = "Testee error";
	private static final Integer TESTEE_CODE  = 9999;
	
	private static final String TESTEE_STRING       = "Testee message";
	private static final String TESTEE_STRING_EMPTY = "";
	private static final String TESTEE_STRING_NULL  = null;

	private static final Integer TESTEE_INT_NULL  = null;
	private static final Integer TESTEE_INT_ZERO  = 0;
	private static final Integer TESTEE_INT_GREATHER_ZERO  = 100;
	private static final Integer TESTEE_INT_NEGATIVE  = -1;

	@SuppressWarnings("serial")
	private class TesteeException extends FieldCheckException {
		public TesteeException(Integer code, String error) {
			super(code, error);
		}
	}
	
	private Boolean _valid;
	private TesteeException _testeeException;
	private Integer _code;

	@Before
	public void setUp() throws Exception {
		_valid = null;
		_code = null;
		_testeeException = new TesteeException(TESTEE_CODE, TESTEE_ERROR);
	}

	@Test
	public void isNotEmptyRequired() {
		try {
			FieldCheck.isNotEmptyRequired(TESTEE_STRING);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNull(_code);

		try {
			_code = null;
			FieldCheck.isNotEmptyRequired(TESTEE_STRING_EMPTY);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNotNull(_code);
		assertTrue(_code == FieldError.Required.getCode());

		try {
			_code = null;
			FieldCheck.isNotEmptyRequired(TESTEE_STRING_NULL);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNotNull(_code);
		assertTrue(_code == FieldError.Required.getCode());
	}	

	@Test
	public void isNotEmptyWithFieldCheckException() {
		try {
			FieldCheck.isNotEmpty(TESTEE_STRING, _testeeException);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNull(_code);

		try {
			_code = null;
			FieldCheck.isNotEmpty(TESTEE_STRING_EMPTY, _testeeException);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNotNull(_code);
		assertTrue(_code == TESTEE_CODE);

		try {
			_code = null;
			FieldCheck.isNotEmpty(TESTEE_STRING_NULL, _testeeException);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNotNull(_code);
		assertTrue(_code == TESTEE_CODE);
	}

	@Test
	public void isNotEmpty() {
		_valid = FieldCheck.isNotEmpty(TESTEE_STRING);
		assertNotNull(_valid);
		assertTrue(_valid);

		_valid = null;
		_valid = FieldCheck.isNotEmpty(TESTEE_STRING_EMPTY);
		assertNotNull(_valid);
		assertFalse(_valid);

		_valid = null;
		_valid = FieldCheck.isNotEmpty(TESTEE_STRING_NULL);
		assertNotNull(_valid);
		assertFalse(_valid);
	}

	@Test
	public void isInt() {
		_valid = FieldCheck.isInt(TESTEE_INT_NULL);
		assertNotNull(_valid);
		assertFalse(_valid);

		_valid = null;
		_valid = FieldCheck.isInt(TESTEE_INT_NEGATIVE);
		assertNotNull(_valid);
		assertTrue(_valid);
		
		_valid = null;
		_valid = FieldCheck.isInt(TESTEE_INT_ZERO);
		assertNotNull(_valid);
		assertTrue(_valid);

		_valid = null;
		_valid = FieldCheck.isInt(TESTEE_INT_GREATHER_ZERO);
		assertNotNull(_valid);
		assertTrue(_valid);
	}
	
	@Test
	public void isIntPositive() {
		_valid = FieldCheck.isIntPositive(TESTEE_INT_NULL);
		assertNotNull(_valid);
		assertFalse(_valid);

		_valid = null;
		_valid = FieldCheck.isIntPositive(TESTEE_INT_NEGATIVE);
		assertNotNull(_valid);
		assertFalse(_valid);
		
		_valid = null;
		_valid = FieldCheck.isIntPositive(TESTEE_INT_ZERO);
		assertNotNull(_valid);
		assertTrue(_valid);

		_valid = null;
		_valid = FieldCheck.isIntPositive(TESTEE_INT_GREATHER_ZERO);
		assertNotNull(_valid);
		assertTrue(_valid);
	}
	
	@Test
	public void isIntGreaterThanZero() {
		_valid = FieldCheck.isIntGreaterThanZero(TESTEE_INT_NULL);
		assertNotNull(_valid);
		assertFalse(_valid);

		_valid = null;
		_valid = FieldCheck.isIntGreaterThanZero(TESTEE_INT_NEGATIVE);
		assertNotNull(_valid);
		assertFalse(_valid);
		
		_valid = null;
		_valid = FieldCheck.isIntGreaterThanZero(TESTEE_INT_ZERO);
		assertNotNull(_valid);
		assertFalse(_valid);

		_valid = null;
		_valid = FieldCheck.isIntGreaterThanZero(TESTEE_INT_GREATHER_ZERO);
		assertNotNull(_valid);
		assertTrue(_valid);				
	}
	@Test
	public void isIntPositiveWithFieldCheckException()	{
		try {
			FieldCheck.isIntPositive(TESTEE_INT_NULL, _testeeException);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNotNull(_code);
		assertTrue(_code == TESTEE_CODE);
		
		try {
			_code = null;
			FieldCheck.isIntPositive(TESTEE_INT_NEGATIVE, _testeeException);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNotNull(_code);
		assertTrue(_code == TESTEE_CODE);

		try {
			_code = null;
			FieldCheck.isIntPositive(TESTEE_INT_ZERO, _testeeException);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNull(_code);

		try {
			_code = null;
			FieldCheck.isIntPositive(TESTEE_INT_GREATHER_ZERO, _testeeException);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNull(_code);	
	}
	
	@Test
	public void isIntGreaterWithThanZeroFieldCheckException()	{
		try {
			FieldCheck.isIntGreaterThanZero(TESTEE_INT_NULL, _testeeException);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNotNull(_code);
		assertTrue(_code == TESTEE_CODE);
		
		try {
			_code = null;
			FieldCheck.isIntGreaterThanZero(TESTEE_INT_NEGATIVE, _testeeException);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNotNull(_code);
		assertTrue(_code == TESTEE_CODE);

		try {
			_code = null;
			FieldCheck.isIntGreaterThanZero(TESTEE_INT_ZERO, _testeeException);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNotNull(_code);
		assertTrue(_code == TESTEE_CODE);

		try {
			_code = null;
			FieldCheck.isIntGreaterThanZero(TESTEE_INT_GREATHER_ZERO, _testeeException);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNull(_code);		
	}
	
	@Test
	public void isIntRequired() {
		try {
			FieldCheck.isIntRequired(TESTEE_INT_NULL);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNotNull(_code);
		assertTrue(_code == FieldError.Number.getCode());
		
		try {
			_code = null;
			FieldCheck.isIntRequired(TESTEE_INT_NEGATIVE);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNull(_code);

		try {
			_code = null;
			FieldCheck.isIntRequired(TESTEE_INT_ZERO);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNull(_code);

		try {
			_code = null;
			FieldCheck.isIntRequired(TESTEE_INT_GREATHER_ZERO);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNull(_code);
	}

	@Test
	public void isIntPositiveRequired() {
		try {
			FieldCheck.isIntPositiveRequired(TESTEE_INT_NULL);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNotNull(_code);
		assertTrue(_code == FieldError.PositiveNumber.getCode());
		
		try {
			_code = null;
			FieldCheck.isIntPositiveRequired(TESTEE_INT_NEGATIVE);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNotNull(_code);
		assertTrue(_code == FieldError.PositiveNumber.getCode());

		try {
			_code = null;
			FieldCheck.isIntPositiveRequired(TESTEE_INT_ZERO);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNull(_code);

		try {
			_code = null;
			FieldCheck.isIntPositiveRequired(TESTEE_INT_GREATHER_ZERO);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNull(_code);			
	}	
	
	@Test
	public void isIntGreaterThanZeroRequired() {		
		try {
			FieldCheck.isIntGreaterThanZeroRequired(TESTEE_INT_NULL);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNotNull(_code);
		assertTrue(_code == FieldError.GreaterThanZero.getCode());
		
		try {
			_code = null;
			FieldCheck.isIntGreaterThanZeroRequired(TESTEE_INT_NEGATIVE);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNotNull(_code);
		assertTrue(_code == FieldError.GreaterThanZero.getCode());

		try {
			_code = null;
			FieldCheck.isIntGreaterThanZeroRequired(TESTEE_INT_ZERO);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNotNull(_code);
		assertTrue(_code == FieldError.GreaterThanZero.getCode());

		try {
			_code = null;
			FieldCheck.isIntGreaterThanZeroRequired(TESTEE_INT_GREATHER_ZERO);
		} catch (FieldCheckException e) {
			_code = e.getCode();
		}
		assertNull(_code);				
	}

	// TODO not yet implemented: test for isLong ... 
	// TODO not yet implemented: test for isFloat ...
	// TODO not yet implemented: test for isDouble ...
	// TODO not yet implemented: ... usw. ...
}
	

Imaginäres Beispiel: class SaveTodos


package de.snowbits.db;

import static de.snowbits.tools.FieldCheck.isIntPositiveRequired;
import static de.snowbits.tools.FieldCheck.isNotEmpty;
import static de.snowbits.tools.FieldCheck.isNotEmptyRequired;
import static de.snowbits.tools.SQLTools.close;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import de.snowbits.pojo.Todo;
import de.snowbits.tools.FieldCheckException;

public class SaveTodos {

    @Resource(name="todoDB")
    private DataSource _dataSource;
	
	public void save(List<Todo> result) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;

		try {
			String sql = "INSERT INTO todo(id, userid, title, description) VALUES (?, ?, ?, ?)";
			conn = _dataSource.getConnection();
			stmt = conn.prepareStatement(sql);
			
			for (Todo element : result) {
				int index = 1;
				try {
					// required fields
					isIntPositiveRequired(element.getId(), "id");
					isIntPositiveRequired(element.getUserId(), "userId");
					isNotEmptyRequired(element.getTitle(), "title");
					stmt.setInt(index++, element.getId());
					stmt.setInt(index++, element.getUserId());
					stmt.setString(index++, element.getTitle());

					// optional fields
					stmt.setString(index++, (isNotEmpty(element.getDescription()) ? element.getDescription() : "-"));

					stmt.execute();					
				} catch (FieldCheckException e) {
					// TODO: log and/or save error
				}			
			}
			
			close(stmt, conn);
		} catch (SQLException e1) {
			close(stmt, conn);
			// TODO: log and/or save error
		}
	}

}
	

class Todo


package de.snowbits.pojo;

public class Todo {
	private Integer _id;
	private Integer _userId;
	private String _title;
	private String _description;

	public Integer getId() {
		return _id;
	}

	public void setId(Integer id) {
		_id = id;
	}

	public Integer getUserId() {
		return _userId;
	}

	public void setUserId(Integer userId) {
		_userId = userId;
	}

	public String getTitle() {
		return _title;
	}

	public void setTitle(String title) {
		_title = title;
	}

	public String getDescription() {
		return _description;
	}

	public void setDescription(String description) {
		_description = description;
	}
}
	

Top


Example
Sitemap Kontakt Impressum