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/21 21:35:35 UTC

[5/6] 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/7d90bde2
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/7d90bde2
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/7d90bde2

Branch: refs/heads/master
Commit: 7d90bde2abfd74bc0f912afcc0d06566701a0474
Parents: c9a6241
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Thu Jun 21 11:10:04 2012 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Thu Jun 21 11:10:04 2012 -0700

----------------------------------------------------------------------
 .../groovy/ioc/specs/ClasspathResourceSpec.groovy  |  226 +++++++++++++
 .../ioc/internal/util/ClasspathResourceTest.java   |  251 ---------------
 2 files changed, 226 insertions(+), 251 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/7d90bde2/tapestry-ioc/src/test/groovy/ioc/specs/ClasspathResourceSpec.groovy
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/test/groovy/ioc/specs/ClasspathResourceSpec.groovy b/tapestry-ioc/src/test/groovy/ioc/specs/ClasspathResourceSpec.groovy
new file mode 100644
index 0000000..fc96e0e
--- /dev/null
+++ b/tapestry-ioc/src/test/groovy/ioc/specs/ClasspathResourceSpec.groovy
@@ -0,0 +1,226 @@
+package ioc.specs
+
+import org.apache.tapestry5.ioc.internal.util.ClasspathResource
+import spock.lang.Specification
+
+class ClasspathResourceSpec extends Specification {
+
+  static final String RESOURCE_TXT_CONTENT = "content from resource.txt";
+
+  static final String FOLDER = "org/apache/tapestry5/ioc/internal/util";
+
+  static final String PATH = FOLDER + "/resource.txt";
+  public static final String SAME_FOLDER_RESOURCE_CONTENT = "content from same-folder resource"
+
+  def content(resource) {
+    resource.toURL().text.trim()
+  }
+
+  def "get URL of resource"() {
+    def r = new ClasspathResource(PATH)
+
+    expect:
+
+    content(r) == RESOURCE_TXT_CONTENT
+  }
+
+  def "expend path from root resource yields same URL as full path"() {
+    def r = new ClasspathResource("").forFile(PATH)
+
+    expect:
+
+    content(r) == RESOURCE_TXT_CONTENT
+  }
+
+  def "a leading slash is ignored for the root resource"() {
+    def r = new ClasspathResource("/").forFile(PATH)
+
+    expect:
+
+    content(r) == RESOURCE_TXT_CONTENT
+  }
+
+  def "a leading slash is ignored for the path from the root resource"() {
+    def r = new ClasspathResource("/").forFile("/" + PATH)
+
+    expect:
+
+    content(r) == RESOURCE_TXT_CONTENT
+  }
+
+  def "path and file for resource are available"() {
+    when:
+
+    def r = new ClasspathResource(PATH)
+
+    then:
+
+    r.folder == FOLDER
+    r.file == "resource.txt"
+  }
+
+  def "access a file in the same folder"() {
+    when:
+
+    def r = new ClasspathResource(PATH)
+    def n = r.forFile("same-folder.txt")
+
+    then:
+
+    content(n) == SAME_FOLDER_RESOURCE_CONTENT
+  }
+
+  def "access a file in same folder using using ./ relative path"() {
+    when:
+
+    def r = new ClasspathResource(PATH)
+    def n = r.forFile("./same-folder.txt")
+
+    then:
+
+    content(n) == SAME_FOLDER_RESOURCE_CONTENT
+
+  }
+
+  def "multiple slashes treated as a single slash"() {
+    when:
+
+    def r = new ClasspathResource(PATH)
+    def n = r.forFile("././/.///same-folder.txt")
+
+    then:
+
+    content(n) == SAME_FOLDER_RESOURCE_CONTENT
+
+  }
+
+  def "file in subfolder"() {
+    when:
+
+    def r = new ClasspathResource(PATH)
+    def n = r.forFile("sub/sub-folder.txt")
+
+    then:
+
+    content(n) == "content from sub-folder resource"
+
+  }
+
+  def "reference same resource yields same instance"() {
+    when:
+
+    def r = new ClasspathResource(PATH)
+
+
+    then:
+
+    r.forFile("../util/resource.txt").is(r)
+  }
+
+  def "access file in parent folder"() {
+    def r = new ClasspathResource(PATH)
+
+    when:
+
+    def n = r.forFile("../parent-folder.txt")
+
+    then:
+
+    content(n) == "content from parent-folder resource"
+  }
+
+  def "toString() is meaningful"() {
+    expect:
+
+    new ClasspathResource(PATH).toString() == "classpath:$PATH"
+  }
+
+  def "URL for a missing resource is null"() {
+    when:
+
+    def r = new ClasspathResource("$FOLDER/missing-resource.txt")
+
+    then:
+
+    r.toURL() == null
+  }
+
+  def "localization of a resource"() {
+
+    def r = new ClasspathResource(PATH)
+
+    when:
+
+    def l = r.forLocale(Locale.FRENCH)
+
+    then:
+
+    content(l) == "french content"
+  }
+
+  def "localization finds closest match"() {
+    def r = new ClasspathResource(PATH)
+
+    when:
+
+    def l = r.forLocale(Locale.CANADA_FRENCH)
+
+    then:
+
+    content(l) == "french content"
+  }
+
+  def "localization to base resource"() {
+    def r = new ClasspathResource(PATH)
+
+    when:
+
+    def l = r.forLocale(Locale.JAPANESE)
+
+    then:
+
+    l.is(r)
+  }
+
+  def "localization of missing resource is null"() {
+    def r = new ClasspathResource("$FOLDER/missing-resource.txt")
+
+    expect:
+
+    r.forLocale(Locale.KOREAN) == null
+
+  }
+
+  def "extending to new extension that matches old extension yields same Resource"() {
+    def r = new ClasspathResource(PATH)
+
+    when:
+
+    def n = r.withExtension("txt")
+
+    then:
+
+    n.is(r)
+  }
+
+  def "extending with a new extension"() {
+    def r = new ClasspathResource(PATH)
+
+    when:
+
+    def n = r.withExtension("ext")
+
+    then:
+
+    content(n) == "ext content"
+  }
+
+  def "a new extension that does not exist is a null URL"() {
+    def r = new ClasspathResource(PATH)
+
+
+    expect:
+
+    r.withExtension("does-not-exist").toURL() == null
+  }
+}

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/7d90bde2/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/util/ClasspathResourceTest.java
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/util/ClasspathResourceTest.java b/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/util/ClasspathResourceTest.java
deleted file mode 100644
index 73d80de..0000000
--- a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/util/ClasspathResourceTest.java
+++ /dev/null
@@ -1,251 +0,0 @@
-// Copyright 2006 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 org.apache.tapestry5.ioc.Resource;
-import org.testng.Assert;
-import org.testng.annotations.Test;
-
-import java.io.BufferedInputStream;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
-import java.net.URL;
-import java.util.Locale;
-
-public class ClasspathResourceTest extends Assert
-{
-    private static final String RESOURCE_TXT_CONTENT = "content from resource.txt";
-
-    private static final String FOLDER = "org/apache/tapestry5/ioc/internal/util";
-
-    private static final String PATH = FOLDER + "/resource.txt";
-
-    @Test
-    public void get_resource_URL() throws Exception
-    {
-        Resource r = new ClasspathResource(PATH);
-
-        assertEquals(content(r), RESOURCE_TXT_CONTENT);
-    }
-
-    @Test
-    public void relative_to_root_resource() throws Exception
-    {
-        Resource r = new ClasspathResource("").forFile(PATH);
-
-        assertEquals(content(r), RESOURCE_TXT_CONTENT);
-    }
-
-    @Test
-    public void relative_to_root_resource_using_leading_slash() throws Exception
-    {
-        Resource r = new ClasspathResource("/").forFile(PATH);
-
-        assertEquals(content(r), RESOURCE_TXT_CONTENT);
-    }
-
-    @Test
-    public void leading_slash_on_path_relative_to_root_doesnt_matter() throws Exception
-    {
-        Resource r = new ClasspathResource("/").forFile("/" + PATH);
-
-        assertEquals(content(r), RESOURCE_TXT_CONTENT);
-    }
-
-    @Test
-    public void path_and_file()
-    {
-        Resource r = new ClasspathResource(PATH);
-
-        assertEquals(r.getFolder(), FOLDER);
-        assertEquals(r.getFile(), "resource.txt");
-
-    }
-
-    @Test
-    public void for_file_in_same_folder() throws Exception
-    {
-        Resource r = new ClasspathResource(PATH);
-
-        Resource n = r.forFile("same-folder.txt");
-
-        assertEquals(content(n), "content from same-folder resource");
-    }
-
-    @Test
-    public void for_file_single_dot() throws Exception
-    {
-        Resource r = new ClasspathResource(PATH);
-
-        Resource n = r.forFile("./same-folder.txt");
-
-        assertEquals(content(n), "content from same-folder resource");
-    }
-
-    @Test
-    public void multiple_slashes_treated_as_single_slash() throws Exception
-    {
-        Resource r = new ClasspathResource(PATH);
-
-        Resource n = r.forFile("././/.///same-folder.txt");
-
-        assertEquals(content(n), "content from same-folder resource");
-    }
-
-    @Test
-    public void for_file_in_subfolder() throws Exception
-    {
-        Resource r = new ClasspathResource(PATH);
-
-        Resource n = r.forFile("sub/sub-folder.txt");
-
-        assertEquals(content(n), "content from sub-folder resource");
-    }
-
-    @Test
-    public void for_file_same_resource() throws Exception
-    {
-        Resource r = new ClasspathResource(PATH);
-
-        assertSame(r.forFile("../util/resource.txt"), r);
-    }
-
-    @Test
-    public void for_file_in_parent_folder() throws Exception
-    {
-        Resource r = new ClasspathResource(PATH);
-
-        Resource n = r.forFile("../parent-folder.txt");
-
-        assertEquals(content(n), "content from parent-folder resource");
-    }
-
-    @Test
-    public void to_string() throws Exception
-    {
-        Resource r = new ClasspathResource(PATH);
-
-        assertEquals(r.toString(), "classpath:" + PATH);
-    }
-
-    @Test
-    public void get_URL_for_missing_resource() throws Exception
-    {
-        Resource r = new ClasspathResource(FOLDER + "/missing-resource.txt");
-
-        assertNull(r.toURL());
-    }
-
-    @Test
-    public void localization_of_resource() throws Exception
-    {
-        Resource r = new ClasspathResource(PATH);
-
-        Resource l = r.forLocale(Locale.FRENCH);
-
-        assertEquals(content(l), "french content");
-    }
-
-    @Test
-    public void localization_to_closest_match() throws Exception
-    {
-        Resource r = new ClasspathResource(PATH);
-
-        Resource l = r.forLocale(Locale.CANADA_FRENCH);
-
-        assertEquals(content(l), "french content");
-    }
-
-    @Test
-    public void localization_to_base_resource() throws Exception
-    {
-        Resource r = new ClasspathResource(PATH);
-
-        Resource l = r.forLocale(Locale.JAPANESE);
-
-        assertSame(l, r);
-    }
-
-    @Test
-    public void with_extension_same_extension()
-    {
-        Resource r = new ClasspathResource(PATH);
-
-        assertSame(r.withExtension("txt"), r);
-    }
-
-    @Test
-    public void with_extension() throws Exception
-    {
-        Resource r = new ClasspathResource(PATH);
-        Resource e = r.withExtension("ext");
-
-        assertEquals(content(e), "ext content");
-    }
-
-    @Test
-    public void with_extension_adds_extension() throws Exception
-    {
-        Resource r = new ClasspathResource(FOLDER + "/resource");
-        Resource e = r.withExtension("ext");
-
-        assertEquals(content(e), "ext content");
-    }
-
-    @Test
-    public void with_extension_missing_resource_is_null()
-    {
-        Resource r = new ClasspathResource(PATH);
-        Resource e = r.withExtension("does-not-exist");
-
-        assertNull(e.toURL());
-    }
-
-    @Test
-    public void localization_of_missing_resource() throws Exception
-    {
-        Resource r = new ClasspathResource(FOLDER + "/missing-resource.txt");
-
-        assertNull(r.forLocale(Locale.FRENCH));
-    }
-
-    private String content(Resource resource) throws Exception
-    {
-        return content(resource.toURL());
-    }
-
-    private String content(URL url) throws Exception
-    {
-        InputStream is = new BufferedInputStream(url.openStream());
-        Reader r = new InputStreamReader(is);
-
-        StringBuilder builder = new StringBuilder();
-        char[] buffer = new char[2000];
-
-        while (true)
-        {
-            int length = r.read(buffer, 0, buffer.length);
-
-            if (length < 0) break;
-
-            builder.append(buffer, 0, length);
-        }
-
-        r.close();
-
-        return builder.toString().trim();
-    }
-}