-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a blob comparator #30; Date converters refactoring to avoid duplication of code.
- Loading branch information
1 parent
7b7bfd9
commit e8d3dce
Showing
13 changed files
with
758 additions
and
621 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
specrunner-core/src/main/java/org/specrunner/comparators/core/ComparatorMd5.java
This file contains 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,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())); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
...rc/main/java/org/specrunner/converters/core/AbstractConverterJodatimeCurrentTemplate.java
This file contains 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,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); | ||
|
||
} |
114 changes: 114 additions & 0 deletions
114
...src/main/java/org/specrunner/converters/core/AbstractConverterJvmTimeCurrentTemplate.java
This file contains 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,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; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
specrunner-core/src/main/java/org/specrunner/converters/core/ConverterBytes.java
This file contains 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,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(); | ||
} | ||
} |
Oops, something went wrong.