spring boot remote shell custom command - java

I try to add a new custom command to the spring boot remote shell without success.
In the documentation is only a groovy example available but I like to use Java do create a new command.
http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-remote-shell.html
I also check the CRaSH documentation: http://www.crashub.org/1.3/reference.html#_java_commands
I put my class under package commands and crash.commands but I don't see my new command if I connect to the shell via ssh and type help. Any idea what I'm doing wrong?
Here is my Java Code:
package commands;
import org.crsh.cli.Command;
import org.crsh.cli.Usage;
import org.crsh.command.BaseCommand;
import org.springframework.stereotype.Component;
#Component
public class hello extends BaseCommand {
#Command
#Usage("Say Hello")
public String test() {
return "HELLO";
}
}

If your class is called 'hello', put it in
resources/crash/commands/hello/hello.java
Having a package statement doesn't matter.
You don't have to put anything inside src, just have this inside resources.
#Component isn't required.
This class is compiled during runtime. See:
org.crsh.lang.impl.java.JavaCompiler#compileCommand
Therefor any dependency injection requirements need to be considered accordingly with lazy initialization.

Put the class in the default package (i.e. omit the package statement) and put the Java source for the class in src/main/resources/commands. Spring/Crash will compile the Java code the first time you invoke the command. Also, if the class implements a single command the method name should be ‘main’ otherwise users have to type:
> hello test
You also don't need the #Component annotation on the class as it is not a Spring managed bean.

Related

Need to assign dynamic string value to #PactFolder annotation in Java-Pact Contract test

I need to add a variable value to a class-level annotation "#PactFolder" in a JUnit test. I've been able to achieve the dynamic assignment for this annotation using simple reflection in the #BeforeClass method.
However, This JUnit contract test runs with another annotation (#RunWith(PactRunner.class)). Now, since the #PactFolder annotation's purpose is to pass an absolute path to the PactRunner class, and PactRunner reads the value of #PactFolder at the time of invocation, the reflected variable value is of no use.
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import au.com.dius.pact.provider.junit.PactRunner;
import au.com.dius.pact.provider.junit.Provider;
import au.com.dius.pact.provider.junit.loader.PactFolder;
import au.com.dius.pact.provider.junit.loader.PactSource;
import au.com.dius.pact.provider.junit.target.HttpTarget;
import au.com.dius.pact.provider.junit.target.TestTarget;
#PactFolder("<Dynamic value>")
#RunWith(PactRunner.class)
#Provider("CvSaveDataServiceProvider")
public class ContractTest {
#BeforeClass
public static void setConditions() {
PactFolder pactFolder = ContractTest.class.getAnnotation(PactFolder.class);
AlterPactFolderAnnotation.setAbsolutePactDirectory(ContractTest.class, PactFolder.class, ContractDirectoryRelativeModulePath.HRIS.getRelativeModulePath(), pactFolder);
System.out.println(ContractTest.class.getAnnotation(PactFolder.class).value());
}
#TestTarget
public HttpTarget target = new HttpTarget(8080);
}
I need to access contracts from a locally cloned repository, kept in multiple directories, with final aim to add paths of all directories in a single config file and initialize all tests in different directories from a single point using "mvn test" command (a simple batch file)
Is there a possible solution or any workaround to do this ?

Cucumber:- Unable to generate step definitions by running feature file as well as testrunner class

I am trying to generate the step definitions from my feature file and as well as I have also designed test runner class but upon execution both give output on console as :-
0 scenarios
0 steps
0m0s.000s
Even though my feature file contains scenarios and steps.
Remove the colon (:) after the keywords (Given, When, etc) in your feature file.
Since you haven't shared any code or much details as to what you've done the only assumption that I can make is you have done something wrong in your testrunner class.
#RunWith(Cucumber.class)
#CucumberOptions(
features = "Feature"
,glue={"stepDefinition"}
)
public class TestRunner {
}
in the features make sure the path to your feature files is correct. i.e. if they are stored at some other directory, provide the path for the same
Ex: features = {"src/test/java/features"}
Also, please share your project structure, your feature file and your testrunner class code if possible in case this doesn't work for you.
Actually my runner class file looks like this:-
package runner;
import org.testng.annotations.Test;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
#CucumberOptions(features={"src//test//resources//featurefiles"},glue= {"im801clsteps"},plugin={"html:target/cucumber-html-report",
"json:target/cucumber.json", "pretty:target/cucumber-pretty.txt"})
#Test
public class MainRunner extends AbstractTestNGCucumberTests {
}
And I am using testng not junit to run my tests,please let me know why I am wrong?

