/hg/icedtea-web: Added unit tests for PropertiesFile.java and re...
jkang at icedtea.classpath.org
jkang at icedtea.classpath.org
Wed Sep 10 13:33:17 UTC 2014
changeset faeb3e209001 in /hg/icedtea-web
details: http://icedtea.classpath.org/hg/icedtea-web?cmd=changeset;node=faeb3e209001
author: Jie Kang <jkang at redhat.com>
date: Wed Sep 10 09:31:36 2014 -0400
Added unit tests for PropertiesFile.java and refactored existing unit
tests to not use external code.
2014-09-10 Jie Kang <jkang at redhat.com>
Added unit tests to PropertiesFile.java and refactored existing unit tests
to not use external code.
* tests/netx/unit/net/sourceforge/jnlp/util/PopertiesFileTest.java
diffstat:
ChangeLog | 6 +
tests/netx/unit/net/sourceforge/jnlp/util/PropertiesFileTest.java | 213 ++++++---
2 files changed, 134 insertions(+), 85 deletions(-)
diffs (261 lines):
diff -r b0690979967f -r faeb3e209001 ChangeLog
--- a/ChangeLog Tue Sep 09 14:51:16 2014 +0200
+++ b/ChangeLog Wed Sep 10 09:31:36 2014 -0400
@@ -1,3 +1,9 @@
+2014-09-10 Jie Kang <jkang at redhat.com>
+
+ Added unit tests to PropertiesFile.java and refactored existing unit tests
+ to not use external code.
+ * tests/netx/unit/net/sourceforge/jnlp/util/PopertiesFileTest.java
+
2014-09-09 Jiri Vanek <jvanek at redhat.com>
Outdated documentation replaced by documentation generation
diff -r b0690979967f -r faeb3e209001 tests/netx/unit/net/sourceforge/jnlp/util/PropertiesFileTest.java
--- a/tests/netx/unit/net/sourceforge/jnlp/util/PropertiesFileTest.java Tue Sep 09 14:51:16 2014 +0200
+++ b/tests/netx/unit/net/sourceforge/jnlp/util/PropertiesFileTest.java Wed Sep 10 09:31:36 2014 -0400
@@ -37,116 +37,159 @@
package net.sourceforge.jnlp.util;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
-import java.nio.channels.FileLock;
-import java.nio.channels.OverlappingFileLockException;
-import net.sourceforge.jnlp.cache.CacheLRUWrapper;
+import java.nio.file.Files;
-import net.sourceforge.jnlp.config.DeploymentConfiguration;
-import net.sourceforge.jnlp.runtime.JNLPRuntime;
-
-import org.junit.BeforeClass;
+import org.junit.Before;
import org.junit.Test;
public class PropertiesFileTest {
- private int lockCount = 0;
+ private PropertiesFile propertiesFile;
- /* lock for the file RecentlyUsed */
- private FileLock fl = null;
-
- private final String cacheDir = new File(JNLPRuntime.getConfiguration()
- .getProperty(DeploymentConfiguration.KEY_USER_CACHE_DIR)).getPath();
-
- // does no DeploymentConfiguration exist for this file name?
- private final String cacheIndexFileName = CacheLRUWrapper.CACHE_INDEX_FILE_NAME;
-
- private final PropertiesFile cacheIndexFile = new PropertiesFile(new File(cacheDir + File.separatorChar + cacheIndexFileName));
- private final int noEntriesCacheFile = 1000;
-
- @BeforeClass
- static public void setupJNLPRuntimeConfig() {
- JNLPRuntime.getConfiguration().setProperty(DeploymentConfiguration.KEY_USER_CACHE_DIR, System.getProperty("java.io.tmpdir"));
+ @Before
+ public void setup() throws IOException {
+ File lru = Files.createTempFile("properties_file", ".tmp").toFile();
+ lru.createNewFile();
+ lru.deleteOnExit();
+ propertiesFile = new PropertiesFile(lru);
}
- private void fillCacheIndexFile(int noEntries) {
+ @Test
+ public void testSetProperty() {
+ propertiesFile.setProperty("key", "value");
+ assertTrue(propertiesFile.containsKey("key") && propertiesFile.containsValue("value"));
+ }
- // fill cache index file
+ @Test
+ public void testGetProperty() {
+ propertiesFile.setProperty("key", "value");
+ String v = propertiesFile.getProperty("key");
+ assertEquals("value", v);
+ }
+
+ @Test
+ public void testGetDefaultProperty() {
+ String v = propertiesFile.getProperty("key", "default");
+ assertEquals("default", v);
+ }
+
+ @Test
+ public void testStore() throws IOException {
+ String key = "key";
+ String value = "value";
+ propertiesFile.setProperty(key, value);
+ try {
+ propertiesFile.lock();
+ propertiesFile.store();
+ } finally {
+ propertiesFile.unlock();
+ }
+
+ File f = propertiesFile.getStoreFile();
+ String output = new String(Files.readAllBytes(f.toPath()));
+ assertTrue(output.contains(key + "=" + value));
+ }
+
+ @Test
+ public void testReloadAfterStore() {
+ try {
+ boolean reloaded;
+ propertiesFile.lock();
+
+ // 1. clear entries + store
+ clearPropertiesFile();
+
+ // 2. load from file
+ reloaded = propertiesFile.load();
+ assertTrue("File was not reloaded!", reloaded);
+
+ // 3. add some entries and store
+ fillProperties(10);
+
+ propertiesFile.store();
+ reloaded = propertiesFile.load();
+
+ assertTrue("File was not reloaded!", reloaded);
+ } finally {
+ propertiesFile.unlock();
+ }
+ }
+
+ private void fillProperties(int noEntries) {
for(int i = 0; i < noEntries; i++) {
- String path = cacheDir + File.separatorChar + i + File.separatorChar + "test" + i + ".jar";
- String key = String.valueOf(System.currentTimeMillis());
- cacheIndexFile.setProperty(key, path);
+ propertiesFile.setProperty(String.valueOf(i), String.valueOf(i));
+ }
+ }
+
+ private void clearPropertiesFile() {
+ try {
+ propertiesFile.lock();
+
+ // clear cache + store file
+ propertiesFile.clear();
+ propertiesFile.store();
+ } finally {
+ propertiesFile.unlock();
}
}
@Test
- public void testReloadAfterStore() {
+ public void testLoad() throws InterruptedException {
+ try {
+ propertiesFile.lock();
- lock();
+ propertiesFile.setProperty("key", "value");
+ propertiesFile.store();
- boolean reloaded = false;
+ propertiesFile.setProperty("shouldNotRemainAfterLoad", "def");
+ propertiesFile.load();
- // 1. clear cache entries + store
- clearCacheIndexFile();
+ assertFalse(propertiesFile.contains("shouldNotRemainAfterLoad"));
+ } finally {
+ propertiesFile.unlock();
- // 2. load cache file
- reloaded = cacheIndexFile.load();
- assertTrue("File was not reloaded!", reloaded);
-
- // 3. add some cache entries and store
- fillCacheIndexFile(noEntriesCacheFile);
- cacheIndexFile.store();
- reloaded = cacheIndexFile.load();
- assertTrue("File was not reloaded!", reloaded);
-
- unlock();
- }
-
- private void clearCacheIndexFile() {
-
- lock();
-
- // clear cache + store file
- cacheIndexFile.clear();
- cacheIndexFile.store();
-
- unlock();
+ }
}
-
- // add locking, because maybe some JNLP runtime user is running. copy/paste from CacheLRUWrapper
+ @Test
+ public void testLoadWithNoChanges() throws InterruptedException {
+ try {
+ propertiesFile.lock();
- /**
- * Lock the file to have exclusive access.
- */
- private void lock() {
- try {
- fl = FileUtils.getFileLock(cacheIndexFile.getStoreFile().getPath(), false, true);
- } catch (OverlappingFileLockException e) { // if overlap we just increase the count.
- } catch (Exception e) { // We didn't get a lock..
- e.printStackTrace();
- }
- if (fl != null) lockCount++;
- }
-
- /**
- * Unlock the file.
- */
- private void unlock() {
- if (fl != null) {
- lockCount--;
- try {
- if (lockCount == 0) {
- fl.release();
- fl.channel().close();
- fl = null;
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
+ propertiesFile.setProperty("key", "value");
+ propertiesFile.store();
+
+ Thread.sleep(1000l);
+
+ assertFalse(propertiesFile.load());
+ } finally {
+ propertiesFile.unlock();
}
}
+
+ @Test
+ public void testLock() throws IOException {
+ try {
+ propertiesFile.lock();
+ assertTrue(propertiesFile.isHeldByCurrentThread());
+ } finally {
+ propertiesFile.unlock();
+ }
+ }
+
+ @Test
+ public void testUnlock() throws IOException {
+ try {
+ propertiesFile.lock();
+ } finally {
+ propertiesFile.unlock();
+ }
+ assertTrue(!propertiesFile.isHeldByCurrentThread());
+ }
}
More information about the distro-pkg-dev
mailing list