import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Date;
import java.util.Locale;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.hibernate.junit.UnitTestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import sino.dao.jpa.PostDaoImpl;
import sino.entities.Post;
import sino.entities.PostType;
import sino.entities.UnionStation;
import sino.service.PostService;
import junit.framework.TestCase;
public class Test extends TestCase {
public void testC() throws Exception {
String userName = "root";
String password = "topcoder";
String url = "jdbc:mysql://localhost:3306/sino?useUnicode=true&characterEncoding=UTF-8";
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, userName, password);
PreparedStatement ps = conn.prepareStatement("INSERT INTO post (content, title, preview, modification_date,"
+ " creation_date, type) VALUES (?, ?, ?, ?, ?, ?)");
ps.setString(1, "内容");
ps.setString(2, "标题");
ps.setString(3, "预览");
ps.setDate(4, new java.sql.Date(new Date().getTime()));
ps.setDate(5, new java.sql.Date(new Date().getTime()));
ps.setString(6, "NEWS");
ps.execute();
}
public void testA() throws Exception {
PostDaoImpl postDao = new PostDaoImpl();
EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnit");
postDao.setEntityManager(factory.createEntityManager());
postDao.getEntityManager().getTransaction().begin();
Post post = new Post();
post.setContent("内容");
post.setTitle("标题");
post.setPreview("预览");
post.setModificationDate(new Date());
post.setCreationDate(new Date());
post.setType(PostType.NEWS);
postDao.persist(post);
postDao.getEntityManager().getTransaction().commit();
postDao.setEntityManager(factory.createEntityManager());
System.out.println(postDao.retrieve(post.getId()).getContent());
System.out.println(postDao.retrieve(1).getPreview());
}
public void testB() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("/config.xml");
PostService postService = (PostService) context.getBean("postService");
Post post = new Post();
post.setContent("内容");
post.setTitle("标题");
post.setPreview("预览");
post.setModificationDate(new Date());
post.setCreationDate(new Date());
post.setType(PostType.NEWS);
postService.createPost(post);
System.out.println(postService.getPost(post.getId()).getContent());
}
public void testD() throws Exception {
// System.out.println(new Locale("ru").getLanguage());
System.out.println(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME);
}
}