Search This Blog

Monday, September 17, 2012

NUnit AutoFixture unable to create an instance from IList


I encountered a problem with creating fake fixture objects in .NET tests for classes which contant a property with IList<T> (instead plain List<T>).

Suppose you have:

public class A
{
public IList<B> {
get { return list; }
set { list = value; }
}
private IList<B> list;
}

and you want to create a fake object like this:

var fake = new Fixture().Build<A>();

You'll get an error:
"AutoFixture was unable to create an instance from System.Collections.Generic.IList`1[A], most likely because it has no public constructor, is an abstract or non-public type."

What you need to do is tell AutoFixture what to use when it encounters an IList inferface:
(when there is an IList create a new List of some type):

var fixture = new Fixture();
fixture.Register<IList<A>>(fixture.CreateAnonymous<List<A>>);
fixture.Build<A>();


Thursday, September 13, 2012

log4net nhibernate stop logging to console

After using log4net and nhibernate in one project I wanted to log sql commands in a separate file. I didn't want these sql commands to appear on console either. My castle implementation of nhibernate logged either all to console and separate file or nothing.
That's the configuration I used to stop nhibenrate from logging to console and force it to log to a separate file:

download

The important part is to use a filter inside a ConsoleAppender:


<filter type="log4net.Filter.LoggerMatchFilter">
<LoggerToMatch value="NHibernate"/>
<acceptOnMatch value="false"/>
</filter>

Tuesday, September 11, 2012

log4net quickstart; log4net FileAppender not working in ConsoleApplication

To quickly set up log4net logging in you application open you app.config file:

Add a section for log4net:


<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" requirePermission="false"/>
  </configSections>

then add the section itself:


<log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="ConsoleAppender" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{HH:mm:ss,fff} - %message%newline" />
      </layout>
    </appender>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="c:\a\xx.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="500MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
      </layout>
    </appender>
  </log4net>

In your code do the following:

XmlConfigurator.Configure();
ILog logger = LogManager.GetLogger(typeof(Program));

logger.Info("a");

WARNING:
Do not use: BasicConfigurator.Configure(); as it does not work with this example.
Normally you'd declare a private variable for logging:

private static readonly ILog logger = LogManager.GetLogger(typeof (Program));


Sunday, July 15, 2012

Midnight Commander how to compress a file/directory; Make a tar archive with midnight commander

To compress a file in Midnight Commader (e.g. to make a tar.gz archive) navigate to the directory you want to pack and press 'F2'. This will bring up the 'User menu'. Choose the option 'Compress the current subdirectory'. This will compress the WHOLE directory you're currently in - not the highlighted directory.


Tuesday, July 10, 2012

Silverlight set cell focus in DataGrid; Silverlight DataGrid set cell focus

This can be done by adding a behaviour to DataGrid like so:

<sdk:DataGrid>
   <i:InteractionBehaviors>
<Behaviors:BrowserSelectedDataGridRowSelectsCellInSpecificRow ColumnNumber="1"/>
   </i:Interaction.Behaviors>
<sdk:DataGrid>

where ColumnNumber specifies the column number to focus.

void DataGridSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (AssociatedObject.SelectedItem == null)
return;
AssociatedObject.UpdateLayout();
//AssociatedObject.ScrollIntoView(item, (DataGridColumn)Target.Columns[ColumnIndexForEdit]);
AssociatedObject.Focus();
AssociatedObject.CurrentColumn = AssociatedObject.Columns[ColumnNumber];
AssociatedObject.BeginEdit();
}
source

Monday, July 9, 2012

Install Drupal 7.14 on Ubuntu 12.04


Download drupal archive from the site and copy it to the root of your www directory
(on Ubuntu that is: /var/www  for apache2).

Unpack the drupal archive:
tar -zxvf drupal-7.14.tar.gz

Change the name to drupal (or any other):
mv drupal-7.14 drupal

Make sure ALL the files are owned by the www-data user (Apache2 user):
chown -R www-data:www-data ./drupal

Make sure you have the following packages installed:
php5-gd, libapache2-mod-php5, php5-pgsql (if you use postresql with drupal)

apt-get install php5-gd
(use apt-get, because aptitude uninstalls libapache2-mod-php5)
aptitude install libapache2-mod-php5
apt-get install php5-pgsql

Open the page (installation script from the browser):

www.yourdomain.com/drupal

That's about it.

Sunday, July 8, 2012

Configure Apache to host Tomcat 7 on Ubuntu

If you don't have tomcat7 installed run:
aptitude install tomcat7

Configure Tomcat
------------------

Edit /var/lib/tomcat7/conf/server.xml
Uncomment the line:
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
to tell Tomcat to use connector protocol on port 8009 for connection with Apache.

Restart tomcat:
/etc/init.d/tomcat7 restart

Check whether the connector is working with:
netstat -ln | grep :8009


Configure Apache
------------------

Install module for apache which will be used to connect to Tomcat:
apt-get install libapache2-mod-jk

Edit /etc/apache2/sites-available/default
and add JkMountCopy On in the <VirtualHost *:80> section

...
DocumentRoot  /var/www
JkMountCopy   On
...

Edit /etc/apache2/mods-enabled/jk.conf

<IfModule jk_module>
    JkWorkersFile /etc/libapache2-mod-jk/workers.properties
    JkLogFile /var/log/apache2/mod_jk.log
    JkLogLevel info
    JkShmFile /var/log/apache2/jk-runtime-status
    JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
    JkMount /YouApplicationName/* ajp13_worker
    JkMount /YouApplicationName ajp13_worker
</IfModule>


Note:
---------
the path should be accessible from Tomcat7 under the same application name as well. i.e:
localhost:8080/YourApplicationName

This is because Apache hosts your tomcat7's specific path.