Error in JUnit with static final List - java

I've written a JUnit where it is calling a Constant class, here we are trying to access a static final List
public static final List<String> SKIPPED_DIMENSION_LIST = new ArrayList<String>();
static{ SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_DISPLAY);
SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_SUB);
SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_SUB_GROUP);
SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_SUB_GROUP_ID);
SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_ID);
SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_GROUP_ID); }
Now, while accessing this List via JUnit thread I'm gettting this error.
java.lang.ExceptionInInitializerError
at java.lang.J9VMInternals.initialize(J9VMInternals.java:222)
at com.mns.commerce.searchnav.constants.MSEndecaConstants.<clinit>(MSEndecaConstants.java:169)
at java.lang.J9VMInternals.initializeImpl(Native Method)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:200)
at com.mns.commerce.searchnav.utils.MSSearchResposeBOTransformer.getRefinementsDetails(MSSearchResposeBOTransformer.java:460)
at com.mns.commerce.searchnav.utils.MSSearchResposeBOTransformer.buildFacets(MSSearchResposeBOTransformer.java:362)
at com.mns.commerce.searchnav.utils.MSSearchResposeBOTransformer.populateResults(MSSearchResposeBOTransformer.java:106)
at com.mns.commerce.searchnavunit.test.MSSearchResposeBOTransformerTest.testPopulateResults(MSSearchResposeBOTransformerTest.java:97)
Whereas, if an static Enum is defined instead of a static final list then it is working fine. Any idea why this is happening?

This code is also running and I don't have any error with this code. My JDK version is 1.6.
import java.util.ArrayList;
import java.util.List;
public class test {
private static final String PRODUCT_CATEGORY_DISPLAY = "a";
private static final String PRODUCT_CATEGORY_SUB = "b";
private static final String PRODUCT_CATEGORY_SUB_GROUP = "c";
private static final String PRODUCT_CATEGORY_SUB_GROUP_ID = "d";
private static final String PRODUCT_CATEGORY_ID = "e";
private static final String PRODUCT_CATEGORY_GROUP_ID = "f";
public static final List<String> SKIPPED_DIMENSION_LIST = new ArrayList<String>();
static{
SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_DISPLAY);
SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_SUB);
SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_SUB_GROUP);
SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_SUB_GROUP_ID);
SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_ID);
SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_GROUP_ID);
} /** * #param args */
public static void main(String[] args) {
System.out.println(SKIPPED_DIMENSION_LIST);
}
}

Related

Would setting a field in a class as a new instance of the same class cause memory overflow? InetValidator class (apache.commons)

