package com.perforce;
import static org.junit.Assert.*;
import java.io.FileInputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletContext;
import junit.framework.Assert;
import org.apache.log4j.Logger;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.AssertThrows;
import com.perforce.model.UserInfo;
public class UtilityTest {
static Logger logger = Logger.getLogger(UtilityTest.class.getName());
static Properties _props = null;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
logger.debug("init()");
System.setProperty(Constants.CONFIGURATION_FILE_SYSTEM_PROPERTY, "test.properties");
_props = new Properties();
_props.load(new FileInputStream("test.properties"));
ServletContext context = new MockServletContext();
Enumeration<?> enumeration = _props.propertyNames();
while(enumeration.hasMoreElements()) {
String key = (String)enumeration.nextElement();
context.setAttribute(key, _props.getProperty(key));
}
ServletContextHolder.getInstance().setServletContext(context);
}
@Test
public void testEncrypt() throws Exception {
logger.info("### testEncrypt");
String testString = "This is a test string.";
String encrypted = Utility.encrypt(testString);
logger.info(encrypted);
String decrypted = Utility.decrypt(encrypted);
logger.info(decrypted);
}
@Test
public void testConvertToJson() throws Exception {
logger.info("### testConvertToJson");
UserInfo ui = new UserInfo();
ui.setName("bob.smith");
ui.setEmail("bob@host.com");
ui.setFullName("Bob Smith");
String json = Utility.convertToJson(ui);
logger.info(json);
UserInfo ui2 = (UserInfo) Utility.convertFromJson(json, UserInfo.class);
logger.info(ui2.getFullName());
String encrypted = Utility.encrypt(json);
logger.info(encrypted);
}
@Test
public void testConvertToEncryptedJson() throws Exception {
logger.info("### testConvertToEncryptedJson");
Map<String, String> m = new HashMap<String, String>();
m.put("key1", "value one");
m.put("key2", "value two");
String encrypted = Utility.convertToEncryptedJson(m);
logger.info(encrypted);
Map<String, String> n = (Map<String, String>) Utility.convertFromEncryptedJson(encrypted, Map.class);
logger.info(n);
}
@Test
public void testSendEmail() throws Exception {
logger.info("### testSendEmail");
String to = Utility.getStringAttribute("test.email");
Utility.sendEmail(to, "TEST MESSAGE FROM JUNIT", "THIS IS A TEST MESSAGE FROM JUNIT");
}
@Test
public void testSendHtmlEmail() throws Exception {
logger.info("### testSendEmail");
String to = Utility.getStringAttribute("test.email");
String htmlMsg = "<h1>TESTING</h1><p>This is just a test! <a href='http://www.google.com'>click here to google</a>";
String textMsg = "this is Non-html stuff";
Utility.sendEmail(to, "TEST HTML MESSAGE FROM JUNIT", textMsg, htmlMsg);
}
@Test
public void testGetRandomKey() throws Exception {
logger.info("### testGetRandomKey");
String key = Utility.getRandomKey();
logger.info(key);
assertTrue(key.length() > 0);
}
}