modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftFilesSender.java
| 1
modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftFilesSenderTest.java
| 164 +++++++---
2 files changed, 125 insertions(+), 40 deletions(-)
New commits:
commit b61193a4fb2594e4fcdc1a7b4ed59077c7914f75
Author: John Sanda <jsanda(a)redhat.com>
Date: Wed Jun 29 22:39:53 2011 -0400
Fixing infinite loop bug in DriftFilesSender
Adding another test for the bug and refactoring the tests adding helper
methods.
diff --git
a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftFilesSender.java
b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftFilesSender.java
index e7cf34c..589cc0b 100644
---
a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftFilesSender.java
+++
b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftFilesSender.java
@@ -82,6 +82,7 @@ public class DriftFilesSender implements Runnable {
return new File(dir, fileEntry.getFile());
}
}
+ dirEntry = reader.readDirectoryEntry();
}
return null;
}
diff --git
a/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftFilesSenderTest.java
b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftFilesSenderTest.java
index 6a5eb71..53233e5 100644
---
a/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftFilesSenderTest.java
+++
b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/drift/DriftFilesSenderTest.java
@@ -1,16 +1,9 @@
package org.rhq.core.pc.drift;
-import static java.util.Arrays.asList;
-import static org.apache.commons.io.FileUtils.deleteDirectory;
-import static org.apache.commons.io.FileUtils.touch;
-import static org.apache.commons.io.IOUtils.writeLines;
-import static org.rhq.core.domain.drift.DriftChangeSetCategory.COVERAGE;
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertTrue;
-
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
+import java.util.ArrayList;
import java.util.List;
import org.testng.annotations.BeforeClass;
@@ -20,6 +13,14 @@ import org.testng.annotations.Test;
import org.rhq.common.drift.Headers;
import org.rhq.core.domain.drift.DriftFile;
+import static java.util.Arrays.asList;
+import static org.apache.commons.io.FileUtils.deleteDirectory;
+import static org.apache.commons.io.FileUtils.touch;
+import static org.apache.commons.io.IOUtils.writeLines;
+import static org.rhq.core.domain.drift.DriftChangeSetCategory.COVERAGE;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
public class DriftFilesSenderTest {
File changeSetsDir;
@@ -38,11 +39,8 @@ public class DriftFilesSenderTest {
deleteDirectory(basedir);
basedir.mkdir();
- changeSetsDir = new File(basedir, "changesets");
- changeSetsDir.mkdir();
-
- resourcesDir = new File(basedir, "resources");
- resourcesDir.mkdir();
+ changeSetsDir = mkdir(basedir, "changesets");
+ resourcesDir = mkdir(basedir, "resources");
}
@BeforeMethod
@@ -51,48 +49,134 @@ public class DriftFilesSenderTest {
}
@Test
- public void sendFiles() throws Exception {
- String driftConfigName = "test";
+ public void sendFilesInOneDirectory() throws Exception {
+ String driftConfigName = "single-directory-test";
- File server1Dir = new File(resourcesDir, "server-1");
- server1Dir.mkdir();
-
- File confDir = new File(server1Dir, "conf");
- confDir.mkdir();
-
- touch(new File(confDir, "server.conf"));
+ File server1Dir = mkdir(resourcesDir, "server-1");
+ File confDir = mkdir(server1Dir, "conf");
+ touch(new File(confDir, "server-1.conf"));
+ touch(new File(confDir, "server-2.conf"));
int resourceId = 1;
+ File changeSetDir = createChangeSetDir(resourceId, driftConfigName);
+
+ // Each item in changeSet is listed by the line it will appear in the actual
file
+ // in an attemp to make it more self-documenting.
+ writeChangeSet(changeSetDir, asList(
+ driftConfigName,
+ server1Dir.getAbsolutePath(),
+ COVERAGE.code(),
+ "conf 2",
+ "2e345df 0 server-1.conf A",
+ "a5d8c3e 0 server-2.conf A",
+ ""
+ ));
+
+ DriftFilesSender sender = new DriftFilesSender();
+ sender.setDriftClient(new DriftClientTestStub());
+ sender.setChangeSetManager(changeSetMgr);
+ sender.setResourceId(resourceId);
+ sender.setDriftFiles(driftFiles("2e345df", "a5d8c3e"));
+ sender.setHeaders(new Headers(driftConfigName, server1Dir.getAbsolutePath(),
COVERAGE));
+ sender.run();
- File changeSetDir = new File(new File(changeSetsDir,
Integer.toString(resourceId)), driftConfigName);
- changeSetDir.mkdirs();
+ File contentDir = mkdir(changeSetDir, "content");
- List<String> changeSet = asList(driftConfigName,
server1Dir.getAbsolutePath(), COVERAGE.code(), "conf 1",
- "2e345df 0 server.conf A", "");
- BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new
File(changeSetDir,
- "changeset.txt")));
- writeLines(changeSet, "\n", stream);
- stream.close();
+ assertEquals(contentDir.list().length, 2, "Expected to find two files in
" + contentDir.getAbsolutePath());
+ assertFileCopiedToContentDir(contentDir, "2e345df");
+ assertFileCopiedToContentDir(contentDir, "a5d8c3e");
+ }
- Headers headers = new Headers(driftConfigName, server1Dir.getAbsolutePath(),
COVERAGE);
- List<DriftFile> driftFiles = asList(new DriftFile("2e345df"));
+ @Test
+ public void sendFilesInMultipleDirectories() throws Exception {
+ String driftConfigName = "multiple-directories-test";
+
+ File server2Dir = mkdir(resourcesDir, "server-2");
+ File confDir = mkdir(server2Dir, "conf");
+ File libDir = mkdir(server2Dir, "lib");
+ touch(new File(confDir, "server-1.conf"));
+ touch(new File(confDir, "server-2.conf"));
+ touch(new File(libDir, "server-1.jar"));
+ touch(new File(libDir, "server-2.jar"));
+
+ int resourceId = 2;
+ File changeSetDir = createChangeSetDir(resourceId, driftConfigName);
+
+ // Each item in changeSet is listed by the line it will appear in the actual
file
+ // in an attemp to make it more self-documenting.
+ writeChangeSet(changeSetDir, asList(
+ driftConfigName,
+ server2Dir.getAbsolutePath(),
+ COVERAGE.code(),
+ "conf 2",
+ "2e345df 0 server-1.conf A",
+ "a5d8c3e 0 server-2.conf A",
+ "",
+ "lib 2",
+ "91d4abb 0 server-1.jar A",
+ "92c4abb 0 server-2.jar A",
+ ""
+ ));
DriftFilesSender sender = new DriftFilesSender();
sender.setDriftClient(new DriftClientTestStub());
sender.setChangeSetManager(changeSetMgr);
sender.setResourceId(resourceId);
- sender.setDriftFiles(driftFiles);
- sender.setHeaders(headers);
+ // Note that the order of the drift files is random. When the server sends a
request
+ // for files we cannot assume that the files will be in any particular order.
+ sender.setDriftFiles(driftFiles("2e345df", "91d4abb",
"a5d8c3e", "92c4abb"));
+ sender.setHeaders(new Headers(driftConfigName, server2Dir.getAbsolutePath(),
COVERAGE));
sender.run();
- File contentDir = new File(changeSetDir, "content");
- contentDir.mkdir();
+ File contentDir = mkdir(changeSetDir, "content");
+
+ assertEquals(contentDir.list().length, 4, "Expected to find four files in
" + contentDir.getAbsolutePath());
+ assertFileCopiedToContentDir(contentDir, "2e345df");
+ assertFileCopiedToContentDir(contentDir, "a5d8c3e");
+ assertFileCopiedToContentDir(contentDir, "91d4abb");
+ assertFileCopiedToContentDir(contentDir, "92c4abb");
+ }
+
+ /**
+ * This method only verifies that a file having a particular hash or SHA-256 has
been
+ * copied to the specified content directory.
+ *
+ * @param contentDir The directory to which the file should have been copied
+ *
+ * @param fileHash The file hash which is expected to be the name of the file in the
+ * content directory.
+ */
+ void assertFileCopiedToContentDir(File contentDir, String fileHash) {
+ File file = new File(contentDir, fileHash);
+ assertTrue(file.exists(), "Expected to find file named " +
file.getName() + " in content directory " +
+ contentDir.getPath() + ". The SHA-256 hash should be used as the file
name.");
+ }
+
+ File mkdir(File parent, String name) {
+ File dir = new File(parent, name);
+ dir.mkdirs();
+ return dir;
+ }
+
+ File createChangeSetDir(int resourceId, String driftConfigName) {
+ File dir = new File(new File(changeSetsDir, Integer.toString(resourceId)),
driftConfigName);
+ dir.mkdirs();
+ return dir;
+ }
- File content = new File(contentDir, "2e345df");
+ void writeChangeSet(File changeSetDir, List<String> changeSet) throws Exception
{
+ BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new
File(changeSetDir,
+ "changeset.txt")));
+ writeLines(changeSet, "\n", stream);
+ stream.close();
+ }
- assertEquals(contentDir.list().length, 1, "Expected to find one file in
" + contentDir.getAbsolutePath());
- assertTrue(content.exists(), "Expected to find file named " +
content.getName() + " in content directory. "
- + "SHA-256 hashes should be used as file names");
+ List<DriftFile> driftFiles(String... hashes) {
+ List<DriftFile> files = new ArrayList<DriftFile>();
+ for (String hash : hashes) {
+ files.add(new DriftFile(hash));
+ }
+ return files;
}
}