Skip to content

Commit

Permalink
Merge pull request #127 from orhanobut/refactoring
Browse files Browse the repository at this point in the history
Refactor old tests and remove deprecated codes
  • Loading branch information
orhanobut committed Jan 24, 2016
2 parents 990e513 + 7b43083 commit cf9b3d4
Show file tree
Hide file tree
Showing 30 changed files with 1,497 additions and 1,002 deletions.
31 changes: 25 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,35 @@ language: android

android:
components:
- tools
- build-tools-23.0.2
- build-tools-23.0.1
- android-23
licenses:
- 'android-sdk-license-.+'

notifications:
email: true
addons:
apt_packages:
- pandoc

sudo: false
before_install:
echo "MAVEN_OPTS='-Xmx2048m -XX:MaxPermSize=1024m'" > ~/.mavenrc

script:
- ./gradlew test
- ./gradlew ciCheck

notifications:
email: false

sudo: true

before_cache:
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock

cache:
directories:
- $HOME/.m2
- $HOME/.gradle/caches/
- $HOME/.gradle/wrapper/

after_failure:
- pandoc app/build/outputs/lint-results.html -t plain
- pandoc app/build/reports/debug/index.html -t plain
10 changes: 10 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@ allprojects {
jcenter()
}
}

subprojects {
task ciCheck {
group 'verification'
}
}

// Added specific declaration here because :sdk doesn't have testDebugUnitTest
project(':hawk').ciCheck.dependsOn(['checkstyle'])
project(':hawk').ciCheck.dependsOn(['testDebugUnitTest'])
6 changes: 3 additions & 3 deletions checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
</module>
<module name="PackageName"/>
<module name="ParameterName"/>
<module name="StaticVariableName"/>
<!--<module name="StaticVariableName"/>-->
<module name="TypeName">
<property name="format" value="^[A-Z][a-zA-Z0-9_]*$"/>
</module>
Expand Down Expand Up @@ -105,10 +105,10 @@
<!-- Checks for blocks. You know, those {}'s -->
<!-- See http://checkstyle.sf.net/config_blocks.html -->
<module name="AvoidNestedBlocks"/>
<module name="EmptyBlock"/>
<!--<module name="EmptyBlock"/>-->
<module name="LeftCurly"/>
<module name="NeedBraces">
<property name="tokens" value="LITERAL_DO, LITERAL_ELSE, LITERAL_FOR, LITERAL_WHILE"/>
<property name="tokens" value="LITERAL_DO, LITERAL_ELSE, LITERAL_FOR, LITERAL_WHILE"/>
</module>
<module name="RightCurly"/>

