Title: [1275] trunk/web/examples/trader-web/src/main/resources: JBEHAVE-180: Added resource finder.

Diff

Modified: trunk/web/examples/trader-web/src/main/java/org/jbehave/web/examples/trader/scenarios/StockExchangeSteps.java (1274 => 1275)

--- trunk/web/examples/trader-web/src/main/java/org/jbehave/web/examples/trader/scenarios/StockExchangeSteps.java	2009-09-24 17:18:02 UTC (rev 1274)
+++ trunk/web/examples/trader-web/src/main/java/org/jbehave/web/examples/trader/scenarios/StockExchangeSteps.java	2009-09-25 16:38:23 UTC (rev 1275)
@@ -1,17 +1,22 @@
 package org.jbehave.web.examples.trader.scenarios;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
 import org.jbehave.scenario.annotations.AfterScenario;
 import org.jbehave.scenario.annotations.BeforeScenario;
 import org.jbehave.scenario.annotations.Given;
+import org.jbehave.scenario.annotations.Then;
 import org.jbehave.scenario.annotations.When;
 import org.jbehave.scenario.steps.Steps;
+import org.jbehave.web.io.ResourceFinder;
 
 public class StockExchangeSteps extends Steps {
 
+	private ResourceFinder resourceFinder = new ResourceFinder("classpath:org/jbehave/web/examples/trader/scenarios");
+	
     private String stockExchange;
-
+    
     @BeforeScenario
     public void beforeStockExchangeOpens(){
     	
@@ -31,5 +36,10 @@
 	public void theStockExchangeIsOpened(){
 		assertNotNull(stockExchange);
 	}
+	
+	@Then("the stock exchanges opened are as contained in $resource")
+	public void theStockExchangesAreEqualTo(String resource){
+		assertEquals(resourceFinder.resourceAsString(resource), stockExchange);
+	}
 
 }

Added: trunk/web/examples/trader-web/src/main/java/org/jbehave/web/examples/trader/scenarios/data/STOCK-EXCHANGES.txt (0 => 1275)

--- trunk/web/examples/trader-web/src/main/java/org/jbehave/web/examples/trader/scenarios/data/STOCK-EXCHANGES.txt	                        (rev 0)
+++ trunk/web/examples/trader-web/src/main/java/org/jbehave/web/examples/trader/scenarios/data/STOCK-EXCHANGES.txt	2009-09-25 16:38:23 UTC (rev 1275)
@@ -0,0 +1 @@
+EXCH1
\ No newline at end of file

Modified: trunk/web/examples/trader-web/src/main/resources/status_alert_can_be_activated.scenario (1274 => 1275)

--- trunk/web/examples/trader-web/src/main/resources/status_alert_can_be_activated.scenario	2009-09-24 17:18:02 UTC (rev 1274)
+++ trunk/web/examples/trader-web/src/main/resources/status_alert_can_be_activated.scenario	2009-09-25 16:38:23 UTC (rev 1275)
@@ -1,5 +1,6 @@
-Given a stock exchange EXCH
+Given a stock exchange EXCH1
 When the stock exchange is opened
+Then the stock exchanges opened are as contained in data/STOCK-EXCHANGES.txt
 Given a stock of prices 0.5,1.0 and a threshold of 10.0
 When the stock is traded at 5.0
 Then the alert status should be OFF

Added: trunk/web/web-io/src/main/java/org/jbehave/web/io/ResourceFinder.java (0 => 1275)

--- trunk/web/web-io/src/main/java/org/jbehave/web/io/ResourceFinder.java	                        (rev 0)
+++ trunk/web/web-io/src/main/java/org/jbehave/web/io/ResourceFinder.java	2009-09-25 16:38:23 UTC (rev 1275)
@@ -0,0 +1,111 @@
+package org.jbehave.web.io;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+
+public class ResourceFinder {
+
+	public static final String DEFAULT_CLASSPATH_PREFIX = "classpath:";
+	private final ClassLoader classLoader;
+	private final String classpathPrefix;
+	private String rootDirectory;
+
+	public ResourceFinder(String rootDirectory) {
+		this(Thread.currentThread().getContextClassLoader(), rootDirectory,
+				DEFAULT_CLASSPATH_PREFIX);
+	}
+
+	public ResourceFinder(ClassLoader classLoader, String rootDirectory) {
+		this(classLoader, rootDirectory, DEFAULT_CLASSPATH_PREFIX);
+	}
+
+	public ResourceFinder(ClassLoader classLoader, String rootDirectory,
+			String classpathPrefix) {
+		this.classLoader = classLoader;
+		this.classpathPrefix = classpathPrefix;
+		this.rootDirectory = rootDirectory;
+	}
+
+	public String resourceAsString(String relativePath) {
+		String resourcePath = resolvePath(relativePath);
+		try {
+			try {
+				return classpathResource(resourcePath);
+			} catch (ResourceNotFoundException e) {
+				return filesystemResource(resourcePath);
+			}
+		} catch (IOException e) {
+			throw new ResourceRetrievalFailedException(resourcePath);
+		}
+
+	}
+
+	public void useRootDirectory(String rootDirectory) {
+		this.rootDirectory = rootDirectory;
+	}
+
+	private String classpathResource(String resourcePath) throws IOException {
+		InputStream inputStream = classLoader.getResourceAsStream(resourcePath);
+		if (inputStream != null) {
+			return IOUtils.toString(inputStream);
+		}
+		throw new ResourceNotFoundException(resourcePath, classLoader);
+	}
+
+	private String filesystemResource(String resourcePath) throws IOException {
+		File file = new File(resourcePath);
+		if (file.exists()) {
+			return FileUtils.readFileToString(file);
+		}
+		throw new ResourceNotFoundException(resourcePath);
+	}
+
+	private String resolvePath(String relativePath) {
+		String resourcePath;
+		if (rootDirectory.startsWith(classpathPrefix)) {
+			resourcePath = resourcePath(stripPrefix(rootDirectory,
+					classpathPrefix), relativePath);
+		} else {
+			resourcePath = resourcePath(rootDirectory, relativePath);
+
+		}
+		return resourcePath;
+	}
+
+	private String resourcePath(String rootDirectory, String relativePath) {
+		return rootDirectory + "/" + relativePath;
+	}
+
+	private String stripPrefix(String path, String prefix) {
+		return path.substring(prefix.length());
+	}
+
+	@SuppressWarnings("serial")
+	public static class ResourceNotFoundException extends RuntimeException {
+
+		public ResourceNotFoundException(String resourcePath) {
+			super("Resource " + resourcePath + " not found");
+		}
+
+		public ResourceNotFoundException(String resourcePath,
+				ClassLoader classLoader) {
+			super("Resource " + resourcePath + " not found in classLoader "
+					+ classLoader);
+		}
+
+	}
+
+	@SuppressWarnings("serial")
+	public static class ResourceRetrievalFailedException extends
+			RuntimeException {
+
+		public ResourceRetrievalFailedException(String resourcePath) {
+			super("Failed to retrieve resource " + resourcePath);
+		}
+
+	}
+}

Added: trunk/web/web-io/src/test/java/org/jbehave/web/io/ResourceFinderTest.java (0 => 1275)

--- trunk/web/web-io/src/test/java/org/jbehave/web/io/ResourceFinderTest.java	                        (rev 0)
+++ trunk/web/web-io/src/test/java/org/jbehave/web/io/ResourceFinderTest.java	2009-09-25 16:38:23 UTC (rev 1275)
@@ -0,0 +1,53 @@
+package org.jbehave.web.io;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+
+import org.jbehave.web.io.ResourceFinder.ResourceNotFoundException;
+import org.junit.Test;
+
+public class ResourceFinderTest {
+
+	@Test
+	public void canFindResourceInClasspath() throws IOException {
+		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+		String rootDirectory = "classpath:org/jbehave/web";
+		ResourceFinder finder = new ResourceFinder(classLoader, rootDirectory);
+		assertEquals("A test resource", finder.resourceAsString("io/resource.txt"));
+	}
+	
+	@Test
+	public void canChangeRootDirectory() throws IOException {
+		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+		String rootDirectory = "classpath:org/jbehave/web";
+		ResourceFinder finder = new ResourceFinder(classLoader, rootDirectory);
+		finder.useRootDirectory("classpath:org/jbehave/web/io");
+		assertEquals("A test resource", finder.resourceAsString("resource.txt"));
+	}
+	
+	@Test
+	public void canFindResourceInFilesystem() throws IOException {
+		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+		String rootDirectory = "src/test/java/org/jbehave/web";
+		ResourceFinder finder = new ResourceFinder(classLoader, rootDirectory);
+		assertEquals("A test resource", finder.resourceAsString("io/resource.txt"));
+	}
+
+	@Test(expected=ResourceNotFoundException.class)
+	public void cannotFindInexistenResourceInClasspath() throws IOException {
+		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+		String rootDirectory = "classpath:inexistent";
+		ResourceFinder finder = new ResourceFinder(classLoader, rootDirectory);
+		finder.resourceAsString("resource.txt");
+	}
+
+	@Test(expected=ResourceNotFoundException.class)
+	public void cannotFindInexistenResourceInFileSystem() throws IOException {
+		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+		String rootDirectory = "/inexistent";
+		ResourceFinder finder = new ResourceFinder(classLoader, rootDirectory);
+		finder.resourceAsString("resource.txt");
+	}
+
+}
\ No newline at end of file

Added: trunk/web/web-io/src/test/java/org/jbehave/web/io/resource.txt (0 => 1275)

--- trunk/web/web-io/src/test/java/org/jbehave/web/io/resource.txt	                        (rev 0)
+++ trunk/web/web-io/src/test/java/org/jbehave/web/io/resource.txt	2009-09-25 16:38:23 UTC (rev 1275)
@@ -0,0 +1 @@
+A test resource
\ No newline at end of file


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to