Skip to content

Commit d38cb99

Browse files
committedMay 30, 2017
added closeable to test framework. So resources are closed even if assert stops the test
1 parent 80aad95 commit d38cb99

File tree

5 files changed

+582
-529
lines changed

5 files changed

+582
-529
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,156 +1,173 @@
1-
/*
2-
* TeleStax, Open Source Cloud Communications
3-
* Copyright 2011-2014, Telestax Inc and individual contributors
4-
* by the @authors tag.
5-
*
6-
* This program is free software: you can redistribute it and/or modify
7-
* under the terms of the GNU Affero General Public License as
8-
* published by the Free Software Foundation; either version 3 of
9-
* the License, or (at your option) any later version.
10-
*
11-
* This program is distributed in the hope that it will be useful,
12-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14-
* GNU Affero General Public License for more details.
15-
*
16-
* You should have received a copy of the GNU Affero General Public License
17-
* along with this program. If not, see <http://www.gnu.org/licenses/>
18-
*/
19-
20-
package org.mobicents.servlet.sip;
21-
22-
import java.io.File;
23-
import java.io.InputStream;
24-
import java.util.Properties;
25-
26-
import javax.sip.ListeningPoint;
27-
28-
import junit.framework.TestCase;
29-
30-
import org.apache.catalina.connector.Connector;
31-
import org.apache.log4j.Logger;
32-
33-
/**
34-
* This class is responsible for reading up the properties configuration file
35-
* and starting/stopping tomcat. It delegates to the test case inheriting from it
36-
* the deployment of the context and the location of the dar configuration file
37-
* since it should map to the test case.
38-
*/
39-
public abstract class SipServletTestCase extends TestCase {
40-
private static transient Logger logger = Logger.getLogger(SipServletTestCase.class);
41-
protected String tomcatBasePath;
42-
protected String projectHome;
43-
protected SipEmbedded tomcat;
44-
protected String sipIpAddress = null;
45-
protected String httpIpAddress = null;
46-
protected String serviceFullClassName = "org.mobicents.servlet.sip.catalina.SipStandardService";
47-
protected String serverName = "SIP-Servlet-Tomcat-Server";
48-
protected String listeningPointTransport = ListeningPoint.UDP;
49-
protected boolean createTomcatOnStartup = true;
50-
protected boolean autoDeployOnStartup = true;
51-
protected boolean initTomcatOnStartup = true;
52-
protected boolean startTomcatOnStartup = true;
53-
protected boolean addSipConnectorOnStartup = true;
54-
protected Connector sipConnector;
55-
56-
public SipServletTestCase(String name) {
57-
super(name);
58-
}
59-
60-
@Override
61-
protected void setUp() throws Exception {
62-
super.setUp();
63-
if(System.getProperty("org.mobicents.testsuite.testhostaddr") == null) {
64-
System.setProperty("org.mobicents.testsuite.testhostaddr", "127.0.0.1");// [::1] for IPv6
65-
}
66-
if(sipIpAddress == null) {
67-
sipIpAddress = "" + System.getProperty("org.mobicents.testsuite.testhostaddr") + "";
68-
}
69-
httpIpAddress = "" + System.getProperty("org.mobicents.testsuite.testhostaddr") + "";
70-
logger.info("sip ip address is " + sipIpAddress);
71-
logger.info("http ip address is " + httpIpAddress);
72-
//Reading properties
73-
Properties properties = new Properties();
74-
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(
75-
"org/mobicents/servlet/sip/testsuite/testsuite.properties");
76-
try{
77-
properties.load(inputStream);
78-
} catch (NullPointerException e) {
79-
inputStream = SipServletTestCase.class.getResourceAsStream(
80-
"org/mobicents/servlet/sip/testsuite/testsuite.properties");
81-
properties.load(inputStream);
82-
}
83-
84-
// First try to use the env variables - useful for shell scripting
85-
tomcatBasePath = System.getenv("CATALINA_HOME");
86-
projectHome = System.getenv("SIP_SERVLETS_HOME");
87-
88-
// Otherwise use the properties
89-
if(this.tomcatBasePath == null || this.tomcatBasePath.length() <= 0)
90-
this.tomcatBasePath = properties.getProperty("tomcat.home");
91-
if(this.projectHome == null || this.projectHome.length() <= 0)
92-
this.projectHome = properties.getProperty("project.home");
93-
logger.info("Tomcat base Path is : " + tomcatBasePath);
94-
logger.info("Project Home is : " + projectHome);
95-
//starting tomcat
96-
if(createTomcatOnStartup) {
97-
createTomcat();
98-
}
99-
}
100-
101-
protected void createTomcat() throws Exception {
102-
tomcat = new SipEmbedded(serverName, serviceFullClassName);
103-
tomcat.setLoggingFilePath(
104-
projectHome + File.separatorChar + "sip-servlets-test-suite" +
105-
File.separatorChar + "testsuite" +
106-
File.separatorChar + "src" +
107-
File.separatorChar + "test" +
108-
File.separatorChar + "resources" + File.separatorChar);
109-
logger.info("Log4j path is : " + tomcat.getLoggingFilePath());
110-
String darConfigurationFile = getDarConfigurationFile();
111-
tomcat.setDarConfigurationFilePath(darConfigurationFile);
112-
if(initTomcatOnStartup) {
113-
Properties sipStackProperties = getSipStackProperties();
114-
tomcat.initTomcat(tomcatBasePath, sipStackProperties);
115-
tomcat.addHttpConnector(httpIpAddress, 8080);
116-
/*
117-
* <Connector debugLog="../logs/debuglog.txt" ipAddress="0.0.0.0"
118-
* logLevel="DEBUG" port="5070"
119-
* protocol="org.mobicents.servlet.sip.startup.SipProtocolHandler"
120-
* serverLog="../logs/serverlog.txt" signalingTransport="udp"
121-
* sipPathName="gov.nist" sipStackName="SIP-Servlet-Tomcat-Server"/>
122-
*/
123-
if(addSipConnectorOnStartup) {
124-
sipConnector = tomcat.addSipConnector(serverName, sipIpAddress, 5070, listeningPointTransport);
125-
}
126-
}
127-
if(startTomcatOnStartup) {
128-
tomcat.startTomcat();
129-
}
130-
if(autoDeployOnStartup) {
131-
deployApplication();
132-
}
133-
}
134-
135-
protected Properties getSipStackProperties() {
136-
return null;
137-
}
138-
139-
@Override
140-
protected void tearDown() throws Exception {
141-
if(createTomcatOnStartup)
142-
tomcat.stopTomcat();
143-
super.tearDown();
144-
}
145-
146-
/**
147-
* Delegates the choice of the application to deploy to the test case
148-
*/
149-
protected abstract void deployApplication();
150-
151-
/**
152-
* Delegates the choice of the default application router
153-
* configuration file to use to the test case
154-
*/
155-
protected abstract String getDarConfigurationFile();
156-
}
1+
/*
2+
* TeleStax, Open Source Cloud Communications
3+
* Copyright 2011-2014, Telestax Inc and individual contributors
4+
* by the @authors tag.
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* under the terms of the GNU Affero General Public License as
8+
* published by the Free Software Foundation; either version 3 of
9+
* the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>
18+
*/
19+
20+
package org.mobicents.servlet.sip;
21+
22+
import java.io.Closeable;
23+
import java.io.File;
24+
import java.io.InputStream;
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
import java.util.Properties;
28+
29+
import javax.sip.ListeningPoint;
30+
31+
import junit.framework.TestCase;
32+
33+
import org.apache.catalina.connector.Connector;
34+
import org.apache.log4j.Logger;
35+
36+
/**
37+
* This class is responsible for reading up the properties configuration file
38+
* and starting/stopping tomcat. It delegates to the test case inheriting from it
39+
* the deployment of the context and the location of the dar configuration file
40+
* since it should map to the test case.
41+
*/
42+
public abstract class SipServletTestCase extends TestCase {
43+
private static transient Logger logger = Logger.getLogger(SipServletTestCase.class);
44+
protected String tomcatBasePath;
45+
protected String projectHome;
46+
protected SipEmbedded tomcat;
47+
protected String sipIpAddress = null;
48+
protected String httpIpAddress = null;
49+
protected String serviceFullClassName = "org.mobicents.servlet.sip.catalina.SipStandardService";
50+
protected String serverName = "SIP-Servlet-Tomcat-Server";
51+
protected String listeningPointTransport = ListeningPoint.UDP;
52+
protected boolean createTomcatOnStartup = true;
53+
protected boolean autoDeployOnStartup = true;
54+
protected boolean initTomcatOnStartup = true;
55+
protected boolean startTomcatOnStartup = true;
56+
protected boolean addSipConnectorOnStartup = true;
57+
protected Connector sipConnector;
58+
59+
public SipServletTestCase(String name) {
60+
super(name);
61+
}
62+
63+
protected List<Closeable> testResources;
64+
65+
private void closeResources() throws Exception {
66+
for (Closeable rAux : testResources) {
67+
try {
68+
rAux.close();
69+
} catch (Exception e) {
70+
71+
}
72+
}
73+
}
74+
75+
@Override
76+
protected void setUp() throws Exception {
77+
super.setUp();
78+
testResources = new ArrayList();
79+
if(System.getProperty("org.mobicents.testsuite.testhostaddr") == null) {
80+
System.setProperty("org.mobicents.testsuite.testhostaddr", "127.0.0.1");// [::1] for IPv6
81+
}
82+
if(sipIpAddress == null) {
83+
sipIpAddress = "" + System.getProperty("org.mobicents.testsuite.testhostaddr") + "";
84+
}
85+
httpIpAddress = "" + System.getProperty("org.mobicents.testsuite.testhostaddr") + "";
86+
logger.info("sip ip address is " + sipIpAddress);
87+
logger.info("http ip address is " + httpIpAddress);
88+
//Reading properties
89+
Properties properties = new Properties();
90+
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(
91+
"org/mobicents/servlet/sip/testsuite/testsuite.properties");
92+
try{
93+
properties.load(inputStream);
94+
} catch (NullPointerException e) {
95+
inputStream = SipServletTestCase.class.getResourceAsStream(
96+
"org/mobicents/servlet/sip/testsuite/testsuite.properties");
97+
properties.load(inputStream);
98+
}
99+
100+
// First try to use the env variables - useful for shell scripting
101+
tomcatBasePath = System.getenv("CATALINA_HOME");
102+
projectHome = System.getenv("SIP_SERVLETS_HOME");
103+
104+
// Otherwise use the properties
105+
if(this.tomcatBasePath == null || this.tomcatBasePath.length() <= 0)
106+
this.tomcatBasePath = properties.getProperty("tomcat.home");
107+
if(this.projectHome == null || this.projectHome.length() <= 0)
108+
this.projectHome = properties.getProperty("project.home");
109+
logger.info("Tomcat base Path is : " + tomcatBasePath);
110+
logger.info("Project Home is : " + projectHome);
111+
//starting tomcat
112+
if(createTomcatOnStartup) {
113+
createTomcat();
114+
}
115+
}
116+
117+
protected void createTomcat() throws Exception {
118+
tomcat = new SipEmbedded(serverName, serviceFullClassName);
119+
tomcat.setLoggingFilePath(
120+
projectHome + File.separatorChar + "sip-servlets-test-suite" +
121+
File.separatorChar + "testsuite" +
122+
File.separatorChar + "src" +
123+
File.separatorChar + "test" +
124+
File.separatorChar + "resources" + File.separatorChar);
125+
logger.info("Log4j path is : " + tomcat.getLoggingFilePath());
126+
String darConfigurationFile = getDarConfigurationFile();
127+
tomcat.setDarConfigurationFilePath(darConfigurationFile);
128+
if(initTomcatOnStartup) {
129+
Properties sipStackProperties = getSipStackProperties();
130+
tomcat.initTomcat(tomcatBasePath, sipStackProperties);
131+
tomcat.addHttpConnector(httpIpAddress, 8080);
132+
/*
133+
* <Connector debugLog="../logs/debuglog.txt" ipAddress="0.0.0.0"
134+
* logLevel="DEBUG" port="5070"
135+
* protocol="org.mobicents.servlet.sip.startup.SipProtocolHandler"
136+
* serverLog="../logs/serverlog.txt" signalingTransport="udp"
137+
* sipPathName="gov.nist" sipStackName="SIP-Servlet-Tomcat-Server"/>
138+
*/
139+
if(addSipConnectorOnStartup) {
140+
sipConnector = tomcat.addSipConnector(serverName, sipIpAddress, 5070, listeningPointTransport);
141+
}
142+
}
143+
if(startTomcatOnStartup) {
144+
tomcat.startTomcat();
145+
}
146+
if(autoDeployOnStartup) {
147+
deployApplication();
148+
}
149+
}
150+
151+
protected Properties getSipStackProperties() {
152+
return null;
153+
}
154+
155+
@Override
156+
protected void tearDown() throws Exception {
157+
closeResources();
158+
if(createTomcatOnStartup)
159+
tomcat.stopTomcat();
160+
super.tearDown();
161+
}
162+
163+
/**
164+
* Delegates the choice of the application to deploy to the test case
165+
*/
166+
protected abstract void deployApplication();
167+
168+
/**
169+
* Delegates the choice of the default application router
170+
* configuration file to use to the test case
171+
*/
172+
protected abstract String getDarConfigurationFile();
173+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.mobicents.servlet.sip.testsuite;
2+
3+
import java.io.Closeable;
4+
import java.io.IOException;
5+
import org.cafesip.sipunit.SipPhone;
6+
7+
public class PhoneCloseable implements Closeable {
8+
SipPhone phone;
9+
10+
public PhoneCloseable(SipPhone phone) {
11+
this.phone = phone;
12+
}
13+
14+
public void close() throws IOException {
15+
phone.dispose();
16+
}
17+
}

0 commit comments

Comments
 (0)
Please sign in to comment.