diff --git a/pom.xml b/pom.xml index 37163bd..ebc30ab 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ org.apache.sling sling-bundle-parent - 41 + 48 @@ -61,6 +61,18 @@ + + + + org.junit + junit-bom + 5.11.3 + pom + import + + + + org.osgi @@ -104,7 +116,7 @@ org.apache.sling org.apache.sling.commons.osgi - 2.1.0 + 2.4.0 provided @@ -142,16 +154,21 @@ provided + + org.junit.jupiter + junit-jupiter + test + org.apache.sling org.apache.sling.jcr.resource - 2.3.8 + 3.3.2 test org.apache.sling org.apache.sling.commons.testing - 2.1.0 + 2.1.2 test @@ -162,32 +179,38 @@ org.apache.sling - org.apache.sling.testing.osgi-mock - 2.3.4 + org.apache.sling.testing.osgi-mock.junit5 + 3.5.0 + test + + + org.apache.sling + org.apache.sling.testing.sling-mock.junit5 + 3.5.0 test org.mockito - mockito-all - 1.10.19 + mockito-core + 5.14.2 test org.apache.jackrabbit - jackrabbit-api - 2.19.2 - test + oak-jackrabbit-api + 1.72.0 + test org.apache.jackrabbit jackrabbit-jcr-commons - 2.19.4 + 2.19.6 test org.apache.jackrabbit jackrabbit-core - 2.19.4 + 2.19.6 test diff --git a/src/test/java/org/apache/sling/scripting/javascript/RepositoryScriptingTestBase.java b/src/test/java/org/apache/sling/scripting/javascript/RepositoryScriptingTestBase.java index 038e049..0318332 100644 --- a/src/test/java/org/apache/sling/scripting/javascript/RepositoryScriptingTestBase.java +++ b/src/test/java/org/apache/sling/scripting/javascript/RepositoryScriptingTestBase.java @@ -24,6 +24,8 @@ import org.apache.sling.commons.testing.jcr.RepositoryTestBase; import org.apache.sling.scripting.javascript.internal.ScriptEngineHelper; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; /** Base class for tests which need a Repository @@ -33,11 +35,18 @@ public class RepositoryScriptingTestBase extends RepositoryTestBase { private int counter; @Override + @BeforeEach protected void setUp() throws Exception { super.setUp(); script = new ScriptEngineHelper(); } - + + @Override + @AfterEach + protected void tearDown() throws Exception { + super.tearDown(); + } + protected Node getNewNode() throws RepositoryException, NamingException { return getTestRootNode().addNode("test-" + (++counter)); } diff --git a/src/test/java/org/apache/sling/scripting/javascript/TestSetupTest.java b/src/test/java/org/apache/sling/scripting/javascript/TestSetupTest.java index 9c37bb0..562e32a 100644 --- a/src/test/java/org/apache/sling/scripting/javascript/TestSetupTest.java +++ b/src/test/java/org/apache/sling/scripting/javascript/TestSetupTest.java @@ -19,21 +19,25 @@ package org.apache.sling.scripting.javascript; import org.apache.sling.scripting.javascript.internal.ScriptEngineHelper; +import org.junit.jupiter.api.Test; /** Verify that our test environment works */ public class TestSetupTest extends RepositoryScriptingTestBase { /** Test our test repository setup */ + @Test public void testRootNode() throws Exception { assertNotNull(getTestRootNode()); } /** Test our script engine setup */ + @Test public void testScripting() throws Exception { assertEquals("something",script.evalToString("out.print('something')")); } + @Test public void testScriptingWithData() throws Exception { final ScriptEngineHelper.Data data = new ScriptEngineHelper.Data(); data.put("a", "A"); diff --git a/src/test/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineFactoryTest.java b/src/test/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineFactoryTest.java index b0a8d53..d3527e1 100644 --- a/src/test/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineFactoryTest.java +++ b/src/test/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineFactoryTest.java @@ -24,19 +24,20 @@ import org.apache.sling.commons.classloader.DynamicClassLoaderManager; import org.apache.sling.scripting.api.ScriptCache; -import org.apache.sling.testing.mock.osgi.junit.OsgiContext; -import org.junit.Rule; -import org.junit.Test; +import org.apache.sling.testing.mock.osgi.junit5.OsgiContext; +import org.apache.sling.testing.mock.osgi.junit5.OsgiContextExtension; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +@ExtendWith(OsgiContextExtension.class) public class RhinoJavaScriptEngineFactoryTest { - @Rule - public OsgiContext context = new OsgiContext(); + private final OsgiContext context = new OsgiContext(); @Test public void testRegistrationProperties() { @@ -49,7 +50,7 @@ public void testRegistrationProperties() { assertEquals(Arrays.asList("rhino", "Rhino", "javascript", "JavaScript", "ecmascript", "ECMAScript"), instance.getNames()); assertEquals("ECMAScript", instance.getLanguageName()); assertEquals("partial ECMAScript 2015 support", instance.getLanguageVersion()); - assertTrue("Unexpected engine name", instance.getEngineName() != null && instance.getEngineName().contains("Rhino 1.7.7.1_1")); + assertTrue( instance.getEngineName() != null && instance.getEngineName().contains("Rhino 1.7.7.1_1"), "Unexpected engine name" ); } } diff --git a/src/test/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineTest.java b/src/test/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineTest.java index 63fb572..2122f27 100644 --- a/src/test/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineTest.java +++ b/src/test/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineTest.java @@ -31,12 +31,17 @@ import org.mozilla.javascript.ImporterTopLevel; import org.mozilla.javascript.Scriptable; -import junit.framework.TestCase; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; -public class RhinoJavaScriptEngineTest extends TestCase { +public class RhinoJavaScriptEngineTest { private static ScriptCache scriptCache = Mockito.mock(ScriptCache.class); + @Test public void testPreserveScopeBetweenEvals() throws ScriptException { MockRhinoJavaScriptEngineFactory factory = new MockRhinoJavaScriptEngineFactory(); ScriptEngine engine = factory.getScriptEngine(); @@ -46,12 +51,13 @@ public void testPreserveScopeBetweenEvals() throws ScriptException { try { result = engine.eval("f += 1", context); } catch (ScriptException e) { - TestCase.fail(e.getMessage()); + fail(e.getMessage()); } assertTrue(result instanceof Double); assertEquals(2.0, result); } + @Test public void testNullSuppliedValue() throws ScriptException { MockRhinoJavaScriptEngineFactory factory = new MockRhinoJavaScriptEngineFactory(); ScriptEngine engine = factory.getScriptEngine(); @@ -69,6 +75,7 @@ public void testNullSuppliedValue() throws ScriptException { assertTrue(throwable.getMessage().contains("\"suppliedNullValue\" is not defined")); } + @Test public void testNotNullSuppliedValue() throws ScriptException { MockRhinoJavaScriptEngineFactory factory = new MockRhinoJavaScriptEngineFactory(); ScriptEngine engine = factory.getScriptEngine(); diff --git a/src/test/java/org/apache/sling/scripting/javascript/internal/ScriptEngineHelper.java b/src/test/java/org/apache/sling/scripting/javascript/internal/ScriptEngineHelper.java index 5a434a8..0cabbc2 100644 --- a/src/test/java/org/apache/sling/scripting/javascript/internal/ScriptEngineHelper.java +++ b/src/test/java/org/apache/sling/scripting/javascript/internal/ScriptEngineHelper.java @@ -33,7 +33,10 @@ import org.apache.sling.commons.testing.osgi.MockBundle; import org.apache.sling.commons.testing.osgi.MockComponentContext; import org.apache.sling.scripting.api.ScriptCache; -import org.mockito.internal.util.reflection.Whitebox; +import org.apache.sling.testing.mock.osgi.MockOsgi; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; import org.mozilla.javascript.Context; import org.mozilla.javascript.ScriptableObject; import org.mozilla.javascript.Wrapper; @@ -43,20 +46,29 @@ /** Helpers to run javascript code fragments in tests */ public class ScriptEngineHelper { + private static ScriptEngine engine; - private static ScriptCache scriptCache = mock(ScriptCache.class); + + @Mock + private static ScriptCache scriptCache; + + @Mock + private RhinoJavaScriptEngineFactoryConfiguration factoryConfiguration; + + @InjectMocks + private RhinoJavaScriptEngineFactory factory; + + public ScriptEngineHelper() { + MockitoAnnotations.openMocks(this); + } public static class Data extends HashMap { } - private static ScriptEngine getEngine() { + private ScriptEngine getEngine() { if (engine == null) { synchronized (ScriptEngineHelper.class) { - final RhinoMockComponentContext componentContext = new RhinoMockComponentContext(); - final RhinoJavaScriptEngineFactoryConfiguration configuration = mock(RhinoJavaScriptEngineFactoryConfiguration.class); - RhinoJavaScriptEngineFactory factory = new RhinoJavaScriptEngineFactory(); - Whitebox.setInternalState(factory, "scriptCache", scriptCache); - factory.activate(componentContext, configuration); + factory.activate( new RhinoMockComponentContext(), factoryConfiguration); engine = factory.getScriptEngine(); } } diff --git a/src/test/java/org/apache/sling/scripting/javascript/internal/TestPathRegexp.java b/src/test/java/org/apache/sling/scripting/javascript/internal/TestPathRegexp.java index 5d36910..12f830b 100644 --- a/src/test/java/org/apache/sling/scripting/javascript/internal/TestPathRegexp.java +++ b/src/test/java/org/apache/sling/scripting/javascript/internal/TestPathRegexp.java @@ -18,10 +18,12 @@ */ package org.apache.sling.scripting.javascript.internal; -import junit.framework.TestCase; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +public class TestPathRegexp { -public class TestPathRegexp extends TestCase { + @Test public void testParentPath() { String regexp = "([^/]*/)?[^/]*/\\.\\./"; assertEquals("math", "/../math".replaceAll(regexp, "")); @@ -30,6 +32,7 @@ public void testParentPath() { assertEquals("foo/math", "foo/bar/increment/../math".replaceAll(regexp, "")); } + @Test public void testCurrentPath() { String regexp = "[^/]*/\\./"; assertEquals("math", "/./math".replaceAll(regexp, "")); diff --git a/src/test/java/org/apache/sling/scripting/javascript/io/EspReaderTest.java b/src/test/java/org/apache/sling/scripting/javascript/io/EspReaderTest.java index 58af788..d17750e 100644 --- a/src/test/java/org/apache/sling/scripting/javascript/io/EspReaderTest.java +++ b/src/test/java/org/apache/sling/scripting/javascript/io/EspReaderTest.java @@ -21,79 +21,95 @@ import java.io.IOException; import java.io.Reader; import java.io.StringReader; - import javax.script.ScriptException; - -import junit.framework.TestCase; - import org.apache.sling.scripting.javascript.internal.ScriptEngineHelper; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * The EspReaderTest contains some simple test cases for the * EspReader class which processes ESP (ECMA Server Page) templated * JavaScript and produces plain JavaScript. */ -public class EspReaderTest extends TestCase { +public class EspReaderTest { /** Test read() method */ + @Test public void testReadSingle() throws IOException { String src = "<%var%>"; // expect var on reader Reader reader = new EspReader(new StringReader(src)); - - assertTrue("Character 1 must be 'v'", 'v' == reader.read()); - assertTrue("Character 2 must be 'a'", 'a' == reader.read()); - assertTrue("Character 3 must be 'r'", 'r' == reader.read()); - assertTrue("Character 4 must be -1", -1 == reader.read()); + try { + assertTrue('v' == reader.read(), "Character 1 must be 'v'"); + assertTrue('a' == reader.read(), "Character 2 must be 'a'"); + assertTrue('r' == reader.read(), "Character 3 must be 'r'"); + assertTrue(-1 == reader.read(), "Character 4 must be -1"); + } finally { + reader.close(); + } } /** Test read(char[], int, int) method */ + @Test public void testReadArrayAll() throws IOException { String src = "<%var%>"; // expect var on reader Reader reader = new EspReader(new StringReader(src)); - char[] buf = new char[3]; - int rd = reader.read(buf, 0, buf.length); + try { + char[] buf = new char[3]; + int rd = reader.read(buf, 0, buf.length); - assertEquals(3, rd); - assertEquals("var", new String(buf, 0, rd)); + assertEquals(3, rd); + assertEquals("var", new String(buf, 0, rd)); - // nothing more to read, expect EOF - rd = reader.read(buf, 0, buf.length); - assertEquals(-1, rd); + // nothing more to read, expect EOF + rd = reader.read(buf, 0, buf.length); + assertEquals(-1, rd); + } finally { + reader.close(); + } } /** Test read(char[], int, int) method */ + @Test public void testReadArrayOffset() throws IOException { String jsSrc = "var x = 0;"; String src = "<%" + jsSrc + "%>"; Reader reader = new EspReader(new StringReader(src)); - char[] buf = new char[10]; - int off = 2; - int len = 3; - int rd = reader.read(buf, off, len); - assertEquals(len, rd); - assertEquals("var", new String(buf, off, rd)); + try { + char[] buf = new char[10]; + int off = 2; + int len = 3; + int rd = reader.read(buf, off, len); + assertEquals(len, rd); + assertEquals("var", new String(buf, off, rd)); - off = 2; - len = 7; - rd = reader.read(buf, off, len); - assertEquals(len, rd); - assertEquals(" x = 0;", new String(buf, off, rd)); + off = 2; + len = 7; + rd = reader.read(buf, off, len); + assertEquals(len, rd); + assertEquals(" x = 0;", new String(buf, off, rd)); - // nothing more to read, expect EOF - rd = reader.read(buf, 0, buf.length); - assertEquals(-1, rd); + // nothing more to read, expect EOF + rd = reader.read(buf, 0, buf.length); + assertEquals(-1, rd); + } finally { + reader.close(); + } } /** Test standard template text */ + @Test public void testTemplate() throws IOException { assertEquals("out=response.writer;out.write(\"test\");", parse("test")); assertEquals("out=response.writer;out.write(\"test\\n\");\nout.write(\"test2\");", parse("test\ntest2")); } /** Test with a custom "out" initialization */ + @Test public void testOutInit() throws IOException { final String input = "test"; final String expected = "out=getOut();out.write(\"test\");"; @@ -111,22 +127,26 @@ public void testOutInit() throws IOException { } /** Test plain JavaScript code */ + @Test public void testCode() throws IOException { assertEquals(" test(); ", parse("<% test(); %>")); assertEquals(" \ntest();\ntest2(); ", parse("<% \ntest();\ntest2(); %>")); } /** Test JavaScript expressions */ + @Test public void testExpr() throws IOException { assertEquals("out=response.writer;out.write( x + 1 );", parse("<%= x + 1 %>")); assertEquals("out=response.writer;out.write(\"\");", parse("")); } /** Test JavaScript comment */ + @Test public void testComment() throws IOException { assertEquals("", parse("<%-- test(); --%>")); } + @Test public void testCompactExpressionsDouble() throws IOException { final String input = "\n"; final String expected = "out=response.writer;out.write(\"\\n\");\n"; @@ -134,6 +154,7 @@ public void testCompactExpressionsDouble() throws IOException { assertEquals(flatten(expected), flatten(actual)); } + @Test public void testCompactExpressionsDoubleNegative() throws IOException { final String input = "\n"; final String expected = "out=response.writer;out.write(\"\\n\");\n"; @@ -141,6 +162,7 @@ public void testCompactExpressionsDoubleNegative() throws IOException { assertEquals(flatten(expected), flatten(actual)); } + @Test public void testCompactExpressionsSingle() throws IOException { final String input = "\n"; final String expected = "out=response.writer;out.write(\"\\n\");\n"; @@ -148,6 +170,7 @@ public void testCompactExpressionsSingle() throws IOException { assertEquals(flatten(expected), flatten(actual)); } + @Test public void testCompactExpressionsSingleNegative() throws IOException { final String input = "\n"; final String expected = "out=response.writer;out.write(\"\\n\");\n"; @@ -156,6 +179,7 @@ public void testCompactExpressionsSingleNegative() throws IOException { } /** Test a complete template, using all features */ + @Test public void testCompleteTemplate() throws IOException { final String input = "\n" @@ -197,6 +221,7 @@ public void testCompleteTemplate() throws IOException { } /** Test a complete template, using all features */ + @Test public void testNumericExpression() throws IOException { String input = "<%= 1 %>"; String expected = "out=response.writer;out.write( 1 );"; @@ -215,6 +240,7 @@ public void testNumericExpression() throws IOException { } /** Test a complete template, using all features */ + @Test public void testNumericExpressionOutput() throws ScriptException { ScriptEngineHelper script = new ScriptEngineHelper(); @@ -234,6 +260,7 @@ public void testNumericExpressionOutput() throws ScriptException { assertEquals(expected, actual); } + @Test public void testColon() throws IOException { final String input = "currentNode.text:<%= currentNode.text %>"; final String expected = @@ -245,6 +272,7 @@ public void testColon() throws IOException { assertEquals(expected, actual); } + @Test public void testEqualSigns() throws IOException { final String input = "currentNode.text=<%= currentNode.text %>"; final String expected = @@ -256,6 +284,7 @@ public void testEqualSigns() throws IOException { assertEquals(expected, actual); } + @Test public void testSingleQuoted() throws IOException { final String input = "currentNode.text='<%= currentNode.text %>'"; final String expected = @@ -268,6 +297,7 @@ public void testSingleQuoted() throws IOException { assertEquals(expected, actual); } + @Test public void testDoubleQuoted() throws IOException { final String input = "currentNode.text=\"<%= currentNode.text %>\""; final String expected = @@ -285,12 +315,16 @@ private String parse(String text) throws IOException { StringBuffer buf = new StringBuffer(); Reader r = new EspReader(new StringReader(text)); - int c; - while ( (c=r.read()) >= 0) { - buf.append( (char) c); - } + try { + int c; + while ( (c=r.read()) >= 0) { + buf.append( (char) c); + } - return buf.toString(); + return buf.toString(); + } finally { + r.close(); + } } /** Replace \n with . in strings to make it easier to compare visually for testing */ diff --git a/src/test/java/org/apache/sling/scripting/javascript/wrapper/ScriptableMapTest.java b/src/test/java/org/apache/sling/scripting/javascript/wrapper/ScriptableMapTest.java index 96b2bde..c9bb536 100644 --- a/src/test/java/org/apache/sling/scripting/javascript/wrapper/ScriptableMapTest.java +++ b/src/test/java/org/apache/sling/scripting/javascript/wrapper/ScriptableMapTest.java @@ -23,15 +23,18 @@ import org.apache.sling.api.wrappers.ValueMapDecorator; import org.apache.sling.scripting.javascript.RepositoryScriptingTestBase; import org.apache.sling.scripting.javascript.internal.ScriptEngineHelper; -import org.junit.After; -import org.junit.Before; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + public class ScriptableMapTest extends RepositoryScriptingTestBase { private ValueMap valueMap; private ScriptEngineHelper.Data data; - @Before + @Override + @BeforeEach public void setUp() throws Exception { super.setUp(); valueMap = new ValueMapDecorator(new HashMap() {{ @@ -42,13 +45,14 @@ public void setUp() throws Exception { data.put("properties", valueMap); } - @After + @AfterEach public void tearDown() throws Exception { valueMap.clear(); data.clear(); super.tearDown(); } + @Test public void testPropertyAccess() throws ScriptException { assertEquals("a", script.eval("properties['a']", data)); assertEquals("a", script.eval("properties.a", data)); @@ -57,6 +61,7 @@ public void testPropertyAccess() throws ScriptException { assertEquals(null, script.eval("properties['c']", data)); } + @Test public void testJavaMethods() throws ScriptException { assertEquals(2, script.eval("properties.size()", data)); } diff --git a/src/test/java/org/apache/sling/scripting/javascript/wrapper/ScriptableNodeTest.java b/src/test/java/org/apache/sling/scripting/javascript/wrapper/ScriptableNodeTest.java index a157a40..6e238bf 100644 --- a/src/test/java/org/apache/sling/scripting/javascript/wrapper/ScriptableNodeTest.java +++ b/src/test/java/org/apache/sling/scripting/javascript/wrapper/ScriptableNodeTest.java @@ -27,6 +27,9 @@ import org.apache.sling.scripting.javascript.RepositoryScriptingTestBase; import org.apache.sling.scripting.javascript.internal.ScriptEngineHelper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + /** Test the ScriptableNode class "live", by retrieving * Nodes from a Repository and executing javascript code @@ -44,6 +47,7 @@ public class ScriptableNodeTest extends RepositoryScriptingTestBase { private ScriptEngineHelper.Data data; @Override + @BeforeEach protected void setUp() throws Exception { super.setUp(); @@ -68,6 +72,7 @@ protected void setUp() throws Exception { data.put("calProperty", calProperty); } + @Test public void testDefaultValue() throws Exception { final ScriptEngineHelper.Data data = new ScriptEngineHelper.Data(); data.put("node", getTestRootNode()); @@ -77,6 +82,7 @@ public void testDefaultValue() throws Exception { ); } + @Test public void testPrimaryNodeType() throws Exception { final ScriptEngineHelper.Data data = new ScriptEngineHelper.Data(); data.put("node", getTestRootNode()); @@ -86,6 +92,7 @@ public void testPrimaryNodeType() throws Exception { ); } + @Test public void testPrimaryNodeTypeProperty() throws Exception { final ScriptEngineHelper.Data data = new ScriptEngineHelper.Data(); data.put("node", getTestRootNode()); @@ -95,6 +102,7 @@ public void testPrimaryNodeTypeProperty() throws Exception { ); } + @Test public void testViaPropertyNoWrappers() throws Exception { assertEquals( testText, @@ -102,6 +110,7 @@ public void testViaPropertyNoWrappers() throws Exception { ); } + @Test public void testViaPropertyWithWrappers() throws Exception { assertEquals( textProperty.getString(), @@ -109,6 +118,7 @@ public void testViaPropertyWithWrappers() throws Exception { ); } + @Test public void testViaNodeDirectPropertyAccess() throws Exception { assertEquals( testText, @@ -116,6 +126,7 @@ public void testViaNodeDirectPropertyAccess() throws Exception { ); } + @Test public void testViaPropertyNoWrappersNum() throws Exception { assertEquals( testNum, @@ -123,6 +134,7 @@ public void testViaPropertyNoWrappersNum() throws Exception { ); } + @Test public void testViaPropertyWithWrappersNum() throws Exception { assertEquals( testNum, @@ -130,6 +142,7 @@ public void testViaPropertyWithWrappersNum() throws Exception { ); } + @Test public void testViaNodeDirectPropertyAccessNum() throws Exception { assertEquals( testNum, @@ -137,6 +150,7 @@ public void testViaNodeDirectPropertyAccessNum() throws Exception { ); } + @Test public void testViaPropertyNoWrappersCal() throws Exception { assertEquals( testCal.getTimeInMillis(), @@ -144,6 +158,7 @@ public void testViaPropertyNoWrappersCal() throws Exception { ); } + @Test public void testViaNodeDirectPropertyAccessCal() throws Exception { final SimpleDateFormat f = new SimpleDateFormat(ScriptableCalendar.ECMA_DATE_FORMAT, ScriptableCalendar.DATE_FORMAT_LOCALE); final String expected = f.format(testCal.getTime()); @@ -153,6 +168,7 @@ public void testViaNodeDirectPropertyAccessCal() throws Exception { ); } + @Test public void testCalDateClass() throws Exception { assertEquals( "number", @@ -160,6 +176,7 @@ public void testCalDateClass() throws Exception { ); } + @Test public void testPropertyParent() throws Exception { // need to use node.getProperty('num') to have a ScriptableProperty, // node.num only returns a wrapped value @@ -169,6 +186,7 @@ public void testPropertyParent() throws Exception { ); } + @Test public void testPropertyAncestor() throws Exception { // call getAncestor which is not explicitly defined in ScriptableProperty, // to verify that all Property methods are available and that we get a @@ -179,6 +197,7 @@ public void testPropertyAncestor() throws Exception { ); } + @Test public void testPropertiesIterationNoWrapper() throws Exception { final String code = "var props = node.getProperties();" @@ -191,6 +210,7 @@ public void testPropertiesIterationNoWrapper() throws Exception { } } + @Test public void testAddNodeDefaultType() throws Exception { final String path = "subdt_" + System.currentTimeMillis(); final String code = @@ -200,6 +220,7 @@ public void testAddNodeDefaultType() throws Exception { assertEquals("nt:unstructured", script.evalToString(code, data)); } + @Test public void testAddNodeSpecificType() throws Exception { final String path = "subst_" + System.currentTimeMillis(); final String code = @@ -209,6 +230,7 @@ public void testAddNodeSpecificType() throws Exception { assertEquals("nt:folder", script.evalToString(code, data)); } + @Test public void testGetNode() throws Exception { final String path = "subgn_" + System.currentTimeMillis(); final String code = @@ -219,11 +241,13 @@ public void testGetNode() throws Exception { assertEquals("nt:resource", script.evalToString(code, data)); } + @Test public void testGetProperty() throws Exception { final String code = "out.print(node.getProperty('text'));"; assertEquals(testText, script.evalToString(code, data)); } + @Test public void testGetNodesNoPattern() throws Exception { final String path = "subgnnp_" + System.currentTimeMillis(); final String code = @@ -235,6 +259,7 @@ public void testGetNodesNoPattern() throws Exception { assertEquals(path + "_A " + path + "_B ", script.evalToString(code, data)); } + @Test public void testGetNodesWithPattern() throws Exception { final String path = "subgnnp_" + System.currentTimeMillis(); final String code = @@ -247,6 +272,7 @@ public void testGetNodesWithPattern() throws Exception { assertEquals("1_" + path + "_A 1_" + path + "_B ", script.evalToString(code, data)); } + @Test public void testRemoveNode() throws Exception { final String code = "node.addNode('toremove');\n" @@ -259,11 +285,13 @@ public void testRemoveNode() throws Exception { } /** Test SLING-389 */ + @Test public void testForCurrentNode() throws Exception { final String code = "for (var a in node) {}; out.print('ok')"; assertEquals("ok", script.evalToString(code, data)); } + @Test public void testChildNodeAccess() throws Exception { final String path = "subtcna_" + System.currentTimeMillis(); final String code = @@ -280,6 +308,7 @@ public void testChildNodeAccess() throws Exception { /** Verify that the getAncestor() method (which is not explicitely defined in ScriptableNode) * is available, to check SLING-397. */ + @Test public void testGetAncestor() throws Exception { { final String code = "out.print(node.getAncestor(0).getPath());"; @@ -292,6 +321,7 @@ public void testGetAncestor() throws Exception { } } + @Test public void testIsNodeType() throws Exception { final String code = "out.print(node.isNodeType('nt:unstructured'));\n" @@ -301,6 +331,7 @@ public void testIsNodeType() throws Exception { assertEquals("true false", script.evalToString(code, data)); } + @Test public void testGetSession() throws Exception { assertEquals( "Root node found via node.session", @@ -318,6 +349,7 @@ public void testGetSession() throws Exception { * Test for regressing this issue: * https://issues.apache.org/jira/browse/SLING-534 */ + @Test public void testMultiValReferencePropLookup() throws Exception { Node refNode1 = getNewNode(); refNode1.addMixin("mix:referenceable"); diff --git a/src/test/java/org/apache/sling/scripting/javascript/wrapper/ScriptableResourceTest.java b/src/test/java/org/apache/sling/scripting/javascript/wrapper/ScriptableResourceTest.java index 5d250b5..54d1f59 100644 --- a/src/test/java/org/apache/sling/scripting/javascript/wrapper/ScriptableResourceTest.java +++ b/src/test/java/org/apache/sling/scripting/javascript/wrapper/ScriptableResourceTest.java @@ -43,20 +43,29 @@ import org.apache.sling.api.resource.ResourceUtil; import org.apache.sling.api.resource.ValueMap; import org.apache.sling.api.wrappers.ValueMapDecorator; -import org.apache.sling.commons.testing.sling.MockResourceResolver; -import org.apache.sling.jcr.resource.JcrResourceConstants; +import org.apache.sling.jcr.resource.api.JcrResourceConstants; import org.apache.sling.scripting.javascript.RepositoryScriptingTestBase; import org.apache.sling.scripting.javascript.internal.ScriptEngineHelper; +import org.apache.sling.testing.mock.sling.junit5.SlingContextExtension; +import org.apache.sling.testing.mock.sling.junit5.SlingContext; +import org.apache.sling.testing.mock.sling.ResourceResolverType; import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mozilla.javascript.Wrapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + +@ExtendWith(SlingContextExtension.class) public class ScriptableResourceTest extends RepositoryScriptingTestBase { + private static final SlingContext context = new SlingContext(ResourceResolverType.RESOURCERESOLVER_MOCK); + private Node node; - private static final ResourceResolver RESOURCE_RESOLVER = new MockResourceResolver(); + private static final ResourceResolver RESOURCE_RESOLVER = context.resourceResolver(); private static final String RESOURCE_TYPE = "testWrappedResourceType"; @@ -65,6 +74,7 @@ public class ScriptableResourceTest extends RepositoryScriptingTestBase { private static final Logger LOGGER = LoggerFactory.getLogger(ScriptableResourceTest.class); @Override + @BeforeEach protected void setUp() throws Exception { super.setUp(); @@ -79,6 +89,7 @@ protected void setUp() throws Exception { } } + @Test public void testDefaultValuePath() throws Exception { final ScriptEngineHelper.Data data = new ScriptEngineHelper.Data(); data.put("resource", new TestResource(node)); @@ -90,6 +101,7 @@ public void testDefaultValuePath() throws Exception { assertEquals(node.getPath(), script.eval("resource.getPath()", data)); } + @Test public void testResourceType() throws Exception { // set resource and resource super type node.setProperty(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, @@ -108,6 +120,7 @@ public void testResourceType() throws Exception { data)); } + @Test public void testChildren() throws Exception { node.addNode("first-child"); node.addNode("second-child"); @@ -119,6 +132,7 @@ public void testChildren() throws Exception { assertEquals("first-child", script.eval("resource.getChildren()[0].name", data)); } + @Test public void testListChildren() throws Exception { Node firstChild = node.addNode("first-child"); node.addNode("second-child"); @@ -130,6 +144,7 @@ public void testListChildren() throws Exception { assertEquals(firstChild.getPath(), script.eval("resource.listChildren()[0].path", data)); } + @Test public void testGetChild() throws Exception { Node child = node.addNode("child"); @@ -139,6 +154,7 @@ public void testGetChild() throws Exception { assertEquals(child.getPath(), script.eval("resource.getChild('./child').path", data)); } + @Test public void testGetParent() throws Exception { Node child = node.addNode("child"); Node grandChild = child.addNode("grandchild"); @@ -149,6 +165,7 @@ public void testGetParent() throws Exception { assertEquals(child.getPath(), script.eval("resource.getParent().getPath()", data)); } + @Test public void testParent() throws Exception { Node child = node.addNode("child"); Node grandChild = child.addNode("grandchild"); @@ -159,12 +176,14 @@ public void testParent() throws Exception { assertEquals(child.getPath(), script.eval("resource.parent.path", data)); } + @Test public void testIsResourceType() throws Exception { final ScriptEngineHelper.Data data = new ScriptEngineHelper.Data(); data.put("resource", new TestResource(node)); assertEquals(Boolean.TRUE, script.eval("resource.isResourceType('" + RESOURCE_TYPE + "')", data)); } + @Test public void testResourceSuperType() throws Exception { // set resource and resource super type node.setProperty(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, @@ -183,6 +202,7 @@ public void testResourceSuperType() throws Exception { "resource.getResourceSuperType()", data)); } + @Test public void testResourceMetadata() throws Exception { final ScriptEngineHelper.Data data = new ScriptEngineHelper.Data(); data.put("resource", new TestResource(node)); @@ -197,6 +217,7 @@ public void testResourceMetadata() throws Exception { assertResourceMetaData(script.eval("resource.getMetadata()", data)); } + @Test public void testResourceResolver() throws Exception { final ScriptEngineHelper.Data data = new ScriptEngineHelper.Data(); data.put("resource", new TestResource(node)); @@ -208,6 +229,7 @@ public void testResourceResolver() throws Exception { "resource.getResourceResolver()", data)); } + @Test public void testAdaptToNode() throws Exception { final ScriptEngineHelper.Data data = new ScriptEngineHelper.Data(); data.put("resource", new TestResource(node)); @@ -219,6 +241,7 @@ public void testAdaptToNode() throws Exception { "resource.adaptTo(Packages.javax.jcr.Node)", data)); } + @Test public void testAdaptToNothing() throws Exception { final ScriptEngineHelper.Data data = new ScriptEngineHelper.Data(); data.put("resource", new TestResource(node)); @@ -230,6 +253,7 @@ public void testAdaptToNothing() throws Exception { assertEquals(true, script.eval("resource.adaptTo(Packages.java.util.Date) == undefined", data)); } + @Test public void testProperties() throws Exception { final ScriptEngineHelper.Data data = new ScriptEngineHelper.Data(); Calendar date = new GregorianCalendar(); diff --git a/src/test/java/org/apache/sling/scripting/javascript/wrapper/ScriptableVersionTest.java b/src/test/java/org/apache/sling/scripting/javascript/wrapper/ScriptableVersionTest.java index 4cb5c5b..fdcf9d0 100644 --- a/src/test/java/org/apache/sling/scripting/javascript/wrapper/ScriptableVersionTest.java +++ b/src/test/java/org/apache/sling/scripting/javascript/wrapper/ScriptableVersionTest.java @@ -22,6 +22,8 @@ import org.apache.sling.scripting.javascript.RepositoryScriptingTestBase; import org.apache.sling.scripting.javascript.internal.ScriptEngineHelper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; /** Test access to Version and VersionHistory objects */ public class ScriptableVersionTest extends RepositoryScriptingTestBase { @@ -30,6 +32,7 @@ public class ScriptableVersionTest extends RepositoryScriptingTestBase { private ScriptEngineHelper.Data data = new ScriptEngineHelper.Data(); @Override + @BeforeEach protected void setUp() throws Exception { super.setUp(); @@ -42,21 +45,25 @@ protected void setUp() throws Exception { getSession().save(); } + @Test public void testVersionHistoryAccess() throws Exception { Object result = script.eval("node.getVersionHistory().getAllVersions()", data); assertNotNull(result); } + @Test public void testVersionHistoryIsWrapped() throws Exception { assertEquals("nt:versionHistory", script.eval("node.versionHistory['jcr:primaryType']", data)); assertEquals("nt:version", script.eval("node.versionHistory.rootVersion['jcr:primaryType']", data)); } + @Test public void testVersionAccess() throws Exception { Object result = script.eval("node.getBaseVersion().getCreated()", data); assertNotNull(result); } + @Test public void testVersionIsWrapped() throws Exception { assertEquals("nt:version", script.eval("node.baseVersion['jcr:primaryType']", data)); assertNotNull(script.eval("node.baseVersion.created", data));