Author: scottbw
Date: Fri Aug 5 09:32:53 2011
New Revision: 1154140
URL: http://svn.apache.org/viewvc?rev=1154140&view=rev
Log:
Fixed the issue with using the wrong location (see WOOKIE-230) and added more
code comments. Also created a set of unit tests for feature loading.
Added:
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/FeaturesTest.java
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/feature.xml
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/feature_invalid_name.xml
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/feature_with_resources.xml
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_features_folder/
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_features_folder/not_a_feature.xml
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_features_folder/test/
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_features_folder/test/feature.xml
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_features_folder/test_empty/
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_features_folder/test_no_feature_xml/
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_features_folder/test_no_feature_xml/not_a_feature.xml
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_styles.css
Modified:
incubator/wookie/trunk/src/org/apache/wookie/feature/Features.java
Added:
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/FeaturesTest.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/FeaturesTest.java?rev=1154140&view=auto
==============================================================================
---
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/FeaturesTest.java
(added)
+++
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/FeaturesTest.java
Fri Aug 5 09:32:53 2011
@@ -0,0 +1,121 @@
+/*
+ *
+ * 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.
+ *
+ */
+
+package org.apache.wookie.tests.features;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+
+import org.apache.wookie.feature.Feature;
+import org.apache.wookie.feature.Features;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class FeaturesTest {
+
+ private static final String testDataFolder =
"src-tests/org/apache/wookie/tests/features/";
+ private static final String basePath = "/test";
+
+ /**
+ * Load a basic feature, consisting just of a name
+ * @throws Exception
+ */
+ @Test
+ public void loadFeature() throws Exception{
+ File featureFile = new File(testDataFolder+"feature.xml");
+ assertTrue(featureFile.exists());
+ Feature feature = Features.loadFeature(featureFile, basePath);
+ assertEquals("test:feature", feature.getName());
+ assertEquals(0, feature.stylesheets().length);
+ assertEquals(0, feature.scripts().length);
+ }
+
+ /**
+ * Load a feature with an invalid name
+ * @throws Exception
+ */
+ @Test(expected = Exception.class)
+ public void loadFeatureInvalidName() throws Exception{
+ File featureFile = new File(testDataFolder+"feature_invalid_name.xml");
+ assertTrue(featureFile.exists());
+ try {
+ @SuppressWarnings("unused")
+ Feature feature = Features.loadFeature(featureFile, basePath);
+ } catch (Exception e) {
+ assertEquals("Invalid feature: name is not a valid IRI", e.getMessage());
+ throw e;
+ }
+ }
+
+ /**
+ * Load a feature with a stylesheet
+ * @throws Exception
+ */
+ @Test
+ public void loadFeatureWithResources() throws Exception{
+ File featureFile = new File(testDataFolder+"feature_with_resources.xml");
+ assertTrue(featureFile.exists());
+ Feature feature = Features.loadFeature(featureFile, basePath);
+ assertEquals(1, feature.stylesheets().length);
+ assertEquals(0, feature.scripts().length);
+ assertEquals("/test/test_styles.css", feature.stylesheets()[0]);
+ }
+
+ /**
+ * Load a feature with scripts, both local and absolute
+ * @throws Exception
+ */
+ @Test
+ public void loadFeatureWithScripts() throws Exception{
+ File featureFile = new File(testDataFolder+"feature_with_scripts.xml");
+ assertTrue(featureFile.exists());
+ Feature feature = Features.loadFeature(featureFile, basePath);
+ assertEquals(0, feature.stylesheets().length);
+ assertEquals(2, feature.scripts().length);
+ assertEquals("/test/test_script.js", feature.scripts()[0]);
+ assertEquals("/absolute_path_to_script.js", feature.scripts()[1]);
+ }
+
+ @Test
+ public void loadFeatureWithFlattenAttribute() throws Exception{
+ File featureFile = new
File(testDataFolder+"feature_with_flatten_attr.xml");
+ assertTrue(featureFile.exists());
+ Feature feature = Features.loadFeature(featureFile, basePath);
+ assertTrue(feature.flattenOnExport());
+ }
+
+
+ /**
+ * Loads features from the test folder, ignoring everrything
+ * that isn't a folder that contains a "features.xml" file
+ */
+ @Test
+ public void loadFeatures(){
+ File featureFolder = new File(testDataFolder+"test_features_folder");
+ assertTrue(featureFolder.exists());
+ assertTrue(featureFolder.isDirectory());
+ Features.loadFeatures(featureFolder, basePath);
+ assertEquals(1, Features.getFeatures().size());
+ assertEquals(1, Features.getFeatureNames().length);
+ assertEquals("test:feature", Features.getFeatureNames()[0]);
+ assertEquals(testDataFolder+"test_features_folder/test",
Features.getFeatures().get(0).getFolder());
+ }
+}
Added:
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/feature.xml
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/feature.xml?rev=1154140&view=auto
==============================================================================
---
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/feature.xml
(added)
+++
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/feature.xml
Fri Aug 5 09:32:53 2011
@@ -0,0 +1,20 @@
+<?xml version="1.0"?>
+<!--
+ 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.
+-->
+<feature>
+ <name>test:feature</name>
+</feature>
\ No newline at end of file
Added:
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/feature_invalid_name.xml
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/feature_invalid_name.xml?rev=1154140&view=auto
==============================================================================
---
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/feature_invalid_name.xml
(added)
+++
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/feature_invalid_name.xml
Fri Aug 5 09:32:53 2011
@@ -0,0 +1,20 @@
+<?xml version="1.0"?>
+<!--
+ 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.
+-->
+<feature>
+ <name>FAIL</name>
+</feature>
\ No newline at end of file
Added:
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/feature_with_resources.xml
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/feature_with_resources.xml?rev=1154140&view=auto
==============================================================================
---
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/feature_with_resources.xml
(added)
+++
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/feature_with_resources.xml
Fri Aug 5 09:32:53 2011
@@ -0,0 +1,21 @@
+<?xml version="1.0"?>
+<!--
+ 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.
+-->
+<feature>
+ <name>test:feature</name>
+ <stylesheet src="test_styles.css"/>
+</feature>
\ No newline at end of file
Added:
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_features_folder/not_a_feature.xml
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_features_folder/not_a_feature.xml?rev=1154140&view=auto
==============================================================================
---
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_features_folder/not_a_feature.xml
(added)
+++
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_features_folder/not_a_feature.xml
Fri Aug 5 09:32:53 2011
@@ -0,0 +1,20 @@
+<?xml version="1.0"?>
+<!--
+ 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.
+-->
+<feature>
+ <name>test:feature</name>
+</feature>
\ No newline at end of file
Added:
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_features_folder/test/feature.xml
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_features_folder/test/feature.xml?rev=1154140&view=auto
==============================================================================
---
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_features_folder/test/feature.xml
(added)
+++
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_features_folder/test/feature.xml
Fri Aug 5 09:32:53 2011
@@ -0,0 +1,20 @@
+<?xml version="1.0"?>
+<!--
+ 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.
+-->
+<feature>
+ <name>test:feature</name>
+</feature>
\ No newline at end of file
Added:
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_features_folder/test_no_feature_xml/not_a_feature.xml
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_features_folder/test_no_feature_xml/not_a_feature.xml?rev=1154140&view=auto
==============================================================================
---
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_features_folder/test_no_feature_xml/not_a_feature.xml
(added)
+++
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_features_folder/test_no_feature_xml/not_a_feature.xml
Fri Aug 5 09:32:53 2011
@@ -0,0 +1,20 @@
+<?xml version="1.0"?>
+<!--
+ 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.
+-->
+<feature>
+ <name>test:feature</name>
+</feature>
\ No newline at end of file
Added:
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_styles.css
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_styles.css?rev=1154140&view=auto
==============================================================================
---
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_styles.css
(added)
+++
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/features/test_styles.css
Fri Aug 5 09:32:53 2011
@@ -0,0 +1,16 @@
+/*
+ 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.
+*/
\ No newline at end of file
Modified: incubator/wookie/trunk/src/org/apache/wookie/feature/Features.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/feature/Features.java?rev=1154140&r1=1154139&r2=1154140&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/feature/Features.java
(original)
+++ incubator/wookie/trunk/src/org/apache/wookie/feature/Features.java Fri Aug
5 09:32:53 2011
@@ -37,31 +37,28 @@ import org.jdom.input.SAXBuilder;
*
* The <name> element contains the Feature name IRI; the <script> and
<stylesheet> elements should contain a src attribute
* with the filename of the resource relative to the feature folder (e.g.
"myfile.js")
+ *
+ * For more information on developing features, see
http://incubator.apache.org/wookie/docs/developer/features.html.
*/
public class Features {
- /*
+ /**
* The current features installed
*/
private static ArrayList<IFeature> features;
static Logger _logger = Logger.getLogger(Features.class.getName());
- /*
+ /**
* The default folder name for features
*/
- public static final File DEFAULT_FEATURE_FOLDER = new File("features");
+ public static final String DEFAULT_FEATURE_FOLDER = "features";
- /*
+ /**
* The folder where deployed features live
*/
private static File featuresFolder;
- public static File getFeaturesFolder(){
- if (featuresFolder == null) return DEFAULT_FEATURE_FOLDER;
- return featuresFolder;
- }
-
/**
* Get the currently installed features
* @return a List of IFeature objects
@@ -88,32 +85,63 @@ public class Features {
*/
public static void loadFeatures(ServletContext context){
+ featuresFolder = new File(context.getRealPath(DEFAULT_FEATURE_FOLDER));
+ System.out.println(featuresFolder.list());
+
+ //
// Clear any existing installed features
+ //
features = new ArrayList<IFeature>();
+ //
// Load features from file
- loadFeatures(DEFAULT_FEATURE_FOLDER, context.getContextPath() + "/" +
DEFAULT_FEATURE_FOLDER + "/");
+ //
+ loadFeatures(featuresFolder, context.getContextPath() + "/" +
DEFAULT_FEATURE_FOLDER + "/");
}
+ /**
+ * Loads features from a specified folder
+ *
+ * @param theFeaturesFolder the folder to use for loading features
+ * @param basePath the base path to prepend to feature resources
+ */
public static void loadFeatures(File theFeaturesFolder, String basePath){
featuresFolder = theFeaturesFolder;
+ //
+ // Create a new ArrayList if it hasn't been instantiated
+ //
if (features == null) features = new ArrayList<IFeature>();
+ //
// Iterate over child folders of the /features folder
+ //
for (File folder: featuresFolder.listFiles()){
- // If the folder contains a feature.xml file, parse it and create a
Feature object
+ //
+ // If the file is a folder that contains a feature.xml file, parse it
and create a Feature object
+ //
if (folder.isDirectory()){
File featureXml = new File(folder.getPath()+"/feature.xml");
if (featureXml.exists() && featureXml.canRead()){
try {
- // Create a base path for resources using the current servlet
context and default feature folder
- String path = "/wookie/features/" + folder.getName();
+
+ //
+ // Create the feature path by prepending the feature folder with
the base path
+ //
+ String path = basePath + folder.getName();
+
+ //
// Load the feature and add it to the features collection
+ //
Feature feature = loadFeature(featureXml, path);
feature.setFolder(folder.getPath());
+
+ //
+ // Add feature to the features collection
+ //
features.add(feature);
+
_logger.info("Installed feature:"+feature.getName());
} catch (Exception e) {
_logger.error("Error installing feature:"+e.getMessage());
@@ -130,22 +158,36 @@ public class Features {
* @return an IFeature implementation
* @throws Exception
*/
- private static Feature loadFeature(File featureFile, String basePath) throws
Exception{
+ public static Feature loadFeature(File featureFile, String basePath) throws
Exception{
+ //
// Parse the XML
+ //
Document doc;
doc = new SAXBuilder().build(featureFile);
+ //
+ // Get the name of the feature
+ //
String name = doc.getRootElement().getChild("name").getText();
+
+ //
+ // Get any child <script> and <stylesheet> elements
+ //
@SuppressWarnings("unchecked")
List<Element> scriptElements = doc.getRootElement().getChildren("script");
@SuppressWarnings("unchecked")
List<Element> stylesheetElements =
doc.getRootElement().getChildren("stylesheet");
+ //
// Is the feature name a valid IRI?
+ //
if (!IRIValidator.isValidIRI(name)){
throw new Exception("Invalid feature: name is not a valid IRI");
}
+
+ //
// Construct arrays for scripts and stylesheet URLs
+ //
String[] scripts = new
String[doc.getRootElement().getChildren("script").size()];
for (int i=0;i<scriptElements.size();i++){
String src = scriptElements.get(i).getAttributeValue("src");
@@ -159,14 +201,25 @@ public class Features {
}
scripts[i] = src;
}
+
+ //
+ // Create an array of strings and populate it with
+ // the src attributes of the stylesheet elements prepended
+ // with the base path.
+ //
String[] stylesheets = new
String[doc.getRootElement().getChildren("stylesheet").size()];
for (int i=0;i<stylesheetElements.size();i++){
stylesheets[i] = basePath + "/" +
stylesheetElements.get(i).getAttributeValue("src");
}
+
+ //
// Create a Feature object and return it
+ //
Feature feature = new Feature(name, scripts, stylesheets);
- // Set the "flatten" flag if set
+ //
+ // Set the "flatten" flag if the flatten attribute is set in feature.xml
+ //
if (doc.getRootElement().getAttributeValue("flatten")!=null){
if (doc.getRootElement().getAttributeValue("flatten").equals("true")){
((Feature)feature).setFlattenOnExport(true);