protected static String getBaseEnvLinkURL() {
String baseEnvLinkURL=null;
HttpServletRequest currentRequest =
((ServletRequestAttributes)RequestContextHolder.
currentRequestAttributes()).getRequest();
// lazy about determining protocol but can be done too
baseEnvLinkURL = "http://" + currentRequest.getLocalName();
if(currentRequest.getLocalPort() != 80) {
baseEnvLinkURL += ":" + currentRequest.getLocalPort();
}
if(!StringUtils.isEmpty(currentRequest.getContextPath())) {
baseEnvLinkURL += currentRequest.getContextPath();
}
return baseEnvLinkURL;
}
Friday, December 12, 2014
Spring Web: Get current request and generate base URL
Just a simple way to generate application's base URL according to the environment context using current request:
Tuesday, November 4, 2014
DOS: List files recursively
To list files recursively in Windows you don't need a complex script as I thought. For example to list all .html inside subfolders you can just run simple DOS command:
dir /s/b *.html > results.txt
dir /s/b *.html > results.txt
Wednesday, October 15, 2014
Ant: adding additional JARs to classpath
I was working in a custom ATG module in which I required to extend a core ATG class. When I ran the build ant command I got compile errors because of "cannot find symbol", in other words, my dear reference class was not being recognized. It was missing! In my Eclipse project I got no errors because I had the right library included in the project's classpath.
I started looking on how to add additional JAR files to the classpath, but after digging a little bit more in the compile target referenced in the build.xml:
<target name="build" depends="echo-build-message,clean,compile,jar-classes,jar-configs,copy-to-install" />
I saw how could I add additional classpath entries using the reference id "classpath.additions". In the included common.xml file we had this:
<target name="compile">
<mkdir dir="${java.output.dir}"/>
<mkdir dir="${java.src.dir}"/>
<copy todir="${java.output.dir}">
<fileset dir="${java.src.dir}">
<include name="**/*.properties" />
<include name="**/*.xml" />
</fileset>
</copy>
<if>
<isreference refid="classpath.additions" />
<then>
<path id="fullClasspath">
<path refid="classpath" />
<path refid="classpath.additions" />
</path>
</then>
<else>
<path id="fullClasspath">
<path refid="classpath" />
</path>
</else>
</if>
<echo message="java.src.dir: ${java.src.dir}, java.output.dir: ${java.output.dir}" />
<javac srcdir="${java.src.dir}" destdir="${java.output.dir}" classpathref="fullClasspath" debug="on" includeAntRuntime="false" />
</target>
So I just added this in the build.xml:
<path id="classpath.additions">
<fileset dir="${dynamo.home}/../REST/lib"><include name="**/*.jar" /></fileset>
</path>
This seems like an elegant generic way for configuring classpaths in a multi module environment like ATG.
Tuesday, October 14, 2014
ATG: Nucleus tips
Get current request URI
import atg.servlet.ServletUtil; //... ServletUtil.getCurrentRequest().getRequestURI();
Define a collection of components
Java:
protected Component [] componentes;
public void setComponents(Component [] components) {
this.components = components;
}
public Component [] getComponents() {
return components;
}
Properties:
components=\
/path/to/some/Component,\
/path/to/another/Component
Tuesday, July 22, 2014
Spring: Integration testing under Spring Boot application context but mocking services
Use @MockBean annotation
DEPRACATED:
So I wanted to create some integration tests for an API application that uses Spring Boot. The thing is that I needed to load the same application context as the real applicationm, because Spring Boot performs a bunch of configurations for you, and these were not being loaded in a StandAlone mode.
But also I didn't want to use real services which depend upon real data. Depending on databases for your tests creates fragile tests.
After researching on the web I found this way:
This is the typical Spring-Boot entry point class.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@EnableAutoConfiguration
@ComponentScan
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
A base class having the logic to init the application context.import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringApplicationConfiguration(classes = MyApplication.class)
public abstract class BaseIntegrationTest {
protected MockMvc mockMvc;
@Autowired
protected WebApplicationContext wac;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
this.mockMvc = webAppContextSetup(wac).build();
}
}
An implementation class mocking a required service class.public class FooIntegrationTest extends BaseIntegrationTest {
@Configuration
public static class TestConfiguration {
@Bean
@Primary
public FooService fooService() {
return mock(FooService.class);
}
}
@Autowired
FooService mockFooService;
}
Subscribe to:
Posts (Atom)