You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@creadur.apache.org by rd...@apache.org on 2012/10/28 12:51:05 UTC

svn commit: r1402981 - in /creadur/whisker/trunk: apache-whisker-app/src/main/java/org/apache/creadur/whisker/app/load/ apache-whisker-app/src/test/java/org/ apache-whisker-app/src/test/java/org/apache/ apache-whisker-app/src/test/java/org/apache/cread...

Author: rdonkin
Date: Sun Oct 28 11:51:04 2012
New Revision: 1402981

URL: http://svn.apache.org/viewvc?rev=1402981&view=rev
Log:
WHISKER-4 when a resource isn't found on the classpath, try the file system.

Added:
    creadur/whisker/trunk/apache-whisker-app/src/test/java/org/
    creadur/whisker/trunk/apache-whisker-app/src/test/java/org/apache/
    creadur/whisker/trunk/apache-whisker-app/src/test/java/org/apache/creadur/
    creadur/whisker/trunk/apache-whisker-app/src/test/java/org/apache/creadur/whisker/
    creadur/whisker/trunk/apache-whisker-app/src/test/java/org/apache/creadur/whisker/app/
    creadur/whisker/trunk/apache-whisker-app/src/test/java/org/apache/creadur/whisker/app/load/
    creadur/whisker/trunk/apache-whisker-app/src/test/java/org/apache/creadur/whisker/app/load/TestStreamableResourceFactory.java   (with props)
Modified:
    creadur/whisker/trunk/apache-whisker-app/src/main/java/org/apache/creadur/whisker/app/load/StreamableClassPathResource.java
    creadur/whisker/trunk/apache-whisker-app/src/main/java/org/apache/creadur/whisker/app/load/StreamableResourceFactory.java
    creadur/whisker/trunk/apache-whisker-cli/src/main/java/org/apache/creadur/whisker/cli/Main.java
    creadur/whisker/trunk/apache-whisker-cli/src/test/java/org/apache/creadur/whisker/cli/TestCommandParsing.java

Modified: creadur/whisker/trunk/apache-whisker-app/src/main/java/org/apache/creadur/whisker/app/load/StreamableClassPathResource.java
URL: http://svn.apache.org/viewvc/creadur/whisker/trunk/apache-whisker-app/src/main/java/org/apache/creadur/whisker/app/load/StreamableClassPathResource.java?rev=1402981&r1=1402980&r2=1402981&view=diff
==============================================================================
--- creadur/whisker/trunk/apache-whisker-app/src/main/java/org/apache/creadur/whisker/app/load/StreamableClassPathResource.java (original)
+++ creadur/whisker/trunk/apache-whisker-app/src/main/java/org/apache/creadur/whisker/app/load/StreamableClassPathResource.java Sun Oct 28 11:51:04 2012
@@ -55,7 +55,18 @@ public final class StreamableClassPathRe
         return name;
     }
 
-
+    /**
+     * Is this resource found on the classpath?
+     * @return true when the resource is found on the classpath,
+     * null otherwise
+     */
+    public boolean exists()  {
+        try {
+            return open() != null;
+        } catch (IOException e) {
+            return false;
+        }
+    }
 
     /**
      * Opens a resource on the classpath.

Modified: creadur/whisker/trunk/apache-whisker-app/src/main/java/org/apache/creadur/whisker/app/load/StreamableResourceFactory.java
URL: http://svn.apache.org/viewvc/creadur/whisker/trunk/apache-whisker-app/src/main/java/org/apache/creadur/whisker/app/load/StreamableResourceFactory.java?rev=1402981&r1=1402980&r2=1402981&view=diff
==============================================================================
--- creadur/whisker/trunk/apache-whisker-app/src/main/java/org/apache/creadur/whisker/app/load/StreamableResourceFactory.java (original)
+++ creadur/whisker/trunk/apache-whisker-app/src/main/java/org/apache/creadur/whisker/app/load/StreamableResourceFactory.java Sun Oct 28 11:51:04 2012
@@ -59,4 +59,21 @@ public final class StreamableResourceFac
     public StreamableResource streamFromFileResource(final File file) {
         return new StreamableFileResource(file);
     }
+
+    /**
+     * When the resource is found on the classpath,
+     * builds an instance that streams, on demand,
+     * from the classpath. Otherwise, builds an
+     * instances that streams from the file system.
+     * @param resourceName
+     * @return not null
+     */
+    public StreamableResource streamFromResource(final String resourceName) {
+        final StreamableClassPathResource streamFromClasspath =
+                new StreamableClassPathResource(resourceName);
+        if (streamFromClasspath.exists()) {
+            return streamFromClasspath;
+        }
+        return streamFromFileResource(resourceName);
+    }
 }

