Compiling on Solaris isn’t always easy. I added a step by step Tutorial on How to compile and configure suPHP on Solaris 11 Express:
suPHP on Solaris 11
Delete artifacts in Jenkins
In two lectures at the HSLU T&A (Software Components and Software Application) the Students use a build server – jenkins. Yesterday afternoon a student (attend by the professor) came to me, told me, there is a problem with the build server. They can’t build their projects.
Short check on the file system showed me the problem:
root@s0052:/# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda1 19737268 19737266 2 100% / none 1021708 168 1021540 1% /dev none 1028564 164 1028400 1% /dev/shm none 1028564 45780 982784 5% /var/run
ups
– Ninety Projects and about 1500 builds were too much.
Solutions
I had two possibilities to solve the problem. Extend the file system or discard old artifacts.
Extend the file system can be realized quite simple, because of our virtual machine based on an ESX Environment on the Enterprise Lab. But this doesn’t solve the source of the problem. Do we really need all old artifacts from each project or are the last 5 enough? The last 5 are enough, we decided. So let’s discard the old artifacts.
Discard old artifacts
In Jenkins there is an option on each project configuration. You can set “Discard Old Builds” checkbox and click on “Advanced…”. Here you can set 5 to “Max # of builds to keep with artifacts”. Save it and while the butler builds the next time, he will also delete the old artifacts (except the last 5).
Sounds good, but they were two problems. I couldn’t save or update anything on the files ystem (remember 100% usage) and I had to do that on about 90 projects.
Luckily I found a huge file on the file system: the Jenkins log file. It was about 1.5GB big. So I deleted them. Because of file handler-”stuff” which doesn’t de-allocate the space directly (?not so clear to me, why?) I had to restart the OS. After some seconds, thanks to the ESX resource, I could re-login to the machine and check the space on the system: usage of 90% – good so far.
When I update the configuration with the “keep artifacts” property on the webgui of Jenkins, following lines were added to the project configuration file (var/lib/jenkins/jobs/PROJECTNAME/config.xml) from the project
<logRotator> <daysToKeep>-1</daysToKeep> <numToKeep>-1</numToKeep> <artifactDaysToKeep>-1</artifactDaysToKeep> <artifactNumToKeep>5</artifactNumToKeep> </logRotator>
But how can I accomplish that automatically on about 90 projects. The solution: a shell script. Thanks to Marcel, our enterpriselab system administrator and unix guru, he wrote following script:
for each in grp*/config.xml; do cp "$each" "${each}.bkp"; sed '4a <logRotator> <daysToKeep>-1</daysToKeep> <numToKeep>-1</numToKeep> <artifactDaysToKeep>-1</artifactDaysToKeep> <artifactNumToKeep>5</artifactNumToKeep> </logRotator>' ${each}.bkp >$each ;done
Before I executed this, I would check if there is no configuration file, which already has set this property. I have done this with
root@s0051:/var/lib/hudson/jobs# grep -i logRotator grp*/config.xml
That gave me no output – well, that was that was I except. So I could execute the script above
root@s0052:/var/lib/jenkins/jobs# for each in grp*/config.xml; do cp ....
Explanation
for each in grp*/config.xmleach is the variable for the file not a command what you maybe guess.do cp "$each" "${each}.bkp"make a backup from the original configuration file – You never knowsed '4aadd on line 4 the following text. I had to escape some character and with and add a “” on the end of each line.
Because of Jenkins hold the configuration files in the memory, I had to reload the configuration. To do that, Jenkins offers you the function “Reload Configuration from Disk” in the “Manage Jenkins” page (https://…./ /jenkins/manage).
Done! In every next build of each project, Jenkins discard the old artifacts and I get more and more free space on the disk.
Testing Java EE Application
Currently, I make some efforts to improve tests in my Java EE Application. At this time there are only some simple JUnit-Tests which doesn’t truly fulfill the requirements of testing. So I decide to look deeper into the new embedded container (Arquillian) which is provided by Netbeans 7.0 out-of-the-box and a testing framework.
After a short research on the Internet and some own made positive experience previously, I selected mockito as the testing framework.
For the first steps I created a simple class inside my Java EE Application which I want to test it. It looks like:
@Stateful @LocalBean public class SomeCase { @Inject transient private Logger logger; @Inject private UseCase useCase; /** * Default empty constructor. */ public SomeCase() { } /** * Create a string representation of a digit. * Convert a digit between 0 and 5 to the written digit like "zero", * "one", etc. * @param digit between 0 and 5 * @return string representation of a digit */ public String digitToString(int digit){ String response = ""; switch (digit){ case 0: response = "zero"; break; case 1: response = "one"; break; case 2: response = "two"; break; case 3: response = "three"; break; case 4: response = "four"; break; case 5: response = "five"; break; case 99: response = useCase.sayHello(digit + ""); break; //easter egg ;-) default: response = "undefined"; break; } logger.log(Level.INFO, "digit={0};respone={1}", new Object[]{digit, response}); return response; } }
The only container based functionality I use, is to inject the UseCase Bean and my log Factory (@Produces public Logger createLogger(InjectionPoint ip)).
Embedded Container
I started with the embedded container. A year ago, I have already tried to use the embedded container, but I gave up after I investigate hours in finding solution to my occurring exceptions. But now, one year and a new version of Netbeans later, it has to be better – I hoped!
According to the well documented tutorial on netbeans.org. I tried to integrate the embedded test container in my project. But I messed-up with a lot of exceptions and warnings. After some additional hours trying to get the test inside my application up and running, I gave up (again) and decide to go over to the testing framework. The embedded container is maybe better than it was a year ago, but still not enough good for me (according to my knowledge and skills).
Test should be created as simply as possible. Else I don’t use it reasonable. There is also another reason why I stopped investigating more time into the embedded container approach. I think it isn’t unit testing as the website of arquillian tells me, it is a kind of integration testing. So let’s try mockito.
Mockito
Because of the plain text java object of EJBs it is possible to create fast and isolated EJB unit tests with Mockito. You have to add some extra code to the productive source code to test it. That is the only change on it.
But now, step -by-step:
My Class SomeCase uses two injections: @Inject transient private Logger logger;
and @Inject private UseCase useCase;. Therefore I create these two Classes as a field in my Test Class (SomeCaseTest). I created also a field for the testing class itself:
@Mock UseCase useCase; @Mock Logger logger; private SomeCase someCase;
In the setUp method i initialize this fields
@Before public void setUp() { MockitoAnnotations.initMocks( this ); someCase = new SomeCase(logger, useCase); }
MockitoAnnotations.initMocks( this ); create a mock object for all fields annotated with @Mock. For the initialize of SomeCase, I need a new constructor. This is the only change in the origin source code which has to be done (as I already told). This new constructor looks like
//... public SomeCase(Logger loggerMock, SomeCase useCaseMock) { this.logger = loggerMock; this.useCase = useCaseMock; } //...
Now all preparation for the unit testing are done and I can start with writing tests. First I will test some different test inputs on the digitToString method.
@Test
public void testDigitToString() throws Exception {
int i = 1;
String expectResult = "one";
String response = someCase.digitToString(i);
assertEquals(response, expectResult);
i = 6; expectResult = "undefined";
response = someCase.digitToString(i);
assertEquals(response, expectResult);
i = -1; expectResult = "undefined";
response = someCase.digitToString(i);
assertEquals(response, expectResult);
}As you can see, there is nothing special in this test. Without the mocking part in the setup-method, it would throw a nullpointer-exception. Because of the logger which has no instance (in the container it is injected). In this test I don’t really use the power of Mockito. So let’s create another one.
@Test
public void testEasterEggOne() throws Exception {
int i = 99;
String expectResult = "Hello 99";
when(useCase.sayHello(i + "")).thenReturn("Hello 99");
String response = someCase.digitToString(i);
assertEquals(response, expectResult);
}Here I used the useCase mock object as a stub. It returns fix the value of “Hello 99″, when it is called with 99. In the next example I capture the parameter of the useCase.sayHello method call and verify it.
@Test
public void testEasterEggTwo() throws Exception {
int i = 99;
someCase.digitToString(i);
ArgumentCaptor arg = ArgumentCaptor.forClass(String.class);
verify(useCase).sayHello(arg.capture());
assertTrue(arg.getValue().equals("99"));
}It should be self-explained and straightforward. There are a lot of other options, how you can use mockito as a ejb unit test framework. Be creative!