You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by nt...@apache.org on 2018/11/14 14:27:40 UTC

[09/32] cayenne git commit: Create tests for maven, ant and gradle

Create tests for maven, ant and gradle


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/93da6be1
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/93da6be1
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/93da6be1

Branch: refs/heads/master
Commit: 93da6be17b9b8cd5ad9c4cb5184565b426991393
Parents: d9db6e3
Author: Arseni Bulatski <an...@gmail.com>
Authored: Fri Jun 29 13:42:55 2018 +0300
Committer: Arseni Bulatski <an...@gmail.com>
Committed: Wed Oct 24 13:44:26 2018 +0300

----------------------------------------------------------------------
 .../cayenne/tools/CgenWithConfigTest.java       | 91 ++++++++++++++++++++
 cayenne-ant/src/test/resources/cgenTest.map.xml | 27 ++++++
 .../cayenne/tools/CgenTaskWithConfigIT.java     | 71 +++++++++++++++
 .../org/apache/cayenne/tools/cgenMap.map.xml    | 27 ++++++
 .../cayenne/tools/cgen_with_config.gradle       | 28 ++++++
 .../cayenne/tools/CayenneGeneratorMojo.java     |  2 +
 .../cayenne/tools/CgenWithConfigMojoTest.java   | 51 +++++++++++
 .../resources/cgen/project-to-test/cgen-pom.xml | 48 +++++++++++
 .../src/test/resources/cgen/testCgenMap.map.xml | 27 ++++++
 9 files changed, 372 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/93da6be1/cayenne-ant/src/test/java/org/apache/cayenne/tools/CgenWithConfigTest.java
