This commit is contained in:
parent
fe7cf55de9
commit
51e9da7c9b
8 changed files with 118 additions and 156 deletions
|
|
@ -3,7 +3,6 @@
|
||||||
<classpathentry kind="src" path="src"/>
|
<classpathentry kind="src" path="src"/>
|
||||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||||
<classpathentry kind="lib" path="libs/mysql-connector-java-5.0.7-bin.jar"/>
|
<classpathentry kind="lib" path="libs/mysql-connector-java-5.0.7-bin.jar"/>
|
||||||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3"/>
|
|
||||||
<classpathentry kind="lib" path="libs/dom4j-1.6.1.jar" sourcepath="C:/Users/Ziver/Documents/Programmering/Java/libs/dom4j-1.6.1/src"/>
|
<classpathentry kind="lib" path="libs/dom4j-1.6.1.jar" sourcepath="C:/Users/Ziver/Documents/Programmering/Java/libs/dom4j-1.6.1/src"/>
|
||||||
<classpathentry kind="lib" path="libs/wsdl4j-1.6.2.jar"/>
|
<classpathentry kind="lib" path="libs/wsdl4j-1.6.2.jar"/>
|
||||||
<classpathentry kind="lib" path="libs/javassist.jar" sourcepath="C:/Users/Ziver/Documents/Programmering/Java/libs/javassist-3.12.GA"/>
|
<classpathentry kind="lib" path="libs/javassist.jar" sourcepath="C:/Users/Ziver/Documents/Programmering/Java/libs/javassist-3.12.GA"/>
|
||||||
|
|
@ -11,5 +10,6 @@
|
||||||
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
|
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
|
||||||
<classpathentry kind="lib" path="libs/commons-fileupload-1.2.1.jar" sourcepath="C:/Users/Ziver/Documents/Programmering/Java/libs/commons/commons-fileupload-1.2.1-sources.jar"/>
|
<classpathentry kind="lib" path="libs/commons-fileupload-1.2.1.jar" sourcepath="C:/Users/Ziver/Documents/Programmering/Java/libs/commons/commons-fileupload-1.2.1-sources.jar"/>
|
||||||
<classpathentry kind="lib" path="libs/commons-io-1.4.jar"/>
|
<classpathentry kind="lib" path="libs/commons-io-1.4.jar"/>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
|
||||||
<classpathentry kind="output" path="bin"/>
|
<classpathentry kind="output" path="bin"/>
|
||||||
</classpath>
|
</classpath>
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ package zutil;
|
||||||
*/
|
*/
|
||||||
public class StringUtil {
|
public class StringUtil {
|
||||||
public static final String[] sizes = new String[]{"YB", "ZB", "EB", "PB", "TB", "GB", "MB", "kB", "B"};
|
public static final String[] sizes = new String[]{"YB", "ZB", "EB", "PB", "TB", "GB", "MB", "kB", "B"};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Present a size (in bytes) as a human-readable value
|
* Present a size (in bytes) as a human-readable value
|
||||||
*
|
*
|
||||||
|
|
@ -26,5 +26,52 @@ public class StringUtil {
|
||||||
return value+" "+sizes[total];
|
return value+" "+sizes[total];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trims the given char and whitespace at the beginning and the end
|
||||||
|
*
|
||||||
|
* @param str is the string to trim
|
||||||
|
* @param trim is the char to trim
|
||||||
|
* @return a trimmed String
|
||||||
|
*/
|
||||||
|
public static String trim(String str, char trim){
|
||||||
|
if( str == null || str.isEmpty() )
|
||||||
|
return str;
|
||||||
|
int start=0, stop=str.length();
|
||||||
|
// The beginning
|
||||||
|
for(int i=0; i<str.length() ;i++){
|
||||||
|
char c = str.charAt( i );
|
||||||
|
if( c <= ' ' || c == trim )
|
||||||
|
start = i+1;
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// The end
|
||||||
|
for(int i=str.length()-1; i>start ;i--){
|
||||||
|
char c = str.charAt( i );
|
||||||
|
if( c <= ' ' || c == trim )
|
||||||
|
stop = i;
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if( start >= str.length() )
|
||||||
|
return "";
|
||||||
|
//System.out.println("str: \""+str+"\" start: "+start+" stop: "+stop);
|
||||||
|
return str.substring(start, stop);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trims the whitespace and quotes if the string starts and ends with one
|
||||||
|
*
|
||||||
|
* @param str is the string to trim
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String trimQuotes(String str){
|
||||||
|
if( str == null )
|
||||||
|
return null;
|
||||||
|
str = str.trim();
|
||||||
|
if( str.length() >= 2 && str.charAt(0)=='\"' && str.charAt(str.length()-1)=='\"'){
|
||||||
|
str = str.substring(1, str.length()-1);
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,9 +14,11 @@ import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import zutil.db.DBConnection;
|
import zutil.db.DBConnection;
|
||||||
|
import zutil.log.CompactLogFormatter;
|
||||||
import zutil.log.LogUtil;
|
import zutil.log.LogUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -195,19 +197,19 @@ public abstract class DBBean {
|
||||||
query.append( " SET" );
|
query.append( " SET" );
|
||||||
query.append( params );
|
query.append( params );
|
||||||
if( id != null )
|
if( id != null )
|
||||||
query.append( " id=?" );
|
query.append( ", "+config.id_field.getName()+"=?" );
|
||||||
}
|
}
|
||||||
logger.fine("Save query: "+query.toString());
|
logger.fine("Save query: "+query.toString());
|
||||||
PreparedStatement stmt = db.getPreparedStatement( query.toString() );
|
PreparedStatement stmt = db.getPreparedStatement( query.toString() );
|
||||||
// Put in the variables in the SQL
|
// Put in the variables in the SQL
|
||||||
for(int i=0; i<config.fields.size() ;++i ){
|
for(int i=0; i<config.fields.size() ;++i ){
|
||||||
Field field = config.fields.get(i);
|
Field field = config.fields.get(i);
|
||||||
|
|
||||||
// Another DBBean class
|
// Another DBBean class
|
||||||
if( DBBean.class.isAssignableFrom( field.getType() )){
|
if( DBBean.class.isAssignableFrom( field.getType() )){
|
||||||
DBBean subobj = (DBBean)getFieldValue(field);
|
DBBean subobj = (DBBean)getFieldValue(field);
|
||||||
if(subobj != null){
|
if(subobj != null){
|
||||||
subobj.save(db);
|
subobj.save(db);
|
||||||
DBBeanConfig subconfig = getBeanConfig(subobj.getClass());
|
DBBeanConfig subconfig = getBeanConfig(subobj.getClass());
|
||||||
stmt.setObject(i+1, subobj.getFieldValue(subconfig.id_field) );
|
stmt.setObject(i+1, subobj.getFieldValue(subconfig.id_field) );
|
||||||
}
|
}
|
||||||
|
|
@ -246,11 +248,12 @@ public abstract class DBBean {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if( id != null )
|
if( id != null )
|
||||||
stmt.setObject(config.fields.size(), id);
|
stmt.setObject(config.fields.size()+1, id);
|
||||||
|
|
||||||
// Execute the SQL
|
// Execute the SQL
|
||||||
DBConnection.exec(stmt);
|
DBConnection.exec(stmt);
|
||||||
setFieldValue( config.id_field, db.getLastInsertID() );
|
if( id == null )
|
||||||
|
setFieldValue( config.id_field, db.getLastInsertID() );
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw e;
|
throw e;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
@ -265,7 +268,7 @@ public abstract class DBBean {
|
||||||
Class<? extends DBBean> c = this.getClass();
|
Class<? extends DBBean> c = this.getClass();
|
||||||
DBBeanConfig config = beanConfigs.get(c);
|
DBBeanConfig config = beanConfigs.get(c);
|
||||||
if( config.id_field == null )
|
if( config.id_field == null )
|
||||||
throw new NoSuchElementException("DBTableID annotation missing in bean!");
|
throw new NoSuchElementException("ID field is null!");
|
||||||
try {
|
try {
|
||||||
String sql = "DELETE FROM "+config.tableName+" WHERE "+ config.id_field +"=?";
|
String sql = "DELETE FROM "+config.tableName+" WHERE "+ config.id_field +"=?";
|
||||||
logger.fine("Load query: "+sql);
|
logger.fine("Load query: "+sql);
|
||||||
|
|
|
||||||
|
|
@ -131,8 +131,14 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
|
||||||
return obj;
|
return obj;
|
||||||
obj = bean_class.newInstance();
|
obj = bean_class.newInstance();
|
||||||
cacheDBBean(obj, id);
|
cacheDBBean(obj, id);
|
||||||
|
|
||||||
for( Field field : bean_config.fields ){
|
// Get id field
|
||||||
|
obj.setFieldValue(bean_config.id_field, result.getLong(bean_config.id_field.getName()));
|
||||||
|
// Get the rest
|
||||||
|
for( Field field : bean_config.fields ){
|
||||||
|
// Skip id field (its already loaded)
|
||||||
|
if( field.equals(bean_config.id_field) )
|
||||||
|
continue;
|
||||||
String name = field.getName();
|
String name = field.getName();
|
||||||
|
|
||||||
// Another DBBean class
|
// Another DBBean class
|
||||||
|
|
|
||||||
|
|
@ -49,18 +49,25 @@ public class LogUtil {
|
||||||
/**
|
/**
|
||||||
* Sets the global log level
|
* Sets the global log level
|
||||||
*/
|
*/
|
||||||
public static void setGlobalLogLevel(Level level){
|
public static void setGlobalLevel(Level level){
|
||||||
setLevel("", level);
|
setLevel("", level);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the log level for a specified class
|
||||||
|
*/
|
||||||
|
public static void setLevel(Class<?> c, Level level){
|
||||||
|
setLevel(c.getName(), level);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the log level for a specified logger
|
* Sets the log level for a specified logger
|
||||||
*/
|
*/
|
||||||
public static void setLevel(String name, Level level){
|
public static void setLevel(String name, Level level){
|
||||||
Logger root = Logger.getLogger("");
|
Logger logger = Logger.getLogger(name);
|
||||||
root.setLevel(level);
|
logger.setLevel(level);
|
||||||
for (Handler handler : root.getHandlers()) {
|
//for (Handler handler : logger.getHandlers()) {
|
||||||
handler.setLevel(level);
|
// handler.setLevel(level);
|
||||||
}
|
//}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,137 +0,0 @@
|
||||||
package zutil.test;
|
|
||||||
import zutil.algo.sort.QuickSort;
|
|
||||||
import zutil.algo.sort.sortable.SortableIntArray;
|
|
||||||
import junit.framework.*;
|
|
||||||
|
|
||||||
public class QuickSortTest extends TestCase {
|
|
||||||
public static boolean debug = false;
|
|
||||||
//the size of the arrays to be tested
|
|
||||||
private static final int[] cases = new int[]{10000,100000,1000000,10000000};
|
|
||||||
//the type of array to be tested
|
|
||||||
// 0 = random
|
|
||||||
// 1 = mirror
|
|
||||||
// 2 = sorted
|
|
||||||
private static final int[] types = new int[]{0,1,2};
|
|
||||||
//the strings for the diffrent arrays
|
|
||||||
private static final String[] typesS = new String[]{"Random array","Mirrored array","Sorted array"};
|
|
||||||
//the pivots that will be tested
|
|
||||||
// 0 = random
|
|
||||||
// 1 = median
|
|
||||||
// 2 = middle
|
|
||||||
private static final int[] pivots = new int[]{0,1,2};
|
|
||||||
//the strings for the pivots
|
|
||||||
private static final String[] pivotsS = new String[]{"Random pivot","Median pivot","Half pivot"};
|
|
||||||
//the current array size index of cases
|
|
||||||
private static int currentCase = 0;
|
|
||||||
//the current type of arrays
|
|
||||||
private static int typeCase = 0;
|
|
||||||
//the current pivot to use
|
|
||||||
private static int pivotCase = 0;
|
|
||||||
//the current state of using insertionsort in quicksort
|
|
||||||
private static boolean insertSort;
|
|
||||||
//the temp array that will be sorted
|
|
||||||
private int[] array;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*The main method to run the test. was going to use junit but did
|
|
||||||
*no find a way to loop the test like in this method.
|
|
||||||
*/
|
|
||||||
public static void main(String[] args){
|
|
||||||
QuickSortTest test = new QuickSortTest("Test");
|
|
||||||
insertSort = true;
|
|
||||||
//the insertion sort tests loop
|
|
||||||
for(int z=0; z<2 ; insertSort=false,z++){
|
|
||||||
System.out.println("**************** Whit insertionSort: "+insertSort+" *****************");
|
|
||||||
// the pivots tests loop
|
|
||||||
for(pivotCase=0; pivotCase<pivots.length ;pivotCase++){
|
|
||||||
System.out.println("********** "+pivotsS[pivots[pivotCase]]+" ***********");
|
|
||||||
//the array size tests loop
|
|
||||||
for(typeCase=0; typeCase<types.length ;typeCase++){
|
|
||||||
currentCase = 0;
|
|
||||||
System.out.println("**** "+typesS[types[typeCase]]+" ****");
|
|
||||||
//the array size loop
|
|
||||||
for(currentCase=0; currentCase<cases.length ;currentCase++){
|
|
||||||
try{
|
|
||||||
test.setUp();
|
|
||||||
test.TestSort();
|
|
||||||
test.tearDown();
|
|
||||||
}catch(Exception e){
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public QuickSortTest(String name){
|
|
||||||
super(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* setUp() method that initializes common objects
|
|
||||||
*/
|
|
||||||
public void setUp() throws Exception{
|
|
||||||
super.setUp();
|
|
||||||
array = new int[cases[currentCase]];
|
|
||||||
|
|
||||||
switch(types[typeCase]){
|
|
||||||
//Randome Array test
|
|
||||||
case 0:
|
|
||||||
for(int i=0; i<array.length ;i++){
|
|
||||||
array[i] = (int)(Math.random()*array.length*10);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
//Mirrored Array test
|
|
||||||
case 1:
|
|
||||||
for(int i=0; i<array.length ;i++){
|
|
||||||
array[i] = array.length-i;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
//Sorted Array test
|
|
||||||
case 2:
|
|
||||||
for(int i=0; i<array.length ;i++){
|
|
||||||
array[i] = i;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* tearDown() method that cleanup the common objects
|
|
||||||
*/
|
|
||||||
public void tearDown() throws Exception {
|
|
||||||
super.tearDown();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*Tests if the array is sorted
|
|
||||||
*/
|
|
||||||
public void TestSort() {
|
|
||||||
long time = System.currentTimeMillis();
|
|
||||||
if(debug){
|
|
||||||
for(int i=0; i<array.length ;i++)
|
|
||||||
System.out.print(array[i]+", ");
|
|
||||||
System.out.println("");
|
|
||||||
}
|
|
||||||
QuickSort.sort(new SortableIntArray(array),pivotCase,insertSort);
|
|
||||||
//Sort.insertionSort(array);
|
|
||||||
System.out.print("*");
|
|
||||||
//the time to sort
|
|
||||||
System.out.println(array.length+" elements: "+
|
|
||||||
((double)(System.currentTimeMillis()-time)/1000)+" sec("+
|
|
||||||
(System.currentTimeMillis()-time)+" ms)");
|
|
||||||
if(debug){
|
|
||||||
for(int i=0; i<array.length ;i++)
|
|
||||||
System.out.print(array[i]+", ");
|
|
||||||
System.out.println("");
|
|
||||||
}
|
|
||||||
//checking if sorted correct
|
|
||||||
for(int i=1; i<array.length ; i++){
|
|
||||||
if(array[i-1] > array[i]){
|
|
||||||
fail("Array not sorted!! ("+array[i-1]+" > "+array[i]+")");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -3,10 +3,9 @@ import zutil.algo.sort.MergeSort;
|
||||||
import zutil.algo.sort.QuickSort;
|
import zutil.algo.sort.QuickSort;
|
||||||
import zutil.algo.sort.SimpleSort;
|
import zutil.algo.sort.SimpleSort;
|
||||||
import zutil.algo.sort.sortable.SortableIntArray;
|
import zutil.algo.sort.sortable.SortableIntArray;
|
||||||
import junit.framework.*;
|
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public class SortTestSimple extends TestCase {
|
public class SortTestSimple {
|
||||||
public static final int SIZE = 10000;
|
public static final int SIZE = 10000;
|
||||||
public static final int MAX_INT = 10000;
|
public static final int MAX_INT = 10000;
|
||||||
|
|
||||||
|
|
|
||||||
37
src/zutil/test/StringUtilTest.java
Normal file
37
src/zutil/test/StringUtilTest.java
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
package zutil.test;
|
||||||
|
|
||||||
|
import org.junit.*;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import zutil.StringUtil;
|
||||||
|
|
||||||
|
public class StringUtilTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void formatBytesToStringTest() {
|
||||||
|
assertEquals( StringUtil.formatBytesToString( 100 ), "100.0 B" );
|
||||||
|
assertEquals( StringUtil.formatBytesToString( 10000 ), "9.7 kB" );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void trimTest() {
|
||||||
|
assertEquals( StringUtil.trim("", ' '), "" );
|
||||||
|
assertEquals( StringUtil.trim(" aa ", ' '), "aa" );
|
||||||
|
assertEquals( StringUtil.trim("aa ", ' '), "aa" );
|
||||||
|
assertEquals( StringUtil.trim(" aa", ' '), "aa" );
|
||||||
|
assertEquals( StringUtil.trim(" aa ", 'a'), "" );
|
||||||
|
assertEquals( StringUtil.trim("\u0010 aa ", ' '), "aa" );
|
||||||
|
assertEquals( StringUtil.trim("\n\naa\n\t", ' '), "aa" );
|
||||||
|
assertEquals( StringUtil.trim("\"aa\"", '\"'), "aa" );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void trimQuotesTest() {
|
||||||
|
assertEquals( StringUtil.trimQuotes(""), "" );
|
||||||
|
assertEquals( StringUtil.trimQuotes("\""), "\"" );
|
||||||
|
assertEquals( StringUtil.trimQuotes("\"\""), "" );
|
||||||
|
assertEquals( StringUtil.trimQuotes("\"aa"), "\"aa" );
|
||||||
|
assertEquals( StringUtil.trimQuotes("aa\""), "aa\"" );
|
||||||
|
assertEquals( StringUtil.trimQuotes("\"aa\""), "aa" );
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue