Author: rombert
Date: Wed Feb 24 15:22:08 2016
New Revision: 1732167

URL: http://svn.apache.org/viewvc?rev=1732167&view=rev
Log:
SLING-5538 - Create new Sightly file wizards 

Create a JavaUtils class in the sightly ui bundle and create a test for
it.

Added:
    
sling/trunk/tooling/ide/eclipse-sightly-ui/src/org/apache/sling/ide/eclipse/sightly/ui/internal/JavaUtils.java
      - copied, changed from r1732166, 
sling/trunk/tooling/ide/eclipse-sightly-ui/src/org/apache/sling/ide/eclipse/sightly/ui/internal/NewSightlyJavaUseScriptWizard.java
    
sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/ui/sightly/JavaUtilsTest.java
Modified:
    sling/trunk/tooling/ide/eclipse-sightly-ui/META-INF/MANIFEST.MF
    
sling/trunk/tooling/ide/eclipse-sightly-ui/src/org/apache/sling/ide/eclipse/sightly/ui/internal/NewSightlyJavaUseScriptWizard.java
    sling/trunk/tooling/ide/eclipse-test/META-INF/MANIFEST.MF

Modified: sling/trunk/tooling/ide/eclipse-sightly-ui/META-INF/MANIFEST.MF
URL: 
http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-sightly-ui/META-INF/MANIFEST.MF?rev=1732167&r1=1732166&r2=1732167&view=diff
==============================================================================
--- sling/trunk/tooling/ide/eclipse-sightly-ui/META-INF/MANIFEST.MF (original)
+++ sling/trunk/tooling/ide/eclipse-sightly-ui/META-INF/MANIFEST.MF Wed Feb 24 
15:22:08 2016
@@ -34,3 +34,4 @@ Import-Package: org.apache.sling.ide.ecl
  org.eclipse.wst.xml.core.internal.provisional.document,
  org.eclipse.wst.xml.core.internal.regions,
  org.eclipse.wst.xml.ui.internal.contentassist
+Export-Package: 
org.apache.sling.ide.eclipse.sightly.ui.internal;x-friends:="org.apache.sling.ide.eclipse-test"

Copied: 
sling/trunk/tooling/ide/eclipse-sightly-ui/src/org/apache/sling/ide/eclipse/sightly/ui/internal/JavaUtils.java
 (from r1732166, 
sling/trunk/tooling/ide/eclipse-sightly-ui/src/org/apache/sling/ide/eclipse/sightly/ui/internal/NewSightlyJavaUseScriptWizard.java)
URL: 
http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-sightly-ui/src/org/apache/sling/ide/eclipse/sightly/ui/internal/JavaUtils.java?p2=sling/trunk/tooling/ide/eclipse-sightly-ui/src/org/apache/sling/ide/eclipse/sightly/ui/internal/JavaUtils.java&p1=sling/trunk/tooling/ide/eclipse-sightly-ui/src/org/apache/sling/ide/eclipse/sightly/ui/internal/NewSightlyJavaUseScriptWizard.java&r1=1732166&r2=1732167&rev=1732167&view=diff
==============================================================================
--- 
sling/trunk/tooling/ide/eclipse-sightly-ui/src/org/apache/sling/ide/eclipse/sightly/ui/internal/NewSightlyJavaUseScriptWizard.java
 (original)
+++ 
sling/trunk/tooling/ide/eclipse-sightly-ui/src/org/apache/sling/ide/eclipse/sightly/ui/internal/JavaUtils.java
 Wed Feb 24 15:22:08 2016
@@ -22,38 +22,31 @@ import org.eclipse.core.resources.IProje
 import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.IPath;
 
