View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.creadur.whisker.model;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import static org.apache.creadur.whisker.model.LicenseBuilderForTesting.*;
28  import static org.apache.creadur.whisker.model.OrganisationBuilderForTesting.*;
29  
30  public class DescriptorBuilderForTesting {
31  
32      public static final String DEFAULT_PRIMARY_COPYRIGHT_NOTICE = "Copyright (c) Primary";
33      public static final String DEFAULT_SUBSIDARY_COPYRIGHT_NOTICE = "Copyright (c) Secondary";
34  
35      License primaryLicense = defaultPrimaryLicense();
36      String primaryCopyrightNotice = DEFAULT_PRIMARY_COPYRIGHT_NOTICE;
37      Organisation primaryOrg = defaultPrimaryOrganisation();
38      String primaryNotice = "The primary notice.";
39      Collection<WithinDirectory> contents = new ArrayList<WithinDirectory>();
40      Map<String, License> licenses = new HashMap<String, License>();
41      Map<String, String> notices = new HashMap<String, String>();
42      Map<String, Organisation> organisations = new HashMap<String, Organisation>();
43  
44      String subsidaryCopyrightNotice;
45  
46      public Descriptor build() {
47          primaryLicense.storeIn(licenses);
48          primaryOrg.storeIn(organisations);
49  
50          return new Descriptor(primaryLicense,
51              primaryCopyrightNotice,
52              primaryOrg.getId(),
53              primaryNotice,
54              licenses,
55              notices,
56              organisations,
57              contents);
58      }
59  
60      public DescriptorBuilderForTesting withSubsidaryCopyrightNotice() {
61          return withSubsidaryCopyrightNotice(DEFAULT_SUBSIDARY_COPYRIGHT_NOTICE);
62      }
63  
64      public DescriptorBuilderForTesting withSubsidaryCopyrightNotice(String subsidaryCopyrightNotice) {
65          this.subsidaryCopyrightNotice = subsidaryCopyrightNotice;
66          return this;
67      }
68  
69      public DescriptorBuilderForTesting withThirdParty(
70              OrganisationBuilderForTesting builder) {
71          builder.build().storeIn(organisations);
72          return this;
73      }
74  
75      public DescriptorBuilderForTesting withThirdParty() {
76          return withThirdParty(new OrganisationBuilderForTesting());
77      }
78  
79      public DescriptorBuilderForTesting withDirectory(final String directoryName) {
80          return withDirectory(primaryLicense, primaryOrg, directoryName);
81      }
82  
83      public DescriptorBuilderForTesting withDirectory(License license, final Organisation org,
84              final String directoryName) {
85          final WithinDirectory withinDirectory = buildDirectory(license, org,
86                  directoryName);
87          contents.add(withinDirectory);
88          return this;
89      }
90  
91      public DescriptorBuilderForTesting withThirdPartyDirectory(String directoryName) {
92          return withDirectory(
93                  primaryLicense,
94                  new OrganisationBuilderForTesting().build(),
95                  directoryName);
96      }
97  
98      private WithinDirectory buildDirectory(License license,
99              final Organisation org, final String directoryName) {
100         Collection<ByOrganisation> byOrgs = new ArrayList<ByOrganisation>();
101         Collection<Resource> resources = buildResources();
102         byOrgs.add(new ByOrganisation(org, resources));
103 
104         Collection<WithLicense> withLicenses = new ArrayList<WithLicense>();
105         Map<String, String> params = Collections.emptyMap();
106         withLicenses.add(new WithLicense(license, subsidaryCopyrightNotice, params, byOrgs));
107 
108         Collection<ByOrganisation> publicDomain = Collections.emptyList();
109 
110         final WithinDirectory withinDirectory = new WithinDirectory(directoryName, withLicenses, publicDomain);
111         return withinDirectory;
112     }
113 
114     private Collection<Resource> buildResources() {
115         String noticeId = "notice:id";
116         notices.put(noticeId, "Some notice text");
117         Collection<Resource> resources = new ArrayList<Resource>();
118         String source = "";
119         String name = "resource";
120         resources.add(new Resource(name, noticeId, source));
121         return resources;
122     }
123 }