diff --git a/docs/spring/cs0f67148a-dc23-11ee-b73a-acde48001122.java b/docs/spring/cs0f67148a-dc23-11ee-b73a-acde48001122.java new file mode 100644 index 00000000..2668651e --- /dev/null +++ b/docs/spring/cs0f67148a-dc23-11ee-b73a-acde48001122.java @@ -0,0 +1,90 @@ +/** + * Copyright 2009-2019 the original author or authors. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.type; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class LongTypeHandlerTest extends BaseTypeHandlerTest { + + private static final TypeHandler TYPE_HANDLER = new LongTypeHandler(); + + @Override + @Test + public void shouldSetParameter() throws Exception { + TYPE_HANDLER.setParameter(ps, 1, 100L, null); + verify(ps).setLong(1, 100L); + } + + @Override + @Test + public void shouldGetResultFromResultSetByName() throws Exception { + when(rs.getLong("column")).thenReturn(100L); + assertEquals(Long.valueOf(100L), TYPE_HANDLER.getResult(rs, "column")); + + when(rs.getLong("column")).thenReturn(0L); + assertEquals(Long.valueOf(0L), TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByName() throws Exception { + when(rs.getLong("column")).thenReturn(0L); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultFromResultSetByPosition() throws Exception { + when(rs.getLong(1)).thenReturn(100L); + assertEquals(Long.valueOf(100L), TYPE_HANDLER.getResult(rs, 1)); + + when(rs.getLong(1)).thenReturn(0L); + assertEquals(Long.valueOf(0L), TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByPosition() throws Exception { + when(rs.getLong(1)).thenReturn(0L); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultFromCallableStatement() throws Exception { + when(cs.getLong(1)).thenReturn(100L); + assertEquals(Long.valueOf(100L), TYPE_HANDLER.getResult(cs, 1)); + + when(cs.getLong(1)).thenReturn(0L); + assertEquals(Long.valueOf(0L), TYPE_HANDLER.getResult(cs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromCallableStatement() throws Exception { + when(cs.getLong(1)).thenReturn(0L); + when(cs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(cs, 1)); + } + +} \ No newline at end of file diff --git a/docs/spring/cs0f988ad8-dc23-11ee-b73a-acde48001122.java b/docs/spring/cs0f988ad8-dc23-11ee-b73a-acde48001122.java new file mode 100644 index 00000000..b23373cd --- /dev/null +++ b/docs/spring/cs0f988ad8-dc23-11ee-b73a-acde48001122.java @@ -0,0 +1,73 @@ +/** + * Copyright 2009-2019 the original author or authors. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.executor.loader; + +import net.sf.cglib.proxy.Factory; +import org.apache.ibatis.domain.blog.Author; +import org.apache.ibatis.executor.ExecutorException; +import org.apache.ibatis.executor.loader.cglib.CglibProxyFactory; +import org.apache.ibatis.reflection.factory.DefaultObjectFactory; +import org.apache.ibatis.session.Configuration; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashMap; + +import static org.junit.jupiter.api.Assertions.*; + +class CglibProxyTest extends SerializableProxyTest { + + @BeforeAll + static void createProxyFactory() { + proxyFactory = new CglibProxyFactory(); + } + + @Test + void shouldCreateAProxyForAPartiallyLoadedBean() throws Exception { + ResultLoaderMap loader = new ResultLoaderMap(); + loader.addLoader("id", null, null); + Object proxy = proxyFactory.createProxy(author, loader, new Configuration(), new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>()); + Author author2 = (Author) deserialize(serialize((Serializable) proxy)); + assertTrue(author2 instanceof Factory); + } + + @Test + void shouldFailCallingAnUnloadedProperty() { + // yes, it must go in uppercase + HashMap unloadedProperties = new HashMap<>(); + unloadedProperties.put("ID", null); + Author author2 = (Author) ((CglibProxyFactory) proxyFactory).createDeserializationProxy(author, unloadedProperties, new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>()); + Assertions.assertThrows(ExecutorException.class, author2::getId); + } + + @Test + void shouldLetCallALoadedProperty() { + Author author2 = (Author) ((CglibProxyFactory) proxyFactory).createDeserializationProxy(author, new HashMap<>(), new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>()); + assertEquals(999, author2.getId()); + } + + @Test + void shouldSerizalizeADeserlizaliedProxy() throws Exception { + Object proxy = ((CglibProxyFactory) proxyFactory).createDeserializationProxy(author, new HashMap<>(), new DefaultObjectFactory(), new ArrayList<>(), new ArrayList<>()); + Author author2 = (Author) deserialize(serialize((Serializable) proxy)); + assertEquals(author, author2); + assertNotEquals(author.getClass(), author2.getClass()); + } + +} diff --git a/docs/spring/cs0fc957b2-dc23-11ee-b73a-acde48001122.java b/docs/spring/cs0fc957b2-dc23-11ee-b73a-acde48001122.java new file mode 100644 index 00000000..a0d2712d --- /dev/null +++ b/docs/spring/cs0fc957b2-dc23-11ee-b73a-acde48001122.java @@ -0,0 +1,16 @@ +package dolores.oauthserver; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class OauthServerApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/docs/spring/cs0ffa9958-dc23-11ee-b73a-acde48001122.java b/docs/spring/cs0ffa9958-dc23-11ee-b73a-acde48001122.java new file mode 100644 index 00000000..02c16940 --- /dev/null +++ b/docs/spring/cs0ffa9958-dc23-11ee-b73a-acde48001122.java @@ -0,0 +1,17 @@ +package com.huifer.design.builder; + +/** + * 轮子 + */ +public class Wheel { + + private Integer size; + + public Integer getSize() { + return size; + } + + public void setSize(Integer size) { + this.size = size; + } +} diff --git a/docs/spring/cs102d3fd4-dc23-11ee-b73a-acde48001122.java b/docs/spring/cs102d3fd4-dc23-11ee-b73a-acde48001122.java new file mode 100644 index 00000000..5136acf6 --- /dev/null +++ b/docs/spring/cs102d3fd4-dc23-11ee-b73a-acde48001122.java @@ -0,0 +1,384 @@ +/** + * Copyright 2009-2019 the original author or authors. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.session.defaults; + +import org.apache.ibatis.binding.BindingException; +import org.apache.ibatis.cursor.Cursor; +import org.apache.ibatis.exceptions.ExceptionFactory; +import org.apache.ibatis.exceptions.TooManyResultsException; +import org.apache.ibatis.executor.BatchResult; +import org.apache.ibatis.executor.ErrorContext; +import org.apache.ibatis.executor.Executor; +import org.apache.ibatis.executor.result.DefaultMapResultHandler; +import org.apache.ibatis.executor.result.DefaultResultContext; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.session.ResultHandler; +import org.apache.ibatis.session.RowBounds; +import org.apache.ibatis.session.SqlSession; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.*; + +/** + * mybatis 默认的sqlSession + * The default implementation for {@link SqlSession}. + * Note that this class is not Thread-Safe. + * + * @author Clinton Begin + */ +public class DefaultSqlSession implements SqlSession { + + private final Configuration configuration; + private final Executor executor; + + private final boolean autoCommit; + private boolean dirty; + private List> cursorList; + + /** + * 默认的 sql session + * @param configuration mybatis-config.xml + * @param executor {@link Executor} + * @param autoCommit 自动commit + */ + public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) { + this.configuration = configuration; + this.executor = executor; + this.dirty = false; + this.autoCommit = autoCommit; + } + + public DefaultSqlSession(Configuration configuration, Executor executor) { + this(configuration, executor, false); + } + + @Override + public T selectOne(String statement) { + return this.selectOne(statement, null); + } + + /** + * 执行 selectList().get(0) + * @param statement Unique identifier matching the statement to use. + * @param parameter A parameter object to pass to the statement. + * @param + * @return + */ + @Override + public T selectOne(String statement, Object parameter) { + // Popular vote was to return null on 0 results and throw exception on too many. + List list = this.selectList(statement, parameter); + if (list.size() == 1) { + return list.get(0); + } else if (list.size() > 1) { + throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size()); + } else { + return null; + } + } + + @Override + public Map selectMap(String statement, String mapKey) { + return this.selectMap(statement, null, mapKey, RowBounds.DEFAULT); + } + + @Override + public Map selectMap(String statement, Object parameter, String mapKey) { + return this.selectMap(statement, parameter, mapKey, RowBounds.DEFAULT); + } + + /** + * + * @param statement Unique identifier matching the statement to use. + * @param parameter A parameter object to pass to the statement. + * @param mapKey The property to use as key for each value in the list. + * @param rowBounds Bounds to limit object retrieval + * @param + * @param + * @return + */ + @Override + public Map selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) { + final List list = selectList(statement, parameter, rowBounds); + final DefaultMapResultHandler mapResultHandler = new DefaultMapResultHandler<>(mapKey, + configuration.getObjectFactory(), configuration.getObjectWrapperFactory(), configuration.getReflectorFactory()); + final DefaultResultContext context = new DefaultResultContext<>(); + for (V o : list) { + context.nextResultObject(o); + mapResultHandler.handleResult(context); + } + return mapResultHandler.getMappedResults(); + } + + @Override + public Cursor selectCursor(String statement) { + return selectCursor(statement, null); + } + + @Override + public Cursor selectCursor(String statement, Object parameter) { + return selectCursor(statement, parameter, RowBounds.DEFAULT); + } + + @Override + public Cursor selectCursor(String statement, Object parameter, RowBounds rowBounds) { + try { + MappedStatement ms = configuration.getMappedStatement(statement); + Cursor cursor = executor.queryCursor(ms, wrapCollection(parameter), rowBounds); + registerCursor(cursor); + return cursor; + } catch (Exception e) { + throw ExceptionFactory.wrapException("Error querying database. Cause: " + e, e); + } finally { + ErrorContext.instance().reset(); + } + } + + @Override + public List selectList(String statement) { + return this.selectList(statement, null); + } + + /** + * 向下继续调用 + * @param statement Unique identifier matching the statement to use. + * @param parameter A parameter object to pass to the statement. + * @param + * @return + */ + @Override + public List selectList(String statement, Object parameter) { + return this.selectList(statement, parameter, RowBounds.DEFAULT); + } + + /** + * 执行query + * @param statement Unique identifier matching the statement to use. + * @param parameter A parameter object to pass to the statement. + * @param rowBounds Bounds to limit object retrieval + * @param + * @return + */ + @Override + public List selectList(String statement, Object parameter, RowBounds rowBounds) { + try { + MappedStatement ms = configuration.getMappedStatement(statement); + return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER); + } catch (Exception e) { + throw ExceptionFactory.wrapException("Error querying database. Cause: " + e, e); + } finally { + ErrorContext.instance().reset(); + } + } + + @Override + public void select(String statement, Object parameter, ResultHandler handler) { + select(statement, parameter, RowBounds.DEFAULT, handler); + } + + @Override + public void select(String statement, ResultHandler handler) { + select(statement, null, RowBounds.DEFAULT, handler); + } + + @Override + public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) { + try { + MappedStatement ms = configuration.getMappedStatement(statement); + executor.query(ms, wrapCollection(parameter), rowBounds, handler); + } catch (Exception e) { + throw ExceptionFactory.wrapException("Error querying database. Cause: " + e, e); + } finally { + ErrorContext.instance().reset(); + } + } + + @Override + public int insert(String statement) { + return insert(statement, null); + } + + @Override + public int insert(String statement, Object parameter) { + return update(statement, parameter); + } + + @Override + public int update(String statement) { + return update(statement, null); + } + + @Override + public int update(String statement, Object parameter) { + try { + dirty = true; + MappedStatement ms = configuration.getMappedStatement(statement); + return executor.update(ms, wrapCollection(parameter)); + } catch (Exception e) { + throw ExceptionFactory.wrapException("Error updating database. Cause: " + e, e); + } finally { + ErrorContext.instance().reset(); + } + } + + @Override + public int delete(String statement) { + return update(statement, null); + } + + @Override + public int delete(String statement, Object parameter) { + return update(statement, parameter); + } + + @Override + public void commit() { + commit(false); + } + + @Override + public void commit(boolean force) { + try { + executor.commit(isCommitOrRollbackRequired(force)); + dirty = false; + } catch (Exception e) { + throw ExceptionFactory.wrapException("Error committing transaction. Cause: " + e, e); + } finally { + ErrorContext.instance().reset(); + } + } + + @Override + public void rollback() { + rollback(false); + } + + @Override + public void rollback(boolean force) { + try { + executor.rollback(isCommitOrRollbackRequired(force)); + dirty = false; + } catch (Exception e) { + throw ExceptionFactory.wrapException("Error rolling back transaction. Cause: " + e, e); + } finally { + ErrorContext.instance().reset(); + } + } + + @Override + public List flushStatements() { + try { + return executor.flushStatements(); + } catch (Exception e) { + throw ExceptionFactory.wrapException("Error flushing statements. Cause: " + e, e); + } finally { + ErrorContext.instance().reset(); + } + } + + @Override + public void close() { + try { + executor.close(isCommitOrRollbackRequired(false)); + closeCursors(); + dirty = false; + } finally { + ErrorContext.instance().reset(); + } + } + + private void closeCursors() { + if (cursorList != null && !cursorList.isEmpty()) { + for (Cursor cursor : cursorList) { + try { + cursor.close(); + } catch (IOException e) { + throw ExceptionFactory.wrapException("Error closing cursor. Cause: " + e, e); + } + } + cursorList.clear(); + } + } + + @Override + public Configuration getConfiguration() { + return configuration; + } + + @Override + public T getMapper(Class type) { + return configuration.getMapper(type, this); + } + + @Override + public Connection getConnection() { + try { + return executor.getTransaction().getConnection(); + } catch (SQLException e) { + throw ExceptionFactory.wrapException("Error getting a new connection. Cause: " + e, e); + } + } + + @Override + public void clearCache() { + executor.clearLocalCache(); + } + + private void registerCursor(Cursor cursor) { + if (cursorList == null) { + cursorList = new ArrayList<>(); + } + cursorList.add(cursor); + } + + private boolean isCommitOrRollbackRequired(boolean force) { + return (!autoCommit && dirty) || force; + } + + private Object wrapCollection(final Object object) { + if (object instanceof Collection) { + StrictMap map = new StrictMap<>(); + map.put("collection", object); + if (object instanceof List) { + map.put("list", object); + } + return map; + } else if (object != null && object.getClass().isArray()) { + StrictMap map = new StrictMap<>(); + map.put("array", object); + return map; + } + return object; + } + + public static class StrictMap extends HashMap { + + private static final long serialVersionUID = -5741767162221585340L; + + @Override + public V get(Object key) { + if (!super.containsKey(key)) { + throw new BindingException("Parameter '" + key + "' not found. Available parameters are " + this.keySet()); + } + return super.get(key); + } + + } + +}