Accessing Variable from different package - java

I want to access the variable in package 1 from package 2.
Class file TestDriver.java from package 1
public class TestDriver {
private static TestDriver instance = new TestDriver();
private static int check;
private static int envt_num;
public static String envt,port,cpy_key;
public Connection con;
private ManageDBConnection mdbc;
private static String ENCRYPTION_KEY = "0123456789abcdef";
public void TestDriver(){
check = 20;
Properties prop = new Properties();
String propFileName = "properties/environment.properties";
try{
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(propFileName);
System.out.println(inputStream);
if (inputStream != null) {
prop.load(inputStream);
envt = prop.getProperty("envt");
port = prop.getProperty("port");
cpy_key = prop.getProperty("cpy_key");
System.out.println("http://"+envt+"/netprofile/");
//Original Login Link
/* Constants.driver.get("http://"+prop.getProperty("user").replaceAll("\\s","")+":"+NP_Decrypt.getPassword().replaceAll("\\s","")+"#"+envt+"/netprofile/");
inputStream.close();*/
//Added for Local Testing
String user = prop.getProperty("user");
String password = prop.getProperty("password");
Constants.driver.get("http://" + user + ":" + password + "#" + envt + "/test/");
// mdbc = new ManageDBConnection();
//con = mdbc.CreateConnection();
} else {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
Class file Constants.java from package 2
public class Constants
{
static Properties prop = new Properties();
// propFileName = "properties/environment.properties";
//TestDriver testdriver = new TestDriver();
static String envt = TestDriver.envt;
static String cpy_key = TestDriver.cpy_key;
//public static final
public static final WebDriver driver = new FirefoxDriver();
public static final String InventorySummaryURL ="http://"+envt+"/test/npHome.do?cpyKey="+cpy_key+"&custId=&grpId=0";
public static final String HomeSummary ="http://"+envt+"/test/npIndex.do?cpyKey="+cpy_key+"&custId=&grpId=0";
public static final String UploadStatus ="http://"+envt+"/test/downloadModuleStatus.do?cpyKey="+cpy_key+"&custId=&grpId=0" ;
public static final String ProfileStatus ="http://"+envt+"/test/myProfileStatus.do?cpyKey="+cpy_key+"&custId=&grpId=0";
}
In Constants.java the value returned for envt and cpy_key is zero. I want the value from Package 1.

Main issue is that you're confusing static fields with instance fields, if the following variables:
private static final int check;
private static final int envt_num;
private static final String user, password;
public static String envt,port,cpy_key;
are the same across the JVM, don't modify them in a instance constructor, instead you can can create an static block to update the values of them and also, you can mark them as final if the are not supposed to be changed.
public final static String envt,port,cpy_key;
static {
Properties prop = new Properties();
String propFileName = "properties/environment.properties";
try{
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(propFileName);
System.out.println(inputStream);
if (inputStream != null) {
prop.load(inputStream);
envt = prop.getProperty("envt");
port = prop.getProperty("port");
cpy_key = prop.getProperty("cpy_key");
System.out.println("http://"+envt+"/netprofile/");
//Original Login Link
/* Constants.driver.get("http://"+prop.getProperty("user").replaceAll("\\s","")+":"+NP_Decrypt.getPassword().replaceAll("\\s","")+"#"+envt+"/netprofile/");
inputStream.close();*/
//Added for Local Testing
user = prop.getProperty("user");
password = prop.getProperty("password");
} else {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}
}
public void TestDriver(){
Constants.driver.get("http://" + user + ":" + password + "#" + envt + "/test/");
// mdbc = new ManageDBConnection();
//con = mdbc.CreateConnection();
}
The key is to separate what is static field vs what is an instance fields, the issue you had was the the static field wasn't being initialized when the class was loaded, but when the instance was created.
Jose Luis

The variables envt and cpy_key are declaired static but shouldn't be. By declaring a variable to be static, you are telling the compiler/outside developers that this class doesn't have to be instantiated before you use that variable.
In your code, the envt and cpy_key variables are only initialized in the constructor of the TestDriver class. When you reference them from another class without instantiating a TestDriver you are getting a null value which is sometimes mapped to 0.
What you can do is:
Remove the static identifier from the envt and cpy_key variables, since you need to instantiate the TestDriver class before they able to be used.
Move the initialization code out of the constructor and use a static initialization block
Example:
public class TestDriver {
private static TestDriver instance = new TestDriver();
private static int check;
private static int envt_num;
public static String envt,port,cpy_key;
public Connection con;
private ManageDBConnection mdbc;
private static String ENCRYPTION_KEY = "0123456789abcdef";
static {
check = 20;
Properties prop = new Properties();
String propFileName = "properties/environment.properties";
try{
InputStream inputStream = TestDriver.getClass().getClassLoader().getResourceAsStream(propFileName);
System.out.println(inputStream);
if (inputStream != null) {
prop.load(inputStream);
envt = prop.getProperty("envt");
port = prop.getProperty("port");
cpy_key = prop.getProperty("cpy_key");
System.out.println("http://"+envt+"/netprofile/");
//Original Login Link
/* Constants.driver.get("http://"+prop.getProperty("user").replaceAll("\\s","")+":"+NP_Decrypt.getPassword().replaceAll("\\s","")+"#"+envt+"/netprofile/");
inputStream.close();*/
//Added for Local Testing
String user = prop.getProperty("user");
String password = prop.getProperty("password");
Constants.driver.get("http://" + user + ":" + password + "#" + envt + "/test/");
// mdbc = new ManageDBConnection();
//con = mdbc.CreateConnection();
} else {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}
}catch(Exception e){
e.printStackTrace();
}
}
So that fixes your initialization problem, but that doesn't solve your bigger class design problem. You should figure out a better (more Object Oriented) way of handling data. You have two classes relying on each other's static variables. This is a baaaad plan. I would advise you to encapsulate the variables that need initializing and keep them in the TestDriver class. Constants classes really should only be for things that you know ahead of time (hash key labels, numerical constants, etc).

Related

How to generate .dot file using Schemacrawler

Using schemcrawler I've generated html file
public final class ExecutableExample {
public static void main(final String[] args) throws Exception {
// Set log level
new LoggingConfig(Level.OFF);
final LimitOptionsBuilder limitOptionsBuilder = LimitOptionsBuilder.builder()
.includeSchemas(new IncludeAll())
.includeTables(new IncludeAll());
final LoadOptionsBuilder loadOptionsBuilder =
LoadOptionsBuilder.builder()
// Set what details are required in the schema - this affects the
// time taken to crawl the schema
.withSchemaInfoLevel(SchemaInfoLevelBuilder.standard());
final SchemaCrawlerOptions options =
SchemaCrawlerOptionsBuilder.newSchemaCrawlerOptions()
.withLimitOptions(limitOptionsBuilder.toOptions())
.withLoadOptions(loadOptionsBuilder.toOptions());
final Path outputFile = getOutputFile(args);
final OutputOptions outputOptions =
OutputOptionsBuilder.newOutputOptions(TextOutputFormat.html, outputFile);
final String command = "schema";
try (Connection connection = getConnection()) {
final SchemaCrawlerExecutable executable = new SchemaCrawlerExecutable(command);
executable.setSchemaCrawlerOptions(options);
executable.setOutputOptions(outputOptions);
executable.setConnection(connection);
executable.execute();
}
System.out.println("Created output file, " + outputFile);
}
private static Connection getConnection() {
final String connectionUrl = "jdbc:postgresql://localhost:5433/table_accounts";
final DatabaseConnectionSource dataSource = new DatabaseConnectionSource(connectionUrl);
dataSource.setUserCredentials(new SingleUseUserCredentials("postgres", "new_password"));
return dataSource.get();
}
private static Path getOutputFile(final String[] args) {
final String outputfile;
if (args != null && args.length > 0 && !isBlank(args[0])) {
outputfile = args[0];
} else {
outputfile = "./schemacrawler_output.html";
}
final Path outputFile = Paths.get(outputfile).toAbsolutePath().normalize();
return outputFile;
}
But I want to have an output in .dot file that contains diagram, node, graph, edge etc.. So how can I do it using my code or maybe some another way to do it with Java?
Simply change the output format from TextOutputFormat.html to DiagramOutputFormat.scdot.
Sualeh Fatehi, SchemaCrawler

Initialize variable with value stored in an external file

I have an Email class which is abstract. It has several children: AuthenticationEmail, MarketingEmail, etc. I want to initialize value of a field (which is final static) with a string stored in an external file.
At first I though I could use Spring's #Value but it turned out that the class needs to be a component. Then I tried the following code (static initialization and etc.):
public abstract class UserAccountAuthenticationEmail extends Email implements Serializable {
#Value("${email.address.from.authentication}")
private final static String SENDER_EMAIL_ADDRESS;
static {
Properties prop = new Properties();
String propFileName = "config.properties";
InputStream inputStream;
if (inputStream != null) {
prop.load(inputStream);
inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
} else {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}
}
#Override
public String getSender() {
return SENDER_EMAIL_ADDRESS;
}
}
It doesn't work either, as getClass is a non-static method and cannot be instantiated inside the static block.
How can I initialize the value of this variable from a file? and preferably only one time. Is there any standard method to do that? something like #Value, instead of manually reading from IO?
Hope it can help you. A static final variable can't be changed after the first initialization.
public class UserAccountAuthenticationEmail implements Serializable {
private final static String SENDER_EMAIL_ADDRESS =getVal();
public static String getVal() {
try {
Properties prop = new Properties();
String propFileName = "C:\\SMS\\config.properties";
InputStream inputStream;
inputStream = new FileInputStream(propFileName);
if (inputStream != null) {
prop.load(inputStream);
return prop.getProperty("email");
} else {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}
}
catch (Exception e){
e.printStackTrace();
}
return "";
}
public static void main(String[] args) {
System.out.println(SENDER_EMAIL_ADDRESS);
}
}
Fixed it this way:
private final static String DEFAULT_SENDER_EMAIL_ADDRESS;
static {
String value = "";
try {
Properties prop = new Properties();
String propFileName = "application.properties";
InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream(propFileName);
if (inputStream != null) {
prop.load(inputStream);
value = prop.getProperty("email.authentication.sender");
}
}
catch (Exception e){
e.printStackTrace();
}
DEFAULT_SENDER_EMAIL_ADDRESS = value;
}
public String getSender() {
return DEFAULT_SENDER_EMAIL_ADDRESS;
}

Get int, float, boolean and string from Properties

I have int, float, boolean and string from Properties file. Everything has loaded in Properties. Currently, I am parsing values as I know expected value for particular key.
Boolean.parseBoolean("false");
Integer.parseInt("3")
What is better way of setting these constants values, If I don't know what could be primitive value datatype for a key.
public class Messages {
Properties appProperties = null;
FileInputStream file = null;
public void initialization() throws Exception {
appProperties = new Properties();
try {
loadPropertiesFile();
} catch (Exception e) {
throw new Exception(e.getMessage(), e);
}
}
public void loadPropertiesFile() throws IOException {
String path = "./cfg/message.properties";
file = new FileInputStream(path);
appProperties.load(file);
file.close();
}
}
Properties File.
messassge.properties
SSO_URL = https://example.com/connect/token
SSO_API_USERNAME = test
SSO_API_PASSWORD = Uo88YmMpKUp
SSO_API_SCOPE = intraday_api
SSO_IS_PROXY_ENABLED = false
SSO_MAX_RETRY_COUNT = 3
SSO_FLOAT_VALUE = 3.0
Constant.java
public class Constants {
public static String SSO_URL = null;
public static String SSO_API_USERNAME = null;
public static String SSO_API_PASSWORD = null;
public static String SSO_API_SCOPE = null;
public static boolean SSO_IS_PROXY_ENABLED = false;
public static int SSO_MAX_RETRY_COUNT = 0;
public static float SSO_FLOAT_VALUE = 0;
}
If you have a class of configuration values, like your Constants class, and you want to load all values from a configuration (properties) file, you can create a little helper class and use reflection:
public class ConfigLoader {
public static void load(Class<?> configClass, String file) {
try {
Properties props = new Properties();
try (FileInputStream propStream = new FileInputStream(file)) {
props.load(propStream);
}
for (Field field : configClass.getDeclaredFields())
if (Modifier.isStatic(field.getModifiers()))
field.set(null, getValue(props, field.getName(), field.getType()));
} catch (Exception e) {
throw new RuntimeException("Error loading configuration: " + e, e);
}
}
private static Object getValue(Properties props, String name, Class<?> type) {
String value = props.getProperty(name);
if (value == null)
throw new IllegalArgumentException("Missing configuration value: " + name);
if (type == String.class)
return value;
if (type == boolean.class)
return Boolean.parseBoolean(value);
if (type == int.class)
return Integer.parseInt(value);
if (type == float.class)
return Float.parseFloat(value);
throw new IllegalArgumentException("Unknown configuration value type: " + type.getName());
}
}
Then you call it like this:
ConfigLoader.load(Constants.class, "/path/to/constants.properties");
You can extend the code to handle more types. You can also change it to ignore missing properties, instead of failing like it does now, such that assignments in the field declaration will remain unchanged, i.e. be the default.
If you know the type of constant, you can use Apache Commons Collections.
For example, you can use some utilities method based on type of your constant.
booelan SSO_IS_PROXY_ENABLED = MapUtils.getBooleanValue(appProperties, "SSO_IS_PROXY_ENABLED", false);
String SSO_URL = MapUtils.getString(appProperties, "SSO_URL", "https://example.com/connect/token");
You can even use default values to avoid errors.
Dambros is right, every thing you store inside a Properties file is as a String value.
You can track your different primitive data types after retrieving properties value as below like ref. -
Java Properties File: How to Read config.properties Values in Java?
package crunchify.com.tutorial;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;
/**
* #author Crunchify.com
*
*/
public class CrunchifyGetPropertyValues {
String result = "";
InputStream inputStream;
public String getPropValues() throws IOException {
try {
Properties prop = new Properties();
String propFileName = "config.properties";
inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
if (inputStream != null) {
prop.load(inputStream);
} else {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}
Date time = new Date(System.currentTimeMillis());
// get the property value and print it out
String user = prop.getProperty("user");
String company1 = prop.getProperty("company1");
String company2 = prop.getProperty("company2");
String company3 = prop.getProperty("company3");
result = "Company List = " + company1 + ", " + company2 + ", " + company3;
System.out.println(result + "\nProgram Ran on " + time + " by user=" + user);
} catch (Exception e) {
System.out.println("Exception: " + e);
} finally {
inputStream.close();
}
return result;
}
}
and later convert to primitive -
How to convert String to primitive type value?
I suggest you to track your data types value by putting the key values inside String type switch statement and later retrieve the related data type value by using key name cases.
String type switch case is possible after Java 7.
Not entirely sure whether I exactly understand the problem but a possibility could be to include the type of the property value in the (String) value. So for example the properties you showed would become something like:
SSO_URL = URL:https://example.com/connect/token
SSO_API_USERNAME = STRING:test
SSO_API_PASSWORD = STRING:Uo88YmMpKUp
SSO_API_SCOPE = STRING:intraday_api
SSO_IS_PROXY_ENABLED = BOOLEAN:false
SSO_MAX_RETRY_COUNT = INTEGER:3
SSO_FLOAT_VALUE = FLOAT:3.0
During the parsing of the property values you first determine the type of the property by looking at the part before : and use the part after for the actual parsing.
private static Object getValue(Properties props, String name) {
String propertyValue = props.getProperty(name);
if (propertyValue == null) {
throw new IllegalArgumentException("Missing configuration value: " + name);
} else {
String[] parts = string.split(":");
switch(parts[0]) {
case "STRING":
return parts[1];
case "BOOLEAN":
return Boolean.parseBoolean(parts[1]);
....
default:
throw new IllegalArgumentException("Unknown configuration value type: " + parts[0]);
}
}
}
Follow the dropwizard configuration pattern where you define your constants using YAML instead of Properties and use Jackson to deserialize it into your Class. Other than type safety, dropwizard's configuration pattern goes one step further by allowing Hibernate Validator annotations to validate that the values fall into your expected ranges.
For dropwizard's example...
http://www.dropwizard.io/0.9.2/docs/getting-started.html#creating-a-configuration-class
For more information about the technology involved...
github.com/FasterXML/jackson-dataformat-yaml
hibernate.org/validator/
Spring Boot has ready to use and feature reach solution for type-safe configuration properties.
Definitely, use of the Spring just for this task is overkill but Spring has a lot of cool features and this one can attract you to right side ;)
You can define your configurable parameters as 'static' in your class of choice, and from a static init call a method that loads the parameter values from a properties file.
For example:
public class MyAppConfig {
final static String propertiesPath="/home/workspace/MyApp/src/config.properties";
static String strParam;
static boolean boolParam;
static int intParam;
static double dblParam;
static {
// Other static initialization tasks...
loadParams();
}
private static void loadParams(){
Properties prop = new Properties();
try (InputStream propStream=new FileInputStream(propertiesPath)){
// Load parameters from config file
prop.load(propStream);
// Second param is default value in case key-pair is missing
strParam=prop.getProperty("StrParam", "foo");
boolParam=Boolean.parseBoolean(prop.getProperty("boolParam", "false"));
intParam= Integer.parseInt(prop.getProperty("intParam", "1"));
dblParam=Double.parseDouble(prop.getProperty("dblParam", "0.05"));
} catch (IOException e) {
logger.severe(e.getMessage());
e.printStackTrace();
}
}
}
This might help:
props.getProperty("name", Integer.class);

easymock expect not working and still calling actual method

I want to test MessageProcessor1.listAllKeyword method, which in turn
calls HbaseUtil1.getAllKeyword method. Initialy, I had to deal with a problem associated with the static initializer and the constructor. The problem was to initialize a HBASE DB connection. I used powerMock to suppress static and constructor calls and it worked fine.
Even though I mocked HbaseUtil1.getAllKeyword method, actual method is being called and executes all HBase code leading to an exception, in which HBASE server is not up.
EasyMock.expect(hbaseUtil.getAllKeyword("msg", "u1")).andReturn(expectedList);
Please give me any idea on how to avoid an actual method call. I tried many ways but none of them worked.
public class MessageProcessor1
{
private static Logger logger = Logger.getLogger("MQ-Processor");
private final static String CLASS_NAME = "MessageProcessor";
private static boolean keywordsTableExists = false;
public static PropertiesLoader props;
HbaseUtil1 hbaseUtil;
/**
* For checking if table exists in HBase. If doesn't exists, will create a
* new table. This runs only once when class is loaded.
*/
static {
props = new PropertiesLoader();
String[] userTablefamilys = {
props.getProperty(Constants.COLUMN_FAMILY_NAME_COMMON_KEYWORDS),
props.getProperty(Constants.COLUMN_FAMILY_NAME_USER_KEYWORDS) };
keywordsTableExists = new HbaseUtil()
.creatTable(props.getProperty(Constants.HBASE_TABLE_NAME),
userTablefamilys);
}
/**
* This will load new configuration every time this class instantiated.
*/
{
props = new PropertiesLoader();
}
public String listAllKeyword(String userId) throws IOException {
HbaseUtil1 util = new HbaseUtil1();
Map<String, List<String>> projKeyMap = new HashMap<String, List<String>>();
//logger.info(CLASS_NAME+": inside listAllKeyword method");
//logger.debug("passed id : "+userId);
List<String> qualifiers = util.getAllKeyword("msg", userId);
List<String> keywords = null;
for (String qualifier : qualifiers) {
String[] token = qualifier.split(":");
if (projKeyMap.containsKey(token[0])) {
projKeyMap.get(token[0]).add(token[1]);
} else {
keywords = new ArrayList<String>();
keywords.add(token[1]);
projKeyMap.put(token[0], keywords);
}
}
List<Project> projects = buildProject(projKeyMap);
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
.create();
System.out.println("Json projects:::" + gson.toJson(projects));
//logger.debug("list all keyword based on project::::"+ gson.toJson(projects));
//return gson.toJson(projects);
return "raj";
}
private List<Project> buildProject(Map<String, List<String>> projKeyMap) {
List<Project> projects = new ArrayList<Project>();
Project proj = null;
Set<String> keySet = projKeyMap.keySet();
for (String hKey : keySet) {
proj = new Project(hKey, projKeyMap.get(hKey));
projects.add(proj);
}
return projects;
}
//#Autowired
//#Qualifier("hbaseUtil1")
public void setHbaseUtil(HbaseUtil1 hbaseUtil) {
this.hbaseUtil = hbaseUtil;
}
}
public class HbaseUtil1 {
private static Logger logger = Logger.getLogger("MQ-Processor");
private final static String CLASS_NAME = "HbaseUtil";
private static Configuration conf = null;
public HbaseUtil1() {
PropertiesLoader props = new PropertiesLoader();
conf = HBaseConfiguration.create();
conf.set(HConstants.ZOOKEEPER_QUORUM, props
.getProperty(Constants.HBASE_CONFIGURATION_ZOOKEEPER_QUORUM));
conf.set(
HConstants.ZOOKEEPER_CLIENT_PORT,
props.getProperty(Constants.HBASE_CONFIGURATION_ZOOKEEPER_CLIENT_PORT));
conf.set("hbase.zookeeper.quorum", props
.getProperty(Constants.HBASE_CONFIGURATION_ZOOKEEPER_QUORUM));
conf.set(
"hbase.zookeeper.property.clientPort",
props.getProperty(Constants.HBASE_CONFIGURATION_ZOOKEEPER_CLIENT_PORT));
}
public List<String> getAllKeyword(String tableName, String rowKey)
throws IOException {
List<String> qualifiers = new ArrayList<String>();
HTable table = new HTable(conf, tableName);
Get get = new Get(rowKey.getBytes());
Result rs = table.get(get);
for (KeyValue kv : rs.raw()) {
System.out.println("KV: " + kv + ", keyword: "
+ Bytes.toString(kv.getRow()) + ", quaifier: "
+ Bytes.toString(kv.getQualifier()) + ", family: "
+ Bytes.toString(kv.getFamily()) + ", value: "
+ Bytes.toString(kv.getValue()));
qualifiers.add(new String(kv.getQualifier()));
}
table.close();
return qualifiers;
}
/**
* Create a table
*
* #param tableName
* name of table to be created.
* #param familys
* Array of the name of column families to be created with table
* #throws IOException
*/
public boolean creatTable(String tableName, String[] familys) {
HBaseAdmin admin = null;
boolean tableCreated = false;
try {
admin = new HBaseAdmin(conf);
if (!admin.tableExists(tableName)) {
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
for (int i = 0; i < familys.length; i++) {
tableDesc.addFamily(new HColumnDescriptor(familys[i]));
}
admin.createTable(tableDesc);
System.out.println("create table " + tableName + " ok.");
}
tableCreated = true;
admin.close();
} catch (MasterNotRunningException e1) {
e1.printStackTrace();
} catch (ZooKeeperConnectionException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return tableCreated;
}
}
Below is my Test class.
#RunWith(PowerMockRunner.class)
#PrepareForTest(MessageProcessor1.class)
#SuppressStaticInitializationFor("com.serendio.msg.mqProcessor.MessageProcessor1")
public class MessageProcessorTest1 {
private MessageProcessor1 messageProcessor;
private HbaseUtil1 hbaseUtil;
#Before
public void setUp() {
messageProcessor = new MessageProcessor1();
hbaseUtil = EasyMock.createMock(HbaseUtil1.class);
}
#Test
public void testListAllKeyword(){
List<String> expectedList = new ArrayList<String>();
expectedList.add("raj:abc");
suppress(constructor(HbaseUtil1.class));
//suppress(method(HbaseUtil1.class, "getAllKeyword"));
try {
EasyMock.expect(hbaseUtil.getAllKeyword("msg", "u1")).andReturn(expectedList);
EasyMock.replay();
assertEquals("raj", messageProcessor.listAllKeyword("u1"));
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
}
The HbaseUtil1 is instantiated within the listAllKeyword method
public String listAllKeyword(String userId) throws IOException {
HbaseUtil1 util = new HbaseUtil1();
...
So the mock one you create in your test isn't being used at all.
If possible, make the HbaseUtil1 object passable, or settable on the MessageProcessor1 class and then set it in the test class.
Also, and note I'm not 100% familiar with PowerMock, you could include HbaseUtil1 in the prepare for test annotation. I think that will make PowerMock instantiate mocks instead of real objects and then use the expectations you provide in you test.

How to make a variable/constant available all the time once initialized?

I've a swing application that has to connect to database for some resources, for this i used .properties file to store database properties and that can be read at runtime.
For this i am using the following code
public void readPropertiesFile(){
try{
InputStream is = ReadValues.class.getResourceAsStream(PROP_FILE);
Properties prop = new Properties();
prop.load(is);
String URL = prop.getProperty("DB_URL");
String user = prop.getProperty("DB_USER");
String pwd = prop.getProperty("DB_PWD");
is.close();
/* code to use values read from the file*/
}catch(Exception e){
System.out.println("Failed to read from " + PROP_FILE + " file.");
}
}
but i've to call this method whenever i want to connect to the database (for Connection object).
I know the thing that now processing is fast enough to run these lines in micro seconds, but it's for my own knowledge that suggest me the ways through which i can store these DB values when application starts or the first time user try to connect to DB for any operation in such objects or variables or constants that will be usable until the application restarts and can be called directly without reading the file.
P.S. : I know that the DB values will not change oftentimes, and if it happens than i'll be happy to restart my application :)
by making these static fields in a separate class, they will not be loaded until the first time you access URL,USER, or PASSWORD.
public class DbProps {
public static final String URL;
public static final String USER;
public static final String PASSWORD;
static {
try{
InputStream is = ReadValues.class.getResourceAsStream(PROP_FILE);
try {
Properties prop = new Properties();
prop.load(is);
URL = prop.getProperty("DB_URL");
USER = prop.getProperty("DB_USER");
PASSWORD = prop.getProperty("DB_PWD");
} finally {
is.close();
}
}catch(Exception e){
throw new RuntimeException("Failed to read from " + PROP_FILE + " file.", e);
}
}
}
You can nake a check condition which will check if it is first time then set the value other wise use the existing value
public static boolean isFirstTime = true;
public static String URL = true;
public static String user = true;
public static String pwd = true;
public void readPropertiesFile(){
if(isFirstTime){
try{
InputStream is = ReadValues.class.getResourceAsStream(PROP_FILE);
Properties prop = new Properties();
prop.load(is);
URL = prop.getProperty("DB_URL");
user = prop.getProperty("DB_USER");
pwd = prop.getProperty("DB_PWD");
isFirstTime = false;
is.close();
/* code to use values read from the file*/
}catch(Exception e){
System.out.println("Failed to read from " + PROP_FILE + " file.");
}
}
}
//use this URL user and pwd in your application
Here's a generic environment class for you. You can get your DB props like Environment.getEnvironment().getProperty("DB_URL"), etc.
public class Environment {
private static final String PROP_FILE = "somefilename";
private static final Environment singleton = new Environment();
public static Environment getEnvironment() {
return singleton;
}
private Properties properties = new Properties();
protected Environment() {
super();
loadProperties();
}
public Properties getProperties() {
return properties;
}
public String getProperty(String propertyName) {
return getProperty(propertyName, System.getProperty(propertyName));
}
public String getProperty(String propertyName, String defaultValue) {
return getProperties().getProperty(propertyName, defaultValue);
}
public void loadProperties() {
URL resourceURL = null;
try {
resourceURL = Thread.currentThread().getContextClassLoader()
.getResource(PROP_FILE);
getProperties().load(resourceURL.openStream());
System.out.println("Loaded properties from "
+ resourceURL.toExternalForm());
} catch (IOException ioe) {
System.err.println("Failed to load properties from "
+ resourceURL.toExternalForm());
}
}
}

Categories

Resources