Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactors for Testing #114

Merged
merged 7 commits into from Aug 5, 2017
@@ -1,5 +1,4 @@
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
apply plugin: 'realm-android'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'com.github.kt3k.coveralls'
@@ -205,13 +204,13 @@ dependencies {
compile 'com.google.code.gson:gson:2.7'

//Dagger
apt 'com.google.dagger:dagger-compiler:2.2'
compile 'com.google.dagger:dagger:2.2'
compile "com.google.dagger:dagger:2.9"
annotationProcessor "com.google.dagger:dagger-compiler:2.9"
provided 'javax.annotation:jsr250-api:1.0'

//Butterknife
compile 'com.jakewharton:butterknife:8.0.1'
apt 'com.jakewharton:butterknife-compiler:8.0.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.0.1'

//Timber
compile 'com.jakewharton.timber:timber:4.1.2'
@@ -7,6 +7,7 @@
import subreddit.android.appstore.backend.github.GithubRepositoryModule;
import subreddit.android.appstore.backend.reddit.wiki.WikiRepository;
import subreddit.android.appstore.backend.reddit.wiki.WikiRepositoryModule;
import subreddit.android.appstore.backend.reddit.wiki.parser.BodyParser;
import subreddit.android.appstore.backend.scrapers.MediaScraper;
import subreddit.android.appstore.backend.scrapers.ScraperModule;
import subreddit.android.appstore.util.dagger.ApplicationScope;
@@ -27,4 +28,6 @@
MediaScraper mediaScraper();

GithubRepository selfUpdater();

void inject(BodyParser bodyParser);
}
@@ -45,7 +45,10 @@ private void clearCache() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (prefs.getInt("APP_VERSION",0)<BuildConfig.VERSION_CODE) {
Timber.e("New release on %s, clearing cache database", BuildConfig.VERSION_NAME);
Realm.deleteRealm(new RealmConfiguration.Builder(this, getCacheDir()).build());
Realm.init(getApplicationContext());
Realm realm = Realm.getDefaultInstance();
realm.close();
Realm.deleteRealm(new RealmConfiguration.Builder().build());
prefs.edit().putInt("APP_VERSION",BuildConfig.VERSION_CODE).commit();
}
}
@@ -78,7 +81,8 @@ public void restart() {
}

void init(AppStoreApp app) {
RealmConfiguration realmConfig = new RealmConfiguration.Builder(app, app.getCacheDir())
Realm.init(app);
RealmConfiguration realmConfig = new RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(realmConfig);
@@ -22,7 +22,6 @@
import subreddit.android.appstore.backend.reddit.TokenRepository;
import subreddit.android.appstore.backend.reddit.wiki.caching.WikiDiskCache;
import subreddit.android.appstore.backend.reddit.wiki.parser.BodyParser;
import subreddit.android.appstore.backend.reddit.wiki.parser.EncodingFixer;
import timber.log.Timber;


@@ -33,12 +32,17 @@
final WikiDiskCache wikiDiskCache;
final TokenRepository tokenRepository;
final WikiApi wikiApi;
final BodyParser bodyParser;
ReplaySubject<Collection<AppInfo>> dataReplayer;
String authString;

public LiveWikiRepository(TokenRepository tokenRepository, WikiDiskCache wikiDiskCache, UserAgentInterceptor userAgent) {
public LiveWikiRepository(TokenRepository tokenRepository,
WikiDiskCache wikiDiskCache,
UserAgentInterceptor userAgent,
BodyParser bodyParser) {
this.tokenRepository = tokenRepository;
this.wikiDiskCache = wikiDiskCache;
this.bodyParser = bodyParser;

OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (BuildConfig.DEBUG) {
@@ -84,7 +88,7 @@ public void refresh() {
Timber.d(response.toString());
long timeStart = System.currentTimeMillis();
Collection<AppInfo> infos = new ArrayList<>();
infos.addAll(new BodyParser(new EncodingFixer()).parseBody(response.data.content_md));
infos.addAll(bodyParser.parseBody(response.data.content_md));
long timeStop = System.currentTimeMillis();
Timber.d("Initial parse: Parsed %d items in %dms", infos.size(), (timeStop - timeStart));

@@ -6,22 +6,22 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.inject.Inject;

import subreddit.android.appstore.backend.data.AppInfo;

public class BodyParser {
private final List<AppParser> appParsers = new ArrayList<>();
private final CategoryParser categoryParser;

public BodyParser(EncodingFixer encodingFixer) {
categoryParser = new CategoryParser(encodingFixer);
appParsers.add(new NameColumnParser(encodingFixer));
appParsers.add(new PriceColumnParser(encodingFixer));
appParsers.add(new DeviceColumnParser(encodingFixer));
appParsers.add(new DescriptionColumnParser(encodingFixer));
appParsers.add(new ContactColumnParser(encodingFixer));
Set<AppParser> appParsers;
CategoryParser categoryParser;

@Inject
public BodyParser(CategoryParser categoryParser, Set<AppParser> appParsers) {
this.appParsers = appParsers;
this.categoryParser = categoryParser;
}

public Collection<AppInfo> parseBody(String bodyString) {
@@ -2,12 +2,25 @@

import android.content.Context;

import java.util.HashSet;
import java.util.Set;

import dagger.Module;
import dagger.Provides;
import dagger.multibindings.ElementsIntoSet;
import subreddit.android.appstore.backend.DeviceIdentifier;
import subreddit.android.appstore.backend.UserAgentInterceptor;
import subreddit.android.appstore.backend.reddit.TokenRepository;
import subreddit.android.appstore.backend.reddit.wiki.caching.WikiDiskCache;
import subreddit.android.appstore.backend.reddit.wiki.parser.AppParser;
import subreddit.android.appstore.backend.reddit.wiki.parser.BodyParser;
import subreddit.android.appstore.backend.reddit.wiki.parser.CategoryParser;
import subreddit.android.appstore.backend.reddit.wiki.parser.ContactColumnParser;
import subreddit.android.appstore.backend.reddit.wiki.parser.DescriptionColumnParser;
import subreddit.android.appstore.backend.reddit.wiki.parser.DeviceColumnParser;
import subreddit.android.appstore.backend.reddit.wiki.parser.EncodingFixer;
import subreddit.android.appstore.backend.reddit.wiki.parser.NameColumnParser;
import subreddit.android.appstore.backend.reddit.wiki.parser.PriceColumnParser;
import subreddit.android.appstore.util.dagger.ApplicationScope;


@@ -39,7 +52,32 @@ WikiDiskCache provideWikiDiskCache(Context context) {

@Provides
@ApplicationScope
WikiRepository provideBackendService(TokenRepository tokenRepository, UserAgentInterceptor userAgentInterceptor, WikiDiskCache wikiDiskCache) {
return new LiveWikiRepository(tokenRepository, wikiDiskCache, userAgentInterceptor);
WikiRepository provideBackendService(TokenRepository tokenRepository,
UserAgentInterceptor userAgentInterceptor,
WikiDiskCache wikiDiskCache,
BodyParser bodyParser) {
return new LiveWikiRepository(tokenRepository, wikiDiskCache, userAgentInterceptor, bodyParser);
}

@Provides
@ElementsIntoSet
Set<AppParser> provideAppParsers(EncodingFixer encodingFixer) {
Set<AppParser> appParsers = new HashSet<>();
appParsers.add(new NameColumnParser(encodingFixer));
appParsers.add(new PriceColumnParser(encodingFixer));
appParsers.add(new DeviceColumnParser(encodingFixer));
appParsers.add(new DescriptionColumnParser(encodingFixer));
appParsers.add(new ContactColumnParser(encodingFixer));
return appParsers;
}

@Provides
CategoryParser provideCategoryParser(EncodingFixer encodingFixer) {
return new CategoryParser(encodingFixer);
}

@Provides
EncodingFixer provideEncodingFixer() {
return new EncodingFixer();
}
}
@@ -10,28 +10,45 @@

import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import subreddit.android.appstore.backend.data.AppInfo;

import static junit.framework.Assert.assertFalse;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.doAnswer;

public class BodyParserTest {
@Mock EncodingFixer encodingFixer;
@Mock AppParser appParser;
@Mock CategoryParser categoryParser;
private BodyParser bodyParser;

@Before
public void setup() {
MockitoAnnotations.initMocks(this);
when(encodingFixer.fixHtmlEscapes(anyString())).then(new Answer<String>() {
doAnswer(new Answer<Void>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
return invocation.getArgument(0);
public Void answer(InvocationOnMock invocation) throws Throwable {
return null;
}
});
bodyParser = new BodyParser(encodingFixer);
}).when(categoryParser).parse(isA(AppInfo.class), anyList());
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
return null;
}
}).when(appParser).parse(isA(AppInfo.class), anyMap());

Set<AppParser> appParsers = new HashSet<>();
for (int i=0; i<5; i++) {
appParsers.add(appParser);
}

bodyParser = new BodyParser(categoryParser, appParsers);
}

@Test
@@ -6,8 +6,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath "io.realm:realm-gradle-plugin:1.2.0"
classpath "io.realm:realm-gradle-plugin:3.1.2"
classpath 'me.tatarka:gradle-retrolambda:3.3.0'
classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.4.0'
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.