View Javadoc

1   /**
2    * This file Copyright (c) 2010-2013 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.test.fixture.setup;
35  
36  import static info.magnolia.nodebuilder.Ops.*;
37  
38  import info.magnolia.module.AbstractModuleVersionHandler;
39  import info.magnolia.module.InstallContext;
40  import info.magnolia.module.delta.ArrayDelegateTask;
41  import info.magnolia.module.delta.CheckAndModifyPropertyValueTask;
42  import info.magnolia.module.delta.CopyNodeTask;
43  import info.magnolia.module.delta.Delta;
44  import info.magnolia.module.delta.ModuleFilesExtraction;
45  import info.magnolia.module.delta.NodeExistsDelegateTask;
46  import info.magnolia.module.delta.PropertiesImportTask;
47  import info.magnolia.module.delta.RegisterModuleServletsTask;
48  import info.magnolia.module.delta.RemoveNodeTask;
49  import info.magnolia.module.delta.Task;
50  import info.magnolia.module.model.Version;
51  import info.magnolia.nodebuilder.task.ErrorHandling;
52  import info.magnolia.nodebuilder.task.ModuleNodeBuilderTask;
53  import info.magnolia.repository.RepositoryConstants;
54  import info.magnolia.testframework.AbstractMagnoliaIntegrationTest;
55  
56  import java.util.ArrayList;
57  import java.util.List;
58  
59  /**
60   * A version handler which will only work on installs, to ensure we're working with a well-known setup,
61   * and provides a few utility methods to create templates, dialogs, pages, ...
62   *
63   * By default, it imports content into the website and config workspace using the properties file returned by
64   * {@link #getWebsiteImportPropertiesFile()} and {@link #getConfigImportPropertiesFile()} respectively.
65   */
66  public abstract class AbstractTestingVersionHandler extends AbstractModuleVersionHandler {
67      @Override
68      public List<Delta> getDeltas(InstallContext installContext, Version from) {
69          if (from != null) {
70              final String s = "Updates are not supported - please do a fresh install !";
71              final IllegalStateException e = new IllegalStateException(s);
72              installContext.error(s, e);
73              throw e;
74          }
75  
76          // force re-install ..
77          return super.getDeltas(installContext, null);
78      }
79  
80      @Override
81      protected List<Task> getBasicInstallTasks(InstallContext installContext) {
82          final ArrayList<Task> list = new ArrayList<Task>();
83          list.add(new ModuleFilesExtraction());
84          list.add(new RegisterModuleServletsTask());
85          list.add(new ModuleNodeBuilderTask("", "", ErrorHandling.strict,
86                  addNode("templates", "mgnl:content"),
87                  addNode("components", "mgnl:content"),
88                  addNode("dialogs", "mgnl:content")
89          ));
90          list.add(new PropertiesImportTask("Test config content", "Imports content in the config workspace", "config", getConfigImportPropertiesFile()));
91          list.add(new PropertiesImportTask("Test website content", "Imports content in the website workspace", "website", getWebsiteImportPropertiesFile()));
92  
93          list.add(new NodeExistsDelegateTask("Activation", "Changes public URL",  RepositoryConstants.CONFIG,
94                  "/server/activation/subscribers/magnoliaPublic8080", new CheckAndModifyPropertyValueTask("Activation", "Changes public URL", RepositoryConstants.CONFIG,
95                  "/server/activation/subscribers/magnoliaPublic8080", "URL", "http://localhost:8080/magnoliaPublic", AbstractMagnoliaIntegrationTest.Instance.PUBLIC.getURL())));
96  
97          /** DumpHeadersFilter:
98          list.add(new NodeBuilderTask("Setup DebugFilter", "", ErrorHandling.strict, RepositoryConstants.CONFIG, "/server/filters",
99                  addNode("headers-dump").then(
100                         addProperty("class", "info.magnolia.debug.DumpHeadersFilter"),
101                         addProperty("enabled", "true")
102                 )
103         ));
104         list.add(new OrderFilterBeforeTask("headers-dump", new String[]{"context"}));
105         */
106         return list;
107     }
108 
109     protected abstract String getWebsiteImportPropertiesFile();
110 
111     protected abstract String getConfigImportPropertiesFile();
112 
113     @Override
114     protected abstract List<Task> getExtraInstallTasks(InstallContext installContext);
115 
116     protected ModuleNodeBuilderTask newTemplateDefinition(String name, String templateScript, String renderType) {
117         return new ModuleNodeBuilderTask("test template", "", ErrorHandling.strict,
118                 getNode("templates").then(
119                         addNode(name, "mgnl:contentNode").then(
120                                 addProperty("dialog", "sampleDialog"),
121                                 addProperty("templateScript", templateScript),
122                                 addProperty("renderType", renderType),
123                                 addProperty("title", name),
124                                 addProperty("visible", "true")
125                         )
126                 )
127         );
128     }
129 
130     protected ModuleNodeBuilderTask newComponentDefinition(String name, String templateScript, String renderType, Class modelClass) {
131         return new ModuleNodeBuilderTask("test component", "", ErrorHandling.strict,
132                 getNode("templates").then(
133                         addNode(name, "mgnl:contentNode").then(
134                                 addProperty("templateScript", templateScript),
135                                 addProperty("renderType", renderType),
136                                 modelClass != null ? addProperty("modelClass", modelClass.getName()) : noop()
137                         )
138                 )
139         );
140     }
141 
142     protected Task copyArchetypeDialog(final String archetypeName, final String newName) {
143         final String pathPrefix = "/modules/test/dialogs/";
144         final String archetypePath = pathPrefix + archetypeName;
145         final String copyPath = pathPrefix + newName;
146         return new CopyNodeTask("Copy " + archetypeName + " dialog to " + newName, "", "config", archetypePath, copyPath, false);
147     }
148 
149     protected ArrayDelegateTask copyArchetypePageAndChangeTemplate(final String name, final String newPageName, final String newTemplate, final String newTitle, String renderType, String templateScript) {
150         return new ArrayDelegateTask(name, "",
151                 new CopyNodeTask(null, null, "website", "/testpages/test_template_archetype", "/testpages/" + newPageName, false),
152                 new CheckAndModifyPropertyValueTask(null, null, "website", "/testpages/" + newPageName, "mgnl:template", "test:test_template_archetype", "test:"+newTemplate),
153                 new CheckAndModifyPropertyValueTask(null, null, "website", "/testpages/" + newPageName, "title", "Archetype test page for rendering", newTitle),
154                 new CheckAndModifyPropertyValueTask(null, null, "website", "/testpages/" + newPageName, "renderType", "foo", renderType),
155                 new CheckAndModifyPropertyValueTask(null, null, "website", "/testpages/" + newPageName, "templateScript", "bar", templateScript)
156         );
157     }
158 
159     protected Task removeArchetypePage() {
160         return new RemoveNodeTask("", "Remove archetype testpage", "website", "/testpages/test_template_archetype");
161     }
162 }