-public class NewSightlyJavaUseScriptWizard extends 
AbstractNewSightlyFileWizard {
+public class JavaUtils {
 
-    public NewSightlyJavaUseScriptWizard() {
-        super("New Sightly Java Use-Script", "Create a new Sightly Java 
Use-Script");
-    }
-    
-    @Override
-    protected String getInitialContents() {
-        
-        IPath fullPath = fileCreationPage.getContainerFullPath();
-        
-        String className = fileCreationPage.getFileName().replace(".java", "");
+    /**
+     * Tries to infer the package for a Sightly Java Use-Object
+     * 
+     * <p>For instance, for a file placed at 
<tt>jcr_root/apps/components/my/Script.java</tt> and
+     * a content sync root of <tt>jcr_root</tt>, the inferred package will be 
<tt>apps.components.my</tt></p>
+     * 
+     * @param fullPath the path where the Use-Object file will be created ( 
file name excluded )
+     * @return the package name, null if none could be inferred
+     */
+    public static String inferPackage(IPath fullPath) {
         
         IProject project = 
ResourcesPlugin.getWorkspace().getRoot().getProject(fullPath.segment(0));
         
         // we could make this dependency optional if needed, but I don't see a 
great need right now 
         IFolder contentSyncRoot = ProjectUtil.getSyncDirectory(project);
-        String inferredPackage = "unknown // TODO - replace with actual path";
         if ( contentSyncRoot != null ) {
-            inferredPackage = 
fullPath.makeRelativeTo(contentSyncRoot.getFullPath()).toPortableString().replace('/',
 '.');
+            return 
fullPath.makeRelativeTo(contentSyncRoot.getFullPath()).toPortableString().replace('/',
 '.');
         }
         
-        return "" +
-               "package " + inferredPackage + ";\n" + 
-               "\n" +
-               "import java.util.Date;\n" +
-               "\n" +
-               "public class " + className + " {\n" +
-               "    public String getDate() {\n" +
-               "        return new Date().toString();\n" +
-               "    }\n" +
-               "}";
+        return null;
+    }
+    
+    private JavaUtils() {
+        
     }
-
 }

Modified: 
sling/trunk/tooling/ide/eclipse-sightly-ui/src/org/apache/sling/ide/eclipse/sightly/ui/internal/NewSightlyJavaUseScriptWizard.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-sightly-ui/src/org/apache/sling/ide/eclipse/sightly/ui/internal/NewSightlyJavaUseScriptWizard.java?rev=1732167&r1=1732166&r2=1732167&view=diff
==============================================================================
--- 
sling/trunk/tooling/ide/eclipse-sightly-ui/src/org/apache/sling/ide/eclipse/sightly/ui/internal/NewSightlyJavaUseScriptWizard.java
 (original)
+++ 
sling/trunk/tooling/ide/eclipse-sightly-ui/src/org/apache/sling/ide/eclipse/sightly/ui/internal/NewSightlyJavaUseScriptWizard.java
 Wed Feb 24 15:22:08 2016
@@ -16,10 +16,6 @@
  */
 package org.apache.sling.ide.eclipse.sightly.ui.internal;
 
-import org.apache.sling.ide.eclipse.core.ProjectUtil;
-import org.eclipse.core.resources.IFolder;
-import org.eclipse.core.resources.IProject;
-import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.IPath;
 
 public class NewSightlyJavaUseScriptWizard extends 
AbstractNewSightlyFileWizard {
@@ -35,13 +31,9 @@ public class NewSightlyJavaUseScriptWiza
         
         String className = fileCreationPage.getFileName().replace(".java", "");
         
-        IProject project = 
ResourcesPlugin.getWorkspace().getRoot().getProject(fullPath.segment(0));
-        
-        // we could make this dependency optional if needed, but I don't see a 
great need right now 
-        IFolder contentSyncRoot = ProjectUtil.getSyncDirectory(project);
-        String inferredPackage = "unknown // TODO - replace with actual path";
-        if ( contentSyncRoot != null ) {
-            inferredPackage = 
fullPath.makeRelativeTo(contentSyncRoot.getFullPath()).toPortableString().replace('/',
 '.');
+        String inferredPackage = JavaUtils.inferPackage(fullPath);
+        if ( inferredPackage == null ) {
+            inferredPackage = "unknown // TODO - replace with actual path";
         }
         
         return "" +

Modified: sling/trunk/tooling/ide/eclipse-test/META-INF/MANIFEST.MF
URL: 
http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-test/META-INF/MANIFEST.MF?rev=1732167&r1=1732166&r2=1732167&view=diff
==============================================================================
--- sling/trunk/tooling/ide/eclipse-test/META-INF/MANIFEST.MF (original)
+++ sling/trunk/tooling/ide/eclipse-test/META-INF/MANIFEST.MF Wed Feb 24 
15:22:08 2016
@@ -14,6 +14,7 @@ Require-Bundle: org.junit,
  org.apache.sling.ide.eclipse-core,
  org.apache.sling.ide.eclipse-ui,
  org.apache.sling.ide.eclipse-sightly-core,
+ org.apache.sling.ide.eclipse-sightly-ui,
  org.apache.sling.ide.api,
  org.apache.sling.ide.artifacts,
  org.eclipse.debug.core,

Added: 
sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/ui/sightly/JavaUtilsTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/ui/sightly/JavaUtilsTest.java?rev=1732167&view=auto
==============================================================================
--- 
sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/ui/sightly/JavaUtilsTest.java
 (added)
+++ 
sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/ui/sightly/JavaUtilsTest.java
 Wed Feb 24 15:22:08 2016
@@ -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 org.apache.sling.ide.test.impl.ui.sightly;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import org.apache.sling.ide.eclipse.sightly.ui.internal.JavaUtils;
+import org.apache.sling.ide.test.impl.helpers.ProjectAdapter;
+import org.apache.sling.ide.test.impl.helpers.TemporaryProject;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.jdt.core.JavaCore;
+import org.hamcrest.CoreMatchers;
+import org.junit.Rule;
+import org.junit.Test;
+
+public class JavaUtilsTest {
+
+    @Rule
+    public TemporaryProject projectRule = new TemporaryProject();    
+    
+    @Test
+    public void inferPackageName() throws Exception {
+        
+        // create faceted project
+        IProject contentProject = projectRule.getProject();
+
+        ProjectAdapter project = new ProjectAdapter(contentProject);
+        project.addNatures(JavaCore.NATURE_ID, 
"org.eclipse.wst.common.project.facet.core.nature");
+
+        // install facets
+        project.installFacet("sling.content", "1.0");
+        project.installFacet("sightly", "1.1");
+        
+        // create basic html file
+        IPath path = Path.fromOSString("jcr_root/apps/components/app/forum");
+        IPath fullPath = project.ensureDirectoryExists(path).getFullPath();
+        
+        String inferredPackage = JavaUtils.inferPackage(fullPath);
+        assertThat(inferredPackage, 
CoreMatchers.equalTo("apps.components.app.forum"));
+    }
+}


Reply via email to