I am using InetValidator class from apache.commons in my project to validate IP addresses. While going through the source code,
public class InetAddressValidator implements Serializable {
private static final int IPV4_MAX_OCTET_VALUE = 255;
private static final int MAX_UNSIGNED_SHORT = 65535;
private static final int BASE_16 = 16;
private static final long serialVersionUID = -919201640201914789L;
private static final String IPV4_REGEX = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$";
private static final int IPV6_MAX_HEX_GROUPS = 8;
private static final int IPV6_MAX_HEX_DIGITS_PER_GROUP = 4;
private static final InetAddressValidator VALIDATOR = new InetAddressValidator();
private final RegexValidator ipv4Validator = new RegexValidator("^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$");
public InetAddressValidator() {
}
public static InetAddressValidator getInstance() {
return VALIDATOR;
}
I observed there is a VALIDATOR field which is calling the constructor again.
What I'm confused with is if I create a new instance of this class as,
InetAddressValidator addressValidator = new InetAddressValidator();
won't it will recursively keep creating new instances of the same class (as the field VALIDATOR creates a new instance everytime) and eventually I'm gonna run out of heap space?
I haven't tested this yet, but wondering if this is something I have to keep in mind while testing.
private static final InetAddressValidator VALIDATOR = new InetAddressValidator();
it's a static field, so it will be instantiated just once at class load time, not per-instance in the constructor.
Recursive call
InetAddressValidator addressValidator = new InetAddressValidator(); Recursive call

I am not getting FLAVOR string in BuildConfig , it is Missing in BuildConfig in Android Studio

How to generate the missing one ? public static final String FLAVOR = "";`
Missing FLAVOR in BuildConfig in Android Studio.It should be like this
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.arkam.konk.look";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0.0";
}
But in my case getting like without this one public static final String FLAVOR = "";
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.arkam.konk.look";
public static final String BUILD_TYPE = "debug";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0.0";}
How to generate the missing one ???
use : Build -> Clean Project and then File -> Invalidate Caches / Restart. and then build your App on smartphone or emulator.
EDIT :
if it does't work, create a new project and copy your classes and codes in that!

Correct way to use static parameters in Android

I am pretty new in Android and I do not know how is the proper way to manage static constants. I mean, I need to use several constants (such as COMMAND_BACK = 100) in several Java classes and activities. It is not beautiful to declare them as attributes in each single activity so, what is the correct way to do this?
I though about declaring them in strings.xml, but it does not seem suitable neither...
Thanks in advance.
You can make a class like this :
public final class AppConstants {
//put all the constant here
// Eg :
public static final int SPLASH_TIME = 1000;
}
The disadvantage by declaring it in a resource.xml file is that you need a context to receive the value. This is fine as long as you need those values inside a context class otherwise you have to pass one around.
The elegant solution would be extending the Application class since the android os itself uses static fields that way.
Declare
public final class ConstantClass {
public final static int COMMAND_BACK = 100;
}
Usage
int num = ConstantClass.COMMAND_BACK;
Add a Constants class to the project
public class Constants {
public static final String STRING1 = "First String";
public static final String STRING2 = "Second String";
public static final int INTEGER1 = 1;
public static final float FLOAT1 = 0.1f;
}
// Use
textView.setText(Constants.STRING1);
Create a common interface where you can declare all the constants.Constants can further be grouped here to make it mode clean.
public interface Constants {
public interface XYZ{
public static final int A= 1;
public static final int B= 2;
}
public interface REPORT_TYPE_FLAGS{
public static final String C= "0";
public static final String D= "1";
}
}
Another elegant way is to define a constant class with other inner subclasses
`private final class Constant {
public static class TypeOne {
public static final String NAME = "Type 1";
public static final int CODE = 1;
}
public static class TypeTwo {
public static final String NAME = "Type 2";
public static final int CODE = 2;
}
}
`
And you can access it in this way
`String typeOneName = Constant.TypeOne.NAME;
int typeTwoCode = Constant.TypeTwo.CODE;
`

java.lang.ExceptionInInitializerError using MVC

I get this error when I try to compile my code. I am not sure what is wrong with it and I don't understand what is the error saying. Here is the error messaged followed by the lines where it says it errors:
Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
at g52gui.C_login.<clinit>(C_login.java:9)
at g52gui.V_login.<init>(V_login.java:6)
at g52gui.V_login$4.run(V_login.java:181)
...
and the lines where it errors:
line 9 C_login (controller): private static final C_home controller_home = new C_home();
line 6 V_login (view): private final C_login controller_login = new C_login();
line 181 V_login (view): new V_login().setVisible(true);
It seems like the problem comes from the C_home but there is no compilation errors in there.
EDIT
I think the problem might be here, in C_login:
public class C_login {
private static final V_login view_login = new V_login();
private static final M_login model_login = new M_login();
private final static C_home controller_home = new C_home();
private static final C_registration controller_regi = new C_registration();
private static final MySQLAccess sql_connection = new MySQLAccess();
public static void main(String[] args) throws Exception {
view_login.setVisible(true);
}
public static boolean login_button_clicked(String usrn, String psw){
String login = M_login.login(usrn, psw);
if (login == "ok"){
controller_home.start_view(usrn);
view_login.setVisible(false);
} else if (login == "fail" ) {
view_login.error_login();
return false;
} else {
view_login.error_connection();
return false;
}
return false;
}
I delcare C_home controller_home as static so I can access it later. Would there be another way to go around this and could this be the problem ?
EDIT
here are the Initializer in C_Home:
public class C_home {
private static final V_home view_home = new V_home();
private static final M_home model_home = new M_home();
private String username = "";
/* attributes for 3d model */
private BranchGroup sceneBranchGroup = null;
private RotationInterpolator rotator = null;
private Canvas3D offScreenCanvas3D = null;
private ImageComponent2D imageComponent = null;
private static final int offScreenWidth = 200;
private static final int offScreenHeight = 200;

What are the possible ways to store this data?

Well, I need this data to be used in at least two classes:
String navHome = "home";
String navAbout = "about";
String navUpload = "upload";
String navUploadProc = "uploadProc";
String navStartUpload = "startUpload";
String navContact = "contact";
String navSearch = "search";
String navManual = "manual";
String navBackToGmis = "backToGmis";
I was thinking of just pasting that into other classes, but then it violates the principles of programming, since it's repeating the same thing. Text and XML files are not comfortable to store data, inheritance isn't an option, since Java doesn't support multiple inheritance... Any ideas?
Yeah, I know it will sound retarded to most of you, but I need tipps on this.
If these are constants, then you can declare them in one class (or interface) and use them in another:
A.java:
public class A {
public static final String navHome = "home";
public static final String navAbout = "about";
public static final String navUpload = "upload";
public static final String navUploadProc = "uploadProc";
public static final String navStartUpload = "startUpload";
public static final String navContact = "contact";
public static final String navSearch = "search";
public static final String navManual = "manual";
public static final String navBackToGmis = "backToGmis";
. . .
}
B.java:
import static A.*; // or list each String in a separate import
public class B {
. . . // code can use nav* as if they were declared in class B
}

Categories

Resources