Drupal Superfish Menu is rendering without styles

I am upgrading a Drupal website I administer and decided to install the Superfish module for the first time.  As part of the installation, I also uploaded the Superfish-for-Drupal-master.zip to my /sites/all/libraries directory and then extracted all the files.  I then went through the rest of the install steps, including enabling a Superfish block.

However, when I went to view my menu on my website, the menu options and links were all there but there was no menu-like styling or behavior.  I inspected the HTML using Chrome Developer Tools and each of the menu link elements had Superfish CSS classes added to them.  The problem was the CSS files themselves were not being found.

The solution was simply to rename the Superfish directory in my /sites/all/libraries directory from “Superfish-for-Drupal-master” to “Superfish”.  I still feel like this is a missing step in the installation steps (or at a minimum isn’t called out as it should).

I hope this helps someone else as I spent way too much time on this!

Posted in Drupal | Tagged | Leave a comment

Using Matches for null parameter with Mockito

I was using Mockito to write a unit test today and had a brief struggle trying to write a Matcher for a parameter I expected to be null.

Here is the method in my interface that was causing problems:

public interface ItemService
{
    List<Item> findItems(String query, Map<String, String> qualifiers);
}

In my unit test I started with writing this for argument verification since I expected the second parameter for the Map to be null:

 verify(itemService).findItems(anyString(), null);

This resulted in the following error when running the unit test:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
-> at com.stevewall123.ItemServiceTest.testFindITems(ItemServiceTest.java:35)

The error is pretty straight-forward.  Once you use a Matcher on one argument then they need to be used on all arguments.  To fix the error, I needed to use the Matchers class isNull() method.  This method will verify the argument is null.  So in my unit test, I changed the argument verification line to:

 verify(itemService).findItems(anyString(), isNull(Map.class);

And now the test passes!

Posted in Java, Mockito | Tagged , | Leave a comment

FindBugs warning with Guava’s Predicate interface

When running FindBugs on some code I wrote, I received a  NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE warning:

Bug type NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE (click for details)
In class com.wall.steve.HelperMethods$1
In method com.wall.steve.HelperMethods$1.apply(MyItem)
Parameter item
At HelperMethods.java:[line 27]

This was the code I had written:

//com.wall.steve.HelperMethods class
public static final Predicate<MyItem> NON_REJECTED_ITEMS = new Predicate<MyItem>() {
    @Override
    public boolean apply(final MyItem item)  //This is Line 27
    {
        return !item.isRejected();
    }
};

The issue was with my implementation of the apply() method.  Guava has the parameter of the apply method marked @Nullable.

//Guava 18.0 - Predicate class 
boolean apply(@Nullable T input);

FindBugs flagged my implementation since it does not allow for the item parameter to be null.  To fix this issue, I used the @Nonnull annotation (from the javax.annotation package):

//com.wall.steve.HelperMethods class
public static final Predicate<MyItem> NON_REJECTED_ITEMS = new Predicate<MyItem>() {
    @Override
    public boolean apply(@Nonnull final MyItem item)  //Line 27
    {
        return !item.isRejected();
    }
};

This question on Stack Overflow was very similar and provides some more background in answering “why”.

Posted in Guava, Java | Tagged , | Leave a comment

My Presentation at the Twin Cities Java User Group

This week I gave a presentation titled “What’s New in Java 8″ at the November meeting of the Twin Cities Java User Group (TCJUG).

Topics covered include:

  1. Interfaces – static and default methods
  2. Lambdas
  3. Functional Programming
  4. Streams API
  5. New java.time API
  6. Nashorn JavaScript Engine
  7. Other new classes and methods

The code I wrote during the presentation can be found on my GitHub site.

Posted in Java, Java 8 | Tagged , | Leave a comment

Java 8 Streams.max() and Streams.min() example

I have been playing around with the new Stream interface in Java 8 and struggled briefly with the max() and min() methods.  Here is some example code I wrote for how to use the method max() from the Stream class.  The min() method will be very similar.

First, I had a Person class which is just a POJO with two fields and getters/setters:

public class Person {
    String name;
    int age;

    //Constructors and Getters/Setters are omitted for brevity
}

Finally, here is my example using streams:

 public static void main(String[] args) {
     Person obama = new Person("Barack Obama", 53);
     Person bush2 = new Person("George Bush", 68);
     Person clinton = new Person("Bill Clinton", 68);
     Person bush1 = new Person("George HW Bush", 90);
 
     Person[] personArray = new Person[] {obama, bush2, clinton, bush1};
     List<Person> personList = Arrays.asList(personArray);

    //Find Oldest Person
    final Comparator<Person> comp = (p1, p2) -> Integer.compare( p1.getAge(), p2.getAge());
    Person oldest = personList.stream()
                              .max(comp)
                              .get();

    //Find Youngest Person
    //  -This time instead create the Comparator as the argument to the min() method
    Person youngest = personList.stream()
                                .min((p1, p2) -> Integer.compare(p1.getAge(), p2.getAge()))
                                .get();
}
Posted in Java, Java 8 | Tagged , | Leave a comment

Fix for Eclispe generated WAR file not including Ivy libraries

I was using Eclipse to create a relatively simple Dynamic Web Project that used Ivy for dependency mangement.  Also, I wanted to use Eclipse’s export to a WAR feature so I would not need to write a build script.  My WAR file was created, but after deploying my web app to Tomcat and starting I received this error:

SEVERE: Servlet /EntitySandboxWeb threw load() exception
java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:529)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:511)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:139)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1143)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5210)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5493)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

