You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@creadur.apache.org by rd...@apache.org on 2012/10/28 20:48:41 UTC

svn commit: r1403086 - in /creadur/whisker/trunk/apache-whisker-model/src/test/java/org/apache/creadur/whisker/model: DescriptorBuilderForTesting.java LicenseBuilderForTesting.java OrganisationBuilderForTesting.java TestDescriptorPrimaryOnly.java

Author: rdonkin
Date: Sun Oct 28 19:48:41 2012
New Revision: 1403086

URL: http://svn.apache.org/viewvc?rev=1403086&view=rev
Log:
WHISKER-6 Improve test readability by pushing setup into builders

Added:
    creadur/whisker/trunk/apache-whisker-model/src/test/java/org/apache/creadur/whisker/model/DescriptorBuilderForTesting.java   (with props)
    creadur/whisker/trunk/apache-whisker-model/src/test/java/org/apache/creadur/whisker/model/OrganisationBuilderForTesting.java   (with props)
Modified:
    creadur/whisker/trunk/apache-whisker-model/src/test/java/org/apache/creadur/whisker/model/LicenseBuilderForTesting.java
    creadur/whisker/trunk/apache-whisker-model/src/test/java/org/apache/creadur/whisker/model/TestDescriptorPrimaryOnly.java

Added: creadur/whisker/trunk/apache-whisker-model/src/test/java/org/apache/creadur/whisker/model/DescriptorBuilderForTesting.java
URL: http://svn.apache.org/viewvc/creadur/whisker/trunk/apache-whisker-model/src/test/java/org/apache/creadur/whisker/model/DescriptorBuilderForTesting.java?rev=1403086&view=auto
==============================================================================
--- creadur/whisker/trunk/apache-whisker-model/src/test/java/org/apache/creadur/whisker/model/DescriptorBuilderForTesting.java (added)
+++ creadur/whisker/trunk/apache-whisker-model/src/test/java/org/apache/creadur/whisker/model/DescriptorBuilderForTesting.java Sun Oct 28 19:48:41 2012
@@ -0,0 +1,112 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.creadur.whisker.model;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.apache.creadur.whisker.model.LicenseBuilderForTesting.*;
+import static org.apache.creadur.whisker.model.OrganisationBuilderForTesting.*;
+
+public class DescriptorBuilderForTesting {
+
+    public static final String DEFAULT_PRIMARY_COPYRIGHT_NOTICE = "Copyright (c) Primary";
+
+    License primaryLicense = defaultPrimaryLicense();
+    String primaryCopyrightNotice = DEFAULT_PRIMARY_COPYRIGHT_NOTICE;
+    Organisation primaryOrg = defaultPrimaryOrganisation();
+    String primaryNotice = "The primary notice.";
+    Collection<WithinDirectory> contents = new ArrayList<WithinDirectory>();
+    Map<String, License> licenses = new HashMap<String, License>();
+    Map<String, String> notices = new HashMap<String, String>();
+    Map<String, Organisation> organisations = new HashMap<String, Organisation>();
+
+    public Descriptor build() {
+        primaryLicense.storeIn(licenses);
+        primaryOrg.storeIn(organisations);
+
+        return new Descriptor(primaryLicense,
+            primaryCopyrightNotice,
+            primaryOrg.getId(),
+            primaryNotice,
+            licenses,
+            notices,
+            organisations,
+            contents);
+    }
+
+    public DescriptorBuilderForTesting withThirdParty(
+            OrganisationBuilderForTesting builder) {
+        builder.build().storeIn(organisations);
+        return this;
+    }
+
+    public DescriptorBuilderForTesting withThirdParty() {
+        return withThirdParty(new OrganisationBuilderForTesting());
+    }
+
+    public DescriptorBuilderForTesting withDirectory(final String directoryName) {
+        return withDirectory(primaryLicense, primaryOrg, directoryName);
+    }
+
+    public DescriptorBuilderForTesting withDirectory(License license, final Organisation org,
+            final String directoryName) {
+        final WithinDirectory withinDirectory = buildDirectory(license, org,
+                directoryName);
+        contents.add(withinDirectory);
+        return this;
+    }
+
+    public DescriptorBuilderForTesting withThirdPartyDirectory(String directoryName) {
+        return withDirectory(
+                primaryLicense,
+                new OrganisationBuilderForTesting().build(),
+                directoryName);
+    }
+
+    private WithinDirectory buildDirectory(License license,
+            final Organisation org, final String directoryName) {
+        Collection<ByOrganisation> byOrgs = new ArrayList<ByOrganisation>();
+        Collection<Resource> resources = buildResources();
+        byOrgs.add(new ByOrganisation(org, resources));
+
+        Collection<WithLicense> withLicenses = new ArrayList<WithLicense>();
+        String copyright = "Copyright Blah";
+        Map<String, String> params = Collections.emptyMap();
+        withLicenses.add(new WithLicense(license, copyright, params, byOrgs));
+
+        Collection<ByOrganisation> publicDomain = Collections.emptyList();
+
+        final WithinDirectory withinDirectory = new WithinDirectory(directoryName, withLicenses, publicDomain);
+        return withinDirectory;
+    }
+
+    private Collection<Resource> buildResources() {
+        String noticeId = "notice:id";
+        notices.put(noticeId, "Some notice text");
+        Collection<Resource> resources = new ArrayList<Resource>();
+        String source = "";
+        String name = "resource";
+        resources.add(new Resource(name, noticeId, source));
+        return resources;
+    }
+}

