Skip to content

Commit

Permalink
Create a bytes converter #32;
Browse files Browse the repository at this point in the history
Create a blob comparator #30;
Date converters refactoring to avoid duplication of code.
  • Loading branch information
thiagolvlsantos committed Feb 4, 2016
1 parent 7b7bfd9 commit e8d3dce
Show file tree
Hide file tree
Showing 13 changed files with 758 additions and 621 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
SpecRunner - Acceptance Test Driven Development Tool
Copyright (C) 2011-2015 Thiago Santos
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package org.specrunner.comparators.core;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Blob;
import java.sql.SQLException;

import org.specrunner.util.UtilSql;

/**
* Compare two objects using MD5 value.
*
* @author Thiago Santos.
*
*/
@SuppressWarnings("serial")
public class ComparatorMd5 extends ComparatorString {

private MessageDigest digester;

@Override
public Class<?> getType() {
return Blob.class;
}

@Override
protected String toString(Object obj) {
if (digester == null) {
try {
digester = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Could not generate MD5 for value.", e);
}
}
digester.reset();
byte[] bytes = null;
if (obj instanceof Blob) {
Blob blob = (Blob) obj;
try {
bytes = blob.getBytes(1L, (int) blob.length());
} catch (SQLException e) {
throw new RuntimeException(e);
}
} else {
String tmp = UtilSql.toString(obj);
bytes = tmp != null ? tmp.getBytes() : null;
}
if (bytes == null) {
return "null";
}
digester.update(bytes);
return String.valueOf(new BigInteger(1, digester.digest()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
SpecRunner - Acceptance Test Driven Development Tool
Copyright (C) 2011-2015 Thiago Santos
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package org.specrunner.converters.core;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

import org.specrunner.converters.ConverterException;

/**
* Create current date.
*
* @author Thiago Santos
*
*/
@SuppressWarnings("serial")
public abstract class AbstractConverterJodatimeCurrentTemplate<T> extends AbstractConverterTimeTemplate<T> {

protected Map<String, String> aliasToField = new HashMap<String, String>();
protected Map<String, String> fieldToMethod = new HashMap<String, String>();
protected Pattern pattern;

/**
* See superclass.
*
* @param values
* Value.
*/
public AbstractConverterJodatimeCurrentTemplate(List<String> values) {
super(values);
pattern = extractPattern(bindAliases(aliasToField).keySet());
bindPatterns(fieldToMethod);
}

protected Map<String, String> bindAliases(Map<String, String> map) {
UtilDate.bindAliases(map);
return map;
}

protected Map<String, String> bindPatterns(Map<String, String> map) {
UtilJodatime.bindDatePatterns(map);
UtilJodatime.bindTimePatterns(map);
return map;
}

protected Pattern extractPattern(Set<String> set) {
return UtilDate.extractPattern(set);
}

@Override
protected boolean testValue(String str, String value) {
return str.startsWith(value);
}

@Override
public Object convert(Object value, Object[] args) throws ConverterException {
if (type().isInstance(value)) {
return value;
}
return super.convert(value, args);
}

/**
* Date object type.
*
* @return The date type.
*/
protected abstract Class<T> type();

/**
* Date instance.
*/
protected abstract T instance();

/**
* Post processing after instance() called.
*/
protected abstract T postProcess(Object value, Object[] args, T result);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
SpecRunner - Acceptance Test Driven Development Tool
Copyright (C) 2011-2015 Thiago Santos
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package org.specrunner.converters.core;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.regex.Pattern;

import org.specrunner.converters.ConverterException;

/**
* Create current date (in timestamp).
*
* @author Thiago Santos
*
*/
@SuppressWarnings("serial")
public abstract class AbstractConverterJvmTimeCurrentTemplate<T> extends AbstractConverterTimeTemplate<T> {

protected Map<String, String> aliasToField = new HashMap<String, String>();
protected Map<String, Integer> fieldToMethod = new HashMap<String, Integer>();
protected Pattern pattern;

/**
* See superclass.
*
* @param values
* Value.
*/
public AbstractConverterJvmTimeCurrentTemplate(List<String> values) {
super(values);
pattern = extractPattern(bindAliases(aliasToField).keySet());
bindPatterns(fieldToMethod);
}

protected Map<String, String> bindAliases(Map<String, String> map) {
UtilDate.bindAliases(map);
return map;
}

protected Map<String, Integer> bindPatterns(Map<String, Integer> map) {
UtilDate.bindPatterns(map);
return map;
}

protected Pattern extractPattern(Set<String> values) {
return UtilDate.extractPattern(values);
}

@Override
protected boolean testValue(String str, String value) {
return str.startsWith(value);
}

@Override
public Object convert(Object value, Object[] args) throws ConverterException {
if (type().isInstance(value)) {
return value;
}
return super.convert(value, args);
}

/**
* Date object type.
*
* @return The date type.
*/
protected abstract Class<T> type();

/**
* Date instance.
*/
protected abstract T instance();

/**
* Post processing after instance() called.
*/
protected abstract T postProcess(Object value, Object[] args, T result);

/**
* Get a calendar object based on timezone.
*
* @return A calendar.
*/
protected Calendar getCalendar() {
Calendar calendar = null;
TimeZone timeZone = getZone();
if (timeZone == null) {
calendar = Calendar.getInstance();
} else {
calendar = Calendar.getInstance(timeZone);
}
return calendar;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
SpecRunner - Acceptance Test Driven Development Tool
Copyright (C) 2011-2015 Thiago Santos
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package org.specrunner.converters.core;

import org.specrunner.converters.ConverterException;

/**
* Convert any object to String using <code>String.valueOf(...)</code> then
* <code>getBytes()<code/>.
*
* @author Thiago Santos.
*
*/
@SuppressWarnings("serial")
public class ConverterBytes extends ConverterDefault {

@Override
public Object convert(Object obj, Object[] args) throws ConverterException {
if (obj == null) {
return null;
}
if (obj instanceof String) {
return ((String) obj).getBytes();
}
return String.valueOf(obj).getBytes();
}
}
Loading

0 comments on commit e8d3dce

Please sign in to comment.