You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by zr...@apache.org on 2017/03/13 09:42:09 UTC

[2/2] camel git commit: CAMEL-10966 Salesforce Maven Plugin doesn't esc...

CAMEL-10966 Salesforce Maven Plugin doesn't esc...

...ape strings when doing the camel-salesforce:generate phase

This changes the test a bit, let me explain how: there is no need to use
reflection to make a method in the object under test accessible, we can
simply change the visibility of the target method and refactor where
needed. This makes the object under test easier to test and in the end
better in quality.

So I changed the visibility of the `engine` field to package private to
make it accessible from the test, and refactored the initialization of
`VelocityEngine` into `createVelocityEngine` method so that it can be
used in the test. Now this can be used in the `setUp` method of the
test.

To clean up the test a bit I used `Parameterized` runner which is nice
for tests that repeat the same checks over a number of inputs --
parameters, there is a unused field `json` but that's the way the
`Parameterized` runner works: every parameter needs to be used. I've
added `@Parameters(name = "json = {0}, source = {2}")` to make the tests
cases for different parameters more distinct from each other, with IDE
support it is possible to run all or only single test case (set of
parameters)[1].

I also added a JUnit rule that will create and cleanup the temporary
directory, that way the test can be run via different runners in
parallel -- for instance I use Infinitest that runs tests in the IDE in
the background, now I'm sure that those won't interfere with the tests I
run manually. So this makes the test run less prone to outside
influences and less _flaky_.

[1] http://blog.moritz.eysholdt.de/2014/11/new-eclipse-junit-feature-run-subtrees.html


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

Branch: refs/heads/master
Commit: cccf6726b1fb59d592453e32909c0c3fa39dad2c
Parents: 91424b8
Author: Zoran Regvart <zr...@apache.org>
Authored: Sat Mar 11 14:45:59 2017 +0100
Committer: Zoran Regvart <zr...@apache.org>
Committed: Mon Mar 13 10:40:44 2017 +0100

----------------------------------------------------------------------
 .../apache/camel/maven/CamelSalesforceMojo.java |  27 ++--
 .../maven/CamelSalesforceMojoOutputTest.java    | 151 +++++++------------
 2 files changed, 69 insertions(+), 109 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/cccf6726/components/camel-salesforce/camel-salesforce-maven-plugin/src/main/java/org/apache/camel/maven/CamelSalesforceMojo.java
----------------------------------------------------------------------
diff --git a/components/camel-salesforce/camel-salesforce-maven-plugin/src/main/java/org/apache/camel/maven/CamelSalesforceMojo.java b/components/camel-salesforce/camel-salesforce-maven-plugin/src/main/java/org/apache/camel/maven/CamelSalesforceMojo.java
index f557a56..33aceba 100644
--- a/components/camel-salesforce/camel-salesforce-maven-plugin/src/main/java/org/apache/camel/maven/CamelSalesforceMojo.java
+++ b/components/camel-salesforce/camel-salesforce-maven-plugin/src/main/java/org/apache/camel/maven/CamelSalesforceMojo.java
@@ -266,7 +266,8 @@ public class CamelSalesforceMojo extends AbstractMojo {
     @Parameter(property = "camelSalesforce.useStringsForPicklists", defaultValue = "false")
     protected Boolean useStringsForPicklists;
 
-    private VelocityEngine engine;
+    VelocityEngine engine;
+
     private long responseTimeout;
 
     /**
@@ -275,14 +276,7 @@ public class CamelSalesforceMojo extends AbstractMojo {
      * @throws MojoExecutionException
      */
     public void execute() throws MojoExecutionException {
-        // initialize velocity to load resources from class loader and use Log4J
-        Properties velocityProperties = new Properties();
-        velocityProperties.setProperty(RuntimeConstants.RESOURCE_LOADER, "cloader");
-        velocityProperties.setProperty("cloader.resource.loader.class", ClasspathResourceLoader.class.getName());
-        velocityProperties.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, Log4JLogChute.class.getName());
-        velocityProperties.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM + ".log4j.logger", LOG.getName());
-        engine = new VelocityEngine(velocityProperties);
-        engine.init();
+        engine = createVelocityEngine();
 
         // make sure we can load both templates
         if (!engine.resourceExists(SOBJECT_POJO_VM)
@@ -426,6 +420,19 @@ public class CamelSalesforceMojo extends AbstractMojo {
         }
     }
 
