Anonymous avatar Anonymous committed 1b78176

Adding the ability to get a snippet of the code that caused the error from the XworkException,
adding unit tests to prove it works

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@996 e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (4)

src/java/com/opensymphony/xwork/XworkException.java

 
 import java.io.PrintStream;
 import java.io.PrintWriter;
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+import java.util.List;
+import java.util.ArrayList;
+
+import java.net.URL;
 
 import com.opensymphony.xwork.util.location.Locatable;
 import com.opensymphony.xwork.util.location.Location;
 import com.opensymphony.xwork.util.location.LocationUtils;
 
+import com.opensymphony.util.FileManager;
+
+
+
 
 /**
  * A generic runtime exception that optionally contains Location information 
     public Location getLocation() {
         return this.location;
     }
+    
+    
+    /**
+     *  Gets a source code snippet with the default padding
+     */
+    public List getSnippet() {
+        return getSnippet(2);
+    }
+    
+    
+    /**
+     *  Gets a source code snippet with the default padding
+     *
+     * @param padding The amount of lines before and after the error to include
+     */
+    public List getSnippet(int padding) {
+        List snippet = new ArrayList();
+        if (location != null && location.getLineNumber() > 0) {
+            try {
+                InputStream in = new URL(location.getURI()).openStream();
+                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
+                
+                int lineno = 0;
+                int errno = location.getLineNumber();
+                String line;
+                while ((line = reader.readLine()) != null) {
+                    lineno++;
+                    if (lineno >= errno - padding && lineno <= errno + padding) {
+                        snippet.add(line);
+                    }
+                }
+            } catch (Exception ex) {
+                // ignoring as snippet not available isn't a big deal
+            }
+        }
+        return snippet;
+    }
+            
 
     /**
      * Returns a short description of this throwable object, including the 

src/java/com/opensymphony/xwork/util/location/LocationUtils.java

      * @return the object's location, or {@link Location#UNKNOWN} if no location could be found
      */
     public static Location getLocation(Object obj, String description) {
+        if (obj instanceof Location) {
+            return (Location) obj;
+        }
+        
         if (obj instanceof Locatable) {
             return ((Locatable)obj).getLocation();
         }

src/test/com/opensymphony/xwork/XworkExceptionTest.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork;
+
+import com.opensymphony.xwork.util.location.*;
+import com.opensymphony.util.ClassLoaderUtil;
+import junit.framework.TestCase;
+
+import java.util.List;
+import java.net.URL;
+
+/**
+ * XworkException tests
+ */
+public class XworkExceptionTest extends TestCase {
+
+    public void testGetSnippet() throws Exception {
+        URL url = ClassLoaderUtil.getResource("com/opensymphony/xwork/somefile.txt", getClass());
+        Location loc = new LocationImpl("foo", url.toString(), 3, 2);
+        XworkException ex = new XworkException("Some message", loc);
+        
+        List snippet = ex.getSnippet(1);
+        assertNotNull(snippet);
+        assertTrue("Wrong length: "+snippet.size(), 3 == snippet.size());
+        
+        assertTrue("is".equals(snippet.get(0)));
+        assertTrue("a".equals(snippet.get(1)));
+        assertTrue("file".equals(snippet.get(2)));
+    }
+    
+    public void testGetSnippetNoPadding() throws Exception {
+        URL url = ClassLoaderUtil.getResource("com/opensymphony/xwork/somefile.txt", getClass());
+        Location loc = new LocationImpl("foo", url.toString(), 3, 2);
+        XworkException ex = new XworkException("Some message", loc);
+        
+        List snippet = ex.getSnippet(0);
+        assertNotNull(snippet);
+        assertTrue("Wrong length: "+snippet.size(), 1 == snippet.size());
+        
+        assertTrue("a".equals(snippet.get(0)));
+    }
+}

src/test/com/opensymphony/xwork/somefile.txt

+this
+is
+a
+file
+of
+great
+import
+or
+something
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.