Skip to content

Commit

Permalink
Merge pull request #228 from driver733/#226
Browse files Browse the repository at this point in the history
#226 - Fix test.
  • Loading branch information
driver733 committed Jul 5, 2018
2 parents 8763db8 + e2a8421 commit 3f837fc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 68 deletions.
94 changes: 30 additions & 64 deletions src/main/java/com/driver733/vkmusicuploader/post/WatchDirs.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.jcabi.aspects.Immutable;
import com.jcabi.immutable.Array;
import com.jcabi.log.Logger;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
Expand All @@ -55,7 +54,7 @@
* @todo #11 Create a test class for the current class.
*/
@Immutable
public final class WatchDirs implements Closeable {
public final class WatchDirs {

/**
* Directories to watch to changes.
Expand Down Expand Up @@ -95,11 +94,6 @@ public WatchDirs(
this.watcher = FileSystems.getDefault().newWatchService();
}

@Override
public void close() {
throw new UnsupportedOperationException("#close()");
}

/**
* Starts watching the directories for changes.
* @throws Exception If a directory cannot be registered.
Expand All @@ -123,26 +117,9 @@ public void start() throws Exception {
private void processEvents() throws Exception {
while (true) {
final WatchKey key;
try {
key = this.watcher.take();
} catch (final InterruptedException ex) {
throw new IOException(
"Failed to get the signal for directory changes.", ex
);
}
key = this.watcher.take();
final Path dir = this.keys.get(key);
if (dir == null) {
throw new IOException("WatchKey not recognized!");
}
this.processSubevents(key, dir);
if (!key.reset()) {
if (this.keys.isEmpty()) {
throw new IOException(
"All subdirectories are inaccessible"
);
}
this.keys.remove(key);
}
this.posts
.postFromDir(
dir.toFile()
Expand All @@ -164,19 +141,16 @@ private void processSubevents(final WatchKey key, final Path dir)
final Path child = dir.resolve(name);
Logger.debug(
this,
"%s: %s%n", event.kind().name(), child
"%s: %s%n",
event.kind()
.name(),
child
);
if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
try {
if (Files.isDirectory(child)) {
this.processSubevents(child);
}
} catch (final IOException ex) {
throw new IOException(
"Failed to register subdirectories.",
ex
);
}
if (
kind == StandardWatchEventKinds.ENTRY_CREATE
&& Files.isDirectory(child)
) {
this.processSubevents(child);
}
}
}
Expand All @@ -187,17 +161,13 @@ private void processSubevents(final WatchKey key, final Path dir)
* @throws IOException If the directory cannot be registered.
*/
private void registerDirectory(final Path dir) throws IOException {
try {
final WatchKey key = dir.register(
this.watcher,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY
);
this.keys.put(key, dir);
} catch (final IOException ex) {
throw new IOException("Failed to register directory", ex);
}
final WatchKey key = dir.register(
this.watcher,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY
);
this.keys.put(key, dir);
}

/**
Expand All @@ -208,22 +178,18 @@ private void registerDirectory(final Path dir) throws IOException {
* @checkstyle NonStaticMethodCheck (20 lines)
*/
private void processSubevents(final Path root) throws IOException {
try {
Files.walkFileTree(
root,
new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(
final Path subdir,
final BasicFileAttributes attrs
) throws IOException {
registerDirectory(subdir);
return FileVisitResult.CONTINUE;
}
});
} catch (final IOException ex) {
throw new IOException("Failed to register subdirectories", ex);
}
Files.walkFileTree(
root,
new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(
final Path subdir,
final BasicFileAttributes attrs
) throws IOException {
registerDirectory(subdir);
return FileVisitResult.CONTINUE;
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public final class WatchDirsTest extends AbstractVkUnitTest {
"PMD.AvoidCatchingGenericException"
})
public void test() throws Exception {
final int delay = 5;
final Path root = Paths.get("src/test/resources/photos");
final File props = root.resolve("testPhotoAlbum")
.resolve("vkmu.properties")
Expand Down Expand Up @@ -193,24 +194,25 @@ public void test() throws Exception {
new WatchDirs(
new PostsBasic(
posts
)
),
root.toFile()
).start();
} catch (final Exception ex) {
throw new IllegalStateException(ex);
}
}
).start();
TimeUnit.SECONDS.sleep(1);
TimeUnit.SECONDS.sleep(delay);
Files.copy(
root.resolve("testPhotoAlbum")
.resolve("1.jpg"),
new FileOutputStream(
temp
)
);
TimeUnit.SECONDS.sleep(1);
TimeUnit.SECONDS.sleep(delay);
posts.updateProperties();
TimeUnit.SECONDS.sleep(1);
TimeUnit.SECONDS.sleep(delay);
MatcherAssert.assertThat(
"The properties files differ",
actual.entrySet(),
Expand Down

0 comments on commit 3f837fc

Please sign in to comment.