+    static VelocityEngine createVelocityEngine() {
+        // initialize velocity to load resources from class loader and use Log4J
+        final Properties velocityProperties = new Properties();
+        velocityProperties.setProperty(RuntimeConstants.RESOURCE_LOADER, "cloader");
+        velocityProperties.setProperty("cloader.resource.loader.class", ClasspathResourceLoader.class.getName());
+        velocityProperties.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, Log4JLogChute.class.getName());
+        velocityProperties.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM + ".log4j.logger", LOG.getName());
+
+        final VelocityEngine engine = new VelocityEngine(velocityProperties);
+
+        return engine;
+    }
+
     protected void filterObjectNames(Set<String> objectNames) throws MojoExecutionException {
         getLog().info("Looking for matching Object names...");
         // create a list of accepted names
@@ -574,7 +581,7 @@ public class CamelSalesforceMojo extends AbstractMojo {
         return httpClient;
     }
 
-    private void processDescription(File pkgDir, SObjectDescription description, GeneratorUtility utility, String generatedDate) throws MojoExecutionException {
+    void processDescription(File pkgDir, SObjectDescription description, GeneratorUtility utility, String generatedDate) throws MojoExecutionException {
         // generate a source file for SObject
         String fileName = description.getName() + JAVA_EXT;
         BufferedWriter writer = null;

http://git-wip-us.apache.org/repos/asf/camel/blob/cccf6726/components/camel-salesforce/camel-salesforce-maven-plugin/src/test/java/org/apache/camel/maven/CamelSalesforceMojoOutputTest.java
----------------------------------------------------------------------
diff --git a/components/camel-salesforce/camel-salesforce-maven-plugin/src/test/java/org/apache/camel/maven/CamelSalesforceMojoOutputTest.java b/components/camel-salesforce/camel-salesforce-maven-plugin/src/test/java/org/apache/camel/maven/CamelSalesforceMojoOutputTest.java
index f4d9851..4ddf2d2 100644
--- a/components/camel-salesforce/camel-salesforce-maven-plugin/src/test/java/org/apache/camel/maven/CamelSalesforceMojoOutputTest.java
+++ b/components/camel-salesforce/camel-salesforce-maven-plugin/src/test/java/org/apache/camel/maven/CamelSalesforceMojoOutputTest.java
@@ -17,131 +17,84 @@
 package org.apache.camel.maven;
 
 import java.io.File;
+import java.io.IOException;
 import java.io.InputStream;
-import java.lang.Exception;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.util.Properties;
-
-import org.junit.Test;
-import org.junit.Assert;
-import org.junit.Before;
-
-import org.apache.maven.plugin.logging.SystemStreamLog;
+import java.util.Arrays;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
 
-import org.apache.camel.component.salesforce.api.utils.JsonUtils;
 import org.apache.camel.component.salesforce.api.dto.SObjectDescription;
-
-import org.apache.log4j.Logger;
-
-import org.apache.velocity.app.VelocityEngine;
-import org.apache.velocity.runtime.RuntimeConstants;
-import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
-import org.apache.velocity.runtime.log.Log4JLogChute;
-
+import org.apache.camel.component.salesforce.api.utils.JsonUtils;
 import org.apache.commons.io.FileUtils;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
 
-//import java.util.Scanner;
-
+@RunWith(Parameterized.class)
 public class CamelSalesforceMojoOutputTest {
     private static final String TEST_CASE_FILE = "case.json";
     private static final String TEST_CALCULATED_FORMULA_FILE = "complex_calculated_formula.json";
-    private static final String OUTPUT_FOLDER = "target/test-generated-sources";
-    private static final String generatedDate = "Thu Mar 09 16:15:49 ART 2017";
-
-    private CamelSalesforceMojoAccessor mojo;
-    private CamelSalesforceMojo.GeneratorUtility utility;
-    private File pkgDir;
-
-    private static class CamelSalesforceMojoAccessor extends CamelSalesforceMojo {
-        private static final Logger LOG = Logger.getLogger(CamelSalesforceMojoAccessor.class.getName());
-
-        public CamelSalesforceMojoAccessor() throws Exception {
-            // initialize velocity to load resources from class loader and use Log4J
-            Properties velocityProperties = new Properties();
-            velocityProperties.setProperty(RuntimeConstants.RESOURCE_LOADER, "cloader");
-            velocityProperties.setProperty("cloader.resource.loader.class", ClasspathResourceLoader.class.getName());
-            velocityProperties.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, Log4JLogChute.class.getName());
-            velocityProperties.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM + ".log4j.logger", LOG.getName());
-
-            VelocityEngine engine = new VelocityEngine(velocityProperties);
-            engine.init();
-
-            Field field = CamelSalesforceMojo.class.getDeclaredField("engine");
-            field.setAccessible(true);
-            field.set(this, engine);
-        }
-
-        // Expose processDescription in order to test it
-        public void processDescription(File pkgDir, SObjectDescription description, GeneratorUtility utility, String generatedDate) throws Exception {
-            Method method = CamelSalesforceMojo.class.getDeclaredMethod("processDescription", File.class, SObjectDescription.class, CamelSalesforceMojo.GeneratorUtility.class, String.class);
-            method.setAccessible(true);
-            method.invoke(this, pkgDir, description, utility, generatedDate);
-        }
+    private static final String FIXED_DATE = "Thu Mar 09 16:15:49 ART 2017";
+
+    private CamelSalesforceMojo mojo;
+    private CamelSalesforceMojo.GeneratorUtility utility = new CamelSalesforceMojo.GeneratorUtility(false);
+
+    @Rule
+    public TemporaryFolder temp = new TemporaryFolder();
+
+    @Parameters(name = "json = {0}, source = {2}")
+    public static Iterable<Object[]> parameters() throws IOException {
+        return Arrays.asList(pair(TEST_CASE_FILE, "Case.java"),
+            pair(TEST_CASE_FILE, "Case_PickListAccentMarkEnum.java"),
+            pair(TEST_CASE_FILE, "Case_PickListQuotationMarkEnum.java"),
+            pair(TEST_CASE_FILE, "Case_PickListSlashEnum.java"), pair(TEST_CASE_FILE, "QueryRecordsCase.java"),
+            pair(TEST_CALCULATED_FORMULA_FILE, "ComplexCalculatedFormula.java"),
+            pair(TEST_CALCULATED_FORMULA_FILE, "QueryRecordsComplexCalculatedFormula.java"));
     }
 
-    @Before
-    public void setUp() throws Exception {
-        mojo = createMojo();
-
-        pkgDir = new File(OUTPUT_FOLDER);
-        if (!pkgDir.exists()) {
-            if (!pkgDir.mkdirs()) {
-                throw new Exception("Unable to create " + pkgDir);
-            }
-        }
-
-        utility = createGeneratorUtility();
+    static Object[] pair(String json, String source) throws IOException {
+        return new Object[] {json, createSObjectDescription(json), source};
     }
 
-    @Test
-    public void testProcessDescriptionPickLists() throws Exception {
-        SObjectDescription description = createSObjectDescription(TEST_CASE_FILE);
+    @Parameter(0)
+    public String json;
+
+    @Parameter(1)
+    public SObjectDescription description;
 
-        mojo.processDescription(pkgDir, description, utility, generatedDate);
+    @Parameter(2)
+    public String source;
 
-        assertClassFile("Case.java");
-        assertClassFile("Case_PickListAccentMarkEnum.java");
-        assertClassFile("Case_PickListQuotationMarkEnum.java");
-        assertClassFile("Case_PickListSlashEnum.java");
-        assertClassFile("QueryRecordsCase.java");
+    @Before
+    public void setUp() throws Exception {
+        mojo = new CamelSalesforceMojo();
+        mojo.engine = CamelSalesforceMojo.createVelocityEngine();
     }
 
     @Test
-    public void testProcessDescriptionCalculatedFormula() throws Exception {
-        SObjectDescription description = createSObjectDescription(TEST_CALCULATED_FORMULA_FILE);
+    public void testProcessDescriptionPickLists() throws Exception {
+        final File pkgDir = temp.newFolder();
 
-        mojo.processDescription(pkgDir, description, utility, generatedDate);
+        mojo.processDescription(pkgDir, description, utility, FIXED_DATE);
 
-        assertClassFile("ComplexCalculatedFormula.java");
-        assertClassFile("QueryRecordsComplexCalculatedFormula.java");
-    }
+        File generatedFile = new File(pkgDir, source);
+        File expectedFile = FileUtils.toFile(CamelSalesforceMojoOutputTest.class.getResource("/generated/" + source));
 
-    public void assertClassFile(String name) throws Exception {
-        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
-        Assert.assertTrue("Class "+name+" must be equal to the target one", FileUtils.contentEquals(new File(OUTPUT_FOLDER, name), FileUtils.toFile(classLoader.getResource("generated/"+name))));
+        Assert.assertTrue("Geberated source file in " + source + " must be equal to the one present in test/resources",
+            FileUtils.contentEquals(generatedFile, expectedFile));
     }
 
-    protected CamelSalesforceMojo.GeneratorUtility createGeneratorUtility() {
-        return new CamelSalesforceMojo.GeneratorUtility(false);
-    }
-
-    protected SObjectDescription createSObjectDescription(String name) throws Exception {
-        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
-        InputStream inputStream = classLoader.getResourceAsStream(name);
+    static SObjectDescription createSObjectDescription(String name) throws IOException {
+        InputStream inputStream = CamelSalesforceMojoOutputTest.class.getResourceAsStream("/" + name);
         ObjectMapper mapper = JsonUtils.createObjectMapper();
-        SObjectDescription description = mapper.readValue(inputStream, SObjectDescription.class);
-        if (description == null)
-            throw new Exception("Couldn't Read description from file");
 
-        return description;
+        return mapper.readValue(inputStream, SObjectDescription.class);
     }
 
-    protected CamelSalesforceMojoAccessor createMojo() throws Exception {
-        CamelSalesforceMojoAccessor mojo = new CamelSalesforceMojoAccessor();
-        mojo.setLog(new SystemStreamLog());
-        return mojo;
-    }
 }