Unit tests with java
I'm working on test cases in java.
I have a code something like this:
class Sample
{
public boolean isALive(boolean isLive, int num)
{
return if isLive ? num == 3 : num == 2 || num == 3;
}
}
In the tests, it will be something like this,
assertTrue(x.isAlive(true,1); with various combinations
Is there way to simplify or optimize this even more for all the tests to
pass ?
Saturday, 14 September 2013
Converting a PHP function to Objective-C and Java?
Converting a PHP function to Objective-C and Java?
I've written a function in PHP that I now need to write in Objective-C and
Java for an iOS and Android app respectively, but having issues with where
to even start.
The function is:
public static function decrypt($string, $key='my_key')
{
$result = '';
$string = str_replace(' ', '+', $string);
$string = base64_decode($string);
$string = str_replace($key, '', $string);
for($i=0; $i<strlen($string); $i++) {
$char = substr($string, $i, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char)-ord($keychar));
$result.=$char;
}
return $result;
}
Result should return string:id
Anyone able to show me how to covert this into both languages? Any
guidance would be greatly appreciated.
Thanks.
I've written a function in PHP that I now need to write in Objective-C and
Java for an iOS and Android app respectively, but having issues with where
to even start.
The function is:
public static function decrypt($string, $key='my_key')
{
$result = '';
$string = str_replace(' ', '+', $string);
$string = base64_decode($string);
$string = str_replace($key, '', $string);
for($i=0; $i<strlen($string); $i++) {
$char = substr($string, $i, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char)-ord($keychar));
$result.=$char;
}
return $result;
}
Result should return string:id
Anyone able to show me how to covert this into both languages? Any
guidance would be greatly appreciated.
Thanks.
Friday, 13 September 2013
CustomListView and CursorAdapter Searching issue
CustomListView and CursorAdapter Searching issue
I have a custom ListView and I can access and show data from database. I
also have serach filter to search data in customListview
/*
* Serach Filter
*/
cursorAdapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return dbHelper.fetchDataByName(constraint.toString());
}
});
searchOption.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int start, int before,
int count) {
// TODO Auto-generated method stub
AbstractActivity.this.cursorAdapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int
count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
The problem is When I don't perform any search, onItemClick is working
fine. But, when I try to Search any ListView item and want to access that
item. It shows this
09-14 06:17:50.280: E/AndroidRuntime(1091): FATAL EXCEPTION: main
09-14 06:17:50.280: E/AndroidRuntime(1091):
java.lang.IllegalArgumentException: column 'AF_NAME' does not exist
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
com.yasiradnan.abstracts.AbstractActivity$1.onItemClick(AbstractActivity.java:111)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.widget.AdapterView.performItemClick(AdapterView.java:298)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.widget.AbsListView.performItemClick(AbsListView.java:1100)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.widget.AbsListView$1.run(AbsListView.java:3423)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.os.Handler.handleCallback(Handler.java:725)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.os.Handler.dispatchMessage(Handler.java:92)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.os.Looper.loop(Looper.java:137)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.app.ActivityThread.main(ActivityThread.java:5041)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
java.lang.reflect.Method.invokeNative(Native Method)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
java.lang.reflect.Method.invoke(Method.java:511)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
dalvik.system.NativeStart.main(Native Method)
Here is the onitemCLick
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3) {
// TODO Auto-generated method stub
cursor = (Cursor)arg0.getAdapter().getItem(position);
String Text = cursor.getString(cursor.getColumnIndexOrThrow("TEXT"));
String Title =
cursor.getString(cursor.getColumnIndexOrThrow("TITLE"));
String Topic =
cursor.getString(cursor.getColumnIndexOrThrow("TOPIC"));
String value = cursor.getString(cursor.getColumnIndexOrThrow("_id"));
String afName =
cursor.getString(cursor.getColumnIndexOrThrow("AF_NAME"));
String email =
cursor.getString(cursor.getColumnIndexOrThrow("CORRESPONDENCE"));
String refs = cursor.getString(cursor.getColumnIndexOrThrow("REFS"));
String acknowledgements =
cursor.getString(cursor.getColumnIndexOrThrow("ACKNOWLEDGEMENTS"));
Log.e("Position", String.valueOf(position));
int itemNumber = cursor.getCount();
Intent in = new Intent(getApplicationContext(),
AbstractContent.class);
in.putExtra("abstracts", Text);
in.putExtra("Title", Title);
in.putExtra("Topic", Topic);
in.putExtra("value", value);
in.putExtra("afName", afName);
in.putExtra("email", email);
in.putExtra("refs", refs);
in.putExtra("itemNumber", itemNumber);
in.putExtra("acknowledgements",acknowledgements);
startActivity(in);
}
});
Here is my Adapter Code:
Cursor cursorOne;
String getName;
@SuppressWarnings("deprecation")
public AbstractCursorAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
TextView title = (TextView)view.findViewById(R.id.abTitle);
title.setText(cursor.getString(cursor.getColumnIndexOrThrow("TITLE")));
TextView topic = (TextView)view.findViewById(R.id.abTopic);
topic.setText(cursor.getString(cursor.getColumnIndexOrThrow("TOPIC")));
TextView type = (TextView)view.findViewById(R.id.abType);
type.setText(cursor.getString(cursor.getColumnIndexOrThrow("TYPE")));
String value = cursor.getString(cursor.getColumnIndexOrThrow("_id"));
String sqlQuery = "select abstracts_item._id AS
ID,abstract_author.NAME AS NAME from
abstracts_item,abstract_author,authors_abstract where
abstracts_item._id = authors_abstract.abstractsitem_id and
abstract_author._id = authors_abstract.abstractauthor_id and ID =
"
+ value;
cursorOne = DatabaseHelper.database.rawQuery(sqlQuery, null);
if (cursorOne != null) {
cursorOne.moveToFirst();
do {
if (cursorOne.getPosition() == 0) {
getName =
cursorOne.getString(cursorOne.getColumnIndexOrThrow("NAME"));
} else if (cursorOne.isLast()) {
getName = getName + " & "
+
cursorOne.getString(cursorOne.getColumnIndexOrThrow("NAME"));
} else {
getName = getName + " , "
+
cursorOne.getString(cursorOne.getColumnIndexOrThrow("NAME"));
}
} while (cursorOne.moveToNext());
}
TextView authorNames = (TextView)view.findViewById(R.id.SubTitle);
/*
* Get Width
*/
WindowManager WinMgr =
(WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
int displayWidth = WinMgr.getDefaultDisplay().getWidth();
Paint paint = new Paint();
Rect bounds = new Rect();
int text_height = 0;
int text_width = 0;
paint.getTextBounds(getName, 0, getName.length(), bounds);
text_height = bounds.height();
text_width = bounds.width();
if (text_width > displayWidth) {
//Log.e("Width inside",
String.valueOf(text_width)+"--------------"+String.valueOf(displayWidth));
String output = getName.split(",")[0] + " et al. ";
authorNames.setText(output);
} else {
//Log.e("Width inside",
String.valueOf(text_width)+"--------------"+String.valueOf(displayWidth));
authorNames
.setText(getName.replaceAll("((?:^|[^A-Z.])[A-Z])[a-z]*\\s(?=[A-Z])",
"$1."));
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup
viewgroup) {
// TODO Auto-generated method stub
LayoutInflater inflater =
LayoutInflater.from(viewgroup.getContext());
View returnView = inflater.inflate(R.layout.abstract_content,
viewgroup, false);
return returnView;
}
}
Now, My question why this is happening? How can I solve that ? Can you
give me a tutorial project link similar search function and
customListview, So that I can see what they're doing. I tried to find it
on google,but didn't found. Thanks
I have a custom ListView and I can access and show data from database. I
also have serach filter to search data in customListview
/*
* Serach Filter
*/
cursorAdapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return dbHelper.fetchDataByName(constraint.toString());
}
});
searchOption.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int start, int before,
int count) {
// TODO Auto-generated method stub
AbstractActivity.this.cursorAdapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int
count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
The problem is When I don't perform any search, onItemClick is working
fine. But, when I try to Search any ListView item and want to access that
item. It shows this
09-14 06:17:50.280: E/AndroidRuntime(1091): FATAL EXCEPTION: main
09-14 06:17:50.280: E/AndroidRuntime(1091):
java.lang.IllegalArgumentException: column 'AF_NAME' does not exist
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
com.yasiradnan.abstracts.AbstractActivity$1.onItemClick(AbstractActivity.java:111)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.widget.AdapterView.performItemClick(AdapterView.java:298)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.widget.AbsListView.performItemClick(AbsListView.java:1100)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.widget.AbsListView$1.run(AbsListView.java:3423)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.os.Handler.handleCallback(Handler.java:725)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.os.Handler.dispatchMessage(Handler.java:92)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.os.Looper.loop(Looper.java:137)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
android.app.ActivityThread.main(ActivityThread.java:5041)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
java.lang.reflect.Method.invokeNative(Native Method)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
java.lang.reflect.Method.invoke(Method.java:511)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-14 06:17:50.280: E/AndroidRuntime(1091): at
dalvik.system.NativeStart.main(Native Method)
Here is the onitemCLick
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3) {
// TODO Auto-generated method stub
cursor = (Cursor)arg0.getAdapter().getItem(position);
String Text = cursor.getString(cursor.getColumnIndexOrThrow("TEXT"));
String Title =
cursor.getString(cursor.getColumnIndexOrThrow("TITLE"));
String Topic =
cursor.getString(cursor.getColumnIndexOrThrow("TOPIC"));
String value = cursor.getString(cursor.getColumnIndexOrThrow("_id"));
String afName =
cursor.getString(cursor.getColumnIndexOrThrow("AF_NAME"));
String email =
cursor.getString(cursor.getColumnIndexOrThrow("CORRESPONDENCE"));
String refs = cursor.getString(cursor.getColumnIndexOrThrow("REFS"));
String acknowledgements =
cursor.getString(cursor.getColumnIndexOrThrow("ACKNOWLEDGEMENTS"));
Log.e("Position", String.valueOf(position));
int itemNumber = cursor.getCount();
Intent in = new Intent(getApplicationContext(),
AbstractContent.class);
in.putExtra("abstracts", Text);
in.putExtra("Title", Title);
in.putExtra("Topic", Topic);
in.putExtra("value", value);
in.putExtra("afName", afName);
in.putExtra("email", email);
in.putExtra("refs", refs);
in.putExtra("itemNumber", itemNumber);
in.putExtra("acknowledgements",acknowledgements);
startActivity(in);
}
});
Here is my Adapter Code:
Cursor cursorOne;
String getName;
@SuppressWarnings("deprecation")
public AbstractCursorAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
TextView title = (TextView)view.findViewById(R.id.abTitle);
title.setText(cursor.getString(cursor.getColumnIndexOrThrow("TITLE")));
TextView topic = (TextView)view.findViewById(R.id.abTopic);
topic.setText(cursor.getString(cursor.getColumnIndexOrThrow("TOPIC")));
TextView type = (TextView)view.findViewById(R.id.abType);
type.setText(cursor.getString(cursor.getColumnIndexOrThrow("TYPE")));
String value = cursor.getString(cursor.getColumnIndexOrThrow("_id"));
String sqlQuery = "select abstracts_item._id AS
ID,abstract_author.NAME AS NAME from
abstracts_item,abstract_author,authors_abstract where
abstracts_item._id = authors_abstract.abstractsitem_id and
abstract_author._id = authors_abstract.abstractauthor_id and ID =
"
+ value;
cursorOne = DatabaseHelper.database.rawQuery(sqlQuery, null);
if (cursorOne != null) {
cursorOne.moveToFirst();
do {
if (cursorOne.getPosition() == 0) {
getName =
cursorOne.getString(cursorOne.getColumnIndexOrThrow("NAME"));
} else if (cursorOne.isLast()) {
getName = getName + " & "
+
cursorOne.getString(cursorOne.getColumnIndexOrThrow("NAME"));
} else {
getName = getName + " , "
+
cursorOne.getString(cursorOne.getColumnIndexOrThrow("NAME"));
}
} while (cursorOne.moveToNext());
}
TextView authorNames = (TextView)view.findViewById(R.id.SubTitle);
/*
* Get Width
*/
WindowManager WinMgr =
(WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
int displayWidth = WinMgr.getDefaultDisplay().getWidth();
Paint paint = new Paint();
Rect bounds = new Rect();
int text_height = 0;
int text_width = 0;
paint.getTextBounds(getName, 0, getName.length(), bounds);
text_height = bounds.height();
text_width = bounds.width();
if (text_width > displayWidth) {
//Log.e("Width inside",
String.valueOf(text_width)+"--------------"+String.valueOf(displayWidth));
String output = getName.split(",")[0] + " et al. ";
authorNames.setText(output);
} else {
//Log.e("Width inside",
String.valueOf(text_width)+"--------------"+String.valueOf(displayWidth));
authorNames
.setText(getName.replaceAll("((?:^|[^A-Z.])[A-Z])[a-z]*\\s(?=[A-Z])",
"$1."));
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup
viewgroup) {
// TODO Auto-generated method stub
LayoutInflater inflater =
LayoutInflater.from(viewgroup.getContext());
View returnView = inflater.inflate(R.layout.abstract_content,
viewgroup, false);
return returnView;
}
}
Now, My question why this is happening? How can I solve that ? Can you
give me a tutorial project link similar search function and
customListview, So that I can see what they're doing. I tried to find it
on google,but didn't found. Thanks
casperjs page loading: how to auto-load page's script files?
casperjs page loading: how to auto-load page's script files?
I'm new to casperjs and my desire is to try to test the javascript loaded
by certain pages of a web app. I'm using casper.thenOpen to load a page
from the web server, but it doesn't in turn load the tags that reference
js files. I even checked the web server's access log and sure enough, only
the page itself was requested, not the js files.
I would imagine there is built in functionality for doing this, but if I
have to do this manually I can live with writing my own utility function
for it. But again, being new to casperjs, I'm not sure how I'd go about it
- I haven't been able to locate examples that will help me figure this
out.
I'm new to casperjs and my desire is to try to test the javascript loaded
by certain pages of a web app. I'm using casper.thenOpen to load a page
from the web server, but it doesn't in turn load the tags that reference
js files. I even checked the web server's access log and sure enough, only
the page itself was requested, not the js files.
I would imagine there is built in functionality for doing this, but if I
have to do this manually I can live with writing my own utility function
for it. But again, being new to casperjs, I'm not sure how I'd go about it
- I haven't been able to locate examples that will help me figure this
out.
Joomla 2.5 Select All as page limit gives zero result
Joomla 2.5 Select All as page limit gives zero result
I developed a joomla 2.5 custom component for showing data table that
implemented filtering, shorting, and pagination. But in the middle of
pagination I selected page limit as All.It doesn't gives me any
result.When I select All as page limit it works well except only above
scenario.Seams page limit is set to 0.Then db query will like SELECT* FROM
_tebleName LIMIT 0,0.
I developed a joomla 2.5 custom component for showing data table that
implemented filtering, shorting, and pagination. But in the middle of
pagination I selected page limit as All.It doesn't gives me any
result.When I select All as page limit it works well except only above
scenario.Seams page limit is set to 0.Then db query will like SELECT* FROM
_tebleName LIMIT 0,0.
why is this happening to my jquery ui animation?
why is this happening to my jquery ui animation?
This is my HTML code
<div id="menu_status_item">
<span class="menu_status_bar"></span>
</div>
This is my CSS:
#menu_status_item
{
margin-top: 40px;
width: 100%;
}
.menu_status_bar
{
margin-left: 45px;
color: #fff;
}
And this is my jquery
var statusMessage = 'Success'
var colorCode = '';
var statusText = '';
if(statusMessage.indexOf("Success") > -1) {
colorCode = '#33CC33';
statusText = 'Success';
}else{
colorCode = '#CC0000';
statusText = 'Failure';
}
$(".menu_status_bar").css("display", "inline-table");
$(".menu_status_bar").text(statusText);
$("#menu_status_item").animate({backgroundColor: colorCode}, 4000);
$("#menu_status_item").animate({backgroundColor: "#ffffff"});
$(".menu_status_bar").css("display", "none");
Basically i want the text "Success" inside the menu_status_bar to appear,
do the animation, and disappear with the display: none.
If i use the display:none at the end, the text won't even show up. If i
remove it, the animation works, but the text will remain there... if
someone selects it, its horrible because the text will be highlighted.
Does any one know what could this be? or how can i achieve the same result
but doing it differently?
Thanks.
This is my HTML code
<div id="menu_status_item">
<span class="menu_status_bar"></span>
</div>
This is my CSS:
#menu_status_item
{
margin-top: 40px;
width: 100%;
}
.menu_status_bar
{
margin-left: 45px;
color: #fff;
}
And this is my jquery
var statusMessage = 'Success'
var colorCode = '';
var statusText = '';
if(statusMessage.indexOf("Success") > -1) {
colorCode = '#33CC33';
statusText = 'Success';
}else{
colorCode = '#CC0000';
statusText = 'Failure';
}
$(".menu_status_bar").css("display", "inline-table");
$(".menu_status_bar").text(statusText);
$("#menu_status_item").animate({backgroundColor: colorCode}, 4000);
$("#menu_status_item").animate({backgroundColor: "#ffffff"});
$(".menu_status_bar").css("display", "none");
Basically i want the text "Success" inside the menu_status_bar to appear,
do the animation, and disappear with the display: none.
If i use the display:none at the end, the text won't even show up. If i
remove it, the animation works, but the text will remain there... if
someone selects it, its horrible because the text will be highlighted.
Does any one know what could this be? or how can i achieve the same result
but doing it differently?
Thanks.
Is a static variable in java treated as a pointer internally?
Is a static variable in java treated as a pointer internally?
I have a question about the key word static. Lets say for instance we have
this piece of code.
public class Foo
{
private int age;
private static int weight;
..
...
}
Say in main you create 2 objects. You change the age in one and then you
change the weight in the other. Does that mean that the weight also
changes in the first object as well? If that is the case then does that
mean that weight is a pointer?
I guess my question in a nutshell would be. How does static work
internally? Is it essentially of a pointer type?
I have a question about the key word static. Lets say for instance we have
this piece of code.
public class Foo
{
private int age;
private static int weight;
..
...
}
Say in main you create 2 objects. You change the age in one and then you
change the weight in the other. Does that mean that the weight also
changes in the first object as well? If that is the case then does that
mean that weight is a pointer?
I guess my question in a nutshell would be. How does static work
internally? Is it essentially of a pointer type?
Subscribe to:
Posts (Atom)