| 1 |
package org.astrogrid.samp; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.util.Random; |
| 5 |
import org.astrogrid.samp.client.ClientProfile; |
| 6 |
import org.astrogrid.samp.client.HubConnection; |
| 7 |
import org.astrogrid.samp.client.SampException; |
| 8 |
import org.astrogrid.samp.hub.BasicHubService; |
| 9 |
import org.astrogrid.samp.hub.Hub; |
| 10 |
import org.astrogrid.samp.hub.HubProfile; |
| 11 |
import org.astrogrid.samp.hub.HubService; |
| 12 |
import org.astrogrid.samp.hub.MessageRestriction; |
| 13 |
import org.astrogrid.samp.hub.ProfileToken; |
| 14 |
import org.astrogrid.samp.web.WebTestProfile; |
| 15 |
import org.astrogrid.samp.xmlrpc.StandardTestProfile; |
| 16 |
import org.astrogrid.samp.xmlrpc.SampXmlRpcClientFactory; |
| 17 |
import org.astrogrid.samp.xmlrpc.SampXmlRpcServerFactory; |
| 18 |
import org.astrogrid.samp.xmlrpc.XmlRpcKit; |
| 19 |
|
| 20 |
/** |
| 21 |
* Abstract class providing both a hub and a client profile; |
| 22 |
* the client profile talks to the hub. |
| 23 |
* This can be used for isolated testing of hub/client profile pairs. |
| 24 |
* For use within a test framework implementations should normally take |
| 25 |
* care not to use the resources (well-known lockfile locations, |
| 26 |
* well-known ports etc) which are commonly used for normal SAMP operations. |
| 27 |
* |
| 28 |
* @author Mark Taylor |
| 29 |
* @since 4 Feb 2011 |
| 30 |
*/ |
| 31 |
public abstract class TestProfile implements ClientProfile { |
| 32 |
|
| 33 |
private final Random random_; |
| 34 |
private final ProfileToken directToken_; |
| 35 |
private Hub hub_; |
| 36 |
private HubService service_; |
| 37 |
|
| 38 |
protected TestProfile( Random random ) { |
| 39 |
random_ = random; |
| 40 |
directToken_ = new ProfileToken() { |
| 41 |
public String getProfileName() { |
| 42 |
return "<direct>"; |
| 43 |
} |
| 44 |
public MessageRestriction getMessageRestriction() { |
| 45 |
return null; |
| 46 |
} |
| 47 |
}; |
| 48 |
} |
| 49 |
|
| 50 |
public synchronized void startHub() throws IOException { |
| 51 |
if ( hub_ != null ) { |
| 52 |
throw new IllegalStateException( "Hub not stopped " |
| 53 |
+ "due to earlier test failure?" ); |
| 54 |
} |
| 55 |
service_ = new BasicHubService( createRandom() ); |
| 56 |
hub_ = new Hub( service_ ); |
| 57 |
service_.start(); |
| 58 |
hub_.startProfile( createHubProfile() ); |
| 59 |
} |
| 60 |
|
| 61 |
public synchronized void stopHub() { |
| 62 |
if ( hub_ == null ) { |
| 63 |
throw new IllegalStateException(); |
| 64 |
} |
| 65 |
hub_.shutdown(); |
| 66 |
hub_ = null; |
| 67 |
service_ = null; |
| 68 |
} |
| 69 |
|
| 70 |
public HubConnection registerDirect() throws SampException { |
| 71 |
return service_.register( directToken_ ); |
| 72 |
} |
| 73 |
|
| 74 |
public abstract HubProfile createHubProfile() throws IOException; |
| 75 |
|
| 76 |
public Random createRandom() { |
| 77 |
return new Random( random_.nextLong() ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Returns an array of TestProfiles suitable for performing tests on. |
| 82 |
*/ |
| 83 |
public static TestProfile[] createTestProfiles( Random random ) |
| 84 |
throws IOException { |
| 85 |
SampXmlRpcClientFactory aClient = |
| 86 |
XmlRpcKit.APACHE.getClientFactory(); |
| 87 |
SampXmlRpcServerFactory aServ = |
| 88 |
XmlRpcKit.APACHE.getServerFactory(); |
| 89 |
SampXmlRpcClientFactory iClient = |
| 90 |
XmlRpcKit.INTERNAL.getClientFactory(); |
| 91 |
SampXmlRpcServerFactory iServ = |
| 92 |
XmlRpcKit.INTERNAL.getServerFactory(); |
| 93 |
return new TestProfile[] { |
| 94 |
new StandardTestProfile( random, aClient, aServ, iClient, iServ ), |
| 95 |
new StandardTestProfile( random, iClient, iServ, aClient, aServ ), |
| 96 |
new StandardTestProfile( random, iClient, iServ, iClient, iServ ), |
| 97 |
new WebTestProfile( random, true, null ), |
| 98 |
}; |
| 99 |
} |
| 100 |
} |