Skip to content

Commit

Permalink
write a line into test.file
Browse files Browse the repository at this point in the history
  • Loading branch information
huifer committed Sep 4, 2024
1 parent 55b0a6b commit 7b7ec5c
Show file tree
Hide file tree
Showing 8 changed files with 621 additions and 0 deletions.
79 changes: 79 additions & 0 deletions docs/spring/cs0df1635c-6a5e-11ef-8f2f-acde48001122.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright 2009-2019 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.submitted.result_handler_type;

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.io.Reader;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

class DefaultResultHandlerTypeTest {

@Test
void testSelectList() throws Exception {
String xmlConfig = "org/apache/ibatis/submitted/result_handler_type/MapperConfig.xml";
SqlSessionFactory sqlSessionFactory = getSqlSessionFactoryXmlConfig(xmlConfig);
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
List<Person> list = sqlSession
.selectList("org.apache.ibatis.submitted.result_handler_type.PersonMapper.doSelect");
assertEquals(list.size(), 2);
assertEquals("java.util.LinkedList", list.getClass().getCanonicalName());
}
}

@Test
void testSelectMap() throws Exception {
String xmlConfig = "org/apache/ibatis/submitted/result_handler_type/MapperConfig.xml";
SqlSessionFactory sqlSessionFactory = getSqlSessionFactoryXmlConfig(xmlConfig);
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Map<Integer, Person> map = sqlSession.selectMap(
"org.apache.ibatis.submitted.result_handler_type.PersonMapper.doSelect", "id");
assertEquals(map.size(), 2);
assertEquals("java.util.LinkedHashMap", map.getClass().getCanonicalName());
}
}

@Test
void testSelectMapAnnotation() throws Exception {
String xmlConfig = "org/apache/ibatis/submitted/result_handler_type/MapperConfig.xml";
SqlSessionFactory sqlSessionFactory = getSqlSessionFactoryXmlConfig(xmlConfig);
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
Map<Integer, Person> map = mapper.selectAsMap();
assertEquals(map.size(), 2);
assertEquals("java.util.LinkedHashMap", map.getClass().getCanonicalName());
}
}

private SqlSessionFactory getSqlSessionFactoryXmlConfig(String resource) throws Exception {
try (Reader configReader = Resources.getResourceAsReader(resource)) {
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
"org/apache/ibatis/submitted/result_handler_type/CreateDB.sql");

return sqlSessionFactory;
}
}

}
41 changes: 41 additions & 0 deletions docs/spring/cs0e6cb700-6a5e-11ef-8f2f-acde48001122.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.huifer.fzjh.service;

import com.huifer.fzjh.bean.RequestEntity;
import com.huifer.fzjh.bean.ServerWeight;
import com.huifer.fzjh.exception.LoadBalanceException;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Random;

/**
* 负载均衡算法实现: 按照随机
* 直接随机一个机器
*/
@Slf4j
public class RandomLoadBalance extends AbstractLoadBalance {
private int count = -1;
private RequestEntity requestEntity;
private List<ServerWeight> serverWeights;

public RandomLoadBalance(RequestEntity requestEntity, List<ServerWeight> serverWeights) {
super(requestEntity, serverWeights);
this.count = serverWeights.size();
this.requestEntity = requestEntity;
this.serverWeights = serverWeights;
}


@Override
public String loadBalance() {
if (count < 0) {
throw new LoadBalanceException("机器数量不能小于0");
}

Random random = new Random();
int machineId = random.nextInt(count);
ServerWeight serverWeight = serverWeights.get(machineId);
log.info("当前请求信息={},负载均衡计算后的机器ip={},端口={}", requestEntity, serverWeight.getIp(), serverWeight.getPort());
return serverWeight.getIp() + ":" + serverWeight.getPort();
}
}
35 changes: 35 additions & 0 deletions docs/spring/cs0ee04dd2-6a5e-11ef-8f2f-acde48001122.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.huifer.rbac.service;

import org.huifer.rbac.entity.db.Demo;
import org.huifer.rbac.mapper.DemoMapper;

import org.springframework.stereotype.Service;

@Service
public class DemoService {

final
DemoMapper demoMapper;

public DemoService(DemoMapper demoMapper) {
this.demoMapper = demoMapper;
}


public void lgs() {
Demo demo = new Demo();
demo.setName("zs");
demoMapper.insert(demo);
}

public void update() {
Demo demo = demoMapper.selectById(2L);
demo.setName("aklsfj");
demoMapper.updateById(demo);

}

public void delete() {
demoMapper.deleteById(2L);
}
}
78 changes: 78 additions & 0 deletions docs/spring/cs0f879830-6a5e-11ef-8f2f-acde48001122.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.github.huifer.full.shiro.service.impl;

