EXTSCRIPT-154: Code Rewrite/Refactoring, save state of affairs
git-svn-id: https://svn.apache.org/repos/asf/myfaces/extensions/scripting/trunk@1300073 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/core/common/util/FileUtils.java b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/core/common/util/FileUtils.java
index 2030cdc..5894256 100644
--- a/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/core/common/util/FileUtils.java
+++ b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/core/common/util/FileUtils.java
@@ -76,10 +76,10 @@
File tempDir;
String baseTempPath = System.getProperty("java.io.tmpdir");
- String tempDirName = "myfaces_compilation_" + _tempMarker;
+ String tempDirName = "myfaces_compilation_"; //+ _tempMarker;
tempDir = new File(baseTempPath + File.separator + tempDirName);
- while (tempDir.exists()) {
+ /*while (tempDir.exists()) {
tempDirName = "myfaces_compilation_" + System.currentTimeMillis() + Math.random();
tempDir = new File(baseTempPath + File.separator + tempDirName);
}
@@ -91,7 +91,7 @@
if (tempDir.mkdirs()) {
tempDir.deleteOnExit();
}
- }
+ } */
return tempDir;
}
diff --git a/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/core/context/Configuration.java b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/core/context/Configuration.java
index 5eeed5f..b1d46bc 100644
--- a/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/core/context/Configuration.java
+++ b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/core/context/Configuration.java
@@ -182,7 +182,8 @@
*/
public void addSourceDir(int scriptingEngine, String sourceDir)
{
- WeavingContext.getInstance().getEngine(scriptingEngine).getSourcePaths().add(sourceDir);
+ if(!WeavingContext.getInstance().getEngine(scriptingEngine).getSourcePaths().contains(sourceDir))
+ WeavingContext.getInstance().getEngine(scriptingEngine).getSourcePaths().add(sourceDir);
}
diff --git a/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/core/reloading/GlobalReloadingStrategy.java b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/core/reloading/GlobalReloadingStrategy.java
index 12e1d61..9c0c42b 100644
--- a/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/core/reloading/GlobalReloadingStrategy.java
+++ b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/core/reloading/GlobalReloadingStrategy.java
@@ -41,6 +41,7 @@
public class GlobalReloadingStrategy implements ReloadingStrategy
{
+ private static final String STRATEGY_PKG = "rewrite.org.apache.myfaces.extensions.scripting.jsf.facelet";
final Logger _logger = Logger.getLogger(GlobalReloadingStrategy.class.getName());
protected ReloadingStrategy _beanStrategy;
@@ -62,14 +63,10 @@
/*
* external handlers coming from various submodules
*/
- // _componentHandlerStrategy = dynaload("org.apache.myfaces.extensions.scripting.facelet" +
- // ".ComponentHandlerReloadingStrategy");
- // _validatorHandlerStrategy = dynaload("org.apache.myfaces.extensions.scripting.facelet" +
- // ".ValidatorHandlerReloadingStrategy");
- // _converterHandlerStrategy = dynaload("org.apache.myfaces.extensions.scripting.facelet" +
- // ".ConverterHandlerReloadingStrategy");
- // _behaviorHandlerStrategy = dynaload("org.apache.myfaces.extensions.scripting.facelet" +
- // ".BehaviorHandlerReloadingStrategy");
+ _componentHandlerStrategy = dynaload(STRATEGY_PKG +".ComponentHandlerReloadingStrategy");
+ _validatorHandlerStrategy = dynaload(STRATEGY_PKG +".ValidatorHandlerReloadingStrategy");
+ _converterHandlerStrategy = dynaload(STRATEGY_PKG +".ConverterHandlerReloadingStrategy");
+ _behaviorHandlerStrategy = dynaload(STRATEGY_PKG +".BehaviorHandlerReloadingStrategy");
}
/**
@@ -99,14 +96,14 @@
case ARTIFACT_TYPE_VALIDATOR:
return _noMappingStrategy.reload(toReload, artifactType);
- // case ARTIFACT_TYPE_COMPONENT_HANDLER:
- // return dynaReload(toReload, _componentHandlerStrategy, artifactType);
- // case ARTIFACT_TYPE_CONVERTER_HANDLER:
- // return dynaReload(toReload, _converterHandlerStrategy, artifactType);
- // case ARTIFACT_TYPE_VALIDATOR_HANDLER:
- // return dynaReload(toReload, _validatorHandlerStrategy, artifactType);
- // case ARTIFACT_TYPE_BEHAVIOR_HANDLER:
- // return dynaReload(toReload, _behaviorHandlerStrategy, artifactType);
+ case ARTIFACT_TYPE_COMPONENT_HANDLER:
+ return dynaReload(toReload, _componentHandlerStrategy, artifactType);
+ case ARTIFACT_TYPE_CONVERTER_HANDLER:
+ return dynaReload(toReload, _converterHandlerStrategy, artifactType);
+ case ARTIFACT_TYPE_VALIDATOR_HANDLER:
+ return dynaReload(toReload, _validatorHandlerStrategy, artifactType);
+ case ARTIFACT_TYPE_BEHAVIOR_HANDLER:
+ return dynaReload(toReload, _behaviorHandlerStrategy, artifactType);
default:
return _allOthers.reload(toReload, artifactType);
diff --git a/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/facelet/BehaviorHandlerReloadingStrategy.java b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/reloading/BehaviorHandlerReloadingStrategy.java
similarity index 97%
rename from extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/facelet/BehaviorHandlerReloadingStrategy.java
rename to extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/reloading/BehaviorHandlerReloadingStrategy.java
index da2c571..e2b9323 100644
--- a/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/facelet/BehaviorHandlerReloadingStrategy.java
+++ b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/reloading/BehaviorHandlerReloadingStrategy.java
@@ -17,7 +17,7 @@
* under the License.
*/
-package rewrite.org.apache.myfaces.extensions.scripting.jsf.facelet;
+package rewrite.org.apache.myfaces.extensions.scripting.jsf.reloading;
import rewrite.org.apache.myfaces.extensions.scripting.core.common.util.Cast;
diff --git a/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/facelet/ComponentHandlerReloadingStrategy.java b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/reloading/ComponentHandlerReloadingStrategy.java
similarity index 96%
rename from extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/facelet/ComponentHandlerReloadingStrategy.java
rename to extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/reloading/ComponentHandlerReloadingStrategy.java
index 01c4370..d8f6bb1 100644
--- a/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/facelet/ComponentHandlerReloadingStrategy.java
+++ b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/reloading/ComponentHandlerReloadingStrategy.java
@@ -17,7 +17,7 @@
* under the License.
*/
-package rewrite.org.apache.myfaces.extensions.scripting.jsf.facelet;
+package rewrite.org.apache.myfaces.extensions.scripting.jsf.reloading;
import rewrite.org.apache.myfaces.extensions.scripting.core.common.util.Cast;
import rewrite.org.apache.myfaces.extensions.scripting.core.common.util.ReflectUtil;
diff --git a/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/facelet/ConverterHandlerReloadingStrategy.java b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/reloading/ConverterHandlerReloadingStrategy.java
similarity index 97%
rename from extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/facelet/ConverterHandlerReloadingStrategy.java
rename to extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/reloading/ConverterHandlerReloadingStrategy.java
index bcaa740..7d887c8 100644
--- a/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/facelet/ConverterHandlerReloadingStrategy.java
+++ b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/reloading/ConverterHandlerReloadingStrategy.java
@@ -17,7 +17,7 @@
* under the License.
*/
-package rewrite.org.apache.myfaces.extensions.scripting.jsf.facelet;
+package rewrite.org.apache.myfaces.extensions.scripting.jsf.reloading;
import rewrite.org.apache.myfaces.extensions.scripting.core.common.util.Cast;
import rewrite.org.apache.myfaces.extensions.scripting.core.common.util.ReflectUtil;
diff --git a/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/facelet/ValidatorHandlerReloadingStrategy.java b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/reloading/ValidatorHandlerReloadingStrategy.java
similarity index 96%
rename from extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/facelet/ValidatorHandlerReloadingStrategy.java
rename to extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/reloading/ValidatorHandlerReloadingStrategy.java
index 153eaf9..ec55f6c 100644
--- a/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/facelet/ValidatorHandlerReloadingStrategy.java
+++ b/extscript-core-root/extscript-core/src/main/java/rewrite/org/apache/myfaces/extensions/scripting/jsf/reloading/ValidatorHandlerReloadingStrategy.java
@@ -17,7 +17,7 @@
* under the License.
*/
-package rewrite.org.apache.myfaces.extensions.scripting.jsf.facelet;
+package rewrite.org.apache.myfaces.extensions.scripting.jsf.reloading;
import rewrite.org.apache.myfaces.extensions.scripting.core.common.util.Cast;
diff --git a/extscript-core-root/extscript-core/src/test/java/rewrite/org/apache/myfaces/extensions/scripting/scanningcore/engine/compiler/JavaCompilerTest.java b/extscript-core-root/extscript-core/src/test/java/rewrite/org/apache/myfaces/extensions/scripting/scanningcore/engine/compiler/JavaCompilerTest.java
index 4cbf7a1..90d6d31 100644
--- a/extscript-core-root/extscript-core/src/test/java/rewrite/org/apache/myfaces/extensions/scripting/scanningcore/engine/compiler/JavaCompilerTest.java
+++ b/extscript-core-root/extscript-core/src/test/java/rewrite/org/apache/myfaces/extensions/scripting/scanningcore/engine/compiler/JavaCompilerTest.java
@@ -50,7 +50,6 @@
File probe2;
File root;
- JSR199Compiler compiler = new JSR199Compiler();
private static final String RESULT_HAS_NO_ERRORS = "result has no errors";
private static final String TARGET_DIR_EXISTS = "targetDir exists files are compiled into the targetDir";
private static final String CLASSFILE1_IS_COMPILED = "Classfile1 is compiled into the targetDir";
@@ -88,11 +87,14 @@
WeavingContext.getInstance().setConfiguration(new Configuration());
WeavingContext.getInstance().getConfiguration().addSourceDir(ScriptingConst.ENGINE_TYPE_JSF_JAVA,
root.getAbsolutePath());
+
}
@Test
public void testFullCompileWhitelist()
{
+ JSR199Compiler compiler = new JSR199Compiler();
+
File targetDir = null;
File target = WeavingContext.getInstance().getConfiguration().getCompileTarget();
@@ -122,16 +124,19 @@
assertTrue(CLASSFILE1_IS_COMPILED1, !classFile1.exists());
assertTrue(CLASSFILE2_IS_COMPILED, !classFile2.exists());
assertTrue(CLASSFILE2_IS_COMPILED, classFile3.exists());
-
+ WeavingContext.getInstance().getConfiguration().getCompileTarget().delete();
+ WeavingContext.getInstance().getConfiguration().getCompileTarget().mkdirs();
}
@Test
public void testFullCompile()
{
+ JSR199Compiler compiler = new JSR199Compiler();
File targetDir = null;
File target = FileUtils.getTempDir();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
+
target.mkdirs();
target.deleteOnExit();
@@ -153,7 +158,9 @@
assertTrue(CLASSFILE1_IS_COMPILED1, classFile1.exists());
assertTrue(CLASSFILE2_IS_COMPILED, classFile2.exists());
-
+ //WeavingContext.getInstance().getConfiguration().getCompileTarget().delete();
+ //WeavingContext.getInstance().getConfiguration().getCompileTarget().mkdirs();
+ //testFullCompileWhitelist();
}
}
diff --git a/extscript-core-root/extscript-core/src/test/java/rewrite/org/apache/myfaces/extensions/scripting/scanningcore/probes/MethodReloadingProbe.java b/extscript-core-root/extscript-core/src/test/java/rewrite/org/apache/myfaces/extensions/scripting/scanningcore/probes/MethodReloadingProbe.java
new file mode 100644
index 0000000..558420b
--- /dev/null
+++ b/extscript-core-root/extscript-core/src/test/java/rewrite/org/apache/myfaces/extensions/scripting/scanningcore/probes/MethodReloadingProbe.java
@@ -0,0 +1,21 @@
+package rewrite.org.apache.myfaces.extensions.scripting.scanningcore.probes;
+
+/**
+ * Interface which will allow the proxying of our probe
+ * in reloading handlers
+ *
+ * @author Werner Punz (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public interface MethodReloadingProbe {
+
+ /**
+ * testmethod 1 goes through
+ */
+ public void testMethod1();
+
+ /**
+ * this one throws an exception
+ */
+ public void testMethod2();
+}
diff --git a/extscript-core-root/extscript-core/src/test/java/rewrite/org/apache/myfaces/extensions/scripting/scanningcore/probes/Probe.java b/extscript-core-root/extscript-core/src/test/java/rewrite/org/apache/myfaces/extensions/scripting/scanningcore/probes/Probe.java
new file mode 100644
index 0000000..a7d7857
--- /dev/null
+++ b/extscript-core-root/extscript-core/src/test/java/rewrite/org/apache/myfaces/extensions/scripting/scanningcore/probes/Probe.java
@@ -0,0 +1,57 @@
+/*
+ * 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 rewrite.org.apache.myfaces.extensions.scripting.scanningcore.probes;
+
+import org.junit.Ignore;
+
+/**
+ * @author Werner Punz (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ * <p/>
+ * testprobe for our reflectutils
+ */
+
+@Ignore
+public class Probe implements MethodReloadingProbe
+{
+
+ public Probe() {
+ }
+
+ public Probe(String hello, String world) {
+
+ }
+
+ public void testMethod1() {
+
+ }
+
+ public void testMethod2() {
+ throw new NullPointerException("for test");
+ }
+
+ public boolean testMethod3(String param1) {
+ return true;
+ }
+
+ public static boolean testMethod4(String param1, String param2) {
+ return true;
+ }
+
+}
diff --git a/extscript-core-root/extscript-core/src/test/java/rewrite/org/apache/myfaces/extensions/scripting/scanningcore/probes/Probe2.java b/extscript-core-root/extscript-core/src/test/java/rewrite/org/apache/myfaces/extensions/scripting/scanningcore/probes/Probe2.java
new file mode 100644
index 0000000..b282abe
--- /dev/null
+++ b/extscript-core-root/extscript-core/src/test/java/rewrite/org/apache/myfaces/extensions/scripting/scanningcore/probes/Probe2.java
@@ -0,0 +1,36 @@
+/*
+ * 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 rewrite.org.apache.myfaces.extensions.scripting.scanningcore.probes;
+
+/**
+ * @author Werner Punz (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+
+public class Probe2 {
+
+ public Probe2(String[] test) {
+
+ }
+
+ public static Boolean myHello(String xxx) {
+ return Boolean.TRUE;
+ }
+}
diff --git a/extscript-core-root/extscript-core/src/test/java/rewrite/org/apache/myfaces/extensions/scripting/scanningcore/utilstest/FileUtilsTest.java b/extscript-core-root/extscript-core/src/test/java/rewrite/org/apache/myfaces/extensions/scripting/scanningcore/utilstest/FileUtilsTest.java
new file mode 100644
index 0000000..f05797d
--- /dev/null
+++ b/extscript-core-root/extscript-core/src/test/java/rewrite/org/apache/myfaces/extensions/scripting/scanningcore/utilstest/FileUtilsTest.java
@@ -0,0 +1,98 @@
+/*
+ * 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 rewrite.org.apache.myfaces.extensions.scripting.scanningcore.utilstest;
+
+import org.junit.Before;
+import org.junit.Test;
+import rewrite.org.apache.myfaces.extensions.scripting.core.common.util.FileUtils;
+import rewrite.org.apache.myfaces.extensions.scripting.scanningcore.support.PathUtils;
+
+import java.io.File;
+import java.util.List;
+
+import static junit.framework.Assert.fail;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Test cases for our FileUtils
+ *
+ * @author Werner Punz (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+
+public class FileUtilsTest {
+ PathUtils pathUtils = new PathUtils();
+
+ @Before
+ public void init() {
+
+ }
+
+ @Test
+ public void testForRegexp() {
+ String fileSep = FileUtils.getFileSeparatorForRegex();
+ assertTrue("must be double backslash instead of single one", (File.separator.equals("\\")) ? fileSep.equals("\\\\") : fileSep.equals(File.separator));
+ }
+
+ @Test
+ public void testGetFileSeparator() {
+ String fileSeparator = FileUtils.getFileSeparator();
+ assertTrue(fileSeparator.equals(File.separator));
+ }
+
+ @Test
+ public void testGetTempDir() {
+ File tempDir = FileUtils.getTempDir();
+ assertTrue(tempDir != null);
+ assertTrue(tempDir.exists());
+ }
+
+ @Test
+ public void testFileStrategy() {
+ List<File> sourceFiles = FileUtils.fetchSourceFiles(new File(pathUtils.getResource("compiler/")), ".java");
+ assertTrue("wildcarding is needed", sourceFiles.size() == 0);
+
+ sourceFiles = FileUtils.fetchSourceFiles(new File(pathUtils.getResource("compiler/")), "java");
+ assertTrue("wildcarding is needed", sourceFiles.size() == 0);
+
+
+ sourceFiles = FileUtils.fetchSourceFiles(new File(pathUtils.getResource("compiler/")), "*.java");
+ assertTrue("source files must have been found", sourceFiles.size() > 2);
+ //check also for subdirs
+ for(File sourceFile: sourceFiles) {
+ if(sourceFile.getAbsolutePath().contains("myPackage")) {
+ return;
+ }
+ }
+ fail("source file must also be in myPackage");
+ }
+
+ @Test
+ public void testDirStrategy() {
+ StringBuilder result = FileUtils.fetchSourcePaths(new File(pathUtils.getResource("compilerx/")), "");
+ assertTrue("invalid dir should result in empty results", result.toString().trim().length() == 0);
+
+ result = FileUtils.fetchSourcePaths(new File(pathUtils.getResource("compiler/")), "");
+ assertTrue("myPackage should be found", result.toString().trim().contains("myPackage"));
+
+
+ }
+
+}
diff --git a/extscript-core-root/extscript-core/src/test/java/rewrite/org/apache/myfaces/extensions/scripting/scanningcore/utilstest/ReflectUtilTest.java b/extscript-core-root/extscript-core/src/test/java/rewrite/org/apache/myfaces/extensions/scripting/scanningcore/utilstest/ReflectUtilTest.java
new file mode 100644
index 0000000..b4e32d2
--- /dev/null
+++ b/extscript-core-root/extscript-core/src/test/java/rewrite/org/apache/myfaces/extensions/scripting/scanningcore/utilstest/ReflectUtilTest.java
@@ -0,0 +1,165 @@
+/*
+ * 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 rewrite.org.apache.myfaces.extensions.scripting.scanningcore.utilstest;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import rewrite.org.apache.myfaces.extensions.scripting.core.common.util.Cast;
+import rewrite.org.apache.myfaces.extensions.scripting.core.common.util.Null;
+import rewrite.org.apache.myfaces.extensions.scripting.core.common.util.ReflectUtil;
+import rewrite.org.apache.myfaces.extensions.scripting.scanningcore.probes.Probe;
+
+import java.lang.reflect.Method;
+import java.util.Collection;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+/**
+ * Testcase for our reflect utils
+ * which we rely heavily upon
+ *
+ * @author Werner Punz (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+
+public class ReflectUtilTest {
+ private static final String HELLO_WORLD = "Hello World";
+ private static final String JAVA_LANG_STRING = "java.lang.String";
+ private static final String MSG_INSTANTIATED = "String must be instantiated";
+ private static final String MSG_PROBE_INSTANTIATED = "Probe must be instantiated";
+ private static final String MSG_INIT_FAIL = "init failed expected";
+
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ }
+
+ @Test
+ public void testInstantiate() throws Exception {
+ String retVal = (String) ReflectUtil.instantiate(JAVA_LANG_STRING);
+ assertTrue(MSG_INSTANTIATED, retVal != null);
+
+ retVal = (String) ReflectUtil.instantiate(JAVA_LANG_STRING, HELLO_WORLD);
+ assertTrue(MSG_INSTANTIATED, retVal != null && retVal.equals(HELLO_WORLD));
+
+ Object myHello = HELLO_WORLD;
+ Object probe = ReflectUtil.instantiate(Probe.class, new Cast(String.class, myHello), HELLO_WORLD);
+ assertTrue(MSG_PROBE_INSTANTIATED, probe != null);
+
+ try {
+ ReflectUtil.instantiate(Probe.class, new Cast(Integer.class, myHello), HELLO_WORLD);
+ fail();
+ } catch (RuntimeException ex) {
+ assertTrue(MSG_INIT_FAIL, true);
+ }
+ probe = ReflectUtil.instantiate(Probe.class, new Null(String.class), new Null(String.class));
+ assertTrue(MSG_PROBE_INSTANTIATED, probe != null);
+
+ try {
+ ReflectUtil.instantiate(Probe.class, new Null(Integer.class), new Null(String.class));
+ fail();
+ } catch (RuntimeException ex) {
+ assertTrue(MSG_INIT_FAIL, true);
+ }
+
+ //TODO (1.1) test fails, but is not used so we can live with it
+ //probe = ReflectUtil.instantiate(Probe2.class,new Array(String.class, HELLO_WORLD, HELLO_WORLD));
+ //assertTrue("Probe must be instantiated", probe != null);
+ }
+
+ @Test
+ public void testNewObject() throws Exception {
+ String retVal = (String) ReflectUtil.newObject(String.class);
+ assertTrue(MSG_INSTANTIATED, retVal != null);
+ }
+
+ @Test
+ public void testExecuteStaticMethod() throws Exception {
+ Boolean retVal = (Boolean) ReflectUtil.executeStaticMethod(Boolean.class, "valueOf", "true");
+ assertTrue("retval must be true", retVal);
+
+ try {
+ ReflectUtil.executeStaticMethod(Boolean.class, "xx_valueOf", "true");
+ fail();
+ } catch (RuntimeException ex) {
+ assertTrue("Exception must be thrown", true);
+ }
+
+ }
+
+ @Test
+ public void testFastExecuteStaticMethod() throws Exception {
+ Boolean retVal = (Boolean) ReflectUtil.fastExecuteStaticMethod(Boolean.class, "valueOf", "true");
+ assertTrue("retval must be true", retVal);
+ }
+
+ @Test
+ public void testGetAllMethods() throws Exception {
+ Collection<Method> retVal = ReflectUtil.getAllMethods(Boolean.class, "valueOf", 1);
+ assertTrue(retVal.size() == 2);/*String and boolean*/
+ retVal = ReflectUtil.getAllMethods(Object.class, "toString", 0);
+ assertTrue(retVal.size() == 1);/*String and boolean*/
+ }
+
+ @Test
+ public void testExecuteMethod() throws Exception {
+
+ Boolean probe = true;
+ Boolean retVal = (Boolean) ReflectUtil.executeMethod(probe, "valueOf", "true");
+ assertTrue(retVal);
+ String sRetVal = (String) ReflectUtil.executeMethod(probe, "toString");
+ assertTrue(sRetVal.equals("true"));
+
+ Object hashVal = ReflectUtil.executeMethod(new Probe(), "hashCode");
+ assertTrue(hashVal != null);
+
+ try {
+ ReflectUtil.executeMethod(new Probe(), "xx_hashCode");
+ fail();
+ } catch (RuntimeException ex) {
+ assertTrue("calling must faile with an RE", true);
+ }
+ }
+
+ @Test
+ public void testFastExecuteMethod() throws Exception {
+
+ Boolean probe = true;
+ Boolean retVal = (Boolean) ReflectUtil.fastExecuteMethod(probe, "valueOf", "true");
+ assertTrue(retVal);
+ String sRetVal = (String) ReflectUtil.fastExecuteMethod(probe, "toString");
+ assertTrue(sRetVal.equals("true"));
+
+ Object hashVal = ReflectUtil.fastExecuteMethod(new Probe(), "hashCode");
+ assertTrue(hashVal != null);
+
+ }
+
+ @Test
+ public void testCast() {
+ assertTrue("Cast testing", ReflectUtil.cast(String.class, HELLO_WORLD) instanceof Cast);
+ assertTrue("Cast testing", ReflectUtil.nullCast(String.class) instanceof Null);
+ }
+}