From 8e0dee495557f13481ee98392f32b82aea076ccb Mon Sep 17 00:00:00 2001 From: Andrew Snare Date: Wed, 7 Sep 2016 11:53:00 +0200 Subject: [PATCH] Fix a bug in the custom name support for the divolte.js tracking script. Also adds some tests for this, as well as verifying that the Javascript still compiles with the logging/debug options enabled. (cherry picked from commit 6f19931f1d01a1d4df543d24199f734edc6f7d94) --- .../server/js/TrackingJavaScriptResource.java | 4 +-- .../server/SeleniumJavaScriptTest.java | 14 ++++++++- .../io/divolte/server/SeleniumTestBase.java | 3 +- .../js/TrackingJavaScriptResourceTest.java | 29 +++++++++++++++-- ...browser-source-custom-javascript-name.conf | 29 +++++++++++++++++ .../browser-source-javascript-debugging.conf | 29 +++++++++++++++++ .../browser-source-javascript-logging.conf | 29 +++++++++++++++++ .../selenium-test-custom-javascript-name.conf | 21 +++++++++++++ .../quirks/test-custom-javascript-name.html | 30 ++++++++++++++++++ .../strict/test-custom-javascript-name.html | 31 +++++++++++++++++++ 10 files changed, 212 insertions(+), 7 deletions(-) create mode 100644 src/test/resources/browser-source-custom-javascript-name.conf create mode 100644 src/test/resources/browser-source-javascript-debugging.conf create mode 100644 src/test/resources/browser-source-javascript-logging.conf create mode 100644 src/test/resources/selenium-test-custom-javascript-name.conf create mode 100644 src/test/resources/static/quirks/test-custom-javascript-name.html create mode 100644 src/test/resources/static/strict/test-custom-javascript-name.html diff --git a/src/main/java/io/divolte/server/js/TrackingJavaScriptResource.java b/src/main/java/io/divolte/server/js/TrackingJavaScriptResource.java index 839fdb94..fb3852ac 100644 --- a/src/main/java/io/divolte/server/js/TrackingJavaScriptResource.java +++ b/src/main/java/io/divolte/server/js/TrackingJavaScriptResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 GoDataDriven B.V. + * Copyright 2016 GoDataDriven B.V. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -72,7 +72,7 @@ public static TrackingJavaScriptResource create(final ValidatedConfiguration vc, final String sourceName) throws IOException { final BrowserSourceConfiguration browserSourceConfiguration = vc.configuration().getSourceConfiguration(sourceName, BrowserSourceConfiguration.class); - return new TrackingJavaScriptResource(browserSourceConfiguration.javascript.name, + return new TrackingJavaScriptResource("divolte.js", createScriptConstants(browserSourceConfiguration), browserSourceConfiguration.javascript.debug); } diff --git a/src/test/java/io/divolte/server/SeleniumJavaScriptTest.java b/src/test/java/io/divolte/server/SeleniumJavaScriptTest.java index 90307942..46c938fd 100644 --- a/src/test/java/io/divolte/server/SeleniumJavaScriptTest.java +++ b/src/test/java/io/divolte/server/SeleniumJavaScriptTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 GoDataDriven B.V. + * Copyright 2016 GoDataDriven B.V. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -261,6 +261,18 @@ public void shouldPickupProvidedPageViewIdFromHash() throws Exception { assertEquals("supercalifragilisticexpialidocious0", eventData.eventId); } + @Test + public void shouldSupportCustomJavascriptName() throws Exception { + doSetUp("selenium-test-custom-javascript-name.conf"); + Preconditions.checkState(null != driver && null != server); + + driver.get(urlOf(CUSTOM_JAVASCRIPT_NAME)); + final EventPayload payload = server.waitForEvent(); + final DivolteEvent eventData = payload.event; + + assertEquals("pageView", eventData.eventType.get()); + } + @Test public void shouldUseConfiguredEventSuffix() throws Exception { doSetUp("selenium-test-custom-event-suffix.conf"); diff --git a/src/test/java/io/divolte/server/SeleniumTestBase.java b/src/test/java/io/divolte/server/SeleniumTestBase.java index f0fb5008..80648001 100644 --- a/src/test/java/io/divolte/server/SeleniumTestBase.java +++ b/src/test/java/io/divolte/server/SeleniumTestBase.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 GoDataDriven B.V. + * Copyright 2016 GoDataDriven B.V. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -126,6 +126,7 @@ public enum TEST_PAGES { BASIC("test-basic-page"), BASIC_COPY("test-basic-page-copy"), PAGE_VIEW_SUPPLIED("test-basic-page-provided-pv-id"), + CUSTOM_JAVASCRIPT_NAME("test-custom-javascript-name"), CUSTOM_PAGE_VIEW("test-custom-page-view"); private final String resourceName; diff --git a/src/test/java/io/divolte/server/js/TrackingJavaScriptResourceTest.java b/src/test/java/io/divolte/server/js/TrackingJavaScriptResourceTest.java index 07a291c8..2eb56778 100644 --- a/src/test/java/io/divolte/server/js/TrackingJavaScriptResourceTest.java +++ b/src/test/java/io/divolte/server/js/TrackingJavaScriptResourceTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 GoDataDriven B.V. + * Copyright 2016 GoDataDriven B.V. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,11 +16,15 @@ package io.divolte.server.js; +import com.google.common.collect.ImmutableList; +import com.typesafe.config.ConfigFactory; import io.divolte.server.config.ValidatedConfiguration; import io.undertow.util.ETag; import java.io.IOException; import java.nio.ByteBuffer; +import java.util.Iterator; +import java.util.Objects; import java.util.Optional; import org.junit.After; @@ -28,17 +32,36 @@ import org.junit.Test; import com.typesafe.config.Config; -import com.typesafe.config.ConfigFactory; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import javax.annotation.ParametersAreNonnullByDefault; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; +@RunWith(Parameterized.class) +@ParametersAreNonnullByDefault public class TrackingJavaScriptResourceTest { - private final Config config = ConfigFactory.load(); + @Parameterized.Parameters(name = "{index}: {0} configuration") + public static Iterable configurations() { + return ImmutableList.of( + new Object[] { "default", ConfigFactory.load() }, + new Object[] { "non-default-name", ConfigFactory.load("browser-source-custom-javascript-name.conf")}, + new Object[] { "logging-enabled", ConfigFactory.load("browser-source-javascript-logging.conf")}, + new Object[] { "verbose-enabled", ConfigFactory.load("browser-source-javascript-debugging.conf")} + ); + } + + private final Config config; private TrackingJavaScriptResource trackingJavaScript; + public TrackingJavaScriptResourceTest(final String name, final Config config) { + this.config = Objects.requireNonNull(config); + } + @Before public void setup() throws IOException { // Essential test to ensure at build-time that our JavaScript can be compiled. diff --git a/src/test/resources/browser-source-custom-javascript-name.conf b/src/test/resources/browser-source-custom-javascript-name.conf new file mode 100644 index 00000000..c9efae53 --- /dev/null +++ b/src/test/resources/browser-source-custom-javascript-name.conf @@ -0,0 +1,29 @@ +// +// Copyright 2016 GoDataDriven B.V. +// +// Licensed 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. +// + +// Specify a browser source that uses a custom name for the Javascript resource. +divolte { + sources.browser { + type = browser + javascript.name = dvt.js + } + + mappings.test = { + sources = [browser] + // Need at least one sink. + sinks = [hdfs] + } +} diff --git a/src/test/resources/browser-source-javascript-debugging.conf b/src/test/resources/browser-source-javascript-debugging.conf new file mode 100644 index 00000000..465cd996 --- /dev/null +++ b/src/test/resources/browser-source-javascript-debugging.conf @@ -0,0 +1,29 @@ +// +// Copyright 2016 GoDataDriven B.V. +// +// Licensed 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. +// + +// Specify a browser source that uses a custom name for the Javascript resource. +divolte { + sources.browser { + type = browser + javascript.debug = true + } + + mappings.test = { + sources = [browser] + // Need at least one sink. + sinks = [hdfs] + } +} diff --git a/src/test/resources/browser-source-javascript-logging.conf b/src/test/resources/browser-source-javascript-logging.conf new file mode 100644 index 00000000..00bec0c7 --- /dev/null +++ b/src/test/resources/browser-source-javascript-logging.conf @@ -0,0 +1,29 @@ +// +// Copyright 2016 GoDataDriven B.V. +// +// Licensed 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. +// + +// Specify a browser source that uses a custom name for the Javascript resource. +divolte { + sources.browser { + type = browser + javascript.logging = true + } + + mappings.test = { + sources = [browser] + // Need at least one sink. + sinks = [hdfs] + } +} diff --git a/src/test/resources/selenium-test-custom-javascript-name.conf b/src/test/resources/selenium-test-custom-javascript-name.conf new file mode 100644 index 00000000..fc129529 --- /dev/null +++ b/src/test/resources/selenium-test-custom-javascript-name.conf @@ -0,0 +1,21 @@ +// +// Copyright 2016 GoDataDriven B.V. +// +// Licensed 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. +// + +// Instead of using the default 'csc-event' endpoint, use a different value. +divolte.sources.browser { + type = browser + javascript.name = dvt.js +} diff --git a/src/test/resources/static/quirks/test-custom-javascript-name.html b/src/test/resources/static/quirks/test-custom-javascript-name.html new file mode 100644 index 00000000..781962b7 --- /dev/null +++ b/src/test/resources/static/quirks/test-custom-javascript-name.html @@ -0,0 +1,30 @@ + + + + + + + + DVT + + + + + + + + diff --git a/src/test/resources/static/strict/test-custom-javascript-name.html b/src/test/resources/static/strict/test-custom-javascript-name.html new file mode 100644 index 00000000..3b40facf --- /dev/null +++ b/src/test/resources/static/strict/test-custom-javascript-name.html @@ -0,0 +1,31 @@ + + + + + + + + + DVT + + + + + + + +