⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 querytranslatortestcase.java

📁 介绍了hibernate的入门有一些基本常用的事例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				count = new Integer(1);			}			else {				count = new Integer( count.intValue() + 1 );			}			result.put(fragment, count);		}		return result;	}		private String stripExtraSpaces(String string) {		if ( string == null ) {			return null;		}		StringBuffer buffer = new StringBuffer( string.length() );		char[] chars = string.toCharArray();		int length = chars.length;		boolean wasSpace = false;		for ( int i = 0; i < length; i++ ) {			boolean isSpace = chars[i] == ' ';			if ( wasSpace && isSpace ) {				continue;			}			else {				buffer.append( chars[i] );			}			wasSpace = isSpace;		}//		StringTokenizer tokenizer = new StringTokenizer( string.trim(), " " );//		while ( tokenizer.hasMoreTokens() ) {//			final String fragment = tokenizer.nextToken();//			buffer.append( fragment );//			buffer.append( " " );//		}//		return buffer.toString();	}	private void checkSqlByResultSet(	        QueryTranslator oldQueryTranslator,	        QueryTranslator newQueryTranslator,	        Object[] binds	) {		String oldsql = oldQueryTranslator.getSQLString();		String newsql = newQueryTranslator.getSQLString();		Session session = openSession();		Connection connection = session.connection();		PreparedStatement oldps = null;		PreparedStatement newps = null;		ResultSet oldrs = null;		ResultSet newrs = null;		try {			try {				oldps = connection.prepareStatement( oldsql );			}			catch( Throwable t ) {				fail( "Unable to prepare sql generated by old parser : " + t );			}			try {				newps = connection.prepareStatement( newsql );			}			catch( Throwable t ) {				fail( "Unable to prepare sql generated by new parser : " + t );			}			checkBinds(oldps, newps, binds);			try {				oldrs = executeQuery( oldps, binds );			}			catch( Throwable t ) {				fail( "Unable to execute sql generated by old parser : " + t );			}			try {				newrs = executeQuery( newps, binds );			}			catch( Throwable t ) {				fail( "Unable to execute sql generated by new parser : " + t );			}			checkResults( oldrs, newrs );		}		finally {			// make *sure* the sql resources get cleaned up			release(oldrs);			release(newrs);			release(oldps);			release(newps);			release(session);		}	}	private void checkBinds(PreparedStatement oldps, PreparedStatement newps, Object[] binds) {		// Make sure the binds "feel" ok		try {			ParameterMetaData oldBindMetaData = oldps.getParameterMetaData();			ParameterMetaData newBindMetaData = newps.getParameterMetaData();			assertEquals( "Different bind parameter count", oldBindMetaData.getParameterCount(), newBindMetaData.getParameterCount() );			assertEquals( "Incorrect number of binds passed in", oldBindMetaData.getParameterCount(), binds == null ? 0 : binds.length );			for ( int i = 0, max = oldBindMetaData.getParameterCount(); i < max; i++ ) {				assertEquals( "Different bind types", oldBindMetaData.getParameterType(i), newBindMetaData.getParameterType(i) );			}		}		catch( Throwable t ) {			fail( "SQLException comparing binds : " + t );		}	}	private ResultSet executeQuery(PreparedStatement ps, Object[] binds) throws SQLException {		if ( binds != null ) {			for ( int i = 0, max = binds.length; i < max; i++ ) {				ps.setObject( i, binds[i] );			}		}		return ps.executeQuery();	}	private void checkResults(ResultSet oldrs, ResultSet newrs) {		ResultSetMetaData oldmeta = null;		ResultSetMetaData newmeta = null;		int colCount = 0;		Type[] types = null;		// first compare the metadata from the two results		try {			oldmeta = oldrs.getMetaData();			newmeta = newrs.getMetaData();			assertEquals( "Different column counts", oldmeta.getColumnCount(), newmeta.getColumnCount() );			colCount = oldmeta.getColumnCount();			types = new Type[colCount];			for ( int i = 1, max = colCount; i < max; i++ ) {				assertEquals( "Column names were different", oldmeta.getColumnName(i), newmeta.getColumnName(i) );				assertEquals( "Column types were different", oldmeta.getColumnType(i), newmeta.getColumnType(i) );				assertEquals( "Java types were different", oldmeta.getColumnClassName(i), newmeta.getColumnClassName(i) );				types[i] = TypeFactory.basic( oldmeta.getColumnClassName(i) );			}		}		catch( Throwable t ) {			fail( "Error comparing result set metadata" );		}		// Then compare the actual results		try {			while ( oldrs.next() & newrs.next() ) {				for ( int i = 1; i < colCount; i++ ) {					Object oldval = oldrs.getObject(i);					if ( oldrs.wasNull() ) oldval = null;					Object newval = newrs.getObject(i);					if ( newrs.wasNull() ) newval = null;					checkLogicalEquality( oldval, newval, types[i] );				}			}			// for "better reporting" purposes, make sure both result sets are fully exhausted			while ( oldrs.next() );			while ( newrs.next() );			assertEquals( "Different row counts", oldrs.getRow(), newrs.getRow() );		}		catch( Throwable t ) {			fail( "Error comparing result set structure" );		}	}	private void checkLogicalEquality(Object oldval, Object newval, Type type) {		if ( oldval == null && newval == null ) {			// two nulls are logically equivalent here...			return;		}		else {			assertTrue( "Different result values", type.isEqual(oldval, newval, EntityMode.POJO) );		}	}	private void release(PreparedStatement ps) {		if ( ps != null ) {			try {				ps.close();			}			catch( Throwable t ) {}		}	}	private void release(ResultSet rs) {		if ( rs != null ) {			try {				rs.close();			}			catch( Throwable t ) {}		}	}	private void release(Session session) {		if ( session != null ) {			try {				session.close();			}			catch( Throwable t ) {}		}	}	protected String[] getMappings() {		return new String[]{			"hql/Animal.hbm.xml",			"batchfetch/ProductLine.hbm.xml",			"cid/Customer.hbm.xml",			"cid/Order.hbm.xml",			"cid/LineItem.hbm.xml",			"cid/Product.hbm.xml",			"legacy/Baz.hbm.xml",			"legacy/Category.hbm.xml",			"legacy/Commento.hbm.xml",			"legacy/Container.hbm.xml",			"legacy/Custom.hbm.xml",			"legacy/Eye.hbm.xml",			"legacy/Fee.hbm.xml",			"legacy/FooBar.hbm.xml",			"legacy/Fum.hbm.xml",			"legacy/Glarch.hbm.xml",			"legacy/Holder.hbm.xml",			"legacy/Many.hbm.xml",			"legacy/Marelo.hbm.xml",			"legacy/MasterDetail.hbm.xml",			"legacy/Middle.hbm.xml",			"legacy/Multi.hbm.xml",			"legacy/Nameable.hbm.xml",			"legacy/One.hbm.xml",			"legacy/Qux.hbm.xml",			"legacy/Simple.hbm.xml",			"legacy/SingleSeveral.hbm.xml",			"legacy/WZ.hbm.xml",			"legacy/UpDown.hbm.xml",			"compositeelement/Parent.hbm.xml",			"onetoone/joined/Person.hbm.xml"		};	}	protected boolean recreateSchema() {		// we do not need to create the schema for these parser tests		return false;	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -