[DS-3832] Recast test support classes.

This commit is contained in:
Mark H. Wood
2018-03-27 21:14:49 -04:00
parent 074337f167
commit 6b9cfec8c6
4 changed files with 101 additions and 13 deletions

View File

@@ -7,6 +7,7 @@
*/
package org.dspace.statistics;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
@@ -24,15 +25,25 @@ import com.maxmind.geoip2.record.Postal;
import com.maxmind.geoip2.record.RepresentedCountry;
import com.maxmind.geoip2.record.Subdivision;
import com.maxmind.geoip2.record.Traits;
import mockit.Deencapsulation;
import mockit.Mock;
import mockit.MockUp;
/**
* Mock service to mock the location Lookup Service used by the SOLR statistics logger
* Mock service to mock the location Lookup Service used by the SOLR statistics
* logger.
*/
public class MockLookupService extends MockUp<DatabaseReader> {
public class FakeDatabaseReader
extends MockUp<DatabaseReader> {
public MockLookupService() throws IOException {
FakeDatabaseReader() {
}
public FakeDatabaseReader(Object object) {
}
public FakeDatabaseReader $init(Builder builder) {
return this;
}
/*
@@ -84,4 +95,24 @@ public class MockLookupService extends MockUp<DatabaseReader> {
subdivisions, traits);
return response;
}
public static class Builder
extends MockUp<DatabaseReader.Builder> {
public Builder() {}
/**
* Fake constructor.
* @param file ignored.
*/
@Mock
public void $init(File file) {
}
@Mock
public DatabaseReader build()
throws IOException {
return Deencapsulation.newUninitializedInstance(DatabaseReader.class);
}
}
}

View File

@@ -15,18 +15,29 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
/**
* Mock service that uses an embedded SOLR server for the statistics core
* Mock service that uses an embedded SOLR server for the statistics core.
*/
public class MockSolrLoggerServiceImpl extends SolrLoggerServiceImpl implements InitializingBean {
public class MockSolrLoggerServiceImpl
extends SolrLoggerServiceImpl
implements InitializingBean {
@Autowired(required = true)
private ConfigurationService configurationService;
public MockSolrLoggerServiceImpl() {
}
@Override
public void afterPropertiesSet() throws Exception {
//We don't use SOLR in the tests of this module
solr = null;
locationService = new DatabaseReader.Builder(new File(".")).build();
new FakeDatabaseReader(); // Activate fake
new FakeDatabaseReader.Builder(); // Activate fake
String locationDbPath = configurationService.getProperty("usage-statistics.dbfile");
File locationDb = new File(locationDbPath);
locationDb.createNewFile();
locationService = new DatabaseReader.Builder(locationDb).build();
useProxies = configurationService.getBooleanProperty("useProxies");
}

View File

@@ -7,6 +7,8 @@
*/
package org.dspace.statistics;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
@@ -23,14 +25,27 @@ import com.maxmind.geoip2.record.Postal;
import com.maxmind.geoip2.record.RepresentedCountry;
import com.maxmind.geoip2.record.Subdivision;
import com.maxmind.geoip2.record.Traits;
import mockit.Deencapsulation;
import mockit.Mock;
import mockit.MockUp;
/**
* Mock service to mock the location Lookup Service used by the SOLR statistics logger
* Mock service to mock the location Lookup Service used by the SOLR statistics
* logger.
*/
public class MockLookupService
public class FakeDatabaseReader
extends MockUp<DatabaseReader> {
FakeDatabaseReader() {
}
public FakeDatabaseReader(Object object) {
}
public FakeDatabaseReader $init(Builder builder) {
return this;
}
/*
@Override
public Location getLocation(String str) {
@@ -80,4 +95,24 @@ public class MockLookupService
subdivisions, traits);
return response;
}
public static class Builder
extends MockUp<DatabaseReader.Builder> {
public Builder() {}
/**
* Fake constructor.
* @param file ignored.
*/
@Mock
public void $init(File file) {
}
@Mock
public DatabaseReader build()
throws IOException {
return Deencapsulation.newUninitializedInstance(DatabaseReader.class);
}
}
}

View File

@@ -17,20 +17,31 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
/**
* Mock service that uses an embedded SOLR server for the statistics core
* Mock service that uses an embedded SOLR server for the statistics core.
*/
public class MockSolrLoggerServiceImpl extends SolrLoggerServiceImpl implements InitializingBean, DisposableBean {
public class MockSolrLoggerServiceImpl
extends SolrLoggerServiceImpl
implements InitializingBean, DisposableBean {
private MockSolrServer mockSolrServer;
@Autowired(required = true)
private ConfigurationService configurationService;
public MockSolrLoggerServiceImpl() {
}
@Override
public void afterPropertiesSet() throws Exception {
mockSolrServer = new MockSolrServer("statistics");
solr = mockSolrServer.getSolrServer();
locationService = new DatabaseReader.Builder(new File(".")).build();
new FakeDatabaseReader(); // Activate fake
new FakeDatabaseReader.Builder(); // Activate fake
String locationDbPath = configurationService.getProperty("usage-statistics.dbfile");
File locationDb = new File(locationDbPath);
locationDb.createNewFile();
locationService = new DatabaseReader.Builder(locationDb).build();
useProxies = configurationService.getBooleanProperty("useProxies");
}