----------------------------------------------------------------------
diff --git a/cayenne-ant/src/test/java/org/apache/cayenne/tools/CgenWithConfigTest.java b/cayenne-ant/src/test/java/org/apache/cayenne/tools/CgenWithConfigTest.java
new file mode 100644
index 0000000..cdccd49
--- /dev/null
+++ b/cayenne-ant/src/test/java/org/apache/cayenne/tools/CgenWithConfigTest.java
@@ -0,0 +1,91 @@
+/*****************************************************************
+ *   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.cayenne.tools;
+
+import org.apache.cayenne.test.file.FileUtil;
+import org.apache.cayenne.test.resource.ResourceUtil;
+import org.apache.tools.ant.Location;
+import org.apache.tools.ant.Project;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.File;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * @since 4.1
+ */
+public class CgenWithConfigTest {
+
+    private static final File baseDir;
+    private static final File map;
+
+    static {
+        baseDir = FileUtil.baseTestDirectory();
+        map = new File(baseDir, "antmap-cgen.xml");
+
+        ResourceUtil.copyResourceToFile("cgenTest.map.xml", map);
+    }
+
+    protected CayenneGeneratorTask task;
+
+    @Before
+    public void setUp() {
+
+        Project project = new Project();
+        project.setBaseDir(baseDir);
+
+        task = new CayenneGeneratorTask();
+        task.setProject(project);
+        task.setTaskName("Test");
+        task.setLocation(Location.UNKNOWN_LOCATION);
+    }
+
+    @Test
+    public void testCgen() throws Exception {
+        File mapDir = new File(baseDir, "cgenConfigTest");
+        assertTrue(mapDir.mkdirs());
+
+        task.setDestDir(mapDir);
+        task.setMap(map);
+        task.setMode("entity");
+
+        // run task
+        task.execute();
+
+        // check results
+        File entity = new File(mapDir, convertPath("ObjEntity1.txt"));
+        assertTrue(entity.isFile());
+
+        File datamap = new File(mapDir, convertPath("TestCgenMap.txt"));
+        assertFalse(datamap.exists());
+
+        File notIncludedEntity = new File(mapDir, "ObjEntity.txt");
+        assertFalse(notIncludedEntity.exists());
+
+        File notIncludeSuperDatamap = new File("_TestCgenMap.txt");
+        assertFalse(notIncludeSuperDatamap.exists());
+    }
+
+    private String convertPath(String unixPath) {
+        return unixPath.replace('/', File.separatorChar);
+    }
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/93da6be1/cayenne-ant/src/test/resources/cgenTest.map.xml
----------------------------------------------------------------------
diff --git a/cayenne-ant/src/test/resources/cgenTest.map.xml b/cayenne-ant/src/test/resources/cgenTest.map.xml
new file mode 100644
index 0000000..930acbc
--- /dev/null
+++ b/cayenne-ant/src/test/resources/cgenTest.map.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<data-map xmlns="http://cayenne.apache.org/schema/10/modelMap"
+          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+          xsi:schemaLocation="http://cayenne.apache.org/schema/10/modelMap http://cayenne.apache.org/schema/10/modelMap.xsd"
+          project-version="10">
+    <embeddable className="Embeddable"/>
+    <obj-entity name="ObjEntity" className="ObjEntity"/>
+    <obj-entity name="ObjEntity1" className="ObjEntity1"/>
+    <cgen xmlns="http://cayenne.apache.org/schema/10/cgen">
+        <objEntity>
+            <name>ObjEntity1</name>
+        </objEntity>
+        <generationMode>all</generationMode>
+        <dataMapTemplate>templates/v4_1/datamap-subclass.vm</dataMapTemplate>
+        <dataMapSuperclassTemplate>templates/v4_1/datamap-superclass.vm</dataMapSuperclassTemplate>
+        <subclassTemplate>templates/v4_1/subclass.vm</subclassTemplate>
+        <superclassTemplate>templates/v4_1/superclass.vm</superclassTemplate>
+        <embeddableTemplate>templates/v4_1/embeddable-subclass.vm</embeddableTemplate>
+        <embeddableSuperclassTemplate>templates/v4_1/embeddable-superclass.vm</embeddableSuperclassTemplate>
+        <outputPattern>*.txt</outputPattern>
+        <makePairs>false</makePairs>
+        <usePkgPath>true</usePkgPath>
+        <overwriteSubclasses>false</overwriteSubclasses>
+        <createPropertyNames>false</createPropertyNames>
+        <encoding>UTF-8</encoding>
+    </cgen>
+</data-map>

http://git-wip-us.apache.org/repos/asf/cayenne/blob/93da6be1/cayenne-gradle-plugin/src/test/java/org/apache/cayenne/tools/CgenTaskWithConfigIT.java
----------------------------------------------------------------------
diff --git a/cayenne-gradle-plugin/src/test/java/org/apache/cayenne/tools/CgenTaskWithConfigIT.java b/cayenne-gradle-plugin/src/test/java/org/apache/cayenne/tools/CgenTaskWithConfigIT.java
new file mode 100644
index 0000000..bf981ec
--- /dev/null
+++ b/cayenne-gradle-plugin/src/test/java/org/apache/cayenne/tools/CgenTaskWithConfigIT.java
@@ -0,0 +1,71 @@
+/*****************************************************************
+ *   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.cayenne.tools;
+
+import org.gradle.testkit.runner.BuildResult;
+import org.gradle.testkit.runner.GradleRunner;
+import org.gradle.testkit.runner.TaskOutcome;
+import org.junit.Test;
+
+
+import java.io.File;
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * @since 4.1
+ */
+public class CgenTaskWithConfigIT extends BaseTaskIT{
+
+    @Test
+    public void cgenWithConfig() throws Exception {
+        GradleRunner runner = createRunner(
+                "cgen_with_config",
+                "cgen",
+                "-PdataMap=" + URLDecoder.decode(getClass().getResource("cgenMap.map.xml").getFile(), "UTF-8")
+        );
+
+        BuildResult result = runner.forwardOutput().build();
+
+        String generatedDirectoryPath = projectDir.getAbsolutePath() + "/customDirectory/";
+
+        String generatedClassPath = generatedDirectoryPath + "ObjEntity1.txt";
+        String datamap = generatedDirectoryPath + "TestCgenMap.txt";
+        String notIncludedEntity = generatedDirectoryPath + "ObjEntity.txt";
+        String notIncludedSuperDatamap = generatedDirectoryPath + "_TestCgenMap.txt";
+
+        File notIncludeSuperDatamap = new File("_TestCgenMap.txt");
+        assertFalse(notIncludeSuperDatamap.exists());
+
+        File generatedClass = new File(generatedClassPath);
+        File generatedDatamap = new File(datamap);
+        File generatedNotIncludedEntity = new File(notIncludedEntity);
+        File generatedNotIncludedSuperDatamap = new File(notIncludedSuperDatamap);
+
+        assertTrue(generatedClass.exists());
+        assertFalse(generatedDatamap.exists());
+        assertFalse(generatedNotIncludedEntity.exists());
+        assertFalse(generatedNotIncludedSuperDatamap.exists());
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/93da6be1/cayenne-gradle-plugin/src/test/resources/org/apache/cayenne/tools/cgenMap.map.xml
----------------------------------------------------------------------
diff --git a/cayenne-gradle-plugin/src/test/resources/org/apache/cayenne/tools/cgenMap.map.xml b/cayenne-gradle-plugin/src/test/resources/org/apache/cayenne/tools/cgenMap.map.xml
new file mode 100644
index 0000000..930acbc
--- /dev/null
+++ b/cayenne-gradle-plugin/src/test/resources/org/apache/cayenne/tools/cgenMap.map.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<data-map xmlns="http://cayenne.apache.org/schema/10/modelMap"
+          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+          xsi:schemaLocation="http://cayenne.apache.org/schema/10/modelMap http://cayenne.apache.org/schema/10/modelMap.xsd"
+          project-version="10">
+    <embeddable className="Embeddable"/>
+    <obj-entity name="ObjEntity" className="ObjEntity"/>
+    <obj-entity name="ObjEntity1" className="ObjEntity1"/>
+    <cgen xmlns="http://cayenne.apache.org/schema/10/cgen">
+        <objEntity>
+            <name>ObjEntity1</name>
+        </objEntity>
+        <generationMode>all</generationMode>
+        <dataMapTemplate>templates/v4_1/datamap-subclass.vm</dataMapTemplate>
+        <dataMapSuperclassTemplate>templates/v4_1/datamap-superclass.vm</dataMapSuperclassTemplate>
+        <subclassTemplate>templates/v4_1/subclass.vm</subclassTemplate>
+        <superclassTemplate>templates/v4_1/superclass.vm</superclassTemplate>
+        <embeddableTemplate>templates/v4_1/embeddable-subclass.vm</embeddableTemplate>
+        <embeddableSuperclassTemplate>templates/v4_1/embeddable-superclass.vm</embeddableSuperclassTemplate>
+        <outputPattern>*.txt</outputPattern>
+        <makePairs>false</makePairs>
+        <usePkgPath>true</usePkgPath>
+        <overwriteSubclasses>false</overwriteSubclasses>
+        <createPropertyNames>false</createPropertyNames>
+        <encoding>UTF-8</encoding>
+    </cgen>
+</data-map>

http://git-wip-us.apache.org/repos/asf/cayenne/blob/93da6be1/cayenne-gradle-plugin/src/test/resources/org/apache/cayenne/tools/cgen_with_config.gradle
----------------------------------------------------------------------
diff --git a/cayenne-gradle-plugin/src/test/resources/org/apache/cayenne/tools/cgen_with_config.gradle b/cayenne-gradle-plugin/src/test/resources/org/apache/cayenne/tools/cgen_with_config.gradle
new file mode 100644
index 0000000..3cad46e
--- /dev/null
+++ b/cayenne-gradle-plugin/src/test/resources/org/apache/cayenne/tools/cgen_with_config.gradle
@@ -0,0 +1,28 @@
+/*****************************************************************
+ *   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.
+ ****************************************************************/
+
+plugins {
+    id 'org.apache.cayenne'
+}
+
+cgen {
+    map dataMap
+    destDir = './customDirectory'
+    mode = 'entity'
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cayenne/blob/93da6be1/maven-plugins/cayenne-maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
----------------------------------------------------------------------
diff --git a/maven-plugins/cayenne-maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java b/maven-plugins/cayenne-maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
index e57d306..3bdf08b 100644
--- a/maven-plugins/cayenne-maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
+++ b/maven-plugins/cayenne-maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
@@ -217,6 +217,7 @@ public class CayenneGeneratorMojo extends AbstractMojo {
 		// Create the destination directory if necessary.
 		// TODO: (KJM 11/2/06) The destDir really should be added as a
 		// compilation resource for maven.
+
 		if (!destDir.exists()) {
 			destDir.mkdirs();
 		}
@@ -238,6 +239,7 @@ public class CayenneGeneratorMojo extends AbstractMojo {
 
 			ClassGenerationAction generator = createGenerator(dataMap);
 			generator.setLogger(logger);
+
 			if(force) {
 				// will (re-)generate all files
 				generator.setForce(true);

http://git-wip-us.apache.org/repos/asf/cayenne/blob/93da6be1/maven-plugins/cayenne-maven-plugin/src/test/java/org/apache/cayenne/tools/CgenWithConfigMojoTest.java
----------------------------------------------------------------------
diff --git a/maven-plugins/cayenne-maven-plugin/src/test/java/org/apache/cayenne/tools/CgenWithConfigMojoTest.java b/maven-plugins/cayenne-maven-plugin/src/test/java/org/apache/cayenne/tools/CgenWithConfigMojoTest.java
new file mode 100644
index 0000000..2532db5
--- /dev/null
+++ b/maven-plugins/cayenne-maven-plugin/src/test/java/org/apache/cayenne/tools/CgenWithConfigMojoTest.java
@@ -0,0 +1,51 @@
+/*****************************************************************
+ *   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.cayenne.tools;
+
+import org.apache.maven.plugin.testing.AbstractMojoTestCase;
+
+import java.io.File;
+
+/**
+ * @since 4.1
+ */
+public class CgenWithConfigMojoTest extends AbstractMojoTestCase {
+
+    public void testCgen() throws Exception {
+        File pom = getTestFile("src/test/resources/cgen/project-to-test/cgen-pom.xml");
+        assertNotNull(pom);
+        assertTrue(pom.exists());
+
+        CayenneGeneratorMojo myMojo = (CayenneGeneratorMojo) lookupMojo("cgen", pom);
+        assertNotNull(myMojo);
+        myMojo.execute();
+
+        File testEntity = new File("target/cgenClasses/ObjEntity1.txt");
+        File notIncludedDataMapEntity = new File("target/cgenClasses/TestCgenMap.txt");
+
+        File notIncludedEntity = new File("target/cgenClasses/ObjEntity.txt");
+        File notIncludedSuperDataMap = new File("target/cgenClasses/_TestCgenMap.txt");
+
+        assertTrue(testEntity.exists());
+        assertFalse(notIncludedDataMapEntity.exists());
+
+        assertFalse(notIncludedEntity.exists());
+        assertFalse(notIncludedSuperDataMap.exists());
+    }
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/93da6be1/maven-plugins/cayenne-maven-plugin/src/test/resources/cgen/project-to-test/cgen-pom.xml
----------------------------------------------------------------------
diff --git a/maven-plugins/cayenne-maven-plugin/src/test/resources/cgen/project-to-test/cgen-pom.xml b/maven-plugins/cayenne-maven-plugin/src/test/resources/cgen/project-to-test/cgen-pom.xml
new file mode 100644
index 0000000..eadddc6
--- /dev/null
+++ b/maven-plugins/cayenne-maven-plugin/src/test/resources/cgen/project-to-test/cgen-pom.xml
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+	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.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
+	http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+    <name>Test CayenneCgenMojo</name>
+
+    <dependencies>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <artifactId>cayenne-maven-plugin</artifactId>
+                <configuration>
+                    <map>src/test/resources/cgen/testCgenMap.map.xml</map>
+                    <destDir>target/cgenClasses</destDir>
+                    <mode>entity</mode>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>

http://git-wip-us.apache.org/repos/asf/cayenne/blob/93da6be1/maven-plugins/cayenne-maven-plugin/src/test/resources/cgen/testCgenMap.map.xml
----------------------------------------------------------------------
diff --git a/maven-plugins/cayenne-maven-plugin/src/test/resources/cgen/testCgenMap.map.xml b/maven-plugins/cayenne-maven-plugin/src/test/resources/cgen/testCgenMap.map.xml
new file mode 100644
index 0000000..930acbc
--- /dev/null
+++ b/maven-plugins/cayenne-maven-plugin/src/test/resources/cgen/testCgenMap.map.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<data-map xmlns="http://cayenne.apache.org/schema/10/modelMap"
+          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+          xsi:schemaLocation="http://cayenne.apache.org/schema/10/modelMap http://cayenne.apache.org/schema/10/modelMap.xsd"
+          project-version="10">
+    <embeddable className="Embeddable"/>
+    <obj-entity name="ObjEntity" className="ObjEntity"/>
+    <obj-entity name="ObjEntity1" className="ObjEntity1"/>
+    <cgen xmlns="http://cayenne.apache.org/schema/10/cgen">
+        <objEntity>
+            <name>ObjEntity1</name>
+        </objEntity>
+        <generationMode>all</generationMode>
+        <dataMapTemplate>templates/v4_1/datamap-subclass.vm</dataMapTemplate>
+        <dataMapSuperclassTemplate>templates/v4_1/datamap-superclass.vm</dataMapSuperclassTemplate>
+        <subclassTemplate>templates/v4_1/subclass.vm</subclassTemplate>
+        <superclassTemplate>templates/v4_1/superclass.vm</superclassTemplate>
+        <embeddableTemplate>templates/v4_1/embeddable-subclass.vm</embeddableTemplate>
+        <embeddableSuperclassTemplate>templates/v4_1/embeddable-superclass.vm</embeddableSuperclassTemplate>
+        <outputPattern>*.txt</outputPattern>
+        <makePairs>false</makePairs>
+        <usePkgPath>true</usePkgPath>
+        <overwriteSubclasses>false</overwriteSubclasses>
+        <createPropertyNames>false</createPropertyNames>
+        <encoding>UTF-8</encoding>
+    </cgen>
+</data-map>