SDA SE Wiki

Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering

User Tools

Site Tools


Code Snippets

SaxBuilder

How to resolve the “Server returned HTTP response code: 503 for URL: http://www.w3.org/TR/xhtml1/DTD/…” when reading a HTML-Page containing a DOCTYPE-Definition. Example is using the SAXBuilder and getting the page “news.html” from the directory “test/testData” for Project “Op4Mo”:

import edu.bonn.cs.sems.help.EntityResolverImpl;
...
	SAXBuilder builder = new SAXBuilder();
	builder.setEntityResolver(new EntityResolverImpl());
	
	File inputFile =  new File(TestData.getTestDataDir("Op4Mo"),"news.html");
	
	Document inputDocument = builder.build(inputFile);

Jena

Creating a Jena-Model with a postgres-database backend


String className = "org.postgresql.Driver";         // path of driver class
Class.forName (className);                          // Load the Driver
String DB_URL =     "jdbc:postgresql://localhost:5432/jena";  // URL of database 
String DB_USER =   "jena";                          // database user id
String DB_PASSWD = "jena";                          // database password
String DB =        "PostgreSQL";                         // database type

// Create database connection
IDBConnection conn = new DBConnection ( DB_URL, DB_USER, DB_PASSWD, DB );
ModelMaker maker = ModelFactory.createModelRDBMaker(conn) ;

// create or open the default model
Model model = maker.createDefaultModel(); 

JavaScript

Getting the XPath element:

<code javascript>

function getXPath(node) {

var path = getXPathArray(node);
if (! path || (path.length == 0))
	return '';
var result = path.shift();
var next;
while (next = path.shift())
	result = result + "/" + next;
return result;

}

function getXPathArray(node, path) {

  path = path || [];
  if(node.parentNode) {
    path = getXPathArray(node.parentNode, path);
  }
  if(node.previousSibling) {
    var count = 1;
    var sibling = node.previousSibling
    do {
      if(sibling.nodeType == 1 && sibling.nodeName == node.nodeName) {count++;}
      sibling = sibling.previousSibling;
    } while(sibling);
    if(count == 1) {count = null;}
  } else if(node.nextSibling) {
    var sibling = node.nextSibling;
    do {
      if(sibling.nodeType == 1 && sibling.nodeName == node.nodeName) {
        var count = 1;
        sibling = null;
      } else {
        var count = null;
        sibling = sibling.previousSibling;
      }
    } while(sibling);
  }
  if(node.nodeType == 1) {
    path.push(node.nodeName.toLowerCase() + (node.id ? "[@id='"+node.id+"']" : count > 0 ? "["+count+"]" : ''));
  }
  return path;

}; </Code>

To convert this XPath into an simplified XPath-Expression used with SemS Subjects, see edu.bonn.cs.sems.analysis.helper.LogHelper.simplifyXPath(String) in the SemSAnalyzer project.

EasyMock

How to test using EasyMock

//create mock object for ContextManager and add the getContext method to be mocked
ContextManager mockContextManager = createMockBuilder(ContextManager.class)
   .addMockedMethod("getContext").createMock();

//create mock object for the Context object and add the getValue method to be mocked
Context mockContext=
   createMockBuilder(ContextImpl.class)
   .addMockedMethod("getValue",String.class).createMock();

//set the expectation for the mockcontextManager and the return value
expect(mockContextManager.getContext()).andReturn(mockContext);	

//set the expectation for the mockcontextManager and the return value
expect(mockContext.getValue(Context.SENSOR_BROWSER_MOBILITY)).
   andReturn("cell");

//swith the mock objects to replay state
replay(mockContextManager);
replay(mockContext);

Assert.assertTrue("the return is false",filter.createJDomHandler(stringBufOS,null,null,null,"utf-8",mockContextManager ).processJDOM(doc));
//verify that the specified behavior has been used
verify(mockContextManager);
verify(mockContext);
System.out.println("test"+buf.toString());

EasyMock 2

EasyMock from the Project Code

@Test
public void testConnectDatabaseMenuItem() throws Exception {

  systemUnderTest = createMockBuilder(MainViewControllerImpl.class)
	.addMockedMethod("showDatabaseDialog").createMock();
	
  MenuItem item = createMock(MenuItem.class);
  Capture<SelectionListener> c = new Capture<SelectionListener>();

  item.addSelectionListener(and(isA(SelectionListener.class),capture(c)));
  // Test if selection listener is added to menu item
  replay(item);
  systemUnderTest.setMenuItemConntectDatabase(item);
  verify(item);
	
  // Test if view is closed upon selection
  reset(systemUnderTest);
  systemUnderTest.showDatabaseDialog();
  replay(systemUnderTest);
  c.getValue().widgetSelected(null);
  verify(systemUnderTest);
}
teaching/labs/xp/2010b/snippets.txt · Last modified: 2018/05/09 01:59 (external edit)

SEWiki, © 2024