Added: creadur/whisker/trunk/apache-whisker-app/src/test/java/org/apache/creadur/whisker/app/load/TestStreamableResourceFactory.java
URL: http://svn.apache.org/viewvc/creadur/whisker/trunk/apache-whisker-app/src/test/java/org/apache/creadur/whisker/app/load/TestStreamableResourceFactory.java?rev=1402981&view=auto
==============================================================================
--- creadur/whisker/trunk/apache-whisker-app/src/test/java/org/apache/creadur/whisker/app/load/TestStreamableResourceFactory.java (added)
+++ creadur/whisker/trunk/apache-whisker-app/src/test/java/org/apache/creadur/whisker/app/load/TestStreamableResourceFactory.java Sun Oct 28 11:51:04 2012
@@ -0,0 +1,43 @@
+/**
+ * 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.creadur.whisker.app.load;
+
+import java.io.InputStream;
+
+import org.apache.creadur.whisker.app.StreamableResource;
+
+import junit.framework.TestCase;
+
+public class TestStreamableResourceFactory extends TestCase {
+
+    StreamableResourceFactory subject;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        subject = new StreamableResourceFactory();
+    }
+
+    public void testWhenResourceExistsOnClasspathThisResourceShouldBeStreamed() throws Exception {
+        StreamableResource streamableResource = subject.streamFromResource("junit/framework/TestCase.class");
+        assertNotNull(streamableResource);
+        InputStream in = streamableResource.open();
+        assertFalse(in.read() == -1);
+    }
+}

Propchange: creadur/whisker/trunk/apache-whisker-app/src/test/java/org/apache/creadur/whisker/app/load/TestStreamableResourceFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: creadur/whisker/trunk/apache-whisker-cli/src/main/java/org/apache/creadur/whisker/cli/Main.java
URL: http://svn.apache.org/viewvc/creadur/whisker/trunk/apache-whisker-cli/src/main/java/org/apache/creadur/whisker/cli/Main.java?rev=1402981&r1=1402980&r2=1402981&view=diff
==============================================================================
--- creadur/whisker/trunk/apache-whisker-cli/src/main/java/org/apache/creadur/whisker/cli/Main.java (original)
+++ creadur/whisker/trunk/apache-whisker-cli/src/main/java/org/apache/creadur/whisker/cli/Main.java Sun Oct 28 11:51:04 2012
@@ -137,9 +137,8 @@ public final class Main {
         whisker.setEngine(new VelocityEngine(new SystemLog()));
         whisker.setSource(CommandLineOption.SOURCE.getOptionValue(commandLine));
         whisker.setLicenseDescriptor(
-                new StreamableResourceFactory().streamFromClassPathResource(
-                        CommandLineOption.LICENSE_DESCRIPTION
-                            .getOptionValue(commandLine)));
+                new StreamableResourceFactory().streamFromResource(
+                        licenseDescriptorName(commandLine)));
         whisker.setWriterFactory(new WriteResultsToSystemOutFactory());
         if (CommandLineOption.ACT_TO_AUDIT.isSetOn(commandLine)) {
             whisker.setAct(Act.AUDIT);
@@ -159,6 +158,17 @@ public final class Main {
     }
 
     /**
+     * Extracts the license descriptor name value,
+     * @param commandLine not null
+     * @return the value for the license descriptor name
+     * passed from the command line
+     */
+    private String licenseDescriptorName(final CommandLine commandLine) {
+        return CommandLineOption.LICENSE_DESCRIPTION
+            .getOptionValue(commandLine);
+    }
+
+    /**
      * Runs Whisker.
      * @param args not null
      * @return system return code

Modified: creadur/whisker/trunk/apache-whisker-cli/src/test/java/org/apache/creadur/whisker/cli/TestCommandParsing.java
URL: http://svn.apache.org/viewvc/creadur/whisker/trunk/apache-whisker-cli/src/test/java/org/apache/creadur/whisker/cli/TestCommandParsing.java?rev=1402981&r1=1402980&r2=1402981&view=diff
==============================================================================
--- creadur/whisker/trunk/apache-whisker-cli/src/test/java/org/apache/creadur/whisker/cli/TestCommandParsing.java (original)
+++ creadur/whisker/trunk/apache-whisker-cli/src/test/java/org/apache/creadur/whisker/cli/TestCommandParsing.java Sun Oct 28 11:51:04 2012
@@ -23,21 +23,14 @@ import junit.framework.TestCase;
 import org.apache.commons.cli.AlreadySelectedException;
 import org.apache.commons.cli.ParseException;
 import org.apache.creadur.whisker.app.Act;
+import org.apache.creadur.whisker.app.StreamableResource;
 import org.apache.creadur.whisker.app.Whisker;
 import org.apache.creadur.whisker.app.load.StreamableClassPathResource;
+import org.apache.creadur.whisker.app.load.StreamableFileNameResource;
 
-/**
- *
- */
 public class TestCommandParsing extends TestCase {
 
-    /**
-     *
-     */
     private static final String LONG_OPT = "--";
-    /**
-     *
-     */
     private static final String SHORT_OPT = "-";
     private Main subject;
 
@@ -204,10 +197,20 @@ public class TestCommandParsing extends 
      */
     private void exerciseLicenseDescriptor(String aPath, String arg)
             throws ParseException {
-        assertEquals("License descriptor arg should set property on Whisker", aPath,
-                ((StreamableClassPathResource)subject.configure(
+        final StreamableResource streamableResource = subject.configure(
                         args(arg, aPath, longOpt(CommandLineOption.ACT_TO_GENERATE.getLongName())))
-                            .getLicenseDescriptor()).getName());
+                            .getLicenseDescriptor();
+
+        assertEquals("License descriptor arg should set property on Whisker", aPath,
+                name(streamableResource));
+    }
+
+    private String name(final StreamableResource streamableResource) {
+        if (streamableResource instanceof StreamableClassPathResource) {
+            return ((StreamableClassPathResource)streamableResource).getName();
+        } else {
+            return ((StreamableFileNameResource)streamableResource).getFileName();
+        }
     }
 
     private String[] args(String ...strings) {