You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
webcrawler/web/src/test/java/SpringTest.java

146 lines
5.9 KiB

import core.dao.BaseDao1;
import db.model.CidEntity;
import db.model.DataModel;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import web.config.AppConfig;
import web.config.SpringConfig;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
//让测试运行于Spring环境
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitWebConfig
@ContextConfiguration(classes = {AppConfig.class,SpringConfig.class})
public class SpringTest {
private static Logger log = LogManager.getLogger();
@Resource
private BaseDao1 baseDao1;
private MockMvc mockMvc;
@Autowired
WebApplicationContext wac;
@Before
public void setup(){
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
public void test() throws Exception {
ResultActions resultActions = this.mockMvc.perform(MockMvcRequestBuilders.get("/menu").param("firstResult","0").param("maxResults","0"));
resultActions.andDo(MockMvcResultHandlers.print()).andReturn();
}
// @Test
public void exportTableSql() {
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(baseDao1.getSessionFactory().getProperties()).build();
Metadata metadata = new MetadataSources(serviceRegistry).addAnnotatedClass(DataModel.class).buildMetadata();
SchemaExport schemaExport = new SchemaExport();
schemaExport.setOutputFile("test.sql");
schemaExport.setFormat(true);
schemaExport.create(EnumSet.of(TargetType.SCRIPT), metadata);
List<Integer> typeid = (List<Integer>) baseDao1.findByCriteria(DetachedCriteria.forClass(CidEntity.class).setProjection(Projections.groupProperty("typeid")));
List<String> tableNames = new ArrayList<>();
StringBuffer sb = new StringBuffer();
for (Integer id : typeid) {
String tableName = "data_" + id;
sb.append("create table " + tableName + " like data_1;\n");
tableNames.add(tableName);
}
sb.append("create table data (\n" +
" cid integer not null,\n" +
" aid integer,\n" +
" author varchar(255),\n" +
" backup_vid varchar(255),\n" +
" cache varchar(255),\n" +
" cover varchar(255),\n" +
" dispatch integer,\n" +
" dispatch_servers integer,\n" +
" dp_done tinyint,\n" +
" dp_done_flv tinyint,\n" +
" dp_done_hdmp4 tinyint,\n" +
" dp_done_mp4 tinyint,\n" +
" duration double precision,\n" +
" files integer,\n" +
" letv_addr varchar(255),\n" +
" letv_vid integer,\n" +
" letv_vu varchar(255),\n" +
" mid integer,\n" +
" page integer,\n" +
" storage integer,\n" +
" storage_server integer,\n" +
" subtitle varchar(255),\n" +
" title varchar(255),\n" +
" type varchar(255),\n" +
" upload integer,\n" +
" upload_meta integer,\n" +
" vid varchar(255),\n" +
" vp integer,\n" +
" type_id integer,\n" +
" primary key (cid)\n" +
" ) engine=MERGE UNION=(" + StringUtils.join(tableNames, ",") + ") insert_method=no;");
try {
FileUtils.writeStringToFile(new File("test.sql"), sb.toString(), "utf-8", true);
} catch (IOException e) {
e.printStackTrace();
}
}
// @Test
public void test4() throws IOException {
DetachedCriteria criteria = DetachedCriteria.forClass(DataModel.class);
List<DataModel> dataEntities = (List<DataModel>) baseDao1.findByCriteria(criteria, 10, 10);
for (DataModel dataEntity : dataEntities) {
log.info(dataEntity.getCid());
}
// 查询总条数
Long count = baseDao1.executeWithNativeSession((Session session) -> (Long) DetachedCriteria.forClass(DataModel.class).setProjection(Projections.rowCount()).setProjection(Projections.groupProperty("groupId")).getExecutableCriteria(session).uniqueResult());
}
}