I found this error confusing as I knew Ivy had pulled down the dependent libraries my project needed.  I decided to look at the generated WAR file and discovered those libraries were not in the WEB-INF/lib directory.

The solution was simple.

  1. Under Project Properties,  select “Deployment Assembly”.
  2. Click the “Add…” button
  3. On the first screen of the wizard, choose “Java Build Path Entries” and click “Next”.
  4. On the second screen of the wizard, select “Ivy” and click “Finish”.
  5. You can change the “Deploy Path” if necessary (the default is “WEB-INF/lib”).
  6. Export your project to a WAR file again and the Ivy libraries should be in the WEB-INF/lib directory now.

ProjectPropertiesDeployAssembly

Posted in Eclipse, Tomcat | Tagged , | Leave a comment

How to mock lazy initialization of objects in C# unit tests using Moq

I was unable to find it clearly documented how to use Moq with objects that are to be lazy initialized, so below are the steps I took to do so.

Here is my class under test:

public class MyClass
{
  [Dependency]
  public Lazy<IWorkflowService> WorkflowService { get; set; }

  [Dependency]
  public Lazy<ISession> Session { get; set; }

  public string MyPublicMethod()
  {
    var someValue = this.WorkflowService.Value.GetSomeValue(this.Session.Value.SessionId);

    //Do something with someValue...
    return someValue;
  }
}

Notice that the GetSomeValue method requires the SessionId off of the Session object.

I now needed to write a unit test for MyPublicMethod().  The question I had was how to actually use Moq with the Session and WorkflowService instances.  Normally, I would just be able to do this in my test:

var mockSession = new Mock<ISession>();
var mockWorkfowService = new Mock<IWorkflowService>(); 

var myClass = new MyClass(); 
myClass.Session = mockSession.Object;
myClass.WorkflowService = mockWorkfowService.Object; 
//Method under test
var actual = myClass.MyPublicMethod();

But when I run the test, I received this error:

System.NullReferenceException: Object reference not set to an instance of an object.

The lazy initialization meant I needed to take another approach.  I read the MSDN Lazy Initialization  documentation and noticed there is a Lazy class constructor that allows you to specify a Func that should be used to return an instance of the object.  I decided to take advantage of this in my test case and return the mock instances that I had already created:

var mockSession = new Mock<ISession>();
var mockWorkfowService = new Mock<IWorkflowService>();

var myClass = new MyClass();

//Make use of the Lazy<T>(Func<T>()) constructor 
//to return the mock instances
myClass.Session = new Lazy<ISession>( () => mockSession.Object);
myClass.WorkflowService = 
    new Lazy<IWorkflowService>( () => mockWorkfowService.Object); 

//Method under test
var actual = myClass.MyPublicMethod();

Re-ran the test and it passed!

 

Posted in C#, Moq | Tagged , | Leave a comment