Skip to content
This repository has been archived by the owner on Mar 5, 2024. It is now read-only.

Commit

Permalink
I really needed a date test
Browse files Browse the repository at this point in the history
  • Loading branch information
globalworming committed Jun 10, 2014
1 parent 626528a commit ed31a19
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 49 deletions.
7 changes: 7 additions & 0 deletions sahi-core/src/main/java/net/sf/sahi/session/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ public static synchronized Session getInstance(final String sessionId) {
return session;
}

public static synchronized boolean hasInstance(final String sessionId) {
if (sessions.containsKey(sessionId)) {
return true;
}
return false;
}

public static synchronized Session getExistingInstance(final String sessionId) {
return sessions.get(sessionId);
}
Expand Down
17 changes: 17 additions & 0 deletions sahi-test-webapp/src/main/sahi/integration/date.sah
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
_include("pages/includeAll.js");

$Index.goTo();

_assertEqual(1266015600000, new Date(2010,1,13).getTime());
_assertEqual(1263337200000, new Date("2010,1,13").getTime());
_assertEqual(1263337200000, new Date(["2010","1","13"]).getTime());
_assertEqual(1263337200000, new Date([2010,1,13]).getTime());
_assertTrue(1266015600000 <= new Date(2010,1,13).getTime());

var $date1=new Date(2010,1,13);

var $dateText=" 02.02.2010 - 13.01.2010".split("-")[1].split(".");
$dateText = $dateText.reverse();
var $date2= new Date($dateText[0],$dateText[1],$dateText[2]);
_assertTrue($date1.getTime()<=$date2.getTime());

Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.headissue.sahi.integration;

import com.headissue.demopages.DemoPageServer;
import net.sf.sahi.Proxy;
import net.sf.sahi.command.Suite;
import net.sf.sahi.config.Configuration;
import net.sf.sahi.report.HtmlReporter;
import net.sf.sahi.report.JunitReporter;
import net.sf.sahi.session.Session;
import net.sf.sahi.session.Status;
import net.sf.sahi.test.SahiTestSuite;
import net.sf.sahi.util.BrowserType;
import net.sf.sahi.util.BrowserTypesLoader;
import net.sf.sahi.util.Utils;
import org.junit.After;
import org.junit.Before;

import java.net.URISyntaxException;
import java.util.Date;

import static org.junit.Assert.assertEquals;

/**
* Created by wormi on 10/06/14.
*/
public class SahiIntegrationTest {

final protected Thread server = new Thread(new DemoPageServer());
final protected Proxy proxy = new Proxy();
final protected String base = "http://localhost:7733";
final protected boolean isSingleSession = true;
final protected int threads = 5;

protected Session session;
protected BrowserType browserType;
protected String userdata;
protected BrowserTypesLoader browserLoader;

private void createSession() {
// more random, no while loop
String sessionId = random();
if (Session.hasInstance(sessionId)) sessionId = random();
session=Session.getInstance(sessionId);
}

private static String random() {
double d= Math.random();
String number = String.valueOf(d);
number = number.substring(number.indexOf( "." ) + 1);
return number;
}

;

private void addCertToFirefox(String userdata) {
try {
Utils.executeCommand(Utils.getCommandTokens("certutil -A -n Sahi_Root -t \"C,,\" -i " + userdata + "/certs/ca.crt -d " + userdata + "/browser/ff/profiles/sahi0"));
} catch (Exception e) {
throw new RuntimeException(e);
}
}

protected void runSuite(String suitePath) {
createSession();
SahiTestSuite suite = new Suite().prepareSuite(suitePath, base, browserType.path(), session.id(), browserType.options(), browserType.processName(), ("" + threads), browserType.useSystemProxy(), isSingleSession, null);
Date d = new Date();
suite.addReporter(new JunitReporter("./target/junitReporter/"));
suite.addReporter(new HtmlReporter("./target/htmlReporter/"));
suite.loadScripts();
suite.run();
// to make sure the status is set
suite.finishCallBack();
assertEquals(Status.SUCCESS, session.getStatus());
}

protected String getSuitePath(String resource) {
String suitePath;
try {
suitePath = this.getClass().getResource(resource).toURI().getPath();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
return suitePath;
}



@Before
public void setup() throws Exception {
Configuration.init("../sahi-core", "./userdata");
browserLoader = new BrowserTypesLoader();
browserType = browserLoader.getBrowserType("firefox");
userdata = Configuration.getUserDataDir();
proxy.start(true);
server.start();
addCertToFirefox(userdata);
}

@After
public void teardown() throws Exception {
server.interrupt();
proxy.stop();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.sf.sahi.util.Utils;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.net.URISyntaxException;
Expand All @@ -25,58 +26,19 @@
* Created by wormi on 25.04.14.
*/

public class TestApiIntegration {

Thread server = new Thread(new DemoPageServer());
Proxy proxy = new Proxy();

@Before
public void setup() throws Exception {
Configuration.init("../sahi-core", "./userdata");
proxy.start(true);
server.start();

}
public class TestApiIntegration extends SahiIntegrationTest {

@Test
public void runTestSuite(){
SahiTestSuite suite = null;
BrowserTypesLoader browserLoader = new BrowserTypesLoader();

String userdata = Configuration.getUserDataDir();

try {
Utils.executeCommand(Utils.getCommandTokens("certutil -A -n Sahi_Root -t \"C,,\" -i "+userdata+"/certs/ca.crt -d "+ userdata +"/browser/ff/profiles/sahi0"));
} catch (Exception e) {
throw new RuntimeException(e);
}
BrowserType browserType = browserLoader.getBrowserType("firefox");
Session session = Session.getInstance("1");
String suitePath = null;
try {
suitePath = this.getClass().getResource("/integration").toURI().getPath();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
String base = "http://localhost:7733";
boolean isSingleSession = true;
final int threads = 1;
// launches browser with pre configured browser settings
suite = new Suite().prepareSuite(suitePath, base, browserType.path(), session.id(), browserType.options(), browserType.processName(), ("" + threads), browserType.useSystemProxy(), isSingleSession, null);
Date d = new Date();
suite.addReporter(new JunitReporter("./target/junitReporter/"));
suite.addReporter(new HtmlReporter("./target/htmlReporter/"));
suite.loadScripts();
suite.run();
// to make sure the status is set
suite.finishCallBack();
assertEquals(Status.SUCCESS, session.getStatus());
public void allIntegrationTests(){
String suitePath = getSuitePath("/integration");
runSuite(suitePath);
}

@After
public void teardown() throws Exception {
server.interrupt();
proxy.stop();
/* just for dev use /
@Test
public void singletest() {
String suitePath = getSuitePath("/integration/date.sah");
runSuite(suitePath);
}

//*/
}

0 comments on commit ed31a19

Please sign in to comment.