Benny's Blog
Navigation: Home » Programmierung
31. März 2012

That’s how you can use enumerations for string values:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
 * ISO 3166
 */
public enum Country
{
  DE
  {
    @Override
    public String toString()
    {
      return "Germany";
    }
  },
  IT
  {
    @Override
    public String toString()
    {
      return "Italy";
    }
  },
  US
  {
    @Override
    public String toString()
    {
      return "United States";
    }
  }
}
1
2
3
4
5
6
public static void main(String[] args)
{
  System.out.println(Country.DE); // Germany
  System.out.println(Country.IT); // Italy
  System.out.println(Country.US); // United States
}
30. März 2012

With JUnit 4 it is pretty simple to test if an exception has been properly thrown because you can use annotations:

1
2
3
4
5
6
7
8
9
10
11
12
13
import static org.junit.Assert.fail;
import org.junit.*;
 
public class MyTestClass
{
 
  @Test(expected = NoMatchFoundException.class)
  public void testSomething() throws NoMatchFoundException
  {
    // Your critical code here!
    fail();
  }
}

If you are using JUnit 3 then it is not that easy because you don’t have annotations. That’s why you have to catch exceptions in the test code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import junit.framework.TestCase;
 
public class MyTestClass extends TestCase
{
  public void testSomething() throws NoMatchFoundException
  {
    try
    {
      // Your critical code here!
    }
    catch (NoMatchFoundException ex)
    {}
  }
}
29. März 2012

Möchte man einen langen Batch-Befehl auf mehrere Zeilen aufteilen, so muss man den Zeilenumbruch maskieren (escapen). Der Zeilenumbruch wird mit einem ^ angedeutet.

Beispiel:

REM Einzeilig:
echo Hello World.
REM Zweizeilig:
echo Hello ^
World.
28. März 2012

JavaScript is able to zoom in and zoom out of a web page. All you need is just this (for 200% zoom):

window.parent.document.body.style.zoom=2.0;
27. März 2012

In modern mobile devices and HTML5 browsers you can set the viewport content (for example the webpage zoom and webpage dimension) with a single meta tag:

<meta name="viewport" content="width=device-width,initial-scale=2.0,minimum-scale=2.0,maximum-scale=2.0, user-scalable=no">

If you want to do this with JavaScript, then you need a viewport meta-tag with empty content:

<meta name="viewport" content="" />

Then you can change the content by using JavaScript:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function setViewPort(viewPortContent){
  var metas = document.getElementsByTagName('meta');
  var i;
  if (navigator.userAgent.match(/iPhone/i)) {
    for (i=0; i<metas.length; i++) {
      if (metas[i].name == 'viewport') {
        metas[i].content = viewPortContent;
      }
    }
  }
}
 
var viewPortContent = 'width=device-width,initial-scale=2.0,minimum-scale=2.0,maximum-scale=2.0, user-scalable=no';
setViewPort(viewPortContent);
26. März 2012

If you want to constantly hide the address bar on mobile devices like iPhone, iPad and Android smartphones/tablets, then you have to scroll the window even if the device is turned:

1
2
3
4
5
6
7
8
9
10
<script type="text/javascript">
function hideAddressBar(){
  window.scrollTo(0, 1);
}
 
window.onload = hideAddressBar;
window.onscroll = hideAddressBar;
window.onresize = hideAddressBar;
window.onorientationchange = hideAddressBar;
</script>

Short version:

<script>
window.onload = window.onscroll = window.onresize = window.onorientationchange = function(){
  window.scrollTo(0, 1);
};
</script>