Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions framework/base/ofbiz-component.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ under the License.
<!-- load the administration server -->
<container name="admin-container" loaders="main" class="org.apache.ofbiz.base.container.AdminServerContainer"/>

<!-- Load base uel methods -->
<uel-mapping name="date-uel" class-name="org.apache.ofbiz.base.util.string.uel.DateUel"/>
<uel-mapping name="math-uel" class-name="org.apache.ofbiz.base.util.string.uel.MathUel"/>
<uel-mapping name="string-uel" class-name="org.apache.ofbiz.base.util.string.uel.StringUel"/>
<uel-mapping name="misc-uel" class-name="org.apache.ofbiz.base.util.string.uel.MiscUel"/>
</ofbiz-component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*******************************************************************************
* 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.ofbiz.base.test.uel

import org.apache.ofbiz.base.util.UtilDateTime
import org.apache.ofbiz.base.util.string.FlexibleStringExpander
import org.apache.ofbiz.base.util.string.UelFunctions
import org.apache.ofbiz.service.testtools.OFBizTestCase

import java.sql.Timestamp

/**
* ./gradlew 'ofbiz -t component=base -t suitename=basetests'
*/
/* codenarc-disable GStringExpressionWithinString,ClosureAsLastMethodParameter */
class DateUelTest extends OFBizTestCase {

DateUelTest(String name) {
super(name)
}

void testDateUel() { // codenarc-disable JUnitTestMethodWithoutAssert
doUelDateTest('${date:second(now, timeZone, locale)}', { Timestamp now ->
UtilDateTime.getSecond(now, TimeZone.getDefault(), Locale.getDefault())
})
doUelDateTest('${date:minute(now, timeZone, locale)}', { Timestamp now ->
UtilDateTime.getMinute(now, TimeZone.getDefault(), Locale.getDefault())
})
doUelDateTest('${date:hour(now, timeZone, locale)}', { Timestamp now ->
UtilDateTime.getHour(now, TimeZone.getDefault(), Locale.getDefault())
})
doUelDateTest('${date:dayOfMonth(now, timeZone, locale)}', { Timestamp now ->
UtilDateTime.getDayOfMonth(now, TimeZone.getDefault(), Locale.getDefault())
})
doUelDateTest('${date:dayOfWeek(now, timeZone, locale)}', { Timestamp now ->
UtilDateTime.getDayOfWeek(now, TimeZone.getDefault(), Locale.getDefault())
})
doUelDateTest('${date:dayOfYear(now, timeZone, locale)}', { Timestamp now ->
UtilDateTime.getDayOfYear(now, TimeZone.getDefault(), Locale.getDefault())
})
doUelDateTest('${date:week(now, timeZone, locale)}', { Timestamp now ->
UtilDateTime.getWeek(now, TimeZone.getDefault(), Locale.getDefault())
})
doUelDateTest('${date:month(now, timeZone, locale)}', { Timestamp now ->
UtilDateTime.getMonth(now, TimeZone.getDefault(), Locale.getDefault())
})
doUelDateTest('${date:year(now, timeZone, locale)}', { Timestamp now ->
UtilDateTime.getYear(now, TimeZone.getDefault(), Locale.getDefault())
})
doUelDateTest('${date:dayStart(now, timeZone, locale)}', { Timestamp now ->
UtilDateTime.getDayStart(now, TimeZone.getDefault(), Locale.getDefault())
})
doUelDateTest('${date:dayEnd(now, timeZone, locale)}', { Timestamp now ->
UtilDateTime.getDayEnd(now, TimeZone.getDefault(), Locale.getDefault())
})
doUelDateTest('${date:weekStart(now, timeZone, locale)}', { Timestamp now ->
UtilDateTime.getWeekStart(now, TimeZone.getDefault(), Locale.getDefault())
})
doUelDateTest('${date:weekEnd(now, timeZone, locale)}', { Timestamp now ->
UtilDateTime.getWeekEnd(now, TimeZone.getDefault(), Locale.getDefault())
})
doUelDateTest('${date:monthStart(now, timeZone, locale)}', { Timestamp now ->
UtilDateTime.getMonthStart(now, TimeZone.getDefault(), Locale.getDefault())
})
doUelDateTest('${date:monthEnd(now, timeZone, locale)}', { Timestamp now ->
UtilDateTime.getMonthEnd(now, TimeZone.getDefault(), Locale.getDefault())
})
doUelDateTest('${date:yearStart(now, timeZone, locale)}', { Timestamp now ->
UtilDateTime.getYearStart(now, TimeZone.getDefault(), Locale.getDefault())
})
doUelDateTest('${date:yearEnd(now, timeZone, locale)}', { Timestamp now ->
UtilDateTime.getYearEnd(now, TimeZone.getDefault(), Locale.getDefault())
})
doUelDateTest('${date:dateStr(now, timeZone, locale)}', { Timestamp now ->
UelFunctions.dateString(now, TimeZone.getDefault(), Locale.getDefault())
})
doUelDateTest('${date:localizedDateStr(now, timeZone, locale)}', { Timestamp now ->
UelFunctions.localizedDateString(now, TimeZone.getDefault(), Locale.getDefault())
})
doUelDateTest('${date:localizedDateTimeStr(now, timeZone, locale)}', { Timestamp now ->
UelFunctions.localizedDateTimeString(now, TimeZone.getDefault(), Locale.getDefault())
})
doUelDateTest('${date:timeStr(now, timeZone, locale)}', { Timestamp now ->
UelFunctions.timeString(now, TimeZone.getDefault(), Locale.getDefault())
})
}

void testNowTimestampUel() {
FlexibleStringExpander fse = FlexibleStringExpander.getInstance('${date:nowTimestamp()}')
assert (fse.expand([:]).time - UtilDateTime.nowTimestamp().time).abs() < 15 // less than 10 ns appart
}

private void doUelDateTest(String uelInput, Closure uelFunction) {
Timestamp now = UtilDateTime.nowTimestamp()
Map context = [now: now,
timeZone: TimeZone.getDefault(),
locale: Locale.getDefault()]
FlexibleStringExpander fse = FlexibleStringExpander.getInstance(uelInput)
assert fse.expand(context) == uelFunction(now)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*******************************************************************************
* 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.ofbiz.base.test.uel

import org.apache.ofbiz.base.util.string.FlexibleStringExpander
import org.apache.ofbiz.service.testtools.OFBizTestCase

/**
* ./gradlew 'ofbiz -t component=base -t suitename=basetests'
*/
/* codenarc-disable GStringExpressionWithinString,ClosureAsLastMethodParameter */

class MathUelTest extends OFBizTestCase {

MathUelTest(String name) { super(name) }

void testMathUel() { // codenarc-disable JUnitTestMethodWithoutAssert
doMathTest('${math:absDouble(a)}', [a: -0.3, b: null], { a, b -> Math.abs(a as double) })
doMathTest('${math:absFloat(a)}', [a: -0.3, b: null], { a, b -> Math.abs(a as float) })
doMathTest('${math:absInt(a)}', [a: -2, b: null], { a, b -> Math.abs(a as int) })
doMathTest('${math:absLong(a)}', [a: -5, b: null], { a, b -> Math.abs(a as long) })
doMathTest('${math:acos(a)}', [a: 0.2, b: null], { a, b -> Math.acos(a) })
doMathTest('${math:asin(a)}', [a: 0.2, b: null], { a, b -> Math.asin(a) })
doMathTest('${math:atan(a)}', [a: 0.2, b: null], { a, b -> Math.atan(a) })
doMathTest('${math:atan2(a, b)}', [a: 0.2, b: 0.2], { a, b -> Math.atan2(a, b) })
doMathTest('${math:cbrt(a)}', [a: 10, b: null], { a, b -> Math.cbrt(a) })
doMathTest('${math:ceil(a)}', [a: 20, b: null], { a, b -> Math.ceil(a) })
doMathTest('${math:cos(a)}', [a: 0.2, b: null], { a, b -> Math.cos(a) })
doMathTest('${math:cosh(a)}', [a: 0.2, b: null], { a, b -> Math.cosh(a) })
doMathTest('${math:exp(a)}', [a: 29, b: null], { a, b -> Math.exp(a) })
doMathTest('${math:expm1(a)}', [a: 20, b: null], { a, b -> Math.expm1(a) })
doMathTest('${math:floor(a)}', [a: 23.4, b: null], { a, b -> Math.floor(a) })
doMathTest('${math:hypot(a, b)}', [a: 29, b: 12], { a, b -> Math.hypot(a, b) })
doMathTest('${math:IEEEremainder(a, b)}', [a: 12, b: 1.3], { a, b -> Math.IEEEremainder(a, b) })
doMathTest('${math:log(a)}', [a: 20, b: null], { a, b -> Math.log(a) })
doMathTest('${math:log10(a)}', [a: 29, b: null], { a, b -> Math.log10(a) })
doMathTest('${math:log1p(a)}', [a: 12, b: null], { a, b -> Math.log1p(a) })
doMathTest('${math:maxDouble(a, b)}', [a: 12, b: 13], { a, b -> Math.max(a as double, b as double) })
doMathTest('${math:maxFloat(a, b)}', [a: 2.4, b: 3.9], { a, b -> Math.max(a as float, b as float) })
doMathTest('${math:maxInt(a, b)}', [a: 2.6, b: 3.7], { a, b -> Math.max(a as int, b as int) })
doMathTest('${math:maxLong(a, b)}', [a: 23, b: 32], { a, b -> Math.max(a as long, b as long) })
doMathTest('${math:minDouble(a, b)}', [a: 10, b: 20], { a, b -> Math.min(a as double, b as double) })
doMathTest('${math:minFloat(a, b)}', [a: 1.2, b: 2.5], { a, b -> Math.min(a as float, b as float) })
doMathTest('${math:minInt(a, b)}', [a: 12, b: 13], { a, b -> Math.min(a as int, b as int) })
doMathTest('${math:minLong(a, b)}', [a: 24, b: 14], { a, b -> Math.min(a as long, b as long) })
doMathTest('${math:pow(a, b)}', [a: 12, b: 13], { a, b -> Math.pow(a, b) })
doMathTest('${math:rint(a)}', [a: 29, b: null], { a, b -> Math.rint(a) })
doMathTest('${math:roundDouble(a)}', [a: 23.4, b: null], { a, b -> Math.round(a as double) })
doMathTest('${math:roundFloat(a)}', [a: 12.4, b: null], { a, b -> Math.round(a as float) })
doMathTest('${math:signumDouble(a)}', [a: 23, b: null], { a, b -> Math.signum(a as double) })
doMathTest('${math:signumFloat(a)}', [a: 23, b: null], { a, b -> Math.signum(a as float) })
doMathTest('${math:sin(a)}', [a: 12, b: null], { a, b -> Math.sin(a) })
doMathTest('${math:sinh(a)}', [a: 12, b: null], { a, b -> Math.sinh(a) })
doMathTest('${math:sqrt(a)}', [a: 34, b: null], { a, b -> Math.sqrt(a) })
doMathTest('${math:tan(a)}', [a: 13, b: null], { a, b -> Math.tan(a) })
doMathTest('${math:tanh(a)}', [a: 30, b: null], { a, b -> Math.tanh(a) })
doMathTest('${math:toDegrees(a)}', [a: 30, b: null], { a, b -> Math.toDegrees(a) })
doMathTest('${math:toRadians(a)}', [a: 12, b: null], { a, b -> Math.toRadians(a) })
doMathTest('${math:ulpDouble(a)}', [a: 12, b: null], { a, b -> Math.ulp(a as double) })
doMathTest('${math:ulpFloat(a)}', [a: 30, b: null], { a, b -> Math.ulp(a as float) })
}

void testMathRandom() {
FlexibleStringExpander fse = FlexibleStringExpander.getInstance('${math:random()}')
assert fse.expand([:]) instanceof Double
assert fse.expand([:]) != BigDecimal.ZERO
}

private void doMathTest(String uelInput, Map context, Closure uelFunction) {
FlexibleStringExpander fse = FlexibleStringExpander.getInstance(uelInput)
assert new BigDecimal(fse.expand(context)) == new BigDecimal(uelFunction(context.a, context.b))
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*******************************************************************************
* 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.ofbiz.base.test.uel

import org.apache.ofbiz.base.util.string.FlexibleStringExpander
import org.apache.ofbiz.base.util.string.UelFunctions
import org.apache.ofbiz.service.testtools.OFBizTestCase

/**
* ./gradlew 'ofbiz -t component=base -t suitename=basetests'
*/
/* codenarc-disable GStringExpressionWithinString,ClosureAsLastMethodParameter */
class MiscUelTest extends OFBizTestCase {

MiscUelTest(String name) { super(name) }

void testSystemUel() { // codenarc-disable JUnitTestMethodWithoutAssert
// todo: Both are null, but mockito doesn't do system mock..
doUelSystemTest('${sys:getenv}', 'foo', { String prop -> UelFunctions.sysGetEnv(prop) })
doUelSystemTest('${sys:getProperty}', 'bar', { String prop -> UelFunctions.sysGetProp(prop) })
}

void testUtilSizeUel() {
List foo = [1, 2, 3]
FlexibleStringExpander fse = FlexibleStringExpander.getInstance('${util:size(foo)}')
assert fse.expand([foo: foo]) == UelFunctions.getSize(foo)
}

void testDefaultLocaleAndTimzoneUel() {
FlexibleStringExpander fse = FlexibleStringExpander.getInstance('${util:defaultLocale()}')
assert fse.expand([foo: 'bar']) == Locale.getDefault()
fse = FlexibleStringExpander.getInstance('${util:defaultTimeZone()}')
assert fse.expand([foo: 'bar']) == TimeZone.getDefault()
}

void testLabelUel() {
String labelFile = 'CommonEntityLabels', labelKey = 'VisualTheme.description.RAINBOWSTONE_AMBER'
FlexibleStringExpander fse = FlexibleStringExpander.getInstance(
'${util:label(\'CommonEntityLabels\',\'VisualTheme.description.RAINBOWSTONE_AMBER\', locale)}')
assert fse.expand([locale: Locale.getDefault()]) == UelFunctions.label(labelFile, labelKey, Locale.getDefault())
}

private void doUelSystemTest(String uelInput, String param, Closure uelFunction) {
FlexibleStringExpander fse = FlexibleStringExpander.getInstance(uelInput)
assert fse.expand([:]) == uelFunction(param)
}

// TODO: Some day
// '${screen:id}', UelFunctions.class.getMethod("resolveCurrentScreenId", ScreenRenderer.ScreenStack.class))
// '${dom:readHtmlDocument}', UelFunctions.class.getMethod("readHtmlDocument", String.class))
// '${dom:readXmlDocument}', UelFunctions.class.getMethod("readXmlDocument", String.class))
// '${dom:toHtmlString}', UelFunctions.class.getMethod("toHtmlString", Node.class, String.class, boolean.class,
// int.class))
// '${dom:toXmlString}', UelFunctions.class.getMethod("toXmlString", Node.class, String.class, boolean.class,
// boolean.class, int.class))
// '${dom:writeXmlDocument}', UelFunctions.class.getMethod("writeXmlDocument", String.class, Node.class,
// String.class, boolean.class, boolean.class, int.class))

}
Loading
Loading