package org.ngrinder;

import java.io.File;
import java.io.IOException;

import javax.sql.DataSource;

import org.junit.BeforeClass;
import org.ngrinder.common.constant.NGrinderConstants;
import org.ngrinder.model.User;
import org.ngrinder.user.repository.UserRepository;
import org.ngrinder.user.service.UserContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;

/**
 * This class is used as base class for test case,and it will initialize the DB
 * related config, like datasource, and it will start a transaction for every
 * test function, and rollback after the execution.
 * 
 * @author Mavlarn
 * 
 */
@ContextConfiguration({ "classpath:applicationContext.xml" })
abstract public class AbstractNGrinderTransactionalTest extends AbstractTransactionalJUnit4SpringContextTests
		implements NGrinderConstants {
	protected static final Logger LOG = LoggerFactory.getLogger(AbstractNGrinderTransactionalTest.class);

	@Autowired
	protected UserRepository userRepository;

	protected User testUser = null;
	
	@Autowired
	private UserContext userContext;

	@BeforeClass
	public static void setupSigar() throws IOException {
		ClassPathResource classPathResource = new ClassPathResource("native_lib/.sigar_shellrc");
		String nativeLib = classPathResource.getFile().getParentFile().getAbsolutePath();
		System.setProperty("java.library.path",
						nativeLib + File.pathSeparator + System.getProperty("java.library.path"));
		System.out.println("Java Lib Path : " + System.getProperty("java.library.path"));
	}
	
	@Autowired
	@Override
	public void setDataSource(@Qualifier("dataSource") DataSource dataSource) {
		super.setDataSource(dataSource);
	}

	public User getUser(String userId) {
		return userRepository.findOneByUserId(userId);
	}

	public User getTestUser() {
		if (testUser == null) {
			testUser = userContext.getCurrentUser();
		}
		return testUser;
	}
	
	public User getAdminUser() {
		return userRepository.findOneByUserId("admin");
	}

	public void sleep(long miliseconds) {
		try {
			Thread.sleep(miliseconds);
		} catch (InterruptedException e) {
			LOG.error("error:", e);
		}
	}

}

@@ -1646,16 +1646,33 @@
 ;%0A%09%09
+String javaLib = 
 System.
-s
+g
 etPr
@@ -1701,57 +1701,63 @@
 ath%22
-,%0A%09%09%09%09%09%09nativeLib + File.pathSeparator + 
+);%0A%09%09if (!javaLib.contains(%22native_lib%22)) %7B%0A%09%09%09
 System.
-g
+s
 etPr
@@ -1782,19 +1782,64 @@
 ry.path%22
-));
+, nativeLib + File.pathSeparator + javaLib);%0A%09%09%7D
 %0A%09%09Syste

Fix test failure add if condition will avert many duplicate "java.library.path"