| 1 |
package org.astrogrid.samp.xmlrpc; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.IOException; |
| 5 |
import java.net.URL; |
| 6 |
import java.util.Random; |
| 7 |
import java.util.logging.Level; |
| 8 |
import java.util.logging.Logger; |
| 9 |
import junit.framework.TestCase; |
| 10 |
import org.astrogrid.samp.SampUtils; |
| 11 |
import org.astrogrid.samp.client.ClientProfile; |
| 12 |
import org.astrogrid.samp.client.HubConnection; |
| 13 |
import org.astrogrid.samp.client.SampException; |
| 14 |
import org.astrogrid.samp.hub.BasicHubService; |
| 15 |
import org.astrogrid.samp.hub.HubService; |
| 16 |
import org.astrogrid.samp.hub.MessageRestriction; |
| 17 |
import org.astrogrid.samp.hub.ProfileToken; |
| 18 |
|
| 19 |
public class StandardHubProfileTest extends TestCase { |
| 20 |
|
| 21 |
private static final ProfileToken TEST_PROFILE = new ProfileToken() { |
| 22 |
public String getProfileName() { |
| 23 |
return "StandardTest"; |
| 24 |
} |
| 25 |
public MessageRestriction getMessageRestriction() { |
| 26 |
return null; |
| 27 |
} |
| 28 |
}; |
| 29 |
|
| 30 |
public StandardHubProfileTest() { |
| 31 |
Logger.getLogger( "org.astrogrid.samp" ).setLevel( Level.WARNING ); |
| 32 |
Logger.getLogger( org.astrogrid.samp.httpd.HttpServer.class.getName() ) |
| 33 |
.setLevel( Level.SEVERE ); |
| 34 |
Logger.getLogger( StandardHubProfile.class.getName() ) |
| 35 |
.setLevel( Level.SEVERE ); |
| 36 |
} |
| 37 |
|
| 38 |
public void testRunHub() throws IOException { |
| 39 |
File tmpfile = File.createTempFile( "tmp", ".samp" ); |
| 40 |
assertTrue( tmpfile.delete() ); |
| 41 |
runHub( tmpfile ); |
| 42 |
runHub( null ); |
| 43 |
} |
| 44 |
|
| 45 |
private void runHub( File lockfile ) throws IOException { |
| 46 |
final String secret = "it's-a-secret"; |
| 47 |
XmlRpcKit xmlrpc = XmlRpcKit.INTERNAL; |
| 48 |
StandardHubProfile hubProf = |
| 49 |
new StandardHubProfile( xmlrpc.getClientFactory(), |
| 50 |
xmlrpc.getServerFactory(), |
| 51 |
lockfile, secret ); |
| 52 |
if ( lockfile != null ) { |
| 53 |
assertTrue( ! lockfile.exists() ); |
| 54 |
} |
| 55 |
final HubService hubService = |
| 56 |
new BasicHubService( new Random( 199099L ) ); |
| 57 |
hubProf.start( new ClientProfile() { |
| 58 |
public HubConnection register() throws SampException { |
| 59 |
return hubService.register( TEST_PROFILE ); |
| 60 |
} |
| 61 |
public boolean isHubRunning() { |
| 62 |
return hubService.isHubRunning(); |
| 63 |
} |
| 64 |
} ); |
| 65 |
if ( lockfile != null ) { |
| 66 |
assertEquals( secret, |
| 67 |
LockInfo.readLockFile( SampUtils |
| 68 |
.fileToUrl( lockfile ) ) |
| 69 |
.getSecret() ); |
| 70 |
} |
| 71 |
URL lockurl = hubProf.publishLockfile(); |
| 72 |
assertEquals( secret, LockInfo.readLockFile( lockurl ).getSecret() ); |
| 73 |
hubProf.stop(); |
| 74 |
if ( lockfile != null ) { |
| 75 |
assertTrue( ! lockfile.exists() ); |
| 76 |
} |
| 77 |
assertNull( LockInfo.readLockFile( lockurl ) ); |
| 78 |
} |
| 79 |
} |