Expand Down
4 changes: 2 additions & 2 deletions hawk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ task checkstyle(type: Checkstyle) {

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
buildToolsVersion "23.0.1"

defaultConfig {
minSdkVersion 9
Expand All @@ -35,7 +35,7 @@ task sourcesJar(type: Jar) {
}

task javadoc(type: Javadoc) {
failOnError false
failOnError false
source = android.sourceSets.main.java.sourceFiles
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
Expand Down
29 changes: 25 additions & 4 deletions hawk/src/main/java/com/orhanobut/hawk/Base64Encryption.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,43 @@
package com.orhanobut.hawk;

import android.util.Base64;

/**
* Provides Base64 Algorithm
* Provides Base64 encoding as non-encryption option.
* This doesn't provide any encryption
*/
public class Base64Encryption implements Encryption {
class Base64Encryption implements Encryption {
@Override public boolean init() {
return true;
}

@Override public String encrypt(byte[] value) {
return DataHelper.encodeBase64(value);
return encodeBase64(value);
}

@Override public byte[] decrypt(String value) {
return DataHelper.decodeBase64(value);
return decodeBase64(value);
}

@Override public boolean reset() {
return true;
}

String encodeBase64(byte[] bytes) {
try {
return Base64.encodeToString(bytes, Base64.DEFAULT);
} catch (Exception e) {
Logger.w(e.getMessage());
return null;
}
}

byte[] decodeBase64(String value) {
try {
return Base64.decode(value, Base64.DEFAULT);
} catch (Exception e) {
Logger.w(e.getMessage());
return null;
}
}
}
59 changes: 10 additions & 49 deletions hawk/src/main/java/com/orhanobut/hawk/DataHelper.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.orhanobut.hawk;

import android.text.TextUtils;
import android.util.Base64;

import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -11,12 +10,10 @@

final class DataHelper {

private static final String DELIMITER = "@";
private static final char DELIMITER = '@';
private static final String INFO_DELIMITER = "#";
private static final char NEW_VERSION = 'V';

@Deprecated private static final char FLAG_SERIALIZABLE = '1';

private static final Map<Character, DataType> TYPE_MAP = new HashMap<>();

static {
Expand All @@ -37,13 +34,13 @@ private DataHelper() {
* @return the DataInfo object which contains all necessary information
*/
static DataInfo getDataInfo(String storedText) {
if (TextUtils.isEmpty(storedText)) {
throw new NullPointerException("Text should not be null or empty");
}
HawkUtils.checkNullOrEmpty("Text", storedText);

int index = storedText.indexOf(DELIMITER);
if (index == -1) {
throw new IllegalArgumentException("Text should contain delimiter");
}

String text = storedText.substring(0, index);
String cipherText = storedText.substring(index + 1);
if (TextUtils.isEmpty(text) || TextUtils.isEmpty(cipherText)) {
Expand All @@ -53,7 +50,8 @@ static DataInfo getDataInfo(String storedText) {
if (firstChar == NEW_VERSION) {
return getNewDataInfo(text, cipherText);
} else {
return getOldDataInfo(text, cipherText);
// old data is no longer supported
throw new IllegalStateException("storedText is not valid");
}
}

Expand Down Expand Up @@ -87,30 +85,11 @@ static DataInfo getNewDataInfo(String text, String cipherText) {
return new DataInfo(dataType, cipherText, keyClazz, valueClazz);
}

@Deprecated static DataInfo getOldDataInfo(String text, String cipherText) {
boolean serializable = text.charAt(text.length() - 1) == FLAG_SERIALIZABLE;
char type = text.charAt(text.length() - 2);
DataType dataType = TYPE_MAP.get(type);

String className = text.substring(0, text.length() - 2);

Class<?> clazz = null;
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException e) {
Logger.d(e.getMessage());
}

return new DataInfo(dataType, serializable, cipherText, clazz);
}

//TODO optimize
static <T> String addType(String cipherText, T t) {
if (TextUtils.isEmpty(cipherText)) {
throw new NullPointerException("Cipher text should not be null or empty");
}
if (t == null) {
throw new NullPointerException("Value should not be null");
}
HawkUtils.checkNullOrEmpty("Cipher text", cipherText);
HawkUtils.checkNull("Value", t);

String keyClassName = "";
String valueClassName = "";
DataType dataType;
Expand Down Expand Up @@ -150,22 +129,4 @@ static <T> String addType(String cipherText, T t) {
cipherText;
}

static String encodeBase64(byte[] bytes) {
try {
return Base64.encodeToString(bytes, Base64.DEFAULT);
} catch (Exception e) {
Logger.w(e.getMessage());
return null;
}
}

static byte[] decodeBase64(String value) {
try {
return Base64.decode(value, Base64.DEFAULT);
} catch (Exception e) {
Logger.w(e.getMessage());
return null;
}
}

}
45 changes: 4 additions & 41 deletions hawk/src/main/java/com/orhanobut/hawk/DataInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,15 @@

final class DataInfo {

private final DataType dataType;
private final String cipherText;
private final Class keyClazz;
private final Class valueClazz;
private final boolean serializable;
private final boolean isNewVersion;

DataInfo(DataType dataType, boolean serializable, String cipherText, Class keyClazz) {
this.cipherText = cipherText;
this.keyClazz = keyClazz;
this.valueClazz = null;
this.dataType = dataType;
this.serializable = serializable;
this.isNewVersion = false;
}
final DataType dataType;
final String cipherText;
final Class keyClazz;
final Class valueClazz;

DataInfo(DataType dataType, String cipherText, Class keyClazz, Class valueClazz) {
this.cipherText = cipherText;
this.keyClazz = keyClazz;
this.valueClazz = valueClazz;
this.dataType = dataType;
this.serializable = false;
this.isNewVersion = true;
}

public DataType getDataType() {
return dataType;
}

String getCipherText() {
return cipherText;
}

Class getKeyClazz() {
return keyClazz;
}

public Class getValueClazz() {
return valueClazz;
}

public boolean isSerializable() {
return serializable;
}

public boolean isNewVersion() {
return isNewVersion;
}
}
4 changes: 0 additions & 4 deletions hawk/src/main/java/com/orhanobut/hawk/DataType.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ enum DataType {
this.type = type;
}

@Override public String toString() {
return super.toString();
}

char getType() {
return type;
}
Expand Down
Loading

0 comments on commit cf9b3d4

Please sign in to comment.