Update AbstractTests

master
Stuart Boston 11 years ago
parent febc5185a4
commit be963f38a6

1
.gitignore vendored

@ -6,6 +6,7 @@
/target/ /target/
/nbactions.xml /nbactions.xml
testing.properties testing.properties
*.bin
.settings .settings
.classpath .classpath
.project .project

@ -43,8 +43,7 @@ public class AbstractTests {
protected static final Logger LOG = LoggerFactory.getLogger(AbstractTests.class); protected static final Logger LOG = LoggerFactory.getLogger(AbstractTests.class);
private static final String PROP_FIlENAME = "testing.properties"; private static final String PROP_FIlENAME = "testing.properties";
private static final String ACCT_FILENAME = "account.bin"; private static final String FILENAME_EXT = ".bin";
private static final String TOKEN_FILENAME = "token.bin";
private static final Properties props = new Properties(); private static final Properties props = new Properties();
private static HttpClient httpClient; private static HttpClient httpClient;
private static HttpTools httpTools; private static HttpTools httpTools;
@ -91,19 +90,12 @@ public class AbstractTests {
* @param filename * @param filename
* @return * @return
*/ */
private static boolean writeObject(Serializable object, String filename) { private static boolean writeObject(final Serializable object, final String baseFilename) {
String filename = baseFilename + FILENAME_EXT;
File serFile = new File(filename); File serFile = new File(filename);
if (serFile.exists()) { if (serFile.exists()) {
long ft = serFile.lastModified(); serFile.delete();
long diff = System.currentTimeMillis() - ft;
LOG.info("File time: {}, diff: {} - {}", ft, diff, TimeUnit.HOURS.toMillis(1));
if (diff < TimeUnit.HOURS.toMillis(1)) {
LOG.info("File is current, no need to reacquire");
return true;
} else {
LOG.info("File is too old, re-acquiring");
}
} }
try { try {
@ -111,7 +103,7 @@ public class AbstractTests {
FileUtils.writeByteArrayToFile(serFile, serObject); FileUtils.writeByteArrayToFile(serFile, serObject);
return true; return true;
} catch (IOException ex) { } catch (IOException ex) {
LOG.info("Failed to write {}: {}", filename, ex.getMessage(), ex); LOG.info("Failed to write object to '{}': {}", filename, ex.getMessage(), ex);
return false; return false;
} }
} }
@ -123,22 +115,24 @@ public class AbstractTests {
* @param filename * @param filename
* @return * @return
*/ */
private static <T> T readObject(String filename) { private static <T> T readObject(final String baseFilename) {
String filename = baseFilename + FILENAME_EXT;
File serFile = new File(filename); File serFile = new File(filename);
if (serFile.exists()) { if (serFile.exists()) {
long diff = System.currentTimeMillis() - serFile.lastModified(); long diff = System.currentTimeMillis() - serFile.lastModified();
if (diff < TimeUnit.HOURS.toMillis(1)) { if (diff < TimeUnit.HOURS.toMillis(1)) {
LOG.info("File is current, no need to reacquire"); LOG.info("File '{}' is current, no need to reacquire", filename);
return null;
} else { } else {
LOG.info("File is too old, re-acquiring"); LOG.info("File '{}' is too old, re-acquiring", filename);
return null;
} }
} else { } else {
LOG.info("File doesn't exist: {}", filename); LOG.info("File '{}' doesn't exist", filename);
return null; return null;
} }
LOG.info("Reading object from '{}'", filename);
try { try {
byte[] serObject = FileUtils.readFileToByteArray(serFile); byte[] serObject = FileUtils.readFileToByteArray(serFile);
return SerializationUtils.deserialize(serObject); return SerializationUtils.deserialize(serObject);
@ -155,12 +149,15 @@ public class AbstractTests {
* @throws MovieDbException * @throws MovieDbException
*/ */
public static final String getSessionId() throws MovieDbException { public static final String getSessionId() throws MovieDbException {
// Read the object from a file LOG.info("Create a session token for the rest of the tests");
tokenSession = readObject(TOKEN_FILENAME);
if (tokenSession == null) {
String filename = TokenSession.class.getSimpleName();
// Try to read the object from a file
tokenSession = readObject(filename);
if (tokenSession == null) { if (tokenSession == null) {
TmdbAuthentication auth = new TmdbAuthentication(getApiKey(), getHttpTools()); TmdbAuthentication auth = new TmdbAuthentication(getApiKey(), getHttpTools());
LOG.info("Test and create a session token for the rest of the tests");
// 1: Create a request token // 1: Create a request token
TokenAuthorisation token = auth.getAuthorisationToken(); TokenAuthorisation token = auth.getAuthorisationToken();
assertTrue("Token (auth) is not valid", token.getSuccess()); assertTrue("Token (auth) is not valid", token.getSuccess());
@ -171,8 +168,8 @@ public class AbstractTests {
assertTrue("Session token is not valid", tokenSession.getSuccess()); assertTrue("Session token is not valid", tokenSession.getSuccess());
// Write the object to a file // Write the object to a file
writeObject(token, TOKEN_FILENAME); writeObject(token, filename);
}
} }
return tokenSession.getSessionId(); return tokenSession.getSessionId();
} }
@ -184,8 +181,12 @@ public class AbstractTests {
* @throws MovieDbException * @throws MovieDbException
*/ */
public static final int getAccountId() throws MovieDbException { public static final int getAccountId() throws MovieDbException {
LOG.info("Getting account information");
if (account == null) {
String filename = Account.class.getSimpleName();
// Read the object from a file // Read the object from a file
account = readObject(ACCT_FILENAME); account = readObject(filename);
if (account == null) { if (account == null) {
TmdbAccount instance = new TmdbAccount(getApiKey(), getHttpTools()); TmdbAccount instance = new TmdbAccount(getApiKey(), getHttpTools());
@ -193,7 +194,8 @@ public class AbstractTests {
account = instance.getAccount(tokenSession.getSessionId()); account = instance.getAccount(tokenSession.getSessionId());
// Write the object to a file // Write the object to a file
writeObject(account, ACCT_FILENAME); writeObject(account, filename);
}
} }
return account.getId(); return account.getId();
} }

Loading…
Cancel
Save