package org.stokesdrift.container;

import java.io.File;
import java.net.URI;
import java.net.URL;
import java.util.List;

import javax.servlet.ServletContext;

import org.jruby.Ruby;
import org.jruby.RubyInstanceConfig;
import org.jruby.rack.servlet.DefaultServletRackContext;
import org.jruby.rack.servlet.ServletRackConfig;
import org.jruby.rack.servlet.ServletRackContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.junit.Assert;
import org.junit.Test;
import org.powermock.api.mockito.PowerMockito;
import org.stokesdrift.container.jruby.DriftRackApplicationFactory;

public class DriftRackApplicationFactoryTest {

	@Test
	public void testLoadPaths() throws Exception {		
		DriftRackApplicationFactory factory = new DriftRackApplicationFactory();
		ServletContext servletContext = PowerMockito.mock(ServletContext.class);
		ServletRackConfig config = new ServletRackConfig(servletContext);
		ServletRackContext context = new DefaultServletRackContext(config);

		// Shouldn't add since the gems direction doesn't exist
		URL configRuFile = this.getClass().getClassLoader().getResource("examples/config.ru");
		File file = new File(configRuFile.toURI());
		String jrubyHome = file.getParent();
		
		URI libUri = Ruby.class.getProtectionDomain().getCodeSource().getLocation().toURI();
		File libDirectory = new File(libUri);
		
		System.setProperty("STOKESDRIFT_LIB_DIR",libDirectory.getParent());
		System.setProperty("JRUBY_HOME",jrubyHome);
	    System.setProperty("GEM_PATH","test/home/gem_path");
	    
		factory.init(context);
		Ruby runtime = factory.newRuntime();
		Assert.assertNotNull(runtime);
				
		RubyInstanceConfig rackConfig = factory.getRuntimeConfig();
		List<String> loadPaths = rackConfig.getLoadPaths();
		Assert.assertTrue(loadPaths.size() > 0);
		System.out.println("LIBS " );
		for (String loadPath: loadPaths) {
			System.out.println(loadPath);
		}
		
		Assert.assertEquals(jrubyHome, rackConfig.getJRubyHome());
		
		IRubyObject rubyObject = runtime.executeScript("ENV.inspect", "jruby_home.rb");
		System.out.println("WHAT IS THIS " + rubyObject.asString());
		
	}	
}
@@ -92,16 +92,114 @@
 et.URL;%0A
+import java.nio.charset.StandardCharsets;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Paths;%0A
 import j
@@ -1884,300 +1884,424 @@
 ;%0A%09%09
-System.out.println(%22LIBS %22 );%0A%09%09for (String loadPath: loadPaths) %7B%0A%09%09%09System.out.println(loadPath);%0A%09%09%7D%0A%09%09%0A%09%09Assert.assertEquals(jrubyHome, rackConfig.getJRubyHome());%0A%09%09%0A%09%09IRubyObject rubyObject = runtime.executeScript(%22ENV.inspect%22, %22jruby_home.rb%22);%0A%09%09System.out.println(%22WHAT IS THIS %22 + 
+Assert.assertTrue(loadPaths.contains(%22test/home/gem_path%22));%0A%09%09Assert.assertEquals(jrubyHome, rackConfig.getJRubyHome());%0A%09%09%0A%09%09%0A%09%09configRuFile = this.getClass().getClassLoader().getResource(%22examples/test_paths.rb%22);%0A%09%09byte%5B%5D scriptBytes = Files.readAllBytes(Paths.get(configRuFile.toURI()));%0A%09%09IRubyObject rubyObject = runtime.executeScript(new String(scriptBytes), %22test_paths.rb%22);%0A%09%09Assert.assertEquals(%22horray%22,
 ruby
@@ -2317,16 +2317,27 @@
 String()
+.toString()
 );%0A%09%09%0A%09%7D

test clean up