You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2012/06/12 23:37:22 UTC
[4/5] git commit: Convert TestNG to Spock
Convert TestNG to Spock
Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/a6637031
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/a6637031
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/a6637031
Branch: refs/heads/master
Commit: a66370311b9e16423eabdea3f662ba1ec1536e0c
Parents: b34f111
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Mon Jun 11 17:19:21 2012 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Mon Jun 11 17:19:21 2012 -0700
----------------------------------------------------------------------
.../tapestry5/ioc/util/URLChangeTrackerSpec.groovy | 187 ++++++++++++++
.../ioc/internal/util/URLChangeTrackerTest.java | 198 ---------------
2 files changed, 187 insertions(+), 198 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/a6637031/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/util/URLChangeTrackerSpec.groovy
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/util/URLChangeTrackerSpec.groovy b/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/util/URLChangeTrackerSpec.groovy
new file mode 100644
index 0000000..ad2c28e
--- /dev/null
+++ b/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/util/URLChangeTrackerSpec.groovy
@@ -0,0 +1,187 @@
+package org.apache.tapestry5.ioc.util
+
+import org.apache.tapestry5.ioc.internal.services.ClasspathURLConverterImpl
+import org.apache.tapestry5.ioc.internal.util.URLChangeTracker
+import org.apache.tapestry5.ioc.services.ClasspathURLConverter
+import spock.lang.Shared
+import spock.lang.Specification
+
+class URLChangeTrackerSpec extends Specification {
+
+ @Shared
+ ClasspathURLConverter converter = new ClasspathURLConverterImpl()
+
+ def tracker = new URLChangeTracker(converter)
+
+ def "new instance does not contain changes"() {
+ expect:
+
+ !tracker.containsChanges()
+ }
+
+ def "adding a null URL returns 0"() {
+
+ expect:
+ tracker.add(null) == 0l
+ }
+
+ def touch(file) {
+
+ def initial = file.lastModified()
+ def index = 0
+
+ while (true) {
+
+ file.lastModified = System.currentTimeMillis()
+
+ if (file.lastModified() != initial) { return }
+
+ Thread.sleep(50 * 2 ^ index++)
+ }
+ }
+
+ def "add a file, touch it, and ensure that the change is noticed"() {
+ def f = newFile()
+
+ when:
+
+ tracker.add(f.toURL())
+
+ then:
+
+ // one for the file, one for its directory
+
+ tracker.trackedFileCount() == 2
+ !tracker.containsChanges()
+
+ when:
+
+ touch(f)
+
+ then:
+
+ tracker.containsChanges()
+ }
+
+ def File newFile() {
+ File.createTempFile("changetracker0", ".tmp")
+ }
+
+ def "creating a new file in an existing tracker folder is a change"() {
+
+ def first = newFile()
+ def dir = first.getParentFile()
+
+ when:
+
+ tracker.add(first.toURL())
+
+ then:
+
+ !tracker.containsChanges()
+
+ when:
+
+ def initial = dir.lastModified()
+ def index = 0
+
+ while (true) {
+ newFile()
+
+ if (dir.lastModified() != initial) { break; }
+ Thread.sleep(50 * 2 ^ index++)
+ }
+
+ then:
+
+ tracker.containsChanges()
+ }
+
+ def "non-file URLs are ignored"() {
+
+ when:
+
+ tracker.add(new URL("http://google.com"))
+
+ then:
+
+ tracker.trackedFileCount() == 0
+ }
+
+ def "caching of URLs and timestamps"() {
+
+ def file = newFile()
+ def url = file.toURL()
+
+ def initial = tracker.add(url)
+
+ when:
+
+ touch(file)
+
+ then:
+
+ tracker.add(url) == initial
+
+ tracker.containsChanges()
+
+ when:
+
+ tracker.clear()
+
+ then:
+
+ tracker.add(url) != initial
+ }
+
+ def "deleting a file shows as changes"() {
+ def file = newFile()
+ def url = file.toURL()
+
+ when:
+
+ def initial = tracker.add(url)
+
+ then:
+
+ initial > 0
+ !tracker.containsChanges()
+
+ when:
+
+ file.delete()
+
+ then:
+
+ tracker.containsChanges()
+ }
+
+ def "can track changes at a 1-second granularity (rather than millisecond)"() {
+ tracker = new URLChangeTracker(converter, true, true)
+
+ def file = newFile()
+
+ when:
+
+ long initial = tracker.add(file.toURL())
+
+ then:
+
+ initial % 1000 == 0
+
+ when:
+
+ Thread.sleep 1500
+
+ touch(file)
+
+ then:
+
+ tracker.containsChanges()
+
+ def updated = tracker.add(file.toURL())
+
+ updated % 1000 == 0
+ updated != initial
+ }
+}
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/a6637031/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/util/URLChangeTrackerTest.java
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/util/URLChangeTrackerTest.java b/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/util/URLChangeTrackerTest.java
deleted file mode 100644
index 51e26d9..0000000
--- a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/util/URLChangeTrackerTest.java
+++ /dev/null
@@ -1,198 +0,0 @@
-// Copyright 2006, 2007, 2010 The Apache Software Foundation
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package org.apache.tapestry5.ioc.internal.util;
-
-import java.io.File;
-import java.net.URL;
-
-import org.apache.tapestry5.ioc.internal.services.ClasspathURLConverterImpl;
-import org.apache.tapestry5.ioc.services.ClasspathURLConverter;
-import org.apache.tapestry5.ioc.test.IOCTestCase;
-import org.testng.annotations.Test;
-
-public class URLChangeTrackerTest extends IOCTestCase
-{
- private final ClasspathURLConverter converter = new ClasspathURLConverterImpl();
-
- @Test
- public void contains_change_when_empty()
- {
- URLChangeTracker t = new URLChangeTracker(converter);
-
- assertFalse(t.containsChanges());
- }
-
- @Test
- public void add_null_returns_zero()
- {
- URLChangeTracker t = new URLChangeTracker(converter);
-
- assertEquals(t.add(null), 0l);
- }
-
- @Test
- public void contains_changes() throws Exception
- {
- URLChangeTracker t = new URLChangeTracker(converter);
-
- File f = File.createTempFile("changetracker0", ".tmp");
- URL url = f.toURL();
-
- t.add(url);
-
- // The file, and the directory containing the file.
-
- assertEquals(t.trackedFileCount(), 2);
-
- assertFalse(t.containsChanges());
-
- boolean changed = false;
-
- // Because of clock accuracy, we need to try a couple of times
- // to ensure that the change to the file is visible in the
- // lastUpdated time stamp on the URL.
-
- for (int i = 0; i < 10 && !changed; i++)
- {
- Thread.sleep(100);
-
- touch(f);
-
- changed = t.containsChanges();
- }
-
- assertTrue(changed);
-
- // And, once a change has been observed ...
-
- assertFalse(t.containsChanges());
- }
-
- @Test
- public void creating_a_new_file_is_a_change() throws Exception
- {
- URLChangeTracker t = new URLChangeTracker(converter);
-
- File f = File.createTempFile("changetracker0", ".tmp");
- URL url = f.toURL();
-
- t.add(url);
-
- assertFalse(t.containsChanges());
-
- File dir = f.getParentFile();
-
- // Create another file in the temporary directory.
-
- long timestamp = dir.lastModified();
-
- while (true)
- {
- File.createTempFile("changetracker1", ".tmp");
-
- if (dir.lastModified() != timestamp)
- break;
-
- // Sometime Java need a moment to catch up in terms of
- // file system changes. Sleep for a few milliseconds
- // and wait for it to catch up.
-
- Thread.sleep(100);
- }
-
- assertTrue(t.containsChanges());
- }
-
- @Test
- public void non_file_URLs_are_ignored() throws Exception
- {
- URLChangeTracker t = new URLChangeTracker(converter);
-
- URL url = new URL("http://google.com");
-
- t.add(url);
-
- assertEquals(t.trackedFileCount(), 0);
- }
-
- @Test
- public void caching() throws Exception
- {
- URLChangeTracker t = new URLChangeTracker(converter);
-
- File f = File.createTempFile("changetracker0", ".tmp");
- URL url = f.toURL();
-
- long initial = t.add(url);
-
- touch(f);
-
- long current = t.add(url);
-
- assertEquals(current, initial);
-
- assertTrue(t.containsChanges());
-
- t.clear();
-
- current = t.add(url);
-
- assertFalse(current == initial);
- }
-
- @Test
- public void deleted_files_show_as_changes() throws Exception
- {
- File f = File.createTempFile("changetracker0", ".tmp");
- URL url = f.toURL();
-
- URLChangeTracker t = new URLChangeTracker(converter);
-
- long timeModified = t.add(url);
-
- assertTrue(timeModified > 0);
-
- // File + Directory
- assertEquals(t.trackedFileCount(), 2);
-
- assertFalse(t.containsChanges());
-
- assertTrue(f.delete());
-
- assertTrue(t.containsChanges());
- }
-
- @Test
- public void second_level_granularity() throws Exception
- {
- URLChangeTracker t = new URLChangeTracker(converter, true, true);
-
- File f = File.createTempFile("changetracker0", ".tmp");
- URL url = f.toURL();
-
- touch(f);
- long timestamp1 = t.add(url);
- assertEquals(0, timestamp1 % 1000);
- assertFalse(t.containsChanges());
-
- Thread.sleep(1500);
-
- touch(f);
- long timestamp2 = t.add(url);
- assertEquals(0, timestamp2 % 1000);
- assertTrue(t.containsChanges());
- }
-
-}