Propchange: creadur/whisker/trunk/apache-whisker-model/src/test/java/org/apache/creadur/whisker/model/DescriptorBuilderForTesting.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: creadur/whisker/trunk/apache-whisker-model/src/test/java/org/apache/creadur/whisker/model/LicenseBuilderForTesting.java
URL: http://svn.apache.org/viewvc/creadur/whisker/trunk/apache-whisker-model/src/test/java/org/apache/creadur/whisker/model/LicenseBuilderForTesting.java?rev=1403086&r1=1403085&r2=1403086&view=diff
==============================================================================
--- creadur/whisker/trunk/apache-whisker-model/src/test/java/org/apache/creadur/whisker/model/LicenseBuilderForTesting.java (original)
+++ creadur/whisker/trunk/apache-whisker-model/src/test/java/org/apache/creadur/whisker/model/LicenseBuilderForTesting.java Sun Oct 28 19:48:41 2012
@@ -23,10 +23,23 @@ import java.util.Collections;
 
 public class LicenseBuilderForTesting {
 
-    private static final String DEFAULT_NAME = "Example License";
-    private static final String DEFAULT_URL = "http://example.org";
-    private static final String DEFAULT_ID = "example.org";
-    private static final String DEFAULT_LICENSE_TEXT = "This is the license text";
+    private static final String DEFAULT_PRIMARY_TEXT = "License text for the primary license.";
+    public static final String DEFAULT_PRIMARY_URL = "http://primary.example.org";
+    public static final String DEFAULT_PRIMARY_NAME = "Primary License";
+    public static final String DEFAULT_PRIMARY_ID = "primary.example.org";
+    public static final String DEFAULT_NAME = "Example License";
+    public static final String DEFAULT_URL = "http://example.org";
+    public static final String DEFAULT_ID = "example.org";
+    public static final String DEFAULT_LICENSE_TEXT = "This is the license text";
+
+    public static License defaultPrimaryLicense() {
+        return new LicenseBuilderForTesting()
+            .withId(DEFAULT_PRIMARY_ID)
+            .withName(DEFAULT_PRIMARY_NAME)
+            .withUrl(DEFAULT_PRIMARY_URL)
+            .withText(DEFAULT_PRIMARY_TEXT)
+            .build();
+    }
 
     boolean isSourceRequired = false;
     String baseText = DEFAULT_LICENSE_TEXT;
@@ -38,4 +51,24 @@ public class LicenseBuilderForTesting {
     public License build() {
         return new License(isSourceRequired, baseText, expectedParameters, id, url, name);
     }
+
+    public LicenseBuilderForTesting withId(String id) {
+        this.id = id;
+        return this;
+    }
+
+    public LicenseBuilderForTesting withUrl(String url) {
+        this.url = url;
+        return this;
+    }
+
+    public LicenseBuilderForTesting withName(String name) {
+        this.name = name;
+        return this;
+    }
+
+    public LicenseBuilderForTesting withText(String baseText) {
+        this.baseText = baseText;
+        return this;
+    }
 }

Added: creadur/whisker/trunk/apache-whisker-model/src/test/java/org/apache/creadur/whisker/model/OrganisationBuilderForTesting.java
URL: http://svn.apache.org/viewvc/creadur/whisker/trunk/apache-whisker-model/src/test/java/org/apache/creadur/whisker/model/OrganisationBuilderForTesting.java?rev=1403086&view=auto
==============================================================================
--- creadur/whisker/trunk/apache-whisker-model/src/test/java/org/apache/creadur/whisker/model/OrganisationBuilderForTesting.java (added)
+++ creadur/whisker/trunk/apache-whisker-model/src/test/java/org/apache/creadur/whisker/model/OrganisationBuilderForTesting.java Sun Oct 28 19:48:41 2012
@@ -0,0 +1,60 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.creadur.whisker.model;
+
+public class OrganisationBuilderForTesting {
+
+    public static final String DEFAULT_ORG_URL = "http://thirdparty.org";
+    public static final String DEFAULT_ORG_NAME = "thirdparty.org";
+    public static final String DEFAULT_ORG_ID = "third-party";
+    public static final String DEFAULT_PRIMARY_ORG_URL = "http://primary.org";
+    public static final String DEFAULT_PRIMARY_ORG_NAME = "primary organisation";
+    public static final String DEFAULT_PRIMARY_ORG_ID = "primary.org";
+
+    public static Organisation defaultPrimaryOrganisation() {
+        return new OrganisationBuilderForTesting()
+            .withId(DEFAULT_PRIMARY_ORG_ID)
+            .withName(DEFAULT_PRIMARY_ORG_NAME)
+            .withUrl(DEFAULT_PRIMARY_ORG_URL)
+            .build();
+    }
+
+    String id = DEFAULT_ORG_ID;
+    String name = DEFAULT_ORG_NAME;
+    String url = DEFAULT_ORG_URL;
+
+    public Organisation build() {
+        return new Organisation(id, name,url);
+    }
+
+    public OrganisationBuilderForTesting withId(String id) {
+        this.id = id;
+        return this;
+    }
+
+    public OrganisationBuilderForTesting withName(String name) {
+        this.name = name;
+        return this;
+    }
+
+    public OrganisationBuilderForTesting withUrl(String url) {
+        this.url = url;
+        return this;
+    }
+}

Propchange: creadur/whisker/trunk/apache-whisker-model/src/test/java/org/apache/creadur/whisker/model/OrganisationBuilderForTesting.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: creadur/whisker/trunk/apache-whisker-model/src/test/java/org/apache/creadur/whisker/model/TestDescriptorPrimaryOnly.java
URL: http://svn.apache.org/viewvc/creadur/whisker/trunk/apache-whisker-model/src/test/java/org/apache/creadur/whisker/model/TestDescriptorPrimaryOnly.java?rev=1403086&r1=1403085&r2=1403086&view=diff
==============================================================================
--- creadur/whisker/trunk/apache-whisker-model/src/test/java/org/apache/creadur/whisker/model/TestDescriptorPrimaryOnly.java (original)
+++ creadur/whisker/trunk/apache-whisker-model/src/test/java/org/apache/creadur/whisker/model/TestDescriptorPrimaryOnly.java Sun Oct 28 19:48:41 2012
@@ -18,68 +18,17 @@
  */
 package org.apache.creadur.whisker.model;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
 import junit.framework.TestCase;
 
 public class TestDescriptorPrimaryOnly extends TestCase {
 
-    License primaryLicense = new License(false, "This is the license text", Collections.<String> emptyList(), "example.org", "http://example.org", "Example License");
-    Organisation primaryOrg = new Organisation("primary", "primary.org", "http://primary.org");
-    Organisation thirdPartyOrg = new Organisation("third-party", "thirdparty.org", "http://thirdparty.org");
-    String primaryNotice = "The primary notice.";
-    Collection<WithinDirectory> contents = new ArrayList<WithinDirectory>();
-    Map<String, License> licenses = new HashMap<String, License>();
-    Map<String, String> notices = new HashMap<String, String>();
-    Map<String, Organisation> organisations = new HashMap<String, Organisation>();
-    
+    DescriptorBuilderForTesting builder;
     Descriptor subject;
-    
+
     protected void setUp() throws Exception {
         super.setUp();
-        primaryLicense.storeIn(licenses);
-        primaryOrg.storeIn(organisations);
-        thirdPartyOrg.storeIn(organisations);
-                       
-        addDirectory(primaryLicense, primaryOrg, ".");
-    }
-
-    private void addDirectory(License license, final Organisation org,
-            final String directoryName) {
-        final WithinDirectory withinDirectory = buildDirectory(license, org,
-                directoryName);
-        contents.add(withinDirectory);
-    }
-
-    private WithinDirectory buildDirectory(License license,
-            final Organisation org, final String directoryName) {
-        Collection<ByOrganisation> byOrgs = new ArrayList<ByOrganisation>();
-        Collection<Resource> resources = buildResources();
-        byOrgs.add(new ByOrganisation(org, resources));
-        
-        Collection<WithLicense> withLicenses = new ArrayList<WithLicense>();
-        String copyright = "Copyright Blah";
-        Map<String, String> params = Collections.emptyMap();
-        withLicenses.add(new WithLicense(license, copyright, params, byOrgs));
-        
-        Collection<ByOrganisation> publicDomain = Collections.emptyList();
-        
-        final WithinDirectory withinDirectory = new WithinDirectory(directoryName, withLicenses, publicDomain);
-        return withinDirectory;
-    }
-
-    private Collection<Resource> buildResources() {
-        String noticeId = "notice:id";
-        notices.put(noticeId, "Some notice text");
-        Collection<Resource> resources = new ArrayList<Resource>();
-        String source = "";
-        String name = "resource";
-        resources.add(new Resource(name, noticeId, source));
-        return resources;
+        builder = new DescriptorBuilderForTesting();
+        builder.withThirdParty().withDirectory(".");
     }
 
     protected void tearDown() throws Exception {
@@ -87,31 +36,24 @@ public class TestDescriptorPrimaryOnly e
     }
 
     public void testIsPrimaryOnlyWithThirdPartyResources() throws Exception {
-        subject = 
-                new Descriptor(primaryLicense, primaryOrg.getId(),  primaryNotice, 
-                        licenses, notices, organisations, contents);
-        addDirectory(primaryLicense, thirdPartyOrg, "lib");
-        assertFalse("Work is not primary only when third party resources exist.", subject.isPrimaryOnly());        
+        subject = builder.build();
+        builder.withThirdPartyDirectory("lib");
+        assertFalse("Work is not primary only when third party resources exist.", subject.isPrimaryOnly());
     }
 
     public void testIsPrimaryOnlyWithoutThirdPartyResources() throws Exception {
-        subject = 
-                new Descriptor(primaryLicense, primaryOrg.getId(),  null, 
-                        licenses, notices, organisations, contents);
-        
-        assertTrue("Work is primary only when no third party resources exist.", 
-                subject.isPrimaryOnly());        
+        subject = builder.build();
+        assertTrue("Work is primary only when no third party resources exist.",
+                subject.isPrimaryOnly());
     }
-    
+
     public void testIsPrimaryOnlyWithoutResources() throws Exception {
-        subject = 
-                new Descriptor(primaryLicense, primaryOrg.getId(),  null, 
-                        licenses, notices, organisations, contents);
-        
-        contents.clear();
-        
-        assertTrue("Work is primary only when no third party resources exist.", 
-                subject.isPrimaryOnly());        
+        subject = builder.build();
+
+        builder.contents.clear();
+
+        assertTrue("Work is primary only when no third party resources exist.",
+                subject.isPrimaryOnly());
     }
 
 }