How to define a class or import one in a Scrapbook?

I need to test some code on the fly using the Eclipse Scrapbook. Is this even the best way to do it? I don't come from a Java background. Anyways, its frustrating me because I'm trying to define a simple class in Java:
public class RegistrationResponse {
public String message;
public Boolean success;
}
I want to use this class in my Scrapbook. So here's my project structure:
src
|-(default package)
| |- RegistrationResponse.java
|- Scrapbook.jpage
I went into my Scrapbook, and added the RegistrationResponse type from the Java Snippet Imports window. Then, when I run my code, it tells me this:
The import RegistrationResponse cannot be resolved
You gotta be kidding me. How do I do this? Anyways, I noticed I wasn't able to add the default package in my snippet imports. Is that why its failing? Is it supposed to be added by default or no?
Do not put classes into Default package. put it into a subpackage.
If you are using eclipse, you con use shift+ctrl+o to Import all referenced classes

How to write an import statement to refer to a java class in a jar?

How can I refer to a Java class in stdlib1.jar when the directory structure is like this? How to write the import statement?
I want to call a method under stdlib1.jar, I have configured it.
The classes are in the default package. According to this answer, it is not possible to import classes from the default package. So, they have to be moved to another package or you have to use reflection.
You call a method from a class and not from a package.
You don't need to specify the jar when you call a method from a class belonging to it. Which matters is your jar is in the classpath
In your screenshot if the lib makes part of the classpath folders, you can import and use classes from it in your code.
Here the classes of your jar use the default package (no package name) which seems weird for a third-party library. Default package is not recommended since it doesn't allow to naturally reference and use the classes of the archive from the client code.
I am not sure you are using the correct version of the jar. Look at that :
http://grepcode.com/snapshot/repo1.maven.org/maven2/com.googlecode.princeton-java-introduction/stdlib/1.0.1
This contains classes in the edu.princeton.cs package :
With package, you could declare this :
For example :
You could create a class like that and use BinaryIn like that:
package main;
import edu.princeton.cs.BinaryIn;
public class MyClass(){
public static void main(String args[]){
BinaryIn in = new BinaryIn();
}
}

Play Framework 2.1 - Syntax error using java.util.properties in Play Controller

I am having a strange sort of conflict when attempting to use the java.util.Properties class in a Play Controller, consider the following:
package controllers;
import play.mvc.*;
import java.util.*;
public class Simple extends Controller {
Properties prop = new Properties();
prop.setProperty("database", "localhost");
}
In Eclipse the setProperty method returns:
Syntax error on token(s), misplaced construct(s)
Syntax error on tokens, delete these tokens
I think there is some conflict with this code being within a Controller, the same two lines work in a simple Java class in a bespoke package.
Any help would be much appreciated, I am new Play with some Java experience.
Put these two lines inside a method block:
import play.mvc.*;
import java.util.*;
public class Simple extends Controller {
public static void pickABetterMethodName() { // Method
Properties prop = new Properties();
prop.setProperty("database", "localhost");
}
}
This will solve the syntax problem. The reason is that you can't have code that is not method or field declaration in the class declaration itself. Behavior is implemented in methods. Here's the Java Tutorial about class declaration and here the about method declaration.
I think what you're trying to do is execute code during the controller initialization. If I remember correctly, in Play 1.x this was possible with a #OnApplicationStart decorator (on a method). I'm not sure this is still the case with Play 2.x.
Edit: With Play 2.x you do this with the Application global settings.

Categories