import static org.tizen.common.util.ArrayUtil.pickupFirst;
import static org.tizen.common.util.IOUtil.getBytes;
import static org.tizen.common.util.IOUtil.tryClose;
+import static org.tizen.common.util.StringUtil.nvl;
import static org.tizen.common.verrari.template.TemplateConstants.ATTR_COPY;
import static org.tizen.common.verrari.template.TemplateConstants.ATTR_EXCLUDE;
import static org.tizen.common.verrari.template.TemplateConstants.ATTR_INCLUDE;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import java.util.Locale;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
+import org.tizen.common.core.command.ExecutionContext;
import org.tizen.common.file.Filter;
+import org.tizen.common.util.IOUtil;
import org.tizen.common.util.StringUtil;
import org.tizen.common.verrari.IModelProvider;
import org.tizen.common.verrari.ITemplateEngine;
try {
pretemplate( models );
- } catch (IOException e) {
+
+ // copy
+ for ( final String name : this.copyNames )
+ {
+ logger.trace( "Copy[{}] start", name );
+ final String target = nvl( this.mapper.map( name ), name );
+
+ InputStream in = null;
+ OutputStream out = null;
+
+ try
+ {
+ in = engine.getTemplateProvider().getTemplate( name ).open();
+ out = storage.getTarget( target );
+ ExecutionContext.getCurrentContext().getPrompter().notify( target + " created" );
+ IOUtil.redirect( in , out );
+
+ }
+ finally
+ {
+ tryClose( in, out );
+ }
+ }
+
+ // templating
+ for ( final String name : this.templateNames )
+ {
+ logger.trace( "Template[{}] start", name );
+ final String target = nvl( this.mapper.map( name ), name );
+
+ final OutputStream out = storage.getTarget( target );
+
+ try
+ {
+ engine.generate( name, models, out );
+ ExecutionContext.getCurrentContext().getPrompter().notify( target + " created" );
+ }
+ finally
+ {
+ tryClose( out );
+ }
+ }
+
+ }
+ catch ( final IOException e )
+ {
throw new TemplateException( e );
}
-
- logger.info( "End template process..." );
- System.out.println("End template process..." );
+ catch ( final Exception e )
+ {
+ throw new TemplateException( e );
+ }
+ finally
+ {
+ context.remove();
+ logger.info( "End template process..." );
+ System.out.println("End template process..." );
+ }
}
}
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
+import java.util.List;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.tizen.common.file.VirtualFileHandler;
import org.tizen.common.verrari.AbstractTestCase;
import org.tizen.common.verrari.IModelProvider;
+import org.tizen.common.verrari.Storage;
import static org.tizen.common.verrari.template.TemplateConstants.ATTR_NAME;
import static org.tizen.common.verrari.template.TemplateConstants.ATTR_MAPPING;
import static org.tizen.common.verrari.template.TemplateConstants.ATTR_EXCLUDE;
import static org.tizen.common.util.IOUtil.tryClose;
+import static org.tizen.common.util.IOUtil.getBytes;
import static org.tizen.common.util.ObjectUtil.nvl;
/**
assertEquals( "0\\.1\\.1_Theme/minified/(.*) -> $1,0\\\\.1\\\\.1_Theme/origin/(.*) -> $1", template.getAttribute( ATTR_MAPPING ) );
assertEquals( "0.1.1", template.getAttribute( ATTR_VERSION ) );
assertEquals( "0.1.1_Theme/original/*", template.getAttribute( ATTR_COPY ) );
- assertEquals( "META-INF/*.*", template.getAttribute( ATTR_EXCLUDE ) );
+ assertEquals( "META-INF/*.*;0.1.1_Theme/*.*", template.getAttribute( ATTR_EXCLUDE ) );
}
/**
}
/**
+ *
+ * @throws Exception in case of failure in test
+ */
+ @Test
+ public
+ void
+ test_loadMessage()
+ throws Exception
+ {
+ template.preload( rootDFileMock, models );
+ assertEquals ( "test", template.getMessage( "NAME" ) );
+ }
+
+ /**
+ * Test {@link DirectoryTemplate#template(IModelProvider, org.tizen.common.verrari.Storage)}
+ *
+ * @throws Exception in case of failure in test
+ *
+ * @see DirectoryTemplate#template(IModelProvider, org.tizen.common.verrari.Storage)
+ */
+ @Test
+ public
+ void
+ test_template()
+ throws Exception
+ {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ Storage storageMock = mock( Storage.class );
+ when( storageMock.getTarget( anyString() ) ).thenReturn( out );
+
+ // test (copy)
+ assertTrue( out.size() == 0 );
+
+ template.template( models, storageMock );
+ assertTrue( out.size() > 0 );
+ byte [] contents = out.toByteArray();
+ String result = new String( contents, 0, contents.length );
+ assertEquals( "TEST ori.js", result );
+ out.reset();
+ assertTrue( out.size() == 0 );
+
+ // test (templating)
+ /*
+ * test directory template mock setting.
+ * mock directory structure, (d) directory, (f) file
+ * - 0.1.1 (d)
+ * - config.xml (f)
+ */
+ File configFMock = mock ( File.class );
+ when( configFMock.isDirectory() ).thenReturn( false );
+ when( configFMock.getAbsolutePath() ).thenReturn( "0.1.1/config.xml" );
+ when( configFMock.getParentFile() ).thenReturn( rootDFileMock );
+ when( configFMock.getName() ).thenReturn( "config.xml" );
+ ArrayList<File> array = new ArrayList<File>();
+ array.addAll( Arrays.asList( rootDFileMock.listFiles() ) );
+ array.add( configFMock );
+ File [] files = array.toArray( new File[] {} );
+ when( rootDFileMock.listFiles() ).thenReturn( files );
+
+ template.template( models, storageMock );
+ assertTrue( out.size() > 0 );
+ contents = out.toByteArray();
+ result = new String( contents, 0, contents.length );
+ assertTrue( result.contains("<name>test-id</name>") );
+ }
+
+ /**
* Set up test
* @throws Exception
*/
rootDFileMock = mock( File.class );
when( rootDFileMock.isDirectory() ).thenReturn( true );
when( rootDFileMock.getAbsolutePath() ).thenReturn( "0.1.1" );
- when( rootDFileMock.listFiles() ).thenReturn( new File [] { themeDMock, metaDMock} );
+ when( rootDFileMock.listFiles() ).thenReturn( new File [] { themeDMock, metaDMock } );
// test mock template class setting
this.template = new DirectoryTemplate( rootDFileMock ) {
{
u = new URL( "cp:///web-ui-fw/0.1.1/META-INF/MANIFEST.MF" );
}
- else if ( "messages.properties".equals( name) )
+ else if ( "messages.properties".equals( name ) )
{
u = new URL( "cp:///web-ui-fw/0.1.1/META-INF/messages.properties" );
}
- else if ( "min.js".equals( name) )
+ else if ( "min.js".equals( name ) )
{
u = new URL( "cp:///web-ui-fw/0.1.1/0.1.1_Theme/minified/min.js" );
}
- else if ( "ori.js".equals( name) )
+ else if ( "ori.js".equals( name ) )
{
u = new URL( "cp:///web-ui-fw/0.1.1/0.1.1_Theme/original/ori.js" );
}
+ else if ( "config.xml".endsWith( name ) )
+ {
+ u = new URL( "cp:///web-ui-fw/0.1.1/config.xml" );
+ }
return u.openStream();
}
};
protected Object inputFromUser( final String key )
{
+ if ( "WidgetID".equals( key ) ) {
+ return "test-id";
+ }
// mocking ( It's like user input key "original" )
return "original";
}