Testing the order of how properties are merged between .archetype file and wizard. Checking search for files to open is using regexp. ArchetypesUI268677
authorJaroslav Tulach <jtulach@netbeans.org>
Mon, 31 Oct 2016 16:56:13 +0100
branchArchetypesUI268677
changeset 313062580eb081e506
parent 313061 ae57f97b7285
child 313063 dc9f6a45afa9
Testing the order of how properties are merged between .archetype file and wizard. Checking search for files to open is using regexp.
maven/src/org/netbeans/modules/maven/newproject/ArchetypeTemplateHandler.java
maven/test/unit/src/org/netbeans/modules/maven/newproject/ArchetypeTemplateHandlerTest.java
     1.1 --- a/maven/src/org/netbeans/modules/maven/newproject/ArchetypeTemplateHandler.java	Wed Oct 26 21:27:15 2016 +0200
     1.2 +++ b/maven/src/org/netbeans/modules/maven/newproject/ArchetypeTemplateHandler.java	Mon Oct 31 16:56:13 2016 +0100
     1.3 @@ -44,7 +44,6 @@
     1.4  import java.io.InputStream;
     1.5  import java.util.ArrayList;
     1.6  import java.util.Collection;
     1.7 -import java.util.Collections;
     1.8  import java.util.Enumeration;
     1.9  import java.util.List;
    1.10  import java.util.Map;
    1.11 @@ -86,11 +85,7 @@
    1.12          try (InputStream is = desc.getTemplate().getInputStream()) {
    1.13              archetype.load(is);
    1.14          }
    1.15 -        putAllTo(desc.getParameters(), archetype);
    1.16 -        Map<String,?> wizardParams = desc.getValue("wizard");
    1.17 -        if (wizardParams != null) {
    1.18 -            putAllTo(wizardParams, archetype);
    1.19 -        }
    1.20 +        mergeProperties(desc, archetype);
    1.21          
    1.22          String version = archetype.getProperty("version"); // NOI18N
    1.23          if (version == null) {
    1.24 @@ -140,6 +135,14 @@
    1.25          }
    1.26          return fos;
    1.27      }
    1.28 +
    1.29 +    static void mergeProperties(CreateDescriptor desc, Properties archetype) {
    1.30 +        putAllTo(desc.getParameters(), archetype);
    1.31 +        Map<String,?> wizardParams = desc.getValue("wizard"); // NOI18N
    1.32 +        if (wizardParams != null) {
    1.33 +            putAllTo(wizardParams, archetype);
    1.34 +        }
    1.35 +    }
    1.36      
    1.37      private static void collectPomDirs(FileObject dir, Collection<? super FileObject> found) {
    1.38          if (dir == null || !dir.isFolder()) {
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/maven/test/unit/src/org/netbeans/modules/maven/newproject/ArchetypeTemplateHandlerTest.java	Mon Oct 31 16:56:13 2016 +0100
     2.3 @@ -0,0 +1,157 @@
     2.4 +/*
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * Copyright 2015 Oracle and/or its affiliates. All rights reserved.
     2.8 + *
     2.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    2.10 + * Other names may be trademarks of their respective owners.
    2.11 + *
    2.12 + * The contents of this file are subject to the terms of either the GNU
    2.13 + * General Public License Version 2 only ("GPL") or the Common
    2.14 + * Development and Distribution License("CDDL") (collectively, the
    2.15 + * "License"). You may not use this file except in compliance with the
    2.16 + * License. You can obtain a copy of the License at
    2.17 + * http://www.netbeans.org/cddl-gplv2.html
    2.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    2.19 + * specific language governing permissions and limitations under the
    2.20 + * License.  When distributing the software, include this License Header
    2.21 + * Notice in each file and include the License file at
    2.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    2.23 + * particular file as subject to the "Classpath" exception as provided
    2.24 + * by Oracle in the GPL Version 2 section of the License file that
    2.25 + * accompanied this code. If applicable, add the following below the
    2.26 + * License Header, with the fields enclosed by brackets [] replaced by
    2.27 + * your own identifying information:
    2.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    2.29 + *
    2.30 + * If you wish your version of this file to be governed by only the CDDL
    2.31 + * or only the GPL Version 2, indicate your decision by adding
    2.32 + * "[Contributor] elects to include this software in this distribution
    2.33 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    2.34 + * single choice of license, a recipient has the option to distribute
    2.35 + * your version of this file under either the CDDL, the GPL Version 2 or
    2.36 + * to extend the choice of license to its licensees as provided above.
    2.37 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    2.38 + * Version 2 license, then the option applies only if the new code is
    2.39 + * made subject to such option by the copyright holder.
    2.40 + *
    2.41 + * Contributor(s):
    2.42 + *
    2.43 + * Portions Copyrighted 2015 Sun Microsystems, Inc.
    2.44 + */
    2.45 +package org.netbeans.modules.maven.newproject;
    2.46 +
    2.47 +import java.util.Collections;
    2.48 +import java.util.HashSet;
    2.49 +import java.util.Properties;
    2.50 +import java.util.Set;
    2.51 +import static org.junit.Assert.*;
    2.52 +import org.junit.Before;
    2.53 +import org.junit.Test;
    2.54 +import org.netbeans.api.templates.CreateDescriptor;
    2.55 +import org.netbeans.api.templates.FileBuilder;
    2.56 +import org.openide.filesystems.FileObject;
    2.57 +import org.openide.filesystems.FileSystem;
    2.58 +import org.openide.filesystems.FileUtil;
    2.59 +
    2.60 +public class ArchetypeTemplateHandlerTest {
    2.61 +    private FileObject root;
    2.62 +    public ArchetypeTemplateHandlerTest() {
    2.63 +    }
    2.64 +
    2.65 +    @Before
    2.66 +    public void setUpMethod() throws Exception {
    2.67 +        FileSystem fs = FileUtil.createMemoryFileSystem();
    2.68 +        FileUtil.createData(fs.getRoot(), "root/wizard/src/main/java/com/yourorg/somepkg/MyClass.java");
    2.69 +        FileUtil.createData(fs.getRoot(), "root/wizard/src/main/java/com/yourorg/somepkg/Main.java");
    2.70 +        FileUtil.createData(fs.getRoot(), "root/wizard/src/main/webapp/index.html");
    2.71 +        root = fs.findResource("root");
    2.72 +        assertNotNull(root);
    2.73 +    }
    2.74 +
    2.75 +
    2.76 +    @Test
    2.77 +    public void testMatchDataModelJava() {
    2.78 +        Set<FileObject> fos = new HashSet<>();
    2.79 +        ArchetypeTemplateHandler.collectFiles(root, fos, "wizard/src/main/java/.*/MyClass.java");
    2.80 +        assertEquals("One item: " + fos, 1, fos.size());
    2.81 +        FileObject fo = fos.iterator().next();
    2.82 +        assertEquals("root/wizard/src/main/java/com/yourorg/somepkg/MyClass.java", fo.getPath());
    2.83 +    }
    2.84 +
    2.85 +    @Test
    2.86 +    public void testMatchIndexHTML() {
    2.87 +        Set<FileObject> fos = new HashSet<>();
    2.88 +        ArchetypeTemplateHandler.collectFiles(root, fos, "wizard/src/main/webapp/index.html");
    2.89 +        assertEquals("One item: " + fos, fos.size(), 1);
    2.90 +        FileObject fo = fos.iterator().next();
    2.91 +        assertEquals("root/wizard/src/main/webapp/index.html", fo.getPath());
    2.92 +    }
    2.93 +
    2.94 +    @Test
    2.95 +    public void testOrderOfMerging1() throws Exception {
    2.96 +        CreateDescriptor desc = new FileBuilder(root.createData("template.txt"), root.createFolder("targetFolder")).
    2.97 +            param("wizard", Collections.singletonMap("key1", "value1")).
    2.98 +            createDescriptor(true);
    2.99 +        Properties archetypeFile = new Properties();
   2.100 +
   2.101 +        ArchetypeTemplateHandler.mergeProperties(desc, archetypeFile);
   2.102 +
   2.103 +        assertEquals("value1", archetypeFile.get("key1"));
   2.104 +    }
   2.105 +
   2.106 +    @Test
   2.107 +    public void testOrderOfMerging2() throws Exception {
   2.108 +        CreateDescriptor desc = new FileBuilder(root.createData("template.txt"), root.createFolder("targetFolder")).
   2.109 +            param("key1", "value1").
   2.110 +            createDescriptor(true);
   2.111 +        Properties archetypeFile = new Properties();
   2.112 +
   2.113 +        ArchetypeTemplateHandler.mergeProperties(desc, archetypeFile);
   2.114 +
   2.115 +        assertEquals("value1", archetypeFile.get("key1"));
   2.116 +    }
   2.117 +
   2.118 +    @Test
   2.119 +    public void testOrderOfMerging3() throws Exception {
   2.120 +        CreateDescriptor desc = new FileBuilder(root.createData("template.txt"), root.createFolder("targetFolder")).
   2.121 +            param("key2", "value2").
   2.122 +            param("wizard", Collections.singletonMap("key3", "value3")).
   2.123 +            createDescriptor(true);
   2.124 +        Properties archetypeFile = new Properties();
   2.125 +        archetypeFile.put("key1", "value1");
   2.126 +
   2.127 +        ArchetypeTemplateHandler.mergeProperties(desc, archetypeFile);
   2.128 +
   2.129 +        assertEquals("value1", archetypeFile.get("key1"));
   2.130 +        assertEquals("value2", archetypeFile.get("key2"));
   2.131 +        assertEquals("value3", archetypeFile.get("key3"));
   2.132 +    }
   2.133 +
   2.134 +    @Test
   2.135 +    public void testOrderOfMergingDescriptorTakePrecedence() throws Exception {
   2.136 +        CreateDescriptor desc = new FileBuilder(root.createData("template.txt"), root.createFolder("targetFolder")).
   2.137 +            param("key1", "value1").
   2.138 +            createDescriptor(true);
   2.139 +        Properties archetypeFile = new Properties();
   2.140 +        archetypeFile.put("key1", "value2");
   2.141 +
   2.142 +        ArchetypeTemplateHandler.mergeProperties(desc, archetypeFile);
   2.143 +
   2.144 +        assertEquals("value1", archetypeFile.get("key1"));
   2.145 +    }
   2.146 +
   2.147 +    @Test
   2.148 +    public void testOrderOfMergingWizardTakePrecedence() throws Exception {
   2.149 +        CreateDescriptor desc = new FileBuilder(root.createData("template.txt"), root.createFolder("targetFolder")).
   2.150 +            param("key1", "value1").
   2.151 +            param("wizard", Collections.singletonMap("key1", "value2")).
   2.152 +            createDescriptor(true);
   2.153 +        Properties archetypeFile = new Properties();
   2.154 +        archetypeFile.put("key1", "value3");
   2.155 +
   2.156 +        ArchetypeTemplateHandler.mergeProperties(desc, archetypeFile);
   2.157 +
   2.158 +        assertEquals("value2", archetypeFile.get("key1"));
   2.159 +    }
   2.160 +}