Extra quotes getting appended to String while testing - spring-boot

I have the following function:
public String getLUrl(String ver) {
return getDHUrl()+"WLCRLogicalModel/"+ver;
}
I started Intellij in debug mode and stopped at the return line and evaluated the expression and I see the following:
The weird thing is the double quotes at the start of the URL and also at the point "WLC... I am not sure why the quotes are getting added. Also, the debugger is started from a Test class which has annotations like :
#RunWith(SpringRunner.class) #SpringBootTest(webEnvironment=SpringBootTest.WebEnvironment.MOCK,classes=Application.class)
#AutoConfigureMockMvc
#TestPropertySource(locations="classpath:application-local.yml")
The application-local.yml has the property:
dhUrlPrefix: "https://sa.../WLCRC/" this is what is returned by the call to getDHUrl() function This is working fine when I run my spring boot server. I am not sure if this is the correct format to use for putting a URL in the yml file. Although it's causing issues only while doing the spring test. Evaluating the call to getDHUrl() also having "" ""

Related

Log4j: Setting the generated log filename to include hostname

I am trying to generate logs using log4j. Logging part is working correctly, but I want to include the hostname (where the logs are generated) to be appended in the filename.
for eg: my current logfile name: logger.2019-06-12-06-14
what i want: logger_$HOST.2019-06-12-06-14
, where $HOST is the hostname
I have already seen other posts in stackoverflow and depending on that I have set the hostname in my Java code where I think its getting set before calling log4j
static {
try {
HOST_NAME = InetAddress.getLocalHost().getCanonicalHostName();
} catch (UnknownHostException e) {
HOST_NAME = "localhost";
}
System.setProperty("hostname", HOST_NAME);
}
and have modified the cfg file (where we have properties defined for all the loggers), to include the system property:
*.*.log4j.appender.MyLogger.File=/logs/logger_$hostname;
But the resultant filename generated is logger_$hostname.2019-06-12-06-14 (which is literally $hostname instead of the property value)
In the posts, it does mention that the way to assign the variable in the cfg file is logger_${hostname} but while compiling it throws err with unexpected {, so I just put $hostname instead.
I have tried other ways too, like putting
*.*.log4j.appender.MyLogger.File=/logs/logger_$env:hostname;
*.*.log4j.appender.MyLogger.File=$hostname;
*.*.log4j.appender.MyLogger.File=/logs/logger_$HOST;
(thinking since its running in unix, it might pick the HOST variable :( )
but no luck so far. Any ideas what I might be doing wrong?
BTW, I am calling the logger by this:
private static Logger logger = Logger.getLogger("MyLogger");
So, is it possible to set the hostname from the class where I am logging. I do want to rotate my logs every minute, so dont want to mess with other log4j settings. My complete log4j appender properties:
*.*.log4j.logger.myLogger=(INFO,MyLogger);
*.*.log4j.additivity.myLogger=false; # this logger does not show up in main logs
*.*.log4j.appender.MyLogger.File=/logs/logger_$hostname;
*.*.log4j.appender.MyLogger.layout=org.apache.log4j.PatternLayout;
# %n: new line for each entry
*.*.log4j.appender.MyLogger.layout.ConversionPattern="%m%n";
*.*.log4j.appender.MyLogger.Append=true;
*.*.log4j.appender.MyLogger.ImmediateFlush=true;
*.*.log4j.appender.MyLogger.Encoding=UTF8;
# Enable 1 minute rotation
*.*.log4j.appender.MyLogger.DatePattern="'.'yyyy-MM-dd-HH-mm";
# Disable periodic flush since files will be flushed upon rotation that happens every minute
*.*.log4j.appender.MyLogger.PeriodicFlush=false;
Any ideas to fix it will be very helpful. (And apologies since its kind of a repetitive question but I am not able to figure out the issue with the earlier posts :( )
You can use the ${env:HOST} option in the log4j2.xml for environment lookup as mentioned in the documentation: https://logging.apache.org/log4j/2.0/manual/lookups.html#EnvironmentLookup
For example:
<RollingRandomAccessFile name="CustomLoggerRandomFile"
fileName="logs/${env:HOST}_system.log"
filePattern="${env:HOST}_system-%d{yyyy-MM-dd}-%i.log.gz"
immediateFlush="false">
EDIT: I assumed that you are using log4j2 and the $HOST variable must be created in the host machine (where the logs are generated) as an environment variable.
I have replaced ${hostname} by ${hostName} and it worked.
<RollingFile name="LogToFile" fileName="${env:LOG_ROOT}/${env:LOG_RELEASE}/myFile.${hostName}.json" in log4j2.xml

Error encountered when running batch file - was unexpected at this time

I have encountered an error when running a batch file. It goes like this, I run test-setup.cmd which calls another batch file test-env.cmd
test-setup.cmd calls by using this line:
call %SCRIPT_HOME%\test-env.cmd
where SCRIPT_HOME is set up as SCRIPT_HOME=%~dp0
test-env.cmd has this line:
if [%TEST_HOME%] == [] set TEST_HOME=D:\Program Files\Test\test-02.2.3.Final
After running the test-setup.cmd a message appears like this:
Files\Test\test-02.2.3.Final was unexpected at this time
Note that I have setup the TEST_HOME in the system environment variables.
Please help, thank you.
The syntax of your if command is incorrect. It would work if %TEST_HOME% didn't contain any spaces, but since it does you must use double quotes:
if "%TEST_HOME%" == "" set TEST_HOME=D:\Program Files\Test\test-02.2.3.Final
Mind you, since you're just testing to see whether the variable exists, it would be a lot more efficient to do that directly:
if not defined TEST_HOME set TEST_HOME=D:\Program Files\Test\test-02.2.3.Final

Properties in JMeter for GUI and CMD

I have a JMeter script, where I have some user defined Variables like FILE_SAVE_PATH. This script should be started on a command line with parameter -J. So in the GUI, I changed the value for the variable FILE_SAVE_PATH to ${__P(FILE_SAVE_PATH, "C:\svn\trunk\dir")}, because the test should save there a file, but only on my machine. On the machine, where the script will be started from command line, it should save the file into another path.
My problem is now this: When I test this JMeter script on my machine in the GUI, I get an output of this:
About to replace in property of type: class org.apache.jmeter.testelement.property.StringProperty: ${__P(FILE_SAVE_PATH, "C:\svn\trunk\dir")}
2017/04/04 17:09:38 DEBUG - jmeter.testelement.property.AbstractProperty: Not running version, return raw function string
2017/04/04 17:09:38 DEBUG - jmeter.engine.util.ValueReplacer: Replacement result: ${__P(FILE_SAVE_PATH, "C:\svn\trunk\dir")}
But I think, the last line should be something like this:
2017/04/04 17:09:38 DEBUG - jmeter.engine.util.ValueReplacer: Replacement result: "C:\svn\trunk\dir"
So, how to change the test to get the result I want?
Escape every backslash with another one - C:\\svn\\trunk\\dir, or use unix slash, JVM's gonna handle it right: C:/svn/trunk/dir
And remove the doublequotes, they're not needed.
P.S. I presumed you're not using that notation in the Beanshell/JSR223 context. If you do - stop there and use the legit way to access properties.

How can I get $ not escaped in application default jvm args for gradle?

Using the application task I am specifying:
applicationDefaultJvmArgs = ['$DEBUG_OPTS',
'-Djava.library.path=${ZMQ_LIB_PATH}']
In the generated start scripts I see:
DEFAULT_JVM_OPTS='"\$DEBUG_OPTS" "-Djava.library.path=\${ZMQ_LIB_PATH}"'
I don't want the \$ in there. I tried using '$$DEBUG_OPTS' and also '\$DEBUG_OPTS' but got the same result. What is the right way to escape the $ so it ends up in the script without a backslash in front of it?
I had a similar issue, trying to add a commandline parameter $1 in there. With some googling came up with this solution, fixing the script after the fact.
applicationDefaultJvmArgs=['-Dmy.property=DOLLARONE']
...
startScripts{
doLast{
def bashFile = new File(getOutputDir(),applicationName)
String bashContent = bashFile.text
bashFile.text = bashContent.replaceFirst('DOLLARONE', Matcher.quoteReplacement('$1'))
}
}
The StartScriptGenerator code implies that '$' will be unconditionally replaced by the '\$'.
I assume that your intention is to use '$' character for shell parameters extension but I would like to point out that such usage (if permitted by the gradle task that generates the scripts) is not interoperable between bash and bat scripts - in the bash it will be used for shell parameters extension but in the bat it will have no meaning.
For Kotlin build script the solution could look like:
tasks.named<CreateStartScripts>("startScripts") {
doLast {
unixScript.writeText(unixScript.readText().replace("{{APP_HOME}}", "\${APP_HOME}"))
windowsScript.writeText(windowsScript.readText().replace("{{APP_HOME}}", "%APP_HOME%"))
}
}

How do I set test.testLogging.showStandardStreams to true from the command line?

It is convenient to debug some of external libraries and even internal code while writing unit tests by reviewing the logging on stdout.
While I can add test.testLogging.showStandardStreams = true to the build.graddle file, I'd rather do something less permanent, such as setting this flag from the command line execution of gradle.
I've tried several approaches, none seem to work:
gradle test -Dtest.testLogging.showStandardStreams=true
gradle test -Ptest.testLogging.showStandardStreams=true
And other variations of those options by changing the property string. Nothing seems to do the trick.
How do I set test.testLogging.showStandardStreams=true from the command line?
There is no built-in way to set build model properties from the command line. You'll have to make the build script query a system or project property that gets passed in via -D or -P, respectively.
Just use environment variables:
test {
testLogging.showStandardStreams = (System.getenv('PRINTF_DEBUG') != null)
}
Now run your test case like this:
PRINTF_DEBUG=1 ./gradlew test --tests=com.yourspace.yourtest
This will run enable console output and just run one single test case. You often not want to enable console output for the entire test suite because of the noise generated.
You can override it like this
gradle -Doverride.test.testLogging.info.showStandardStreams=true test
or you can add this to your gradle.properties either in the project or in ~/.gradle/gradle.properties
systemProp.override.test.testLogging.info.showStandardStreams=true

Resources