import com.github.huifer.full.shiro.dao.ShiroDeptDao;
import com.github.huifer.full.shiro.entity.ShiroDept;
import com.github.huifer.full.shiro.ex.ServerEx;
import com.github.huifer.full.shiro.model.req.dept.DeptCreateParam;
import com.github.huifer.full.shiro.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class DeptServiceImpl implements DeptService {

@Autowired
private ShiroDeptDao shiroDeptDao;

@Override
public boolean create(DeptCreateParam param) {
String name = param.getName();
if (!StringUtils.hasText(name)) {
throw new ServerEx("");
}
Integer companyId = param.getCompanyId();
if (companyId == null) {
throw new ServerEx("");
}
ShiroDept byName = this.shiroDeptDao.findByName(name);
if (byName != null) {
throw new ServerEx("");
}
ShiroDept ins = new ShiroDept();
ins.setCompanyId(param.getCompanyId());
ins.setName(param.getName());
ins.setMainUser(param.getMainUser());
ins.setPid(param.getPid());

return this.shiroDeptDao.insert(ins) > 0;
}

@Override
public boolean update(DeptCreateParam param, int id) {
ShiroDept shiroDept = this.shiroDeptDao.selectById(id);
if (shiroDept != null) {

String name = param.getName();
if (!StringUtils.hasText(name)) {
throw new ServerEx("");
}
Integer companyId = param.getCompanyId();
if (companyId == null) {
throw new ServerEx("");
}
if (!shiroDept.getName().equals(name)) {

ShiroDept byName = this.shiroDeptDao.findByName(name);
if (byName != null) {
throw new ServerEx("");
}
shiroDept.setName(param.getName());
}
shiroDept.setMainUser(param.getMainUser());
shiroDept.setPid(param.getPid());
return shiroDeptDao.updateById(shiroDept) > 0;
}
return false;
}

@Override
public boolean delete(int id) {
return this.shiroDeptDao.deleteById(id) > 0;
}

@Override
public ShiroDept byId(int id) {
return this.shiroDeptDao.selectById(id);
}
}
116 changes: 116 additions & 0 deletions docs/spring/cs1061cece-6a5e-11ef-8f2f-acde48001122.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* Copyright 2009-2019 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.submitted.named_constructor_args;

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.annotations.Arg;
import org.apache.ibatis.annotations.ConstructorArgs;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.builder.BuilderException;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.Reader;

import static com.googlecode.catchexception.apis.BDDCatchException.caughtException;
import static com.googlecode.catchexception.apis.BDDCatchException.when;
import static org.assertj.core.api.BDDAssertions.then;

class InvalidNamedConstructorArgsTest {

private static SqlSessionFactory sqlSessionFactory;

@BeforeAll
static void setUp() throws Exception {
// create an SqlSessionFactory
try (Reader reader = Resources.getResourceAsReader(
"org/apache/ibatis/submitted/named_constructor_args/mybatis-config.xml")) {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}

// populate in-memory database
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
"org/apache/ibatis/submitted/named_constructor_args/CreateDB.sql");
}

@Test
void noMatchingConstructorArgName() {
Configuration configuration = sqlSessionFactory.getConfiguration();
when(() -> configuration.addMapper(NoMatchingConstructorMapper.class));

then(caughtException()).isInstanceOf(BuilderException.class)
.hasMessageContaining(
"'org.apache.ibatis.submitted.named_constructor_args.InvalidNamedConstructorArgsTest$NoMatchingConstructorMapper.select-void'")
.hasMessageContaining("'org.apache.ibatis.submitted.named_constructor_args.User'")
.hasMessageContaining("[noSuchConstructorArg]");
}

@Test
void wrongJavaType() {
Configuration configuration = sqlSessionFactory.getConfiguration();
when(() -> configuration.addMapper(ConstructorWithWrongJavaType.class));
then(caughtException()).isInstanceOf(BuilderException.class)
.hasMessageContaining(
"'org.apache.ibatis.submitted.named_constructor_args.InvalidNamedConstructorArgsTest$ConstructorWithWrongJavaType.select-void'")
.hasMessageContaining("'org.apache.ibatis.submitted.named_constructor_args.User'")
.hasMessageContaining("[id]");
}

@Test
void missingRequiredJavaType() {
Configuration configuration = sqlSessionFactory.getConfiguration();
when(() -> configuration.addMapper(ConstructorMissingRequiresJavaType.class));
then(caughtException()).isInstanceOf(BuilderException.class)
.hasMessageContaining(
"'org.apache.ibatis.submitted.named_constructor_args.InvalidNamedConstructorArgsTest$ConstructorMissingRequiresJavaType.select-void'")
.hasMessageContaining("'org.apache.ibatis.submitted.named_constructor_args.User'")
.hasMessageContaining("[id]");
}

interface NoMatchingConstructorMapper {
@ConstructorArgs({
@Arg(column = "id", name = "noSuchConstructorArg"),
})
@Select("select * from users ")
User select();
}

interface ConstructorWithWrongJavaType {
// There is a constructor with arg name 'id', but
// its type is different from the specified javaType.
@ConstructorArgs({
@Arg(column = "id", name = "id", javaType = Integer.class),
})
@Select("select * from users ")
User select();
}

interface ConstructorMissingRequiresJavaType {
// There is a constructor with arg name 'id', but its type
// is different from the type of a property with the same name.
// javaType is required in this case.
// Debug log shows the detail of the matching error.
@ConstructorArgs({
@Arg(column = "id", name = "id"),
})
@Select("select * from users ")
User select();
}
}
Loading

0 comments on commit 7b7ec5c